Skip to content

Instantly share code, notes, and snippets.

@joemaffei
Last active January 24, 2019 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemaffei/39976001f70d57b43ade0482994f2db2 to your computer and use it in GitHub Desktop.
Save joemaffei/39976001f70d57b43ade0482994f2db2 to your computer and use it in GitHub Desktop.
Naive implementation of lodash.omit
export function omitSingle(object = {}, key) {
if (key === null || key === undefined || !(key in object)) return object;
const { [key]: deleted, ...otherKeys } = object;
return otherKeys;
}
export function omitMultiple(object = {}, keys) {
if (!Array.isArray(keys)) return object;
return keys.reduce((result, key) => {
if (key in result) {
return omitSingle(result, key);
}
return result;
}, object);
}
export default function omit(object = {}, keys) {
if (!keys) return object;
if (Array.isArray(keys)) {
// calling omitMultiple here would result in a second array check
return keys.reduce((result, key) => {
if (key in result) {
return omitSingle(result, key);
}
return result;
}, object);
}
return omitSingle(object, keys);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment