lowercase-keys 2.0.0 → 4.0.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/index.d.ts +48 -4
- package/index.js +16 -4
- package/license +1 -1
- package/package.json +13 -6
- package/readme.md +34 -9
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
|
|
|
@@ -5,12 +51,10 @@ Lowercase the keys of an object.
|
|
|
5
51
|
|
|
6
52
|
@example
|
|
7
53
|
```
|
|
8
|
-
import lowercaseKeys
|
|
54
|
+
import lowercaseKeys from 'lowercase-keys';
|
|
9
55
|
|
|
10
56
|
lowercaseKeys({FOO: true, bAr: false});
|
|
11
57
|
//=> {foo: true, bar: false}
|
|
12
58
|
```
|
|
13
59
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export = lowercaseKeys;
|
|
60
|
+
export default function lowercaseKeys<T>(object: Record<string, T>, options?: Options<T>): Record<string, T>;
|
package/index.js
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
module.exports = object => {
|
|
1
|
+
export default function lowercaseKeys(object, {onConflict} = {}) {
|
|
3
2
|
const result = {};
|
|
4
3
|
|
|
5
4
|
for (const [key, value] of Object.entries(object)) {
|
|
6
|
-
|
|
5
|
+
const lowercasedKey = key.toLowerCase();
|
|
6
|
+
const hasExistingKey = Object.hasOwn(result, lowercasedKey);
|
|
7
|
+
const existingValue = hasExistingKey ? result[lowercasedKey] : undefined;
|
|
8
|
+
|
|
9
|
+
const resolvedValue = onConflict && hasExistingKey
|
|
10
|
+
? onConflict({key: lowercasedKey, newValue: value, existingValue})
|
|
11
|
+
: value;
|
|
12
|
+
|
|
13
|
+
Object.defineProperty(result, lowercasedKey, {
|
|
14
|
+
value: resolvedValue,
|
|
15
|
+
writable: true,
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
});
|
|
7
19
|
}
|
|
8
20
|
|
|
9
21
|
return result;
|
|
10
|
-
}
|
|
22
|
+
}
|
package/license
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
package/package.json
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lowercase-keys",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Lowercase the keys of an object",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/lowercase-keys",
|
|
7
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
7
8
|
"author": {
|
|
8
9
|
"name": "Sindre Sorhus",
|
|
9
10
|
"email": "sindresorhus@gmail.com",
|
|
10
|
-
"url": "sindresorhus.com"
|
|
11
|
+
"url": "https://sindresorhus.com"
|
|
11
12
|
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"default": "./index.js"
|
|
17
|
+
},
|
|
18
|
+
"sideEffects": false,
|
|
12
19
|
"engines": {
|
|
13
|
-
"node": ">=
|
|
20
|
+
"node": ">=20"
|
|
14
21
|
},
|
|
15
22
|
"scripts": {
|
|
16
23
|
"test": "xo && ava && tsd"
|
|
@@ -31,8 +38,8 @@
|
|
|
31
38
|
"key"
|
|
32
39
|
],
|
|
33
40
|
"devDependencies": {
|
|
34
|
-
"ava": "^
|
|
35
|
-
"tsd": "^0.
|
|
36
|
-
"xo": "^
|
|
41
|
+
"ava": "^6.4.1",
|
|
42
|
+
"tsd": "^0.33.0",
|
|
43
|
+
"xo": "^1.2.3"
|
|
37
44
|
}
|
|
38
45
|
}
|
package/readme.md
CHANGED
|
@@ -1,32 +1,57 @@
|
|
|
1
|
-
# lowercase-keys
|
|
1
|
+
# lowercase-keys
|
|
2
2
|
|
|
3
3
|
> Lowercase the keys of an object
|
|
4
4
|
|
|
5
|
+
Check out [`map-obj`](https://github.com/sindresorhus/map-obj) if you need support for deep iteration.
|
|
5
6
|
|
|
6
7
|
## Install
|
|
7
8
|
|
|
9
|
+
```sh
|
|
10
|
+
npm install lowercase-keys
|
|
8
11
|
```
|
|
9
|
-
$ npm install lowercase-keys
|
|
10
|
-
```
|
|
11
|
-
|
|
12
12
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
15
|
```js
|
|
16
|
-
|
|
16
|
+
import lowercaseKeys from 'lowercase-keys';
|
|
17
17
|
|
|
18
18
|
lowercaseKeys({FOO: true, bAr: false});
|
|
19
19
|
//=> {foo: true, bar: false}
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
-
|
|
23
22
|
## API
|
|
24
23
|
|
|
25
|
-
### lowercaseKeys(object)
|
|
24
|
+
### lowercaseKeys(object, options?)
|
|
26
25
|
|
|
27
26
|
Returns a new object with the keys lowercased.
|
|
28
27
|
|
|
28
|
+
#### options
|
|
29
|
+
|
|
30
|
+
Type: `object`
|
|
31
|
+
|
|
32
|
+
##### onConflict
|
|
29
33
|
|
|
30
|
-
|
|
34
|
+
Type: `Function`
|
|
31
35
|
|
|
32
|
-
|
|
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
|
+
```
|