Skip to content

Commit

Permalink
fix: fixes a bug where takeWhile and skipWhile incorrectly exhausted …
Browse files Browse the repository at this point in the history
…the iterable before emitting (#12)
  • Loading branch information
ricokahler committed Jul 8, 2022
1 parent 6aa0ec8 commit 150e092
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,4 +1,4 @@
# @ricokahler/lazy · [![codecov](https://codecov.io/gh/ricokahler/lazy/branch/main/graph/badge.svg)](https://codecov.io/gh/ricokahler/lazy) [![github status checks](https://badgen.net/github/checks/ricokahler/lazy)](https://github.com/ricokahler/lazy/actions) [![bundlephobia](https://badgen.net/bundlephobia/minzip/@ricokahler/lazy)](https://bundlephobia.com/result?p=@ricokahler/lazy) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
# @ricokahler/lazy · [![codecov](https://codecov.io/gh/ricokahler/lazy/branch/main/graph/badge.svg)](https://codecov.io/gh/ricokahler/lazy) [![github status checks](https://badgen.net/github/checks/ricokahler/lazy)](https://github.com/ricokahler/lazy/actions) [![bundlejs.com](https://badgen.net/bundlephobia/minzip/@ricokahler/lazy)](https://bundlejs.com/?q=@ricokahler/lazy) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

A small [(~900B gzip\*)](https://bundle.js.org/?q=@ricokahler/lazy@latest), **useful** set of methods for lazy iteration of iterables.

Expand Down
75 changes: 41 additions & 34 deletions index.js
Expand Up @@ -80,7 +80,7 @@ async function* flatAsync(iterable, depth = 1) {

function take(iterable, n) {
return map(
filter(
takeWhile(
scan(iterable, ([i], item) => [i - 1, item], [n]),
([i]) => i >= 0,
),
Expand All @@ -90,51 +90,56 @@ function take(iterable, n) {

function skip(iterable, n) {
return map(
filter(
skipWhile(
scan(iterable, ([i], item) => [i - 1, item], [n]),
([i]) => i < 0,
([i]) => i >= 0,
),
([, item]) => item,
);
}

function takeWhile(iterable, accept) {
return map(
filter(
scan(
iterable,
([yielding], item) => {
if (yielding && accept(item)) return [true, item];
return [false];
},
[true],
),
([yielding]) => yielding,
),
([, item]) => item,
);
function* takeWhileSync(iterable, accept) {
for (const item of iterable) {
if (accept(item)) yield item;
else return;
}
}

async function* takeWhileAsync(iterable, accept) {
for await (const item of iterable) {
if (accept(item)) yield item;
else return;
}
}

function flatMap(iterable, mapper) {
return flat(map(iterable, mapper));
}

function skipWhile(iterable, accept) {
return map(
filter(
scan(
iterable,
([yielding], item) => {
if (yielding) return [true, item];
if (accept(item)) return [false];
return [true, item];
},
[false],
),
([yielding]) => yielding,
),
([, item]) => item,
);
function* skipWhileSync(iterable, accept) {
let yielding = false;

for (const item of iterable) {
if (yielding) {
yield item;
} else if (!accept(item)) {
yield item;
yielding = true;
}
}
}

function* skipWhileAsync(iterable, accept) {
let yielding = false;

for (const item of iterable) {
if (yielding) {
yield item;
} else if (!accept(item)) {
yield item;
yielding = true;
}
}
}

function firstSync(iterable) {
Expand Down Expand Up @@ -222,6 +227,8 @@ function pick(syncMethod, asyncMethod) {
const map = pick(mapSync, mapAsync);
const filter = pick(filterSync, filterAsync);
const scan = pick(scanSync, scanAsync);
const takeWhile = pick(takeWhileSync, takeWhileAsync);
const skipWhile = pick(skipWhileSync, skipWhileAsync);
const flat = pick(flatSync, flatAsync);
const first = pick(firstSync, firstAsync);
const to = pick(toSync, toAsync);
Expand Down
6 changes: 6 additions & 0 deletions index.test.ts
Expand Up @@ -159,6 +159,9 @@ describe('skipWhile', () => {
.to(Array),
).toEqual([]);
});

it.todo('async/sync');
it.todo('completion prior to iterable exhaustion');
});

describe('takeWhile', () => {
Expand All @@ -177,6 +180,9 @@ describe('takeWhile', () => {
.to(Array),
).toEqual([1, 2, 3]);
});

it.todo('async/sync');
it.todo('completion prior to iterable exhaustion');
});

describe('map', () => {
Expand Down

0 comments on commit 150e092

Please sign in to comment.