super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
64 lines (63 loc) • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.omit = omit;
exports.omitBy = omitBy;
/**
* Creates an object composed of the own enumerable property paths of object that are not omitted.
*
* @param object - The source object
* @param paths - The property paths to omit
* @returns The new object
*
* @example
* ```ts
* const object = { 'a': 1, 'b': 2, 'c': 3 };
*
* omit(object, ['a', 'c']);
* // => { 'b': 2 }
* ```
*/
function omit(object, paths) {
if (!object) {
return {};
}
const result = {};
const pathSet = new Set(paths.map(String));
for (const key in object) {
if (Object.prototype.hasOwnProperty.call(object, key) &&
!pathSet.has(key)) {
result[key] = object[key];
}
}
return result;
}
/**
* Creates an object composed of the object properties predicate returns falsey for.
*
* @param object - The source object
* @param predicate - The function invoked per property
* @returns The new object
*
* @example
* ```ts
* const object = { 'a': 1, 'b': 2, 'c': 3 };
*
* omitBy(object, (value) => value >= 2);
* // => { 'a': 1 }
* ```
*/
function omitBy(object, predicate) {
if (!object) {
return {};
}
const result = {};
for (const key in object) {
if (Object.prototype.hasOwnProperty.call(object, key)) {
const value = object[key];
if (!predicate(value, key)) {
result[key] = value;
}
}
}
return result;
}