Skip to content

Commit

Permalink
Add ObjectHelper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaden ACHAIN committed Sep 9, 2024
1 parent e057801 commit 1d7140f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
18 changes: 18 additions & 0 deletions src/helpers/object.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,22 @@ export abstract class ObjectHelper {
return result;
}, {} as Record<string, V>)
}

static filterByAllowedProperties<T>(obj: T, allowedProperties: (string | number)[]): Partial<T> {
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<T>);
}

static filterByRejectedProperties<T>(obj: T, rejectedProperties: (string | number)[]): Partial<T> {
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<T>);
}
}

0 comments on commit 1d7140f

Please sign in to comment.