-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08af214
commit 650158d
Showing
4 changed files
with
71 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const take = require('../src/take') | ||
|
||
describe('take', () => { | ||
it('should only keep the first N elements of the elements', () => { | ||
expect(take(2, [1, 2, 3])).toEqual([1, 2]) | ||
}) | ||
|
||
it('should work backwards if N is negative', () => { | ||
expect(take(-2, [1, 2, 3])).toEqual([2, 3]) | ||
}) | ||
|
||
it('should return a copy the entire array if N is greather than the array\'s length', () => { | ||
expect(take(100, [1, 2, 3])).toEqual([1, 2, 3]) | ||
}) | ||
|
||
it('should be a pure function', () => { | ||
const input = [1, 2, 3] | ||
const output = take(3, input) | ||
expect(input).toEqual([1, 2, 3]) | ||
expect(output).not.toBe(input) | ||
}) | ||
}) |
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 @@ | ||
# take | ||
|
||
```erlang | ||
take :: (Integer count, Array input) => Array output | ||
``` | ||
|
||
{% hint style="info" %} | ||
Available since v1.5.0 | ||
{% endhint %} | ||
|
||
### description | ||
|
||
`take` returns a new array only keeping the first `count` elements of the input array. If `count` is _negative_, `take` returns the last `count` elements. | ||
|
||
[See source code](https://github.com/WaldoJeffers/conductor/blob/master/src/take.js) | ||
|
||
### examples | ||
|
||
#### using a positive count argument | ||
|
||
```javascript | ||
import { take } from 'conductor' | ||
|
||
take(2, [1, 2, 3]) // [1, 2] | ||
``` | ||
|
||
#### using a negative count argument | ||
|
||
```javascript | ||
import { take } from 'conductor' | ||
|
||
take(-2, [1, 2, 3]) // [2, 3] | ||
``` | ||
|
||
#### using a count argument greater than the array's length | ||
|
||
```javascript | ||
import { take } from 'conductor' | ||
|
||
take(100, [1, 2, 3]) // [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
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,6 @@ | ||
const curry = require('./curry') | ||
|
||
const take = (count, array) => | ||
count >= 0 ? array.slice(0, count) : array.slice(count) | ||
|
||
module.exports = curry(take) |