Skip to content

Commit

Permalink
feat: add applyEllipsisOnLength
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Apr 26, 2024
1 parent d397ab3 commit 72d7521
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,8 @@ export {
type ChatBotMessage,
GPTVersion,
} from './chatbot/chatbot.js';

/**
* string utils
*/
export * from './string/string.js';
18 changes: 18 additions & 0 deletions src/string/string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, it } from 'vitest';

import { applyEllipsisOnLength } from './string.js';

describe('applyEllipsisOnLength', () => {
it('does not add ellipsis', () => {
expect(applyEllipsisOnLength('mystring', 100)).toEqual('mystring');
expect(applyEllipsisOnLength('my very long string is here', 100)).toEqual(
'my very long string is here',
);
});
it('add ellipsis', () => {
expect(applyEllipsisOnLength('mystring', 2)).toEqual('my…');
expect(applyEllipsisOnLength('my very long string is here', 10)).toEqual(
'my very lo…',
);
});
});
14 changes: 14 additions & 0 deletions src/string/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* add ellipsis on string given length
* warning: use css instead of this function if possible
* @param {any} longString:string
* @param {any} maxLength:number
* @returns {any}
*/
export const applyEllipsisOnLength = (
longString: string,
maxLength: number,
): string =>
`${longString.slice(0, maxLength)}${
(longString.length || 0) > maxLength ? '…' : ''
}`;

0 comments on commit 72d7521

Please sign in to comment.