Skip to content

Commit

Permalink
[ADD] drop
Browse files Browse the repository at this point in the history
  • Loading branch information
HubCodes committed Sep 21, 2018
1 parent be7695a commit ec308d7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ const func = $.partialFunction((arg1, arg2) => {
return arg1;
});
const result = func(arg1, arg2); // 1

const func2 = $.partialFunction((arg1) => arg1 > 0, (arg) => arg);
func2.isDefinedAt(10); // true
```
11 changes: 11 additions & 0 deletions drop/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function drop(iterable, n) {
const dropped = [];
let dropping = 0;
for (const element of iterable) {
if (dropping >= n) {
dropped.push(element);
}
dropping++;
}
return dropped;
};
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const andThen = require('./andThen');
const any = require('./any');
const compose = require('./compose');
const distinct = require('./distinct');
const drop = require('./drop');
const filter = require('./filter');
const foldLeft = require('./foldLeft');
const foldRight = require('./foldRight');
Expand All @@ -17,6 +18,7 @@ module.exports = {
any,
compose,
distinct,
drop,
filter,
foldLeft,
foldRight,
Expand Down
37 changes: 37 additions & 0 deletions test/drop.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const assert = require('assert');
const $ = require('../');

describe('drop test', () => {
const array = [3, 4, 5, 6, 7, 8, 9];
it('work with array', () => {
const result = $.drop(array, 3);
assert.deepEqual(result, [6, 7, 8, 9]);
});
it('work with Set', () => {
const set = new Set(array);
const result = $.drop(set, 2);
assert.deepEqual(result, [5, 6, 7, 8, 9]);
});
it('work with generator', () => {
function* zeroToTen() {
for (let i = 0; i <= 10; i++) {
yield i;
}
}
const result = $.drop(zeroToTen(), 3);
assert.deepEqual(result, [3, 4, 5, 6, 7, 8, 9, 10]);
});
it('work with Map', () => {
const map = new Map();
for (let i = 3; i <= 9; i++) {
map.set(i, i * 2);
}
const result = $.drop(map, 4);
assert.deepEqual(result, [[7, 14], [8, 16], [9, 18]]);
});
it('work with string', () => {
const str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const result = $.drop(str, 10).join('');
assert.equal(result, 'KLMNOPQRSTUVWXYZ');
});
});

0 comments on commit ec308d7

Please sign in to comment.