Skip to content

Instantly share code, notes, and snippets.

@arekko
Created May 10, 2021 12:19
Show Gist options
  • Save arekko/5dd3eefa75f4055ce1af80fbc54e967d to your computer and use it in GitHub Desktop.
Save arekko/5dd3eefa75f4055ce1af80fbc54e967d to your computer and use it in GitHub Desktop.
Omit deep by
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