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 | const target = Object.assign({}, obj);
|
27 | return Object.fromEntries(Object.entries(target).map(([key, value]) => {
|
28 | const k = converter.call(null, key);
|
29 | if (deep) {
|
30 | let v = value;
|
31 | if (Array.isArray(value)) {
|
32 | v = value.map((v1) => {
|
33 | if (ts_types_1.isPlainObject(v1)) {
|
34 | return mapKeys(v1, converter, deep);
|
35 | }
|
36 | return v1;
|
37 | });
|
38 | }
|
39 | else if (ts_types_1.isPlainObject(value)) {
|
40 | v = mapKeys(value, converter, deep);
|
41 | }
|
42 | return [k, v];
|
43 | }
|
44 | return [k, value];
|
45 | }));
|
46 | }
|
47 | exports.default = mapKeys;
|
48 | //# sourceMappingURL=mapKeys.js.map |
\ | No newline at end of file |