diff --git a/package.json b/package.json index b17677b..33ae309 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "@size-limit/preset-small-lib": "^8.2.4", "@tsconfig/recommended": "^1.0.2", "@types/bytes": "^3.1.1", + "@types/truncate-middle": "^1.0.3", "dts-cli": "^2.0.3", "husky": "^8.0.3", "size-limit": "^8.2.4", @@ -60,7 +61,8 @@ "dependencies": { "bytes": "^3.1.2", "dayjs": "^1.11.10", - "es6-error": "^4.1.1" + "es6-error": "^4.1.1", + "truncate-middle": "^1.0.6" }, "peerDependencies": { "@putdotio/api-client": ">=8.37.0" diff --git a/src/index.spec.ts b/src/index.spec.ts index d1dc828..e843065 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -9,6 +9,7 @@ describe('lib', () => { "SuspenseError": [Function], "createLocalizeError": [Function], "daysDiff": [Function], + "dotsToSpaces": [Function], "ensureUTC": [Function], "formatDate": [Function], "getUnixTimestamp": [Function], @@ -17,6 +18,8 @@ describe('lib', () => { "secondsToReadableDuration": [Function], "toHumanFileSize": [Function], "toTimeAgo": [Function], + "truncate": [Function], + "truncateMiddle": [Function], } `); }); diff --git a/src/index.ts b/src/index.ts index 9f3c9df..989ea22 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,3 +5,4 @@ export * from './errors'; export * from './file/file'; export * from './localized-error/LocalizedError'; export * from './localized-error/localizeError'; +export * from './string/string'; diff --git a/src/string/string.spec.ts b/src/string/string.spec.ts new file mode 100644 index 0000000..65a834b --- /dev/null +++ b/src/string/string.spec.ts @@ -0,0 +1,27 @@ +import { dotsToSpaces, truncate, truncateMiddle } from './string'; + +describe('string', () => { + describe('dotsToSpaces', () => { + it('should replace dots with spaces', () => { + expect(dotsToSpaces('a.b.c')).toEqual('a b c'); + }); + }); + + describe('truncate', () => { + it('should truncate a string', () => { + expect(truncate('abcdef', { length: 3 })).toEqual('abc...'); + }); + + it('should truncate a string with custom ellipsis', () => { + expect(truncate('abcdef', { length: 3, ellipsis: '-' })).toEqual('abc-'); + }); + }); + + describe('truncateMiddle', () => { + it('should truncate a string in the middle', () => { + expect( + truncateMiddle('abcdef', { frontLength: 1, backLength: 1 }) + ).toEqual('a...f'); + }); + }); +}); diff --git a/src/string/string.ts b/src/string/string.ts new file mode 100644 index 0000000..e450600 --- /dev/null +++ b/src/string/string.ts @@ -0,0 +1,26 @@ +import libTruncateMiddle from 'truncate-middle'; + +export const dotsToSpaces = (input: string) => input.split('.').join(' '); + +export const truncate = ( + input: string, + options: { + length: number; + ellipsis?: string; + } +) => { + const { length, ellipsis = '...' } = options; + return input.slice(0, length) + ellipsis; +}; + +export const truncateMiddle = ( + input: string, + options: { + frontLength: number; + backLength: number; + ellipsis?: string; + } +) => { + const { frontLength, backLength, ellipsis = '...' } = options; + return libTruncateMiddle(input, frontLength, backLength, ellipsis); +}; diff --git a/yarn.lock b/yarn.lock index 84f5040..22d7c88 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1758,6 +1758,11 @@ resolved "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-4.0.2.tgz" integrity sha512-Q5vtl1W5ue16D+nIaW8JWebSSraJVlK+EthKn7e7UcD4KWsaSJ8BqGPXNaPghgtcn/fhvrN17Tv8ksUsQpiplw== +"@types/truncate-middle@^1.0.3": + version "1.0.3" + resolved "https://registry.yarnpkg.com/@types/truncate-middle/-/truncate-middle-1.0.3.tgz#de68f992b9754f3790dacecc4a509e841d64284f" + integrity sha512-va2ApH8QJ8ty/j8WJR3KzHwI+NLmLCvYgUmwApXVXioD9zpAaSmTDaVfiYyCZmY3sjQpPmo/8nqpdOz0F3nOrA== + "@types/yargs-parser@*": version "21.0.0" resolved "https://registry.npmjs.org/@types/yargs-parser/-/yargs-parser-21.0.0.tgz" @@ -5866,6 +5871,11 @@ tr46@^3.0.0: dependencies: punycode "^2.1.1" +truncate-middle@^1.0.6: + version "1.0.6" + resolved "https://registry.yarnpkg.com/truncate-middle/-/truncate-middle-1.0.6.tgz#17c6272ec5a469b75a82fbca38b388b61b50473b" + integrity sha512-oJLDTdHAk27V+JUUu1vKYezKehx/tECV0vnJ1e8JV/rvre5oLoFMaCLP53ZwiPsw4ZIJzyLoZr/TKQABnaNF6A== + ts-jest@^29.0.5: version "29.1.0" resolved "https://registry.npmjs.org/ts-jest/-/ts-jest-29.1.0.tgz"