Created
May 10, 2021 12:19
-
-
Save arekko/5dd3eefa75f4055ce1af80fbc54e967d to your computer and use it in GitHub Desktop.
Omit deep by
This file contains hidden or 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
import { curry, type } from 'ramda'; | |
const isObject = (obj: unknown) => type(obj) === 'Object'; | |
const isArray = (obj: unknown) => Array.isArray(obj); | |
// @ts-ignore-next-line | |
const omitDeepBy = curry( | |
(pred: (key: string, value: unknown) => boolean, obj: any) => { | |
if (isArray(obj)) { | |
return obj.map(omitDeepBy(pred)); | |
} | |
if (isObject(obj)) { | |
return Object.entries(obj).reduce( | |
(acc: Record<string, unknown>, [key, value]: [string, unknown]) => | |
pred(key, value) ? acc : { ...acc, [key]: omitDeepBy(pred, value) }, | |
{} | |
); | |
} | |
return obj; | |
} | |
); | |
export default omitDeepBy; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment