forked from fossabot/ahana-fp
-
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(utilities): add extractProperty and pickProperties utilities
These utility functions make it easy to handle object values when all you want are some of their values
- Loading branch information
Showing
3 changed files
with
69 additions
and
7 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,42 @@ | ||
import { expect, describe, test } from 'vitest'; | ||
import { extractProperty, pickProperties } from './utilities.js'; | ||
|
||
// We may need to play fast and loose with typing here, just to test edge cases | ||
describe('extractProperty', () => { | ||
test('works on an empty object', () => { | ||
expect(extractProperty('')({} as any)).toBeUndefined() | ||
}) | ||
test('returns undefined for a non-existent key', () => { | ||
expect(extractProperty('a')({} as any)).toBeUndefined() | ||
}) | ||
test('returns undefined for a non-existent key', () => { | ||
expect(extractProperty('a')({} as any)).toBeUndefined() | ||
}) | ||
test('returns a single value', () => { | ||
expect(extractProperty('a')({ a: 42 })).toBe(42) | ||
}) | ||
}) | ||
|
||
describe('pickProperties', () => { | ||
test('works on an empty object', () => { | ||
expect(pickProperties([''])({} as any)).toStrictEqual({}) | ||
}) | ||
test('returns undefined for a non-existent key', () => { | ||
expect(pickProperties(['a'])({} as any)).toStrictEqual({}) | ||
}) | ||
test('returns undefined for a non-existent key', () => { | ||
expect(pickProperties(['a'])({} as any)).toStrictEqual({}) | ||
}) | ||
test('returns an object with a single property', () => { | ||
expect(pickProperties(['a'])({ a: 42 })).toStrictEqual({ a: 42 }) | ||
}) | ||
test('does not include non-existent properties', () => { | ||
expect(pickProperties(['a', 'b'])({ a: 42 } as any)).toStrictEqual({ a: 42 }) | ||
}) | ||
test('does include explicitly undefined properties', () => { | ||
expect(pickProperties(['a', 'b'])({ a: 42, b: undefined } as any)).toStrictEqual({ a: 42, b: undefined }) | ||
}) | ||
test('handles duplicated properties', () => { | ||
expect(pickProperties(['a', 'a'])({ a: 42, b: undefined } as any)).toStrictEqual({ a: 42, }) | ||
}) | ||
}) |
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