Skip to content

Commit

Permalink
feat: values and keys functions (remeda#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarenatoe authored Jul 28, 2021
1 parent e310907 commit 034ef64
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export * from './guards';
export * from './identity';
export * from './indexBy';
export * from './intersection';
export * from './keys';
export * from './last';
export * from './map';
export * from './mapKeys';
Expand Down Expand Up @@ -63,5 +64,6 @@ export * from './toPairs';
export * from './type';
export * from './uniq';
export * from './uniqBy';
export * from './values';
export * from './zip';
export * from './zipWith';
11 changes: 11 additions & 0 deletions src/keys.test.ts
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']);
});
});
20 changes: 20 additions & 0 deletions src/keys.ts
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[];
}
11 changes: 11 additions & 0 deletions src/values.test.ts
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']);
});
});
21 changes: 21 additions & 0 deletions src/values.ts
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[];
}

0 comments on commit 034ef64

Please sign in to comment.