Skip to content

Commit

Permalink
fix: update truncate method
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Oct 25, 2023
1 parent 7c4fcf5 commit dd0981c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/string/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ describe('string', () => {
});

describe('truncate', () => {
it('should skip truncation if string is not long enough', () => {
expect(truncate('abcdef', { length: 6 })).toEqual('abcdef');
});

it('should truncate a string', () => {
expect(truncate('abcdef', { length: 3 })).toEqual('abc...');
});
Expand Down
5 changes: 5 additions & 0 deletions src/string/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export const truncate = (
}
) => {
const { length, ellipsis = '...' } = options;

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

return input.slice(0, length) + ellipsis;
};

Expand Down

0 comments on commit dd0981c

Please sign in to comment.