Comments (2)
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.
Looks like a cool addition 👍
from moderndash.
Related Issues (20)
- Add possibility to wait until the queue finishes HOT 3
- Non-constructing `object.set`
- Shouldn't `hotscript` be in the main dependencies since it's imported in `dist/index.d.ts`? HOT 2
- Bundle hotscript types HOT 1
- Feature request - `mergeWith` HOT 3
- Feature request: debounce.pending() HOT 2
- Feature request - `deepFreeze` HOT 1
- Return the result of the last `throttle` invocation HOT 3
- Feature request - `curry`
- PlainObject not working with Interfaces HOT 3
- Deprecate array `group` HOT 2
- Feature request - Array/concat HOT 5
- Feature request - clone HOT 3
- Error: Unexpected token '??=' HOT 2
- splitWordsRegex fails on iOS 16 HOT 6
- T39 Decorators
- Switch to mutations instead of copy HOT 1
- `takeRightWhile` | Improve performance HOT 1
- `set` | Incorrect type if value is replaced by array
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moderndash.