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
objectValues
function (#474)
* feat(utilities): add `objectValues` function * test(objectValues): cover readonly types * fix(objectValues): return type * chore(utilities): re-order export for `objectValues` * Update packages/utilities/src/lib/objectValues.ts Co-authored-by: Jeroen Claassens <jeroen.claassens@live.nl>
- Loading branch information
1 parent
8cd1293
commit 90c2409
Showing
3 changed files
with
37 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 objectValues<T extends NonNullObject>(obj: T): T extends ArrayLike<any> ? T[number][] : T[keyof T][] { | ||
return Object.values(obj) as T extends ArrayLike<infer Values> ? Values[] : 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,31 @@ | ||
import { objectValues } from '../src'; | ||
|
||
describe('objectValues', () => { | ||
test('GIVEN basic readonly THEN returns expected', () => { | ||
const source = { a: 'Hello', b: 420 } as const; | ||
const expected = ['Hello', 420]; | ||
|
||
expect<('Hello' | 420)[]>(objectValues(source)).toEqual(expected); | ||
}); | ||
|
||
test('GIVEN deep readonly THEN returns expected', () => { | ||
const source = { a: 'Hello', b: 420, deep: { i: [] } } as const; | ||
const expected = ['Hello', 420, { i: [] }]; | ||
|
||
expect<('Hello' | 420 | { i: readonly [] })[]>(objectValues(source)).toEqual(expected); | ||
}); | ||
|
||
test('GIVEN basic readonly THEN returns expected', () => { | ||
const source = { a: 'Hello', b: 420 }; | ||
const expected = ['Hello', 420]; | ||
|
||
expect(objectValues(source)).toEqual(expected); | ||
}); | ||
|
||
test('GIVEN deep THEN returns expected', () => { | ||
const source = { a: 'Hello', b: 420, deep: { i: [] } }; | ||
const expected = ['Hello', 420, { i: [] }]; | ||
|
||
expect(objectValues(source)).toEqual(expected); | ||
}); | ||
}); |