From 49c0168897e123b96e24ceffe1f53350e0472792 Mon Sep 17 00:00:00 2001 From: Ruslan Abdullaev Date: Sat, 9 Sep 2023 09:37:53 +0300 Subject: [PATCH] Add lodash _.mapKeys (#351) * add _invert * add _mapKeys --- README.md | 107 ++++++++++++++++++++++++++++++++++++++++++++++ tests/unit/all.js | 41 +++++++++++++++++- 2 files changed, 147 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c75897e..d14ff8e 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,9 @@ For more information, see [Configuring the ESLint Plugin](configuring.md) 1. [_.extend](#_extend) 1. [_.has](#_has) 1. [_.get](#_get) +1. [_.invert](#_invert) 1. [_.keys](#_keys) +1. [_.mapKeys](#_mapKeys) 1. [_.omit](#_omit) 1. [_.pick](#_pick) 1. [_.pickBy](#_pickby) @@ -2588,6 +2590,54 @@ Gets the value at path of object. **[⬆ back to top](#quick-links)** +### _.invert + +Creates an object composed of the inverted keys and values of object. If object contains duplicate values, subsequent values overwrite property assignments of previous values. + + ```js + var object = { 'a': 1, 'b': 2, 'c': 1 }; + + // Underscore/Lodash + var result = _.invert(object); + console.log(result) + // output: { '1': 'c', '2': 'b' } + + // Native (IE6) + function invert(object) { + var obj = {}; + for (var key in object) { + if (object.hasOwnProperty(key)) { + obj[object[key]] = key; + } + } + return obj; + } + var result = invert(object); + console.log(result) + // output: { '1': 'c', '2': 'b' } + + // Native (IE not supported) + const invert = object => Object.entries(object) + .reduce((acc, current) => { + acc[current[1]] = current[0]; + return acc; + }, {} + ); + ``` +#### Browser Support + +![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] +:-: | :-: | :-: | :-: | :-: | :-: | + 4.0 ✔ | ✔ | 2.0 ✔ | 6.0 ✔ | 10.0 ✔ | 3.1 ✔ | + +#### Browser Support for `Object.entries()` + +![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] +:-: | :-: | :-: | :-: | :-: | :-: | + 54.0 ✔ | 14.0 ✔ | 47.0 ✔ | ✖ | 41.0 ✔ | 10.1 ✔ | + +**[⬆ back to top](#quick-links)** + ### _.keys Retrieves all the names of the object's own enumerable properties. @@ -2612,6 +2662,63 @@ Retrieves all the names of the object's own enumerable properties. **[⬆ back to top](#quick-links)** +### _.mapKeys + +The opposite of _.mapValues; this method creates an object with the same values as object and keys generated by running each own enumerable string keyed property of object thru iteratee. The iteratee is invoked with three arguments: (value, key, object). + + ```js + // Lodash + var result = _.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) { + return key + value; + }); + console.log(result) + // output: { 'a1': 1, 'b2': 2 } + + // Native (IE6) + function mapKeys(object, cb) { + var obj = {}; + for (var key in object) { + if (object.hasOwnProperty(key)) { + var newKey = cb(object[key], key, object); + obj[newKey] = object[key]; + } + } + return obj; + } + var result = mapKeys({ 'a': 1, 'b': 2 }, function(value, key) { + return key + value; + }); + console.log(result) + // output: { 'a1': 1, 'b2': 2 } + + // Native (IE not supported) + const mapKeys = (object, cb) => Object.entries(object) + .reduce((acc, current) => { + const newKey = cb(current[1], current[0], object); + acc[newKey] = current[1]; + return acc; + }, {} + ); + const result2 = mapKeys({ 'a': 1, 'b': 2 }, function(value, key) { + return key + value; + }); + console.log(result2) + // output: { 'a1': 1, 'b2': 2 } + ``` +#### Browser Support + +![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] +:-: | :-: | :-: | :-: | :-: | :-: | + 4.0 ✔ | ✔ | 2.0 ✔ | 6.0 ✔ | 10.0 ✔ | 3.1 ✔ | + +#### Browser Support for `Object.entries()` + +![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image] +:-: | :-: | :-: | :-: | :-: | :-: | + 54.0 ✔ | 14.0 ✔ | 47.0 ✔ | ✖ | 41.0 ✔ | 10.1 ✔ | + +**[⬆ back to top](#quick-links)** + ### _.omit Returns a copy of the object, filtered to omit the keys specified. diff --git a/tests/unit/all.js b/tests/unit/all.js index d164f30..0239b8d 100644 --- a/tests/unit/all.js +++ b/tests/unit/all.js @@ -21,6 +21,45 @@ describe('code snippet example', () => { assert.deepEqual(lodashResult, nativeResult) }) + it('invert', () => { + var object = { 'a': 1, 'b': '2', 'c': 3 }; + function invert(object) { + var obj = {}; + for (var key in object) { + if (object.hasOwnProperty(key)) { + obj[object[key]] = key; + } + } + return obj; + } + assert.deepEqual( + _.invert(object), + invert(object) + ) + }) + + it('mapKeys', () => { + var object = { 'a': 1, 'b': 2 }; + function mapKeys(object, cb) { + var obj = {}; + for (var key in object) { + if (object.hasOwnProperty(key)) { + var newKey = cb(object[key], key, object); + obj[newKey] = object[key]; + } + } + return obj; + } + assert.deepEqual( + _.mapKeys(object, function (value, key) { + return key + value; + }), + mapKeys(object, function (value, key) { + return key + value; + }) + ) + }) + it('pick', () => { var object = { 'a': 1, 'b': '2', 'c': 3 }; function pick(object, paths) { @@ -37,7 +76,7 @@ describe('code snippet example', () => { pick(object, ['a', 'c']) ) }) - + it('pickBy', () => { var object = { 'a': 1, 'b': null, 'c': 3, 'd': false, 'e': undefined, 'f': '', 'g': 0 }; function pickBy(object) {