1 | ;
|
2 | /*
|
3 | * Copyright (c) 2021, salesforce.com, inc.
|
4 | * All rights reserved.
|
5 | * Licensed under the BSD 3-Clause license.
|
6 | * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
7 | */
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | const ts_types_1 = require("@salesforce/ts-types");
|
10 | /**
|
11 | * Use mapKeys to convert object keys to another format using the specified conversion function.
|
12 | *
|
13 | * E.g., to deep convert all object keys to camelCase: mapKeys(myObj, _.camelCase, true)
|
14 | * to shallow convert object keys to lower case: mapKeys(myObj, _.toLower)
|
15 | *
|
16 | * NOTE: This mutates the object passed in for conversion.
|
17 | *
|
18 | * @param target - {Object} The object to convert the keys
|
19 | * @param converter - {Function} The function that converts the object key
|
20 | * @param deep - {boolean} Whether to do a deep object key conversion
|
21 | * @return {Object} - the object with the converted keys
|
22 | */
|
23 | function mapKeys(
|
24 | // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
25 | obj, converter, deep) {
|
26 | // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
27 | const target = Object.assign({}, obj);
|
28 | return Object.fromEntries(
|
29 | // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
30 | Object.entries(target).map(([key, value]) => {
|
31 | const k = converter.call(null, key);
|
32 | if (deep) {
|
33 | let v = value;
|
34 | if (Array.isArray(value)) {
|
35 | v = value.map((v1) => {
|
36 | if ((0, ts_types_1.isPlainObject)(v1)) {
|
37 | return mapKeys(v1, converter, deep);
|
38 | }
|
39 | // eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
40 | return v1;
|
41 | });
|
42 | }
|
43 | else if ((0, ts_types_1.isPlainObject)(value)) {
|
44 | v = mapKeys(value, converter, deep);
|
45 | }
|
46 | return [k, v];
|
47 | }
|
48 | return [k, value];
|
49 | }));
|
50 | }
|
51 | exports.default = mapKeys;
|
52 | //# sourceMappingURL=mapKeys.js.map |
\ | No newline at end of file |