Skip to content

Commit

Permalink
Add lodash _.mapKeys (#351)
Browse files Browse the repository at this point in the history
* add _invert

* add _mapKeys
  • Loading branch information
rajdee committed Sep 9, 2023
1 parent 6b62caa commit 49c0168
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 1 deletion.
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
41 changes: 40 additions & 1 deletion tests/unit/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 49c0168

Please sign in to comment.