Last active
January 24, 2019 22:26
-
-
Save joemaffei/39976001f70d57b43ade0482994f2db2 to your computer and use it in GitHub Desktop.
Naive implementation of lodash.omit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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