@baseplate-dev/utils 0.4.4 → 0.5.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/dist/arrays/assert-no-duplicates.d.ts +12 -0
- package/dist/arrays/assert-no-duplicates.d.ts.map +1 -0
- package/dist/arrays/assert-no-duplicates.js +31 -0
- package/dist/arrays/assert-no-duplicates.js.map +1 -0
- package/dist/arrays/index.d.ts +2 -0
- package/dist/arrays/index.d.ts.map +1 -0
- package/dist/arrays/index.js +2 -0
- package/dist/arrays/index.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asserts that an array has no duplicate values based on an optional key function.
|
|
3
|
+
* Throws an error if duplicates are found, listing all duplicate keys.
|
|
4
|
+
*
|
|
5
|
+
* @template T The type of elements in the array.
|
|
6
|
+
* @param items The array to check for duplicates.
|
|
7
|
+
* @param context A string describing the context for the error message (e.g., "route loader fields").
|
|
8
|
+
* @param keyFn A function that extracts the comparison key from each element. Defaults to identity.
|
|
9
|
+
* @throws Error if duplicate keys are found.
|
|
10
|
+
*/
|
|
11
|
+
export declare function assertNoDuplicates<T>(items: readonly T[], context: string, keyFn?: (item: T) => unknown): void;
|
|
12
|
+
//# sourceMappingURL=assert-no-duplicates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert-no-duplicates.d.ts","sourceRoot":"","sources":["../../src/arrays/assert-no-duplicates.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAClC,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,GAC3B,IAAI,CAqBN"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asserts that an array has no duplicate values based on an optional key function.
|
|
3
|
+
* Throws an error if duplicates are found, listing all duplicate keys.
|
|
4
|
+
*
|
|
5
|
+
* @template T The type of elements in the array.
|
|
6
|
+
* @param items The array to check for duplicates.
|
|
7
|
+
* @param context A string describing the context for the error message (e.g., "route loader fields").
|
|
8
|
+
* @param keyFn A function that extracts the comparison key from each element. Defaults to identity.
|
|
9
|
+
* @throws Error if duplicate keys are found.
|
|
10
|
+
*/
|
|
11
|
+
export function assertNoDuplicates(items, context, keyFn) {
|
|
12
|
+
const getKey = keyFn ?? ((item) => item);
|
|
13
|
+
const seen = new Map();
|
|
14
|
+
const duplicates = [];
|
|
15
|
+
for (const item of items) {
|
|
16
|
+
const key = getKey(item);
|
|
17
|
+
const count = seen.get(key) ?? 0;
|
|
18
|
+
seen.set(key, count + 1);
|
|
19
|
+
if (count === 1) {
|
|
20
|
+
// First time we see a duplicate
|
|
21
|
+
duplicates.push(key);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (duplicates.length > 0) {
|
|
25
|
+
const duplicateList = duplicates
|
|
26
|
+
.map((key) => JSON.stringify(key))
|
|
27
|
+
.join(', ');
|
|
28
|
+
throw new Error(`Duplicate ${context} found: ${duplicateList}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=assert-no-duplicates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert-no-duplicates.js","sourceRoot":"","sources":["../../src/arrays/assert-no-duplicates.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,UAAU,kBAAkB,CAChC,KAAmB,EACnB,OAAe,EACf,KAA4B;IAE5B,MAAM,MAAM,GAAG,KAAK,IAAI,CAAC,CAAC,IAAO,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC;IAC5C,MAAM,IAAI,GAAG,IAAI,GAAG,EAAmB,CAAC;IACxC,MAAM,UAAU,GAAc,EAAE,CAAC;IAEjC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;QACzB,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QACzB,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;YAChB,gCAAgC;YAChC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACvB,CAAC;IACH,CAAC;IAED,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC1B,MAAM,aAAa,GAAG,UAAU;aAC7B,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;aACjC,IAAI,CAAC,IAAI,CAAC,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,aAAa,OAAO,WAAW,aAAa,EAAE,CAAC,CAAC;IAClE,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/arrays/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/arrays/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baseplate-dev/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Shared utility functions for Baseplate",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"utilities",
|
|
@@ -51,8 +51,8 @@
|
|
|
51
51
|
"memfs": "4.15.1",
|
|
52
52
|
"prettier": "3.6.2",
|
|
53
53
|
"typescript": "5.8.3",
|
|
54
|
-
"vitest": "
|
|
55
|
-
"@baseplate-dev/tools": "0.
|
|
54
|
+
"vitest": "4.0.16",
|
|
55
|
+
"@baseplate-dev/tools": "0.5.0"
|
|
56
56
|
},
|
|
57
57
|
"engines": {
|
|
58
58
|
"node": "^22.0.0"
|