@forthtilliath/ts-kit 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +83 -19
- package/dist/async/debounce.d.ts +20 -0
- package/dist/async/debounce.d.ts.map +1 -0
- package/dist/async/debounce.js +44 -0
- package/dist/async/index.d.ts +7 -0
- package/dist/async/index.d.ts.map +1 -0
- package/dist/async/index.js +6 -0
- package/dist/async/memoize.d.ts +19 -0
- package/dist/async/memoize.d.ts.map +1 -0
- package/dist/async/memoize.js +25 -0
- package/dist/async/retry.d.ts +25 -0
- package/dist/async/retry.d.ts.map +1 -0
- package/dist/async/retry.js +33 -0
- package/dist/async/sleep.d.ts +9 -0
- package/dist/async/sleep.d.ts.map +1 -0
- package/dist/async/sleep.js +10 -0
- package/dist/async/throttle.d.ts +20 -0
- package/dist/async/throttle.d.ts.map +1 -0
- package/dist/async/throttle.js +43 -0
- package/dist/async/withTimeout.d.ts +19 -0
- package/dist/async/withTimeout.d.ts.map +1 -0
- package/dist/async/withTimeout.js +34 -0
- package/dist/classes/array.d.ts +88 -0
- package/dist/classes/array.d.ts.map +1 -1
- package/dist/classes/array.js +74 -0
- package/dist/classes/index.d.ts +2 -0
- package/dist/classes/index.d.ts.map +1 -0
- package/dist/classes/index.js +1 -0
- package/dist/date/formatRelativeTime.d.ts +13 -0
- package/dist/date/formatRelativeTime.d.ts.map +1 -0
- package/dist/date/formatRelativeTime.js +30 -0
- package/dist/date/index.d.ts +4 -0
- package/dist/date/index.d.ts.map +1 -0
- package/dist/date/index.js +3 -0
- package/dist/date/isSameDay.d.ts +11 -0
- package/dist/date/isSameDay.d.ts.map +1 -0
- package/dist/date/isSameDay.js +14 -0
- package/dist/date/startOfDay.d.ts +10 -0
- package/dist/date/startOfDay.d.ts.map +1 -0
- package/dist/date/startOfDay.js +13 -0
- package/dist/files/downloadText.d.ts +2 -1
- package/dist/files/downloadText.d.ts.map +1 -1
- package/dist/files/downloadText.js +3 -2
- package/dist/files/index.d.ts +2 -0
- package/dist/files/index.d.ts.map +1 -0
- package/dist/files/index.js +1 -0
- package/dist/id/index.d.ts +2 -0
- package/dist/id/index.d.ts.map +1 -0
- package/dist/id/index.js +1 -0
- package/dist/id/randomId.d.ts +10 -0
- package/dist/id/randomId.d.ts.map +1 -0
- package/dist/id/randomId.js +33 -0
- package/dist/index.d.ts +10 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -1
- package/dist/number/clamp.d.ts +13 -0
- package/dist/number/clamp.d.ts.map +1 -0
- package/dist/number/clamp.js +14 -0
- package/dist/number/formatBytes.d.ts +12 -0
- package/dist/number/formatBytes.d.ts.map +1 -0
- package/dist/number/formatBytes.js +19 -0
- package/dist/number/formatDuration.d.ts +12 -0
- package/dist/number/formatDuration.d.ts.map +1 -0
- package/dist/number/formatDuration.js +29 -0
- package/dist/number/index.d.ts +5 -0
- package/dist/number/index.d.ts.map +1 -0
- package/dist/number/index.js +4 -0
- package/dist/number/round.d.ts +12 -0
- package/dist/number/round.d.ts.map +1 -0
- package/dist/number/round.js +14 -0
- package/dist/object/deepClone.d.ts +12 -0
- package/dist/object/deepClone.d.ts.map +1 -0
- package/dist/object/deepClone.js +16 -0
- package/dist/object/deepEqual.d.ts +12 -0
- package/dist/object/deepEqual.d.ts.map +1 -0
- package/dist/object/deepEqual.js +31 -0
- package/dist/object/groupBy.d.ts +12 -0
- package/dist/object/groupBy.d.ts.map +1 -0
- package/dist/object/groupBy.js +18 -0
- package/dist/object/index.d.ts +8 -0
- package/dist/object/index.d.ts.map +1 -0
- package/dist/object/index.js +7 -0
- package/dist/object/mapValues.d.ts +11 -0
- package/dist/object/mapValues.d.ts.map +1 -0
- package/dist/object/mapValues.js +16 -0
- package/dist/object/omit.d.ts +11 -0
- package/dist/object/omit.d.ts.map +1 -0
- package/dist/object/omit.js +16 -0
- package/dist/object/pick.d.ts +11 -0
- package/dist/object/pick.d.ts.map +1 -0
- package/dist/object/pick.js +18 -0
- package/dist/object/uniqueBy.d.ts +13 -0
- package/dist/object/uniqueBy.d.ts.map +1 -0
- package/dist/object/uniqueBy.js +23 -0
- package/dist/string/capitalize.d.ts +11 -0
- package/dist/string/capitalize.d.ts.map +1 -0
- package/dist/string/capitalize.js +14 -0
- package/dist/string/index.d.ts +5 -0
- package/dist/string/index.d.ts.map +1 -0
- package/dist/string/index.js +4 -0
- package/dist/string/pluralize.d.ts +17 -0
- package/dist/string/pluralize.d.ts.map +1 -0
- package/dist/string/pluralize.js +26 -0
- package/dist/string/slugify.d.ts +12 -0
- package/dist/string/slugify.d.ts.map +1 -0
- package/dist/string/slugify.js +18 -0
- package/dist/string/truncate.d.ts +13 -0
- package/dist/string/truncate.d.ts.map +1 -0
- package/dist/string/truncate.js +17 -0
- package/package.json +3 -2
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Groups the elements of an array by a key derived from each element.
|
|
3
|
+
*
|
|
4
|
+
* @param array - The array to group.
|
|
5
|
+
* @param getKey - Computes the group key for an element.
|
|
6
|
+
* @returns A record mapping each key to the array of matching elements.
|
|
7
|
+
* @example
|
|
8
|
+
* groupBy([1, 2, 3, 4], (n) => (n % 2 === 0 ? "even" : "odd"));
|
|
9
|
+
* // => { odd: [1, 3], even: [2, 4] }
|
|
10
|
+
*/
|
|
11
|
+
export function groupBy(array, getKey) {
|
|
12
|
+
const result = {};
|
|
13
|
+
for (const item of array) {
|
|
14
|
+
const key = getKey(item);
|
|
15
|
+
(result[key] ??= []).push(item);
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/object/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new object with the same keys, mapping each value through `fn`.
|
|
3
|
+
*
|
|
4
|
+
* @param obj - The source object.
|
|
5
|
+
* @param fn - Maps a value (and its key) to a new value.
|
|
6
|
+
* @returns A new object with transformed values.
|
|
7
|
+
* @example
|
|
8
|
+
* mapValues({ a: 1, b: 2 }, (n) => n * 2); // => { a: 2, b: 4 }
|
|
9
|
+
*/
|
|
10
|
+
export declare function mapValues<T extends object, R>(obj: T, fn: (value: T[keyof T], key: keyof T) => R): Record<keyof T, R>;
|
|
11
|
+
//# sourceMappingURL=mapValues.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapValues.d.ts","sourceRoot":"","sources":["../../src/object/mapValues.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,EAC3C,GAAG,EAAE,CAAC,EACN,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,GACzC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAMpB"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new object with the same keys, mapping each value through `fn`.
|
|
3
|
+
*
|
|
4
|
+
* @param obj - The source object.
|
|
5
|
+
* @param fn - Maps a value (and its key) to a new value.
|
|
6
|
+
* @returns A new object with transformed values.
|
|
7
|
+
* @example
|
|
8
|
+
* mapValues({ a: 1, b: 2 }, (n) => n * 2); // => { a: 2, b: 4 }
|
|
9
|
+
*/
|
|
10
|
+
export function mapValues(obj, fn) {
|
|
11
|
+
const result = {};
|
|
12
|
+
for (const key of Object.keys(obj)) {
|
|
13
|
+
result[key] = fn(obj[key], key);
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new object without the specified keys.
|
|
3
|
+
*
|
|
4
|
+
* @param obj - The source object.
|
|
5
|
+
* @param keys - The keys to remove.
|
|
6
|
+
* @returns A new object without `keys`.
|
|
7
|
+
* @example
|
|
8
|
+
* omit({ a: 1, b: 2, c: 3 }, ["b"]); // => { a: 1, c: 3 }
|
|
9
|
+
*/
|
|
10
|
+
export declare function omit<T extends object, K extends keyof T>(obj: T, keys: readonly K[]): Omit<T, K>;
|
|
11
|
+
//# sourceMappingURL=omit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"omit.d.ts","sourceRoot":"","sources":["../../src/object/omit.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,EACtD,GAAG,EAAE,CAAC,EACN,IAAI,EAAE,SAAS,CAAC,EAAE,GACjB,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAMZ"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new object without the specified keys.
|
|
3
|
+
*
|
|
4
|
+
* @param obj - The source object.
|
|
5
|
+
* @param keys - The keys to remove.
|
|
6
|
+
* @returns A new object without `keys`.
|
|
7
|
+
* @example
|
|
8
|
+
* omit({ a: 1, b: 2, c: 3 }, ["b"]); // => { a: 1, c: 3 }
|
|
9
|
+
*/
|
|
10
|
+
export function omit(obj, keys) {
|
|
11
|
+
const result = { ...obj };
|
|
12
|
+
for (const key of keys) {
|
|
13
|
+
Reflect.deleteProperty(result, key);
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new object composed of only the specified keys.
|
|
3
|
+
*
|
|
4
|
+
* @param obj - The source object.
|
|
5
|
+
* @param keys - The keys to keep.
|
|
6
|
+
* @returns A new object containing only `keys`.
|
|
7
|
+
* @example
|
|
8
|
+
* pick({ a: 1, b: 2, c: 3 }, ["a", "c"]); // => { a: 1, c: 3 }
|
|
9
|
+
*/
|
|
10
|
+
export declare function pick<T extends object, K extends keyof T>(obj: T, keys: readonly K[]): Pick<T, K>;
|
|
11
|
+
//# sourceMappingURL=pick.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pick.d.ts","sourceRoot":"","sources":["../../src/object/pick.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,EACtD,GAAG,EAAE,CAAC,EACN,IAAI,EAAE,SAAS,CAAC,EAAE,GACjB,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAQZ"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a new object composed of only the specified keys.
|
|
3
|
+
*
|
|
4
|
+
* @param obj - The source object.
|
|
5
|
+
* @param keys - The keys to keep.
|
|
6
|
+
* @returns A new object containing only `keys`.
|
|
7
|
+
* @example
|
|
8
|
+
* pick({ a: 1, b: 2, c: 3 }, ["a", "c"]); // => { a: 1, c: 3 }
|
|
9
|
+
*/
|
|
10
|
+
export function pick(obj, keys) {
|
|
11
|
+
const result = {};
|
|
12
|
+
for (const key of keys) {
|
|
13
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
14
|
+
result[key] = obj[key];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes duplicate elements from an array, keeping the first occurrence for
|
|
3
|
+
* each computed key.
|
|
4
|
+
*
|
|
5
|
+
* @param array - The array to deduplicate.
|
|
6
|
+
* @param getKey - Computes the uniqueness key for an element.
|
|
7
|
+
* @returns A new array with only the first element for each key.
|
|
8
|
+
* @example
|
|
9
|
+
* uniqueBy([{ id: 1 }, { id: 2 }, { id: 1 }], (item) => item.id);
|
|
10
|
+
* // => [{ id: 1 }, { id: 2 }]
|
|
11
|
+
*/
|
|
12
|
+
export declare function uniqueBy<T>(array: readonly T[], getKey: (item: T) => unknown): T[];
|
|
13
|
+
//# sourceMappingURL=uniqueBy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uniqueBy.d.ts","sourceRoot":"","sources":["../../src/object/uniqueBy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EACxB,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,GAC3B,CAAC,EAAE,CAWL"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes duplicate elements from an array, keeping the first occurrence for
|
|
3
|
+
* each computed key.
|
|
4
|
+
*
|
|
5
|
+
* @param array - The array to deduplicate.
|
|
6
|
+
* @param getKey - Computes the uniqueness key for an element.
|
|
7
|
+
* @returns A new array with only the first element for each key.
|
|
8
|
+
* @example
|
|
9
|
+
* uniqueBy([{ id: 1 }, { id: 2 }, { id: 1 }], (item) => item.id);
|
|
10
|
+
* // => [{ id: 1 }, { id: 2 }]
|
|
11
|
+
*/
|
|
12
|
+
export function uniqueBy(array, getKey) {
|
|
13
|
+
const seen = new Set();
|
|
14
|
+
const result = [];
|
|
15
|
+
for (const item of array) {
|
|
16
|
+
const key = getKey(item);
|
|
17
|
+
if (!seen.has(key)) {
|
|
18
|
+
seen.add(key);
|
|
19
|
+
result.push(item);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capitalizes the first character of a string and lower-cases the rest.
|
|
3
|
+
*
|
|
4
|
+
* @param str - The string to capitalize.
|
|
5
|
+
* @returns The capitalized string.
|
|
6
|
+
* @example
|
|
7
|
+
* capitalize("hello world"); // => "Hello world"
|
|
8
|
+
* capitalize("HELLO"); // => "Hello"
|
|
9
|
+
*/
|
|
10
|
+
export declare function capitalize(str: string): string;
|
|
11
|
+
//# sourceMappingURL=capitalize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"capitalize.d.ts","sourceRoot":"","sources":["../../src/string/capitalize.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAG9C"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capitalizes the first character of a string and lower-cases the rest.
|
|
3
|
+
*
|
|
4
|
+
* @param str - The string to capitalize.
|
|
5
|
+
* @returns The capitalized string.
|
|
6
|
+
* @example
|
|
7
|
+
* capitalize("hello world"); // => "Hello world"
|
|
8
|
+
* capitalize("HELLO"); // => "Hello"
|
|
9
|
+
*/
|
|
10
|
+
export function capitalize(str) {
|
|
11
|
+
if (str.length === 0)
|
|
12
|
+
return str;
|
|
13
|
+
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/string/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pluralizes a word based on a count, with a naive English default (`s`
|
|
3
|
+
* suffix, `y` → `ies`, `es` after `s`/`x`/`z`/`sh`/`ch`) or an explicit
|
|
4
|
+
* plural form for irregular words.
|
|
5
|
+
*
|
|
6
|
+
* @param count - The count driving singular vs. plural.
|
|
7
|
+
* @param singular - The singular form.
|
|
8
|
+
* @param plural - An explicit plural form (overrides the naive default).
|
|
9
|
+
* @returns `singular` when `count` is 1 or -1, otherwise the plural form.
|
|
10
|
+
* @example
|
|
11
|
+
* pluralize(1, "item"); // => "item"
|
|
12
|
+
* pluralize(3, "item"); // => "items"
|
|
13
|
+
* pluralize(3, "city"); // => "cities"
|
|
14
|
+
* pluralize(2, "child", "children"); // => "children"
|
|
15
|
+
*/
|
|
16
|
+
export declare function pluralize(count: number, singular: string, plural?: string): string;
|
|
17
|
+
//# sourceMappingURL=pluralize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pluralize.d.ts","sourceRoot":"","sources":["../../src/string/pluralize.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,MAAM,GACd,MAAM,CAMR"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pluralizes a word based on a count, with a naive English default (`s`
|
|
3
|
+
* suffix, `y` → `ies`, `es` after `s`/`x`/`z`/`sh`/`ch`) or an explicit
|
|
4
|
+
* plural form for irregular words.
|
|
5
|
+
*
|
|
6
|
+
* @param count - The count driving singular vs. plural.
|
|
7
|
+
* @param singular - The singular form.
|
|
8
|
+
* @param plural - An explicit plural form (overrides the naive default).
|
|
9
|
+
* @returns `singular` when `count` is 1 or -1, otherwise the plural form.
|
|
10
|
+
* @example
|
|
11
|
+
* pluralize(1, "item"); // => "item"
|
|
12
|
+
* pluralize(3, "item"); // => "items"
|
|
13
|
+
* pluralize(3, "city"); // => "cities"
|
|
14
|
+
* pluralize(2, "child", "children"); // => "children"
|
|
15
|
+
*/
|
|
16
|
+
export function pluralize(count, singular, plural) {
|
|
17
|
+
if (Math.abs(count) === 1)
|
|
18
|
+
return singular;
|
|
19
|
+
if (plural !== undefined)
|
|
20
|
+
return plural;
|
|
21
|
+
if (/[sxz]$|[cs]h$/i.test(singular))
|
|
22
|
+
return `${singular}es`;
|
|
23
|
+
if (/[^aeiou]y$/i.test(singular))
|
|
24
|
+
return `${singular.slice(0, -1)}ies`;
|
|
25
|
+
return `${singular}s`;
|
|
26
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a string into a URL-friendly slug: lower-cased, accents removed,
|
|
3
|
+
* non-alphanumeric runs collapsed into single hyphens.
|
|
4
|
+
*
|
|
5
|
+
* @param str - The string to slugify.
|
|
6
|
+
* @returns The slugified string.
|
|
7
|
+
* @example
|
|
8
|
+
* slugify("Héllo, World!"); // => "hello-world"
|
|
9
|
+
* slugify(" Multiple spaces "); // => "multiple-spaces"
|
|
10
|
+
*/
|
|
11
|
+
export declare function slugify(str: string): string;
|
|
12
|
+
//# sourceMappingURL=slugify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slugify.d.ts","sourceRoot":"","sources":["../../src/string/slugify.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAO3C"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a string into a URL-friendly slug: lower-cased, accents removed,
|
|
3
|
+
* non-alphanumeric runs collapsed into single hyphens.
|
|
4
|
+
*
|
|
5
|
+
* @param str - The string to slugify.
|
|
6
|
+
* @returns The slugified string.
|
|
7
|
+
* @example
|
|
8
|
+
* slugify("Héllo, World!"); // => "hello-world"
|
|
9
|
+
* slugify(" Multiple spaces "); // => "multiple-spaces"
|
|
10
|
+
*/
|
|
11
|
+
export function slugify(str) {
|
|
12
|
+
return str
|
|
13
|
+
.normalize("NFKD")
|
|
14
|
+
.replace(/[̀-ͯ]/g, "")
|
|
15
|
+
.toLowerCase()
|
|
16
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
17
|
+
.replace(/^-+|-+$/g, "");
|
|
18
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncates a string to a maximum length, appending a suffix when it was cut.
|
|
3
|
+
*
|
|
4
|
+
* @param str - The string to truncate.
|
|
5
|
+
* @param maxLength - The maximum length of the result, suffix included.
|
|
6
|
+
* @param suffix - Appended when the string is truncated (defaults to `"…"`).
|
|
7
|
+
* @returns `str` unchanged if it already fits, otherwise the truncated string.
|
|
8
|
+
* @example
|
|
9
|
+
* truncate("Hello world", 8); // => "Hello w…"
|
|
10
|
+
* truncate("Hi", 8); // => "Hi"
|
|
11
|
+
*/
|
|
12
|
+
export declare function truncate(str: string, maxLength: number, suffix?: string): string;
|
|
13
|
+
//# sourceMappingURL=truncate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"truncate.d.ts","sourceRoot":"","sources":["../../src/string/truncate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,SAAM,GAAG,MAAM,CAI7E"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncates a string to a maximum length, appending a suffix when it was cut.
|
|
3
|
+
*
|
|
4
|
+
* @param str - The string to truncate.
|
|
5
|
+
* @param maxLength - The maximum length of the result, suffix included.
|
|
6
|
+
* @param suffix - Appended when the string is truncated (defaults to `"…"`).
|
|
7
|
+
* @returns `str` unchanged if it already fits, otherwise the truncated string.
|
|
8
|
+
* @example
|
|
9
|
+
* truncate("Hello world", 8); // => "Hello w…"
|
|
10
|
+
* truncate("Hi", 8); // => "Hi"
|
|
11
|
+
*/
|
|
12
|
+
export function truncate(str, maxLength, suffix = "…") {
|
|
13
|
+
if (str.length <= maxLength)
|
|
14
|
+
return str;
|
|
15
|
+
const sliceLength = Math.max(0, maxLength - suffix.length);
|
|
16
|
+
return str.slice(0, sliceLength) + suffix;
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forthtilliath/ts-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"dist"
|
|
18
18
|
],
|
|
19
19
|
"exports": {
|
|
20
|
+
".": "./dist/index.js",
|
|
20
21
|
"./*": "./dist/*.js"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
@@ -24,7 +25,7 @@
|
|
|
24
25
|
"jsdom": "^29.1.1",
|
|
25
26
|
"typescript": "^5.9.3",
|
|
26
27
|
"vitest": "^4.1.10",
|
|
27
|
-
"@forthtilliath/eslint-config": "0.
|
|
28
|
+
"@forthtilliath/eslint-config": "0.4.2",
|
|
28
29
|
"@forthtilliath/typescript-config": "0.0.0"
|
|
29
30
|
},
|
|
30
31
|
"scripts": {
|