Skip to content

Commit

Permalink
#14 bug with take before uniq
Browse files Browse the repository at this point in the history
Signed-off-by: Łukasz Sentkiewicz <lukasz@sentkiewicz.pl>
  • Loading branch information
lstkz committed Mar 13, 2019
1 parent 2a6b6db commit 59d408c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/pipe.ts
Expand Up @@ -149,6 +149,7 @@ function _processItem({
return false;
}
let lazyResult: LazyResult<any> = { done: false, hasNext: false };
let isDone = false;
for (let i = 0; i < lazySeq.length; i++) {
const lazyFn = lazySeq[i];
const indexed = lazyFn.indexed;
Expand All @@ -175,14 +176,19 @@ function _processItem({
item = lazyResult.next;
}
}
if (!lazyResult.hasNext || lazyResult.done) {
if (!lazyResult.hasNext) {
break;
}
// process remaining functions in the pipe
// but don't process remaining elements in the input array
if (lazyResult.done) {
isDone = true;
}
}
if (lazyResult.hasNext) {
acc.push(item);
}
if (lazyResult.done) {
if (isDone) {
return true;
}
return false;
Expand Down
13 changes: 13 additions & 0 deletions src/uniq.test.ts
Expand Up @@ -19,4 +19,17 @@ describe('pipe', () => {
expect(counter.count).toHaveBeenCalledTimes(4);
expect(result).toEqual([1, 2, 5]);
});

it('take before uniq', () => {
// bug from https://github.com/remeda/remeda/issues/14
const counter = createCounter();
const result = pipe(
[1, 2, 2, 5, 1, 6, 7],
counter.fn(),
take(3),
uniq()
);
expect(counter.count).toHaveBeenCalledTimes(3);
expect(result).toEqual([1, 2]);
});
});

0 comments on commit 59d408c

Please sign in to comment.