From 03ec44bac8b83498f434402c50998c8b57960e17 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Tue, 25 Jun 2024 22:34:15 -0400 Subject: [PATCH] feat: add `matchKeys` function --- src/curry/matchKeys.ts | 19 +++++++++++++++++++ src/index.ts | 1 + 2 files changed, 20 insertions(+) create mode 100644 src/curry/matchKeys.ts diff --git a/src/curry/matchKeys.ts b/src/curry/matchKeys.ts new file mode 100644 index 000000000..4d57215de --- /dev/null +++ b/src/curry/matchKeys.ts @@ -0,0 +1,19 @@ +import { isArray } from 'radashi' + +export type KeyFilter = + | KeyMatcher + | Key[] + +export type KeyMatcher = ( + value: T[keyof T], + key: keyof T, + obj: T +) => boolean + +export const matchKeys = ( + keys: KeyFilter +): KeyMatcher => + isArray(keys) + ? (_, key, obj) => + Object.hasOwnProperty.call(obj, key) && keys.includes(key as Key) + : keys diff --git a/src/index.ts b/src/index.ts index ac6503ce1..7c991ade7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,6 +43,7 @@ export * from './curry/callable' export * from './curry/chain' export * from './curry/compose' export * from './curry/debounce' +export * from './curry/matchKeys' export * from './curry/memo' export * from './curry/partial' export * from './curry/partob'