-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This function is a tighter version of lodash's `pick` function that enables proper typing
- Loading branch information
1 parent
8eb0efc
commit ad55229
Showing
3 changed files
with
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as clone } from './clone' | ||
export { default as get } from './get' | ||
export { default as pick } from './pick' | ||
export { default as set } from './set' |
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,37 @@ | ||
import pick from './pick' | ||
|
||
describe('Pick Properties (pick)', () => { | ||
it('should exclude all properties that were not specified', () => { | ||
const testObject = { foo: 'hello', bar: 'world', baz: [1, 2, 3], some: 'property' } | ||
const pickedObject = pick(testObject, 'foo', 'bar') | ||
|
||
expect(pickedObject).toEqual({ foo: 'hello', bar: 'world' }) | ||
}) | ||
|
||
it('should return an empty object when `object` is nullish', () => { | ||
const testObjectNull = null as {} | ||
const pickedObjectNull = pick(testObjectNull) | ||
|
||
expect(pickedObjectNull).toEqual({}) | ||
|
||
let testObjectUndefined: {} | ||
const pickedObjectUndefined = pick(testObjectUndefined) | ||
expect(pickedObjectUndefined).toEqual({}) | ||
}) | ||
|
||
describe('Overloaded signatures', () => { | ||
it('should accept an array of strings as keys', () => { | ||
const testObject = { foo: 'hello', bar: 'world', baz: [1, 2, 3], some: 'property' } | ||
const pickedObject = pick(testObject, ['foo', 'bar', 'baz']) | ||
|
||
expect(pickedObject).toEqual({ foo: 'hello', bar: 'world', baz: [1, 2, 3] }) | ||
}) | ||
|
||
it('should accept any number of string arguments as keys', () => { | ||
const testObject = { foo: 'hello', bar: 'world', baz: [1, 2, 3], some: 'property' } | ||
const pickedObject = pick(testObject, 'foo', 'bar', 'baz') | ||
|
||
expect(pickedObject).toEqual({ foo: 'hello', bar: 'world', baz: [1, 2, 3] }) | ||
}) | ||
}) | ||
}) |
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,41 @@ | ||
import flatten from '../array/flatten' | ||
|
||
/** | ||
* | ||
* Creates a new object containing only the properties of `object` that are specified in `keys`. | ||
* | ||
* @param object The base object that properties will be picked from | ||
* @param keys The keys to pick | ||
* | ||
* @example | ||
* const original = { foo: 'hello', bar: 'world', baz: false, something: [1, 2, 3] } | ||
* const picked = pick(original, 'foo', 'something') | ||
* | ||
* console.log(picked) // { foo: 'hello', something: [1, 2, 3] } | ||
* console.log(Object.keys(picked)) // ['foo', 'something'] | ||
* | ||
*/ | ||
export default function pick<T extends Record<string, any>, U extends keyof T>( | ||
object: T, | ||
...keys: U[] | ||
): Pick<T, U> | ||
export default function pick<T extends Record<string, any>, U extends keyof T>( | ||
object: T, | ||
keys: U[], | ||
): Pick<T, U> | ||
export default function pick<T extends Record<string, any>, U extends keyof T>( | ||
object: T, | ||
...keys: U[] | [U[]] | ||
): Pick<T, U> { | ||
const resolvedKeys = flatten(keys) | ||
|
||
const output = resolvedKeys.reduce( | ||
(output, key) => { | ||
output[key] = object[key] | ||
return output | ||
}, | ||
{} as T, | ||
) | ||
|
||
return output as Pick<T, U> | ||
} |