From 6e84e41c3af1163aee7aed5649031275e9f81eaa Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Mon, 10 Dec 2018 09:53:13 +0000 Subject: [PATCH] Travis build: 875 --- README.md | 61 +++++++++++++++++++++++++++++++++++++++++ docs/about.html | 1 + docs/adapter.html | 2 +- docs/archive.html | 2 +- docs/browser.html | 2 +- docs/contributing.html | 1 + docs/date.html | 2 +- docs/function.html | 2 +- docs/glossary.html | 2 +- docs/index.html | 2 +- docs/math.html | 2 +- docs/node.html | 2 +- docs/object.html | 40 ++++++++++++++++++++++++++- docs/string.html | 2 +- docs/type.html | 2 +- docs/utility.html | 2 +- snippets/deepMapKeys.md | 32 ++++++++++----------- test/_30s.js | 13 ++++++++- 18 files changed, 141 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 54144a1c7..be4d6ab5d 100644 --- a/README.md +++ b/README.md @@ -363,6 +363,7 @@ _30s.average(1, 2, 3); * [`bindAll`](#bindall) * [`deepClone`](#deepclone) * [`deepFreeze`](#deepfreeze) +* [`deepMapKeys`](#deepmapkeys-) * [`defaults`](#defaults) * [`dig`](#dig) * [`equals`](#equals-) @@ -6707,6 +6708,66 @@ o[1][0] = 4; // not allowed as well
[⬆ Back to top](#contents) +### deepMapKeys ![advanced](/advanced.svg) + +Deep maps an object keys. + +Creates an object with the same values as the provided object and keys generated by running the provided function for each key. + +Use `Object.keys(obj)` to iterate over the object's keys. +Use `Array.prototype.reduce()` to create a new object with the same values and mapped keys using `fn`. + +```js +const deepMapKeys = (obj, f) => + Array.isArray(obj) + ? obj.map(val => deepMapKeys(val, f)) + : typeof obj === 'object' + ? Object.keys(obj).reduce((acc, current) => { + const val = obj[current]; + acc[f(current)] = + val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val); + return acc; + }, {}) + : obj; +``` + +
+Examples + +```js +const obj = { + foo: '1', + nested: { + child: { + withArray: [ + { + grandChild: ['hello'] + } + ] + } + } +}; +const upperKeysObj = deepMapKeys(obj, key => key.toUpperCase()); +/* +{ + "FOO":"1", + "NESTED":{ + "CHILD":{ + "WITHARRAY":[ + { + "GRANDCHILD":[ 'hello' ] + } + ] + } + } +} +*/ +``` + +
+ +
[⬆ Back to top](#contents) + ### defaults Assigns default values for all properties in an object that are `undefined`. diff --git a/docs/about.html b/docs/about.html index 06e9950b4..e7506e1bf 100644 --- a/docs/about.html +++ b/docs/about.html @@ -378,6 +378,7 @@