From 5ecdc80c3f98079706a79dd15e2026d776fae43c Mon Sep 17 00:00:00 2001 From: Orlando Date: Mon, 22 Jan 2024 22:25:57 +0000 Subject: [PATCH] pick object and array include --- CHANGELOG.md | 6 ++++++ package.json | 2 +- src/helpers/index.ts | 2 ++ src/helpers/pickObjectKeys.ts | 13 +++++++++++++ src/helpers/pickObjectValues.ts | 13 +++++++++++++ src/validators/index.ts | 3 ++- src/validators/isArrayIncluded.ts | 3 +++ 7 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/helpers/pickObjectKeys.ts create mode 100644 src/helpers/pickObjectValues.ts create mode 100644 src/validators/isArrayIncluded.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index d4ca689..889f3d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.41.0 + +### Minor Changes + +- pick object and array include + ## 0.40.0 ### Minor Changes diff --git a/package.json b/package.json index 26fc262..6b33973 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.40.0", + "version": "0.41.0", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/helpers/index.ts b/src/helpers/index.ts index 4aaf6c0..4c316e1 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -22,6 +22,8 @@ export * from "./normalizeNumber"; export * from "./objectDiff"; export * from "./omit"; export * from "./parseDate"; +export * from "./pickObjectKeys"; +export * from "./pickObjectValues"; export * from "./pretty"; export * from "./promiseWithTimeout"; export * from "./scrambleText"; diff --git a/src/helpers/pickObjectKeys.ts b/src/helpers/pickObjectKeys.ts new file mode 100644 index 0000000..f34b1fd --- /dev/null +++ b/src/helpers/pickObjectKeys.ts @@ -0,0 +1,13 @@ +import { ObjectKeys } from "../types"; +import { PlainObject } from "../types/PlainObject"; + +export const pickObjectKeys = ( + obj: T, + keys: ObjectKeys +): Partial => { + const ret: Partial = {}; + for (const key in obj) { + if (keys.includes(key)) ret[key] = obj[key]; + } + return ret; +}; diff --git a/src/helpers/pickObjectValues.ts b/src/helpers/pickObjectValues.ts new file mode 100644 index 0000000..384bce4 --- /dev/null +++ b/src/helpers/pickObjectValues.ts @@ -0,0 +1,13 @@ +import { ObjectValues } from "../types/Object"; +import { PlainObject } from "../types/PlainObject"; + +export const pickObjectValues = ( + obj: T, + values: ObjectValues +): Partial => { + const ret: Partial = {}; + for (const key in obj) { + if (values.includes(obj[key])) ret[key] = obj[key]; + } + return ret; +}; diff --git a/src/validators/index.ts b/src/validators/index.ts index a0395bc..86468b0 100644 --- a/src/validators/index.ts +++ b/src/validators/index.ts @@ -1,4 +1,5 @@ export * from "./isArray"; +export * from "./isArrayIncluded"; export * from "./isBoolean"; export * from "./isBrowser"; export * from "./isClient"; @@ -9,13 +10,13 @@ export * from "./isFutureDate"; export * from "./isJsDate"; export * from "./isKey"; export * from "./isLastIndex"; +export * from "./isNotEmptyString"; export * from "./isNumber"; export * from "./isNumeric"; export * from "./isNumericId"; export * from "./isObject"; export * from "./isPastDate"; export * from "./isPromise"; -export * from "./isNotEmptyString"; export * from "./isPWA"; export * from "./isReactElement"; export * from "./isRegExp"; diff --git a/src/validators/isArrayIncluded.ts b/src/validators/isArrayIncluded.ts new file mode 100644 index 0000000..3ba8838 --- /dev/null +++ b/src/validators/isArrayIncluded.ts @@ -0,0 +1,3 @@ +export const isArrayIncluded = (arr1: T[], arr2: T[]): boolean => { + return arr1.every((value) => arr2.includes(value)); +};