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
objectKeys
function (#472)
Co-authored-by: imranbarbhuiya Co-authored-by: Jeroen Claassens <jeroen.claassens@live.nl> Co-authored-by: Jeroen Claassens <support@favware.tech>
- Loading branch information
1 parent
84c1418
commit 384ff84
Showing
4 changed files
with
44 additions
and
34 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 objectKeys<T extends NonNullObject>(obj: T): T extends ArrayLike<any> ? `${number}`[] : (keyof T)[] { | ||
return Object.keys(obj) as T extends ArrayLike<infer Values> ? Values[] : (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,31 @@ | ||
import { objectKeys } from '../src'; | ||
|
||
describe('objectKeys', () => { | ||
test('GIVEN basic readonly THEN returns expected', () => { | ||
const source = { a: 'Hello', b: 420 } as const; | ||
const expected = ['a', 'b']; | ||
|
||
expect<('a' | 'b')[]>(objectKeys(source)).toEqual(expected); | ||
}); | ||
|
||
test('GIVEN deep readonly THEN returns expected', () => { | ||
const source = { a: 'Hello', b: 420, deep: { i: [] } } as const; | ||
const expected = ['a', 'b', 'deep']; | ||
|
||
expect<('a' | 'b' | 'deep')[]>(objectKeys(source)).toEqual(expected); | ||
}); | ||
|
||
test('GIVEN basic THEN returns expected', () => { | ||
const source = { a: 'Hello', b: 420 }; | ||
const expected = ['a', 'b']; | ||
|
||
expect(objectKeys(source)).toEqual(expected); | ||
}); | ||
|
||
test('GIVEN deep THEN returns expected', () => { | ||
const source = { a: 'Hello', b: 420, deep: { i: [] } }; | ||
const expected = ['a', 'b', 'deep']; | ||
|
||
expect(objectKeys(source)).toEqual(expected); | ||
}); | ||
}); |
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