Skip to content

Commit

Permalink
feat(utilities): add objectKeys function (#472)
Browse files Browse the repository at this point in the history
Co-authored-by: imranbarbhuiya
Co-authored-by: Jeroen Claassens <jeroen.claassens@live.nl>
Co-authored-by: Jeroen Claassens <support@favware.tech>
  • Loading branch information
3 people committed Oct 1, 2022
1 parent 84c1418 commit 384ff84
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 34 deletions.
3 changes: 2 additions & 1 deletion packages/utilities/src/index.ts
Expand Up @@ -33,15 +33,16 @@ export * from './lib/mergeDefault';
export * from './lib/mergeObjects';
export * from './lib/noop';
export * from './lib/objectEntries';
export * from './lib/objectKeys';
export * from './lib/objectToTuples';
export * from './lib/objectValues';
export * from './lib/parseUrl';
export * from './lib/partition';
export * from './lib/range';
export * from './lib/regExpEsc';
export * from './lib/roundNumber';
export * from './lib/splitText';
export * from './lib/sleep';
export * from './lib/splitText';
export { toTitleCase, ToTitleCaseOptions } from './lib/toTitleCase';
export * from './lib/tryParse';
export * from './lib/utilityTypes';
5 changes: 5 additions & 0 deletions packages/utilities/src/lib/objectKeys.ts
@@ -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)[];
}
31 changes: 31 additions & 0 deletions packages/utilities/tests/objectKeys.test.ts
@@ -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);
});
});
39 changes: 6 additions & 33 deletions yarn.lock
Expand Up @@ -982,10 +982,10 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:*":
version: 18.7.18
resolution: "@types/node@npm:18.7.18"
checksum: 8aec61f0f96e2a69ce51f1f40f949ca578bbb4fe05d7c0b8ce3aeeb848e90f755837f17f6ac132ca404d974fe9b2974150ad3b4984fc9dc7c3ceddb10bae0167
"@types/node@npm:*, @types/node@npm:^18.7.23":
version: 18.7.23
resolution: "@types/node@npm:18.7.23"
checksum: 2c8df0830d8345e5cd1ca17feb9cf43fa667aae749888e0a068c5c1b35eaedd2f9b24ed987a0758078395edf7a03681e5e0b7790a518ff7afe1ff6d8459f7b4a
languageName: node
linkType: hard

Expand All @@ -996,13 +996,6 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:^18.7.23":
version: 18.7.23
resolution: "@types/node@npm:18.7.23"
checksum: 2c8df0830d8345e5cd1ca17feb9cf43fa667aae749888e0a068c5c1b35eaedd2f9b24ed987a0758078395edf7a03681e5e0b7790a518ff7afe1ff6d8459f7b4a
languageName: node
linkType: hard

"@types/normalize-package-data@npm:^2.4.0":
version: 2.4.1
resolution: "@types/normalize-package-data@npm:2.4.1"
Expand Down Expand Up @@ -6435,17 +6428,7 @@ __metadata:
languageName: node
linkType: hard

"typescript@npm:^4.6.4, typescript@npm:^4.7.4":
version: 4.8.3
resolution: "typescript@npm:4.8.3"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 8286a5edcaf3d68e65c451aa1e7150ad1cf53ee0813c07ec35b7abdfdb10f355ecaa13c6a226a694ae7a67785fd7eeebf89f845da0b4f7e4a35561ddc459aba0
languageName: node
linkType: hard

"typescript@npm:^4.8.4":
"typescript@npm:^4.6.4, typescript@npm:^4.7.4, typescript@npm:^4.8.3, typescript@npm:^4.8.4":
version: 4.8.4
resolution: "typescript@npm:4.8.4"
bin:
Expand All @@ -6455,17 +6438,7 @@ __metadata:
languageName: node
linkType: hard

"typescript@patch:typescript@^4.6.4#~builtin<compat/typescript>, typescript@patch:typescript@^4.7.4#~builtin<compat/typescript>":
version: 4.8.3
resolution: "typescript@patch:typescript@npm%3A4.8.3#~builtin<compat/typescript>::version=4.8.3&hash=a1c5e5"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 2222d2382fb3146089b1d27ce2b55e9d1f99cc64118f1aba75809b693b856c5d3c324f052f60c75b577947fc538bc1c27bad0eb76cbdba9a63a253489504ba7e
languageName: node
linkType: hard

"typescript@patch:typescript@^4.8.4#~builtin<compat/typescript>":
"typescript@patch:typescript@^4.6.4#~builtin<compat/typescript>, typescript@patch:typescript@^4.7.4#~builtin<compat/typescript>, typescript@patch:typescript@^4.8.3#~builtin<compat/typescript>, typescript@patch:typescript@^4.8.4#~builtin<compat/typescript>":
version: 4.8.4
resolution: "typescript@patch:typescript@npm%3A4.8.4#~builtin<compat/typescript>::version=4.8.4&hash=a1c5e5"
bin:
Expand Down

0 comments on commit 384ff84

Please sign in to comment.