lowercase-keys 3.0.0 → 4.0.1
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/index.d.ts +47 -1
- package/index.js +25 -2
- package/package.json +10 -6
- package/readme.md +29 -4
package/index.d.ts
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
+
export type OnConflictArgument<T> = {
|
|
2
|
+
/**
|
|
3
|
+
The lowercased key that conflicts.
|
|
4
|
+
*/
|
|
5
|
+
key: string;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
The new value that is being set.
|
|
9
|
+
*/
|
|
10
|
+
newValue: T;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
The existing value that was already set.
|
|
14
|
+
*/
|
|
15
|
+
existingValue: T;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type Options<T> = {
|
|
19
|
+
/**
|
|
20
|
+
A function that is called when multiple keys in the input object map to the same lowercased key.
|
|
21
|
+
|
|
22
|
+
The function receives the lowercased key, the new value, and the existing value, and should return the value to use.
|
|
23
|
+
|
|
24
|
+
By default, the last value wins.
|
|
25
|
+
|
|
26
|
+
@example
|
|
27
|
+
```
|
|
28
|
+
import lowercaseKeys from 'lowercase-keys';
|
|
29
|
+
|
|
30
|
+
// Throw on conflict
|
|
31
|
+
lowercaseKeys({Key: 'value1', key: 'value2'}, {
|
|
32
|
+
onConflict({key}) {
|
|
33
|
+
throw new Error(`Duplicate key: ${key}`);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Keep the first value
|
|
38
|
+
lowercaseKeys({Key: 'value1', key: 'value2'}, {
|
|
39
|
+
onConflict: ({existingValue}) => existingValue,
|
|
40
|
+
});
|
|
41
|
+
//=> {key: 'value1'}
|
|
42
|
+
```
|
|
43
|
+
*/
|
|
44
|
+
onConflict?: (argument: OnConflictArgument<T>) => T;
|
|
45
|
+
};
|
|
46
|
+
|
|
1
47
|
/**
|
|
2
48
|
Lowercase the keys of an object.
|
|
3
49
|
|
|
@@ -11,4 +57,4 @@ lowercaseKeys({FOO: true, bAr: false});
|
|
|
11
57
|
//=> {foo: true, bar: false}
|
|
12
58
|
```
|
|
13
59
|
*/
|
|
14
|
-
export default function lowercaseKeys<T>(object: Record<string, T>): Record<string, T>;
|
|
60
|
+
export default function lowercaseKeys<T>(object: Record<string, T>, options?: Options<T>): Record<string, T>;
|
package/index.js
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
-
export default function lowercaseKeys(object) {
|
|
2
|
-
|
|
1
|
+
export default function lowercaseKeys(object, {onConflict} = {}) {
|
|
2
|
+
if (typeof object !== 'object' || object === null) {
|
|
3
|
+
throw new TypeError(`Expected an object, got ${object === null ? 'null' : typeof object}`);
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const result = {};
|
|
7
|
+
|
|
8
|
+
for (const [key, value] of Object.entries(object)) {
|
|
9
|
+
const lowercasedKey = key.toLowerCase();
|
|
10
|
+
const hasExistingKey = Object.hasOwn(result, lowercasedKey);
|
|
11
|
+
const existingValue = hasExistingKey ? result[lowercasedKey] : undefined;
|
|
12
|
+
|
|
13
|
+
const resolvedValue = onConflict && hasExistingKey
|
|
14
|
+
? onConflict({key: lowercasedKey, newValue: value, existingValue})
|
|
15
|
+
: value;
|
|
16
|
+
|
|
17
|
+
Object.defineProperty(result, lowercasedKey, {
|
|
18
|
+
value: resolvedValue,
|
|
19
|
+
writable: true,
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return result;
|
|
3
26
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lowercase-keys",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"description": "Lowercase the keys of an object",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/lowercase-keys",
|
|
@@ -11,9 +11,13 @@
|
|
|
11
11
|
"url": "https://sindresorhus.com"
|
|
12
12
|
},
|
|
13
13
|
"type": "module",
|
|
14
|
-
"exports":
|
|
14
|
+
"exports": {
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"default": "./index.js"
|
|
17
|
+
},
|
|
18
|
+
"sideEffects": false,
|
|
15
19
|
"engines": {
|
|
16
|
-
"node": "
|
|
20
|
+
"node": ">=20"
|
|
17
21
|
},
|
|
18
22
|
"scripts": {
|
|
19
23
|
"test": "xo && ava && tsd"
|
|
@@ -34,8 +38,8 @@
|
|
|
34
38
|
"key"
|
|
35
39
|
],
|
|
36
40
|
"devDependencies": {
|
|
37
|
-
"ava": "^
|
|
38
|
-
"tsd": "^0.
|
|
39
|
-
"xo": "^
|
|
41
|
+
"ava": "^6.4.1",
|
|
42
|
+
"tsd": "^0.33.0",
|
|
43
|
+
"xo": "^1.2.3"
|
|
40
44
|
}
|
|
41
45
|
}
|
package/readme.md
CHANGED
|
@@ -21,12 +21,37 @@ lowercaseKeys({FOO: true, bAr: false});
|
|
|
21
21
|
|
|
22
22
|
## API
|
|
23
23
|
|
|
24
|
-
### lowercaseKeys(object)
|
|
24
|
+
### lowercaseKeys(object, options?)
|
|
25
25
|
|
|
26
26
|
Returns a new object with the keys lowercased.
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
#### options
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
Type: `object`
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
##### onConflict
|
|
33
|
+
|
|
34
|
+
Type: `Function`
|
|
35
|
+
|
|
36
|
+
A function that is called when multiple keys in the input object map to the same lowercased key.
|
|
37
|
+
|
|
38
|
+
The function receives the lowercased key, the new value, and the existing value, and should return the value to use.
|
|
39
|
+
|
|
40
|
+
By default, the last value wins.
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
import lowercaseKeys from 'lowercase-keys';
|
|
44
|
+
|
|
45
|
+
// Throw on conflict
|
|
46
|
+
lowercaseKeys({Key: 'value1', key: 'value2'}, {
|
|
47
|
+
onConflict({key}) {
|
|
48
|
+
throw new Error(`Duplicate key: ${key}`);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Keep the first value
|
|
53
|
+
lowercaseKeys({Key: 'value1', key: 'value2'}, {
|
|
54
|
+
onConflict: ({existingValue}) => existingValue,
|
|
55
|
+
});
|
|
56
|
+
//=> {key: 'value1'}
|
|
57
|
+
```
|