generated from sapphiredev/sapphire-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utilities): add
objectEntries
function (#471)
* feat(utilities): add `objectEntries` function * test(objectEntries): cover readonly types * fix(objectEntires): return type Co-authored-by: imranbarbhuiya * fix(objectEntries): return type for arrays * Update packages/utilities/src/lib/objectEntries.ts Co-authored-by: Jeroen Claassens <jeroen.claassens@live.nl>
- Loading branch information
1 parent
1606208
commit 84c1418
Showing
3 changed files
with
51 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
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 @@ | ||
import type { NonNullObject } from './utilityTypes'; | ||
|
||
export function objectEntries<T extends NonNullObject>(obj: T): T extends ArrayLike<any> ? [`${number}`, T[number]][] : [keyof T, T[keyof T]][] { | ||
return Object.entries(obj) as T extends ArrayLike<infer Values> ? Values[] : [keyof T, T[keyof T]][]; | ||
} |
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,45 @@ | ||
import { objectEntries } from '../src'; | ||
|
||
describe('objectEntries', () => { | ||
test('GIVEN basic readonly THEN returns expected', () => { | ||
const source = { a: 'Hello', b: 420 } as const; | ||
const expected = [ | ||
['a', 'Hello'], | ||
['b', 420] | ||
] as [string, unknown][]; | ||
|
||
expect<['a' | 'b', 'Hello' | 420][]>(objectEntries(source)).toEqual(expected); | ||
}); | ||
|
||
test('GIVEN deep readonly THEN returns expected', () => { | ||
const source = { a: 'Hello', b: 420, deep: { i: [] } } as const; | ||
const expected = [ | ||
['a', 'Hello'], | ||
['b', 420], | ||
['deep', { i: [] }] | ||
] as [string, unknown][]; | ||
|
||
expect<['a' | 'b' | 'deep', 'Hello' | 420 | { i: readonly [] }][]>(objectEntries(source)).toEqual(expected); | ||
}); | ||
|
||
test('GIVEN basic THEN returns expected', () => { | ||
const source = { a: 'Hello', b: 420 }; | ||
const expected = [ | ||
['a', 'Hello'], | ||
['b', 420] | ||
] as [string, unknown][]; | ||
|
||
expect(objectEntries(source)).toEqual(expected); | ||
}); | ||
|
||
test('GIVEN deep THEN returns expected', () => { | ||
const source = { a: 'Hello', b: 420, deep: { i: [] } }; | ||
const expected = [ | ||
['a', 'Hello'], | ||
['b', 420], | ||
['deep', { i: [] }] | ||
] as [string, unknown][]; | ||
|
||
expect(objectEntries(source)).toEqual(expected); | ||
}); | ||
}); |