Skip to content

Commit

Permalink
feat: add entries function
Browse files Browse the repository at this point in the history
fixes #547
  • Loading branch information
kantord committed Sep 15, 2019
1 parent 6a17c84 commit 8b7cd0b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
30 changes: 21 additions & 9 deletions docs/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,23 @@ filter $ => .age >= 18
Returns the length of the input:

```
[1, 0, 1] | length;
[1, 0, 1] | length
```

```
3;
3
```

## reverse

Reverses the input:

```
[1, 2, 3] | reverse;
[1, 2, 3] | reverse
```

```
[3, 2, 1];
[3, 2, 1]
```

## reduce \
Expand All @@ -121,7 +121,7 @@ Returns the keys of an object:
```

```
['foo', 'baz'];
['foo', 'baz']
```

## values
Expand All @@ -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
Expand All @@ -145,17 +157,17 @@ 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

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']]
```
10 changes: 10 additions & 0 deletions src/__tests__/builtins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
keys,
split,
values,
entries,
combinations,
product,
__opt__,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export default {
keys: (input: { [string]: mixed }): Array<string> => Object.keys(input),

values: (input: { [string]: mixed }): Array<mixed> => Object.values(input),
entries: (input: { [string]: mixed }): Array<[string, mixed]> =>
Object.entries(input),

combinations: (r: number): Array<mixed> | (string => Array<Array<mixed>>) => (
input: Array<mixed> | string
Expand Down

0 comments on commit 8b7cd0b

Please sign in to comment.