diff --git a/docs/functions.md b/docs/functions.md index 2d432538..39bfd377 100644 --- a/docs/functions.md +++ b/docs/functions.md @@ -86,11 +86,11 @@ filter $ => .age >= 18 Returns the length of the input: ``` -[1, 0, 1] | length; +[1, 0, 1] | length ``` ``` -3; +3 ``` ## reverse @@ -98,11 +98,11 @@ Returns the length of the input: Reverses the input: ``` -[1, 2, 3] | reverse; +[1, 2, 3] | reverse ``` ``` -[3, 2, 1]; +[3, 2, 1] ``` ## reduce \ @@ -121,7 +121,7 @@ Returns the keys of an object: ``` ``` -['foo', 'baz']; +['foo', 'baz'] ``` ## values @@ -133,7 +133,19 @@ Returns the values of an object: ``` ``` -['bar', 42]; +['bar', 42] +``` + +## entries + +Returns the entries of an object as an array of arrays: + +``` +{"foo": "bar", "baz": 42} | entries +``` + +``` +[["foo", "bar"], ["baz", 42]] ``` ## combinations @@ -145,7 +157,7 @@ Returns all combinations of each possible choice of r elements of the input: ``` ``` -[['A', 'B'], ['A', 'C'], ['A', 'D'], ['B', 'C'], ['B', 'D'], ['C', 'D']]; +[['A', 'B'], ['A', 'C'], ['A', 'D'], ['B', 'C'], ['B', 'D'], ['C', 'D']] ``` ## product @@ -153,9 +165,9 @@ Returns all combinations of each possible choice of r elements of the input: Computes the product of the iterables in the input: ``` -[['a', 'b'], ['1', '2']] | product; +[['a', 'b'], ['1', '2']] | product ``` ``` -[['a', '1'], ['a', '2'], ['b', '1'], ['b', '2']]; +[['a', '1'], ['a', '2'], ['b', '1'], ['b', '2']] ``` diff --git a/src/__tests__/builtins.test.js b/src/__tests__/builtins.test.js index 848e4762..8cc6968e 100644 --- a/src/__tests__/builtins.test.js +++ b/src/__tests__/builtins.test.js @@ -14,6 +14,7 @@ const { keys, split, values, + entries, combinations, product, __opt__, @@ -310,6 +311,15 @@ describe('built ins', () => { }) }) + describe('entries', () => { + it('returns correct value', () => { + expect(entries({})).toEqual([]) + expect(new Set(entries({ foo: 'bar', baz: 4 }))).toEqual( + new Set([['foo', 'bar'], ['baz', 4]]) + ) + }) + }) + describe('sortBy', () => { it('returns correct value', () => { const id = a => a // eslint-disable-line diff --git a/src/builtins.js b/src/builtins.js index d134abd5..1f6f4970 100644 --- a/src/builtins.js +++ b/src/builtins.js @@ -167,6 +167,8 @@ export default { keys: (input: { [string]: mixed }): Array => Object.keys(input), values: (input: { [string]: mixed }): Array => Object.values(input), + entries: (input: { [string]: mixed }): Array<[string, mixed]> => + Object.entries(input), combinations: (r: number): Array | (string => Array>) => ( input: Array | string