Skip to content

Commit

Permalink
feat: add string formatting utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Oct 25, 2023
1 parent 3fb276d commit c0290a3
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('lib', () => {
"SuspenseError": [Function],
"createLocalizeError": [Function],
"daysDiff": [Function],
"dotsToSpaces": [Function],
"ensureUTC": [Function],
"formatDate": [Function],
"getUnixTimestamp": [Function],
Expand All @@ -17,6 +18,8 @@ describe('lib', () => {
"secondsToReadableDuration": [Function],
"toHumanFileSize": [Function],
"toTimeAgo": [Function],
"truncate": [Function],
"truncateMiddle": [Function],
}
`);
});
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
27 changes: 27 additions & 0 deletions src/string/string.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
26 changes: 26 additions & 0 deletions src/string/string.ts
Original file line number Diff line number Diff line change
@@ -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);
};
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit c0290a3

Please sign in to comment.