From 1d7140f8cc9dbf824743ce25c7f09b0e55d0d74d Mon Sep 17 00:00:00 2001 From: Jaden ACHAIN Date: Mon, 9 Sep 2024 10:17:17 +0200 Subject: [PATCH] Add ObjectHelper functions --- package.json | 2 +- src/helpers/object.helper.ts | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ff67108..9272c23 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@alkemist/smart-tools", - "version": "1.1.25", + "version": "1.1.26", "description": "Smart tools", "main": "lib/index.min.mjs", "type": "module", diff --git a/src/helpers/object.helper.ts b/src/helpers/object.helper.ts index 258d24a..b558860 100644 --- a/src/helpers/object.helper.ts +++ b/src/helpers/object.helper.ts @@ -15,4 +15,22 @@ export abstract class ObjectHelper { return result; }, {} as Record) } + + static filterByAllowedProperties(obj: T, allowedProperties: (string | number)[]): Partial { + return Object.keys(obj as Record<(string | number), any>) + .filter(key => allowedProperties.includes(key)) + .reduce((obj, key) => ({ + ...obj, + [key]: obj[(key as keyof T)] + }), {} as Partial); + } + + static filterByRejectedProperties(obj: T, rejectedProperties: (string | number)[]): Partial { + return Object.keys(obj as Record<(string | number), any>) + .filter(key => !rejectedProperties.includes(key)) + .reduce((obj, key) => ({ + ...obj, + [key]: obj[(key as keyof T)] + }), {} as Partial); + } } \ No newline at end of file