@forthtilliath/ts-kit 0.2.0 → 0.4.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 +104 -19
- package/dist/array/getMostRecentIds.d.ts +21 -0
- package/dist/array/getMostRecentIds.d.ts.map +1 -0
- package/dist/array/getMostRecentIds.js +30 -0
- package/dist/array/index.d.ts +3 -0
- package/dist/array/index.d.ts.map +1 -1
- package/dist/array/index.js +3 -0
- package/dist/array/nextInCycle.d.ts +15 -0
- package/dist/array/nextInCycle.d.ts.map +1 -0
- package/dist/array/nextInCycle.js +19 -0
- package/dist/array/rankByNameMatch.d.ts +19 -0
- package/dist/array/rankByNameMatch.d.ts.map +1 -0
- package/dist/array/rankByNameMatch.js +45 -0
- 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/getPeriodStartMs.d.ts +16 -0
- package/dist/date/getPeriodStartMs.d.ts.map +1 -0
- package/dist/date/getPeriodStartMs.js +24 -0
- package/dist/date/index.d.ts +5 -0
- package/dist/date/index.d.ts.map +1 -0
- package/dist/date/index.js +4 -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/index.d.ts +2 -0
- package/dist/files/index.d.ts.map +1 -0
- package/dist/files/index.js +1 -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/formatCsvNumber.d.ts +14 -0
- package/dist/number/formatCsvNumber.d.ts.map +1 -0
- package/dist/number/formatCsvNumber.js +15 -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 +6 -0
- package/dist/number/index.d.ts.map +1 -0
- package/dist/number/index.js +5 -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/escapeCsvField.d.ts +14 -0
- package/dist/string/escapeCsvField.d.ts.map +1 -0
- package/dist/string/escapeCsvField.js +17 -0
- package/dist/string/escapeHtml.d.ts +12 -0
- package/dist/string/escapeHtml.d.ts.map +1 -0
- package/dist/string/escapeHtml.js +17 -0
- package/dist/string/index.d.ts +8 -0
- package/dist/string/index.d.ts.map +1 -0
- package/dist/string/index.js +7 -0
- package/dist/string/normalizeForSearch.d.ts +12 -0
- package/dist/string/normalizeForSearch.d.ts.map +1 -0
- package/dist/string/normalizeForSearch.js +25 -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,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rounds a number to a given number of decimal places.
|
|
3
|
+
*
|
|
4
|
+
* @param value - The number to round.
|
|
5
|
+
* @param decimals - The number of decimal places (default: 0).
|
|
6
|
+
* @returns The rounded number.
|
|
7
|
+
* @example
|
|
8
|
+
* round(1.2345, 2); // => 1.23
|
|
9
|
+
* round(1.005, 2); // => 1.01
|
|
10
|
+
*/
|
|
11
|
+
export declare function round(value: number, decimals?: number): number;
|
|
12
|
+
//# sourceMappingURL=round.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"round.d.ts","sourceRoot":"","sources":["../../src/number/round.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,SAAI,GAAG,MAAM,CAGzD"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rounds a number to a given number of decimal places.
|
|
3
|
+
*
|
|
4
|
+
* @param value - The number to round.
|
|
5
|
+
* @param decimals - The number of decimal places (default: 0).
|
|
6
|
+
* @returns The rounded number.
|
|
7
|
+
* @example
|
|
8
|
+
* round(1.2345, 2); // => 1.23
|
|
9
|
+
* round(1.005, 2); // => 1.01
|
|
10
|
+
*/
|
|
11
|
+
export function round(value, decimals = 0) {
|
|
12
|
+
const factor = 10 ** decimals;
|
|
13
|
+
return Math.round((value + Number.EPSILON) * factor) / factor;
|
|
14
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep clones a value.
|
|
3
|
+
*
|
|
4
|
+
* Uses the native `structuredClone` when available (handles `Date`, `Map`,
|
|
5
|
+
* `Set`, circular references, etc.), and falls back to a JSON round-trip
|
|
6
|
+
* otherwise — which loses `undefined` values, functions, and non-JSON types.
|
|
7
|
+
*
|
|
8
|
+
* @param value - The value to clone.
|
|
9
|
+
* @returns A deep copy of `value`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function deepClone<T>(value: T): T;
|
|
12
|
+
//# sourceMappingURL=deepClone.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deepClone.d.ts","sourceRoot":"","sources":["../../src/object/deepClone.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAKxC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep clones a value.
|
|
3
|
+
*
|
|
4
|
+
* Uses the native `structuredClone` when available (handles `Date`, `Map`,
|
|
5
|
+
* `Set`, circular references, etc.), and falls back to a JSON round-trip
|
|
6
|
+
* otherwise — which loses `undefined` values, functions, and non-JSON types.
|
|
7
|
+
*
|
|
8
|
+
* @param value - The value to clone.
|
|
9
|
+
* @returns A deep copy of `value`.
|
|
10
|
+
*/
|
|
11
|
+
export function deepClone(value) {
|
|
12
|
+
if (typeof structuredClone === "function") {
|
|
13
|
+
return structuredClone(value);
|
|
14
|
+
}
|
|
15
|
+
return JSON.parse(JSON.stringify(value));
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively compares two values for structural equality.
|
|
3
|
+
*
|
|
4
|
+
* @param a - The first value.
|
|
5
|
+
* @param b - The second value.
|
|
6
|
+
* @returns `true` if `a` and `b` are deeply equal.
|
|
7
|
+
* @example
|
|
8
|
+
* deepEqual({ a: [1, 2] }, { a: [1, 2] }); // => true
|
|
9
|
+
* deepEqual({ a: 1 }, { a: 2 }); // => false
|
|
10
|
+
*/
|
|
11
|
+
export declare function deepEqual(a: unknown, b: unknown): boolean;
|
|
12
|
+
//# sourceMappingURL=deepEqual.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deepEqual.d.ts","sourceRoot":"","sources":["../../src/object/deepEqual.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAwBzD"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively compares two values for structural equality.
|
|
3
|
+
*
|
|
4
|
+
* @param a - The first value.
|
|
5
|
+
* @param b - The second value.
|
|
6
|
+
* @returns `true` if `a` and `b` are deeply equal.
|
|
7
|
+
* @example
|
|
8
|
+
* deepEqual({ a: [1, 2] }, { a: [1, 2] }); // => true
|
|
9
|
+
* deepEqual({ a: 1 }, { a: 2 }); // => false
|
|
10
|
+
*/
|
|
11
|
+
export function deepEqual(a, b) {
|
|
12
|
+
if (Object.is(a, b))
|
|
13
|
+
return true;
|
|
14
|
+
if (typeof a !== typeof b || a === null || b === null)
|
|
15
|
+
return false;
|
|
16
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
17
|
+
if (!Array.isArray(a) || !Array.isArray(b))
|
|
18
|
+
return false;
|
|
19
|
+
if (a.length !== b.length)
|
|
20
|
+
return false;
|
|
21
|
+
return a.every((item, index) => deepEqual(item, b[index]));
|
|
22
|
+
}
|
|
23
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
24
|
+
const aKeys = Object.keys(a);
|
|
25
|
+
const bKeys = Object.keys(b);
|
|
26
|
+
if (aKeys.length !== bKeys.length)
|
|
27
|
+
return false;
|
|
28
|
+
return aKeys.every((key) => deepEqual(a[key], b[key]));
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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 declare function groupBy<T, K extends PropertyKey>(array: readonly T[], getKey: (item: T) => K): Record<K, T[]>;
|
|
12
|
+
//# sourceMappingURL=groupBy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"groupBy.d.ts","sourceRoot":"","sources":["../../src/object/groupBy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,EAC9C,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GACrB,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAOhB"}
|
|
@@ -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,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes a CSV field per RFC 4180: wraps it in double quotes and doubles any
|
|
3
|
+
* internal double quote if the value contains a double quote, a semicolon
|
|
4
|
+
* (the delimiter), or a newline — returned as-is otherwise.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The raw field value.
|
|
7
|
+
* @returns The value, quoted and escaped only if needed.
|
|
8
|
+
* @example
|
|
9
|
+
* escapeCsvField("Apple"); // => "Apple"
|
|
10
|
+
* escapeCsvField("a;b"); // => '"a;b"'
|
|
11
|
+
* escapeCsvField('He said "hi"'); // => '"He said ""hi"""'
|
|
12
|
+
*/
|
|
13
|
+
export declare function escapeCsvField(value: string): string;
|
|
14
|
+
//# sourceMappingURL=escapeCsvField.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"escapeCsvField.d.ts","sourceRoot":"","sources":["../../src/string/escapeCsvField.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAGpD"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes a CSV field per RFC 4180: wraps it in double quotes and doubles any
|
|
3
|
+
* internal double quote if the value contains a double quote, a semicolon
|
|
4
|
+
* (the delimiter), or a newline — returned as-is otherwise.
|
|
5
|
+
*
|
|
6
|
+
* @param value - The raw field value.
|
|
7
|
+
* @returns The value, quoted and escaped only if needed.
|
|
8
|
+
* @example
|
|
9
|
+
* escapeCsvField("Apple"); // => "Apple"
|
|
10
|
+
* escapeCsvField("a;b"); // => '"a;b"'
|
|
11
|
+
* escapeCsvField('He said "hi"'); // => '"He said ""hi"""'
|
|
12
|
+
*/
|
|
13
|
+
export function escapeCsvField(value) {
|
|
14
|
+
if (!/[";\n]/.test(value))
|
|
15
|
+
return value;
|
|
16
|
+
return `"${value.replace(/"/g, '""')}"`;
|
|
17
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes the HTML entities that matter when inserting arbitrary text into
|
|
3
|
+
* an HTML template (`&`, `<`, `>`, `"`), without pulling in a templating
|
|
4
|
+
* dependency.
|
|
5
|
+
*
|
|
6
|
+
* @param text - The text to escape.
|
|
7
|
+
* @returns `text` with `&`, `<`, `>` and `"` replaced by their HTML entities.
|
|
8
|
+
* @example
|
|
9
|
+
* escapeHtml("Tom & Jerry <script>"); // => "Tom & Jerry <script>"
|
|
10
|
+
*/
|
|
11
|
+
export declare function escapeHtml(text: string): string;
|
|
12
|
+
//# sourceMappingURL=escapeHtml.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"escapeHtml.d.ts","sourceRoot":"","sources":["../../src/string/escapeHtml.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM/C"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes the HTML entities that matter when inserting arbitrary text into
|
|
3
|
+
* an HTML template (`&`, `<`, `>`, `"`), without pulling in a templating
|
|
4
|
+
* dependency.
|
|
5
|
+
*
|
|
6
|
+
* @param text - The text to escape.
|
|
7
|
+
* @returns `text` with `&`, `<`, `>` and `"` replaced by their HTML entities.
|
|
8
|
+
* @example
|
|
9
|
+
* escapeHtml("Tom & Jerry <script>"); // => "Tom & Jerry <script>"
|
|
10
|
+
*/
|
|
11
|
+
export function escapeHtml(text) {
|
|
12
|
+
return text
|
|
13
|
+
.replace(/&/g, "&")
|
|
14
|
+
.replace(/</g, "<")
|
|
15
|
+
.replace(/>/g, ">")
|
|
16
|
+
.replace(/"/g, """);
|
|
17
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./capitalize.js";
|
|
2
|
+
export * from "./escapeCsvField.js";
|
|
3
|
+
export * from "./escapeHtml.js";
|
|
4
|
+
export * from "./normalizeForSearch.js";
|
|
5
|
+
export * from "./pluralize.js";
|
|
6
|
+
export * from "./slugify.js";
|
|
7
|
+
export * from "./truncate.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/string/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalizes text for accent/case-insensitive search matching: expands
|
|
3
|
+
* œ/æ ligatures, strips accents, lower-cases, and trims.
|
|
4
|
+
*
|
|
5
|
+
* @param text - The text to normalize.
|
|
6
|
+
* @returns The normalized text.
|
|
7
|
+
* @example
|
|
8
|
+
* normalizeForSearch("Pâtes À La Crème"); // => "pates a la creme"
|
|
9
|
+
* normalizeForSearch("Œuf"); // => "oeuf"
|
|
10
|
+
*/
|
|
11
|
+
export declare function normalizeForSearch(text: string): string;
|
|
12
|
+
//# sourceMappingURL=normalizeForSearch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalizeForSearch.d.ts","sourceRoot":"","sources":["../../src/string/normalizeForSearch.ts"],"names":[],"mappings":"AASA;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAMvD"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// The œ/æ ligatures do NOT decompose via normalize("NFD") (unlike accented
|
|
2
|
+
// letters): they're standalone Unicode letters, not a base + combining mark.
|
|
3
|
+
// Without this explicit replacement, searching "Œuf" (the real ligature)
|
|
4
|
+
// never matches "Oeuf" in data spelled without the ligature (as most text
|
|
5
|
+
// databases are).
|
|
6
|
+
function expandLigatures(text) {
|
|
7
|
+
return text.replace(/[œŒ]/g, "oe").replace(/[æÆ]/g, "ae");
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Normalizes text for accent/case-insensitive search matching: expands
|
|
11
|
+
* œ/æ ligatures, strips accents, lower-cases, and trims.
|
|
12
|
+
*
|
|
13
|
+
* @param text - The text to normalize.
|
|
14
|
+
* @returns The normalized text.
|
|
15
|
+
* @example
|
|
16
|
+
* normalizeForSearch("Pâtes À La Crème"); // => "pates a la creme"
|
|
17
|
+
* normalizeForSearch("Œuf"); // => "oeuf"
|
|
18
|
+
*/
|
|
19
|
+
export function normalizeForSearch(text) {
|
|
20
|
+
return expandLigatures(text)
|
|
21
|
+
.normalize("NFD")
|
|
22
|
+
.replace(/[̀-ͯ]/g, "")
|
|
23
|
+
.toLowerCase()
|
|
24
|
+
.trim();
|
|
25
|
+
}
|
|
@@ -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
|
+
}
|