lowercase-keys 3.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.
Files changed (4) hide show
  1. package/index.d.ts +47 -1
  2. package/index.js +21 -2
  3. package/package.json +10 -6
  4. 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,22 @@
1
- export default function lowercaseKeys(object) {
2
- return Object.fromEntries(Object.entries(object).map(([key, value]) => [key.toLowerCase(), value]));
1
+ export default function lowercaseKeys(object, {onConflict} = {}) {
2
+ const result = {};
3
+
4
+ for (const [key, value] of Object.entries(object)) {
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
+ });
19
+ }
20
+
21
+ return result;
3
22
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lowercase-keys",
3
- "version": "3.0.0",
3
+ "version": "4.0.0",
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": "./index.js",
14
+ "exports": {
15
+ "types": "./index.d.ts",
16
+ "default": "./index.js"
17
+ },
18
+ "sideEffects": false,
15
19
  "engines": {
16
- "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
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": "^3.15.0",
38
- "tsd": "^0.18.0",
39
- "xo": "^0.45.0"
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
- ## lowercase-keys for enterprise
28
+ #### options
29
29
 
30
- Available as part of the Tidelift Subscription.
30
+ Type: `object`
31
31
 
32
- The maintainers of lowercase-keys and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-lowercase-keys?utm_source=npm-lowercase-keys&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
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
+ ```