forked from remeda/remeda
-
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: values and keys functions (remeda#137)
- Loading branch information
Showing
5 changed files
with
65 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,11 @@ | ||
import { keys } from './keys'; | ||
|
||
describe('Test for keys', () => { | ||
it('should return keys of array', () => { | ||
expect(keys(['x', 'y', 'z'])).toEqual(['0', '1', '2']); | ||
}); | ||
|
||
it('should return keys of object', () => { | ||
expect(keys({ a: 'x', b: 'y', c: 'z' })).toEqual(['a', 'b', 'c']); | ||
}); | ||
}); |
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,20 @@ | ||
/** | ||
* Returns a new array containing the keys of the array or object. | ||
* @param source Either an array or an object | ||
* @signature | ||
* R.keys(source) | ||
* @example | ||
* R.keys(['x', 'y', 'z']) // => ['1', '2', '3'] | ||
* R.keys({ a: 'x', b: 'y', c: 'z' }) // => ['a', 'b', 'c'] | ||
* R.pipe( | ||
* { a: 'x', b: 'y', c: 'z' }, | ||
* R.keys, | ||
* R.first | ||
* ) // => 'a' | ||
* @pipeable | ||
* @category Object | ||
*/ | ||
|
||
export function keys<T>(source: Record<PropertyKey, T> | ArrayLike<T>) { | ||
return Object.keys(source) as string[]; | ||
} |
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,11 @@ | ||
import { values } from './values'; | ||
|
||
describe('Test for values as data first', () => { | ||
it('should return values of array', () => { | ||
expect(values(['x', 'y', 'z'])).toEqual(['x', 'y', 'z']); | ||
}); | ||
|
||
it('should return values of object', () => { | ||
expect(values({ a: 'x', b: 'y', c: 'z' })).toEqual(['x', 'y', 'z']); | ||
}); | ||
}); |
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,21 @@ | ||
/** | ||
* Returns a new array containing the values of the array or object. | ||
* @param source Either an array or an object | ||
* @signature | ||
* R.values(source) | ||
* @example | ||
* R.values(['x', 'y', 'z']) // => ['x', 'y', 'z'] | ||
* R.values({ a: 'x', b: 'y', c: 'z' }) // => ['x', 'y', 'z'] | ||
* R.pipe( | ||
* { a: 'x', b: 'y', c: 'z' }, | ||
* R.values, | ||
* R.first | ||
* ) // => 'x' | ||
* @pipeable | ||
* @category Object | ||
*/ | ||
|
||
export function values<T>(source: Record<PropertyKey, T> | ArrayLike<T>) { | ||
return Object.values(source) as T[]; | ||
} | ||
|