hd-utils
Version:
A handy utils for modern JS developers
26 lines (25 loc) • 970 B
JavaScript
import includeKeys from './includeKeys';
/**
* "It returns a new object with the same keys as the original object, except for the keys that match
* the predicate."
*
* The predicate is a function that takes three arguments: the key, the value, and the object. It
* returns true if the key should be included in the new object, and false if it should be excluded
* @example excludeKeys({
foo: true,
bar: false
}, (key, value) => value === true) => {bar: false}
* @example excludeKeys({
foo: true,
bar: false
}, ["foo"]) => {bar:true}
* @param object - The object to filter.
* @param predicate - A function that returns true if the key should be included.
*/
export default function excludeKeys(object, predicate) {
if (Array.isArray(predicate)) {
const set = new Set(predicate);
return includeKeys(object, key => !set.has(key));
}
return includeKeys(object, (key, value, object) => !predicate(key, value, object));
}