@germondai/ts-utils 0.0.2 → 0.0.3
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/dist/module.js +7 -11
- package/dist/runtime/collection.d.ts +38 -4
- package/package.json +1 -1
package/dist/module.js
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
// src/runtime/collection.ts
|
|
2
|
-
var hasDuplicates = (array, keyExtractor) =>
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
return false;
|
|
11
|
-
};
|
|
12
|
-
var uniqueArray = (array) => [...new Set(array)];
|
|
2
|
+
var hasDuplicates = (array, keyExtractor) => new Set(array.map((value, index) => keyExtractor?.(value, index, array) || value)).size < array.length;
|
|
3
|
+
var uniqueArray = (array, keyExtractor) => [
|
|
4
|
+
...new Map(array.map((value, index, array2) => [
|
|
5
|
+
keyExtractor?.(value, index, array2) || value,
|
|
6
|
+
value
|
|
7
|
+
])).values()
|
|
8
|
+
];
|
|
13
9
|
// src/runtime/constants.ts
|
|
14
10
|
var SUPPORTED_DISPLAYABLE_MEDIA_TYPES = {
|
|
15
11
|
images: [
|
|
@@ -1,19 +1,53 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Determines whether an array contains duplicate values.
|
|
3
3
|
*
|
|
4
4
|
* @param array - The array to check for duplicates.
|
|
5
|
+
* @param keyExtractor - Optional function to extract a key for comparison. If provided, duplicates are determined based on the extracted key.
|
|
5
6
|
* @returns True if the array contains duplicates, false otherwise.
|
|
6
7
|
*
|
|
7
8
|
* @example
|
|
9
|
+
* // Basic usage with primitive values
|
|
8
10
|
* hasDuplicates([1, 2, 3, 4, 1]); // true
|
|
9
11
|
* hasDuplicates(['a', 'b', 'c']); // false
|
|
12
|
+
*
|
|
13
|
+
* // Usage with objects and a key extractor
|
|
10
14
|
* hasDuplicates([{ id: 1 }, { id: 2 }, { id: 1 }], (item) => item.id); // true
|
|
11
15
|
*/
|
|
12
|
-
export declare const hasDuplicates: <T>(array: T[], keyExtractor?: (
|
|
16
|
+
export declare const hasDuplicates: <T>(array: T[], keyExtractor?: (value: T, index: number, array: T[]) => string | number) => boolean;
|
|
13
17
|
/**
|
|
14
|
-
* Removes duplicates from an array.
|
|
18
|
+
* Removes duplicates from an array based on a key function.
|
|
15
19
|
*
|
|
16
20
|
* @param array - The array to process.
|
|
21
|
+
* @param keyExtractor - Function to generate a unique key for each item.
|
|
17
22
|
* @returns A new array without duplicates.
|
|
18
23
|
*/
|
|
19
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Returns a new array containing only unique elements from the input array.
|
|
26
|
+
* Uniqueness is determined based on the values returned by the `keyExtractor` function,
|
|
27
|
+
* or by the elements themselves if no `keyExtractor` is provided.
|
|
28
|
+
*
|
|
29
|
+
* @typeParam T - The type of elements in the input array.
|
|
30
|
+
* @param array - The input array from which to extract unique elements.
|
|
31
|
+
* @param keyExtractor - An optional function that extracts a key from each element
|
|
32
|
+
* to determine uniqueness. If not provided, the elements themselves are used.
|
|
33
|
+
* The function receives the current element, its index, and the entire array as arguments.
|
|
34
|
+
* @returns A new array containing only unique elements from the input array.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* // Using default behavior (elements themselves determine uniqueness)
|
|
38
|
+
* const numbers = [1, 2, 2, 3, 4, 4];
|
|
39
|
+
* const uniqueNumbers = uniqueArray(numbers);
|
|
40
|
+
* console.log(uniqueNumbers); // Output: [1, 2, 3, 4]
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* // Using a keyExtractor to determine uniqueness
|
|
44
|
+
* const users = [
|
|
45
|
+
* { id: 1, name: 'Alice' },
|
|
46
|
+
* { id: 2, name: 'Bob' },
|
|
47
|
+
* { id: 1, name: 'Alice' },
|
|
48
|
+
* ];
|
|
49
|
+
* const uniqueUsers = uniqueArray(users, user => user.id);
|
|
50
|
+
* console.log(uniqueUsers);
|
|
51
|
+
* // Output: [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
|
|
52
|
+
*/
|
|
53
|
+
export declare const uniqueArray: <T>(array: T[], keyExtractor?: (value: T, index: number, array: T[]) => string | number) => T[];
|