Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ For more information, see [Configuring the ESLint Plugin](configuring.md)
1. [_.keys](#_keys)
1. [_.mapKeys](#_mapKeys)
1. [_.omit](#_omit)
1. [_.omitBy](#_omitBy)
1. [_.pick](#_pick)
1. [_.pickBy](#_pickby)
1. [_.toPairs](#_topairs)
Expand Down Expand Up @@ -2824,6 +2825,45 @@ Returns a copy of the object, filtered to omit the keys specified.

**[⬆ back to top](#quick-links)**

### _.omitBy

Creates an object composed of the object properties that predicate doesn't return truthy for.

```js
const object = { 'a': 1, 'b': '2', 'c': 3 };

// Lodash
const result = _.omitBy(object);
console.log(result)
// output: { 'b': '2' }

// Native
function omitBy(object, predicate) {
return Object.fromEntries(
Object.entries(object).filter(([key, value]) => !predicate(value, key)),
);
}

const object = { 'a': 1, 'b': '2', 'c': 3 };
const predicate = (value) => typeof value === 'number';

console.log(omitBy(object, predicate))
// output: { 'b': '2' }
```

#### 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 ✔ |

#### Browser Support for `Object.fromEntries()`
![Chrome][chrome-image] | ![Edge][edge-image] | ![Firefox][firefox-image] | ![IE][ie-image] | ![Opera][opera-image] | ![Safari][safari-image]
:-: | :-: | :-: | :-: | :-: | :-: |
73.0 ✔ | 79.0 ✔ | 63.0 ✔ | ✖ | 60.0 ✔ | 12.1 ✔ |

**[⬆ back to top](#quick-links)**

### _.pick

Creates an object composed of the object properties predicate returns truthy for.
Expand Down
5 changes: 5 additions & 0 deletions lib/rules/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@
"alternative": "{a, b, c, ...notOmittedValues}",
"ES6": true
},
"omitBy": {
"compatible": true,
"alternative": "Example of native implementation: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_omitBy",
"ES6": true
},
"flatten": {
"compatible": true,
"alternative": "Array.prototype.reduce((a,b) => a.concat(b), [])"
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -1098,4 +1098,17 @@ describe('code snippet example', () => {
assert.deepEqual(_.head([]), [].at(0));
});
})
describe('omitBy', () => {
function omitBy(object, predicate) {
return Object.fromEntries(
Object.entries(object).filter(([key, value]) => !predicate(value, key)),
);
}

it('should omit properties by predicate', () => {
const object = { a: 1, b: '2', c: 3 };
const predicate = (value) => typeof value === 'number';
assert.deepStrictEqual(_.omitBy(object, predicate), omitBy(object, predicate));
});
})
});