Giter VIP home page Giter VIP logo

Comments (2)

tkrotoff avatar tkrotoff commented on July 28, 2024

Proposed implementation:

// File truncate.ts

type Options = {
  /**
   * The maximum string length.
   *
   * Default: 30
   */
  length?: number;

  /**
   * The string to indicate text is omitted.
   *
   * Also named [ellipsis](https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow)
   *
   * Default: "...", you might want to use "…" (… U+02026) instead
   */
  omission?: string;

  /**
   * The separator pattern to truncate to.
   *
   * Default: none
   */
  separator?: string;
};

/**
 * Truncates a string if it's longer than the given maximum length.
 * The last characters of the truncated string are replaced with the omission
 * string which defaults to "...".
 *
 * @param str The string to truncate
 * @param options The options object
 * @returns The truncated string
 */
export function truncate(str: string, options?: Options) {
  // https://stackoverflow.com/q/1199352
  // https://github.com/Maggi64/moderndash/issues/155
  // https://lodash.com/docs/4.17.15#truncate

  const { length = 30, omission = '...', separator } = options ?? {};

  if (str.length <= length) {
    return str;
  }

  let maxLength = length - omission.length;
  if (maxLength < 0) {
    maxLength = 0;
  }
  const subString = str.slice(
    0,
    // FYI .slice() is OK if maxLength > text.length
    maxLength
  );

  return (separator ? subString.slice(0, subString.lastIndexOf(separator)) : subString) + omission;
}

It passes all tests from Lodash:

// File truncate.test.ts

import { truncate } from './truncate';

// Copy-pasted and adapted from https://github.com/lodash/lodash/blob/c7c70a7da5172111b99bb45e45532ed034d7b5b9/test/truncate.spec.js
// See also https://github.com/lodash/lodash/pull/5815

const string = 'hi-diddly-ho there, neighborino';

it('should use a default `length` of `30`', () => {
  expect(truncate(string)).toBe('hi-diddly-ho there, neighbo...');
});

it('should not truncate if `string` is <= `length`', () => {
  expect(truncate(string, { length: string.length })).toBe(string);
  expect(truncate(string, { length: string.length + 2 })).toBe(string);
});

it('should truncate string the given length', () => {
  expect(truncate(string, { length: 24 })).toBe('hi-diddly-ho there, n...');
});

it('should support a `omission` option', () => {
  expect(truncate(string, { omission: ' [...]' })).toBe('hi-diddly-ho there, neig [...]');
});

it('should support empty `omission` option', () => {
  expect(truncate(string, { omission: '' })).toBe('hi-diddly-ho there, neighborin');
});

it('should support a `length` option', () => {
  expect(truncate(string, { length: 4 })).toBe('h...');
});

it('should support a `separator` option', () => {
  expect(truncate(string, { length: 24, separator: ' ' })).toBe('hi-diddly-ho there,...');
});

it('should treat negative `length` as `0`', () => {
  [0, -2].forEach(length => {
    expect(truncate(string, { length })).toBe('...');
  });
});

it('should work as an iteratee for methods like `_.map`', () => {
  const actual = [string, string, string].map(str => truncate(str));
  const truncated = 'hi-diddly-ho there, neighbo...';

  expect(actual).toEqual([truncated, truncated, truncated]);
});

from moderndash.

Maggi64 avatar Maggi64 commented on July 28, 2024

Looks like a cool addition 👍

from moderndash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.