-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(essentials): add whereDefined and filterObject utilities
- Loading branch information
1 parent
b099c0d
commit e2ba61f
Showing
5 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@codedazur/essentials": minor | ||
--- | ||
|
||
The filterObject and whereDefined object utilities were added. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { filterObject } from "./filterObject"; | ||
|
||
describe("filterObject", () => { | ||
it("filters an object", () => { | ||
expect( | ||
filterObject( | ||
{ a: 1, b: 2, c: 3 }, | ||
([key, value]) => key === "a" || value === 3, | ||
), | ||
).toEqual({ a: 1, c: 3 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Filters an object by its entries. | ||
*/ | ||
export function filterObject< | ||
U extends Record<string, unknown>, | ||
T extends Record<string, unknown> = Record<string, unknown>, | ||
K extends keyof T = keyof T, | ||
>(object: T, callback: (entry: [K, T[K]]) => boolean): U { | ||
return Object.fromEntries( | ||
(Object.entries(object) as [K, T[K]][]).filter(callback), | ||
) as U; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { whereDefined } from "./whereDefined"; | ||
|
||
describe("whereDefined", () => { | ||
it("removes undefined properties", () => { | ||
expect( | ||
whereDefined({ | ||
a: 0, | ||
b: "", | ||
c: false, | ||
d: null, | ||
e: undefined, | ||
}), | ||
).toEqual({ | ||
a: 0, | ||
b: "", | ||
c: false, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { filterObject } from "./filterObject"; | ||
|
||
/** | ||
* Removes properties from an object that are `null` or `undefined`. | ||
*/ | ||
export type WhereDefined<T> = { | ||
[P in keyof T]-?: WhereDefined<NonNullable<T[P]>>; | ||
}; | ||
|
||
/** | ||
* Removes properties from an object that are `null` or `undefined`. | ||
*/ | ||
export function whereDefined<T extends Record<string, unknown>>( | ||
object: T, | ||
): WhereDefined<T> { | ||
return filterObject(object, ([, value]) => value !== undefined); | ||
} |