itertools
Advanced tools
Comparing version 2.2.2 to 2.2.3
@@ -5,2 +5,8 @@ type Predicate<T> = (value: T) => boolean; | ||
/** | ||
* Returns the first item in the iterable for which the predicate holds, if | ||
* any. If no predicate is given, it will return the first value returned by | ||
* the iterable. | ||
*/ | ||
declare function find<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): T | undefined; | ||
/** | ||
* Returns true when all of the items in iterable are truthy. An optional key | ||
@@ -427,8 +433,2 @@ * function can be used to define what truthiness means for this specific | ||
/** | ||
* Returns the first item in the iterable for which the predicate holds, if | ||
* any. If no predicate is given, it will return the first value returned by | ||
* the iterable. | ||
*/ | ||
declare function find<T>(iterable: Iterable<T>, keyFn?: Predicate<T>): T | undefined; | ||
/** | ||
* Almost an alias of find(). There only is a difference if no key fn is | ||
@@ -435,0 +435,0 @@ * provided. In that case, `find()` will return the first item in the iterable, |
@@ -181,3 +181,5 @@ // src/utils.ts | ||
const it = iter(iterable); | ||
for (const value of it) { | ||
let res; | ||
while (!(res = it.next()).done) { | ||
const value = res.value; | ||
if (!predicate(value)) { | ||
@@ -255,9 +257,16 @@ yield value; | ||
throw new Error("step cannot be negative"); | ||
for (const [i, value] of enumerate(iterable)) { | ||
let i = -1; | ||
const it = iter(iterable); | ||
let res; | ||
while (true) { | ||
i++; | ||
if (stop !== null && i >= stop) | ||
return; | ||
res = it.next(); | ||
if (res.done) | ||
return; | ||
if (i < start) | ||
continue; | ||
if (stop !== null && i >= stop) | ||
break; | ||
if ((i - start) % step === 0) { | ||
yield value; | ||
yield res.value; | ||
} | ||
@@ -354,3 +363,6 @@ } | ||
function* takewhile(iterable, predicate) { | ||
for (const value of iterable) { | ||
const it = iter(iterable); | ||
let res; | ||
while (!(res = it.next()).done) { | ||
const value = res.value; | ||
if (!predicate(value)) | ||
@@ -371,2 +383,18 @@ return; | ||
// src/builtins.ts | ||
function find(iterable, keyFn) { | ||
const it = iter(iterable); | ||
if (keyFn === void 0) { | ||
const value = it.next(); | ||
return value.done ? value.value : value.value; | ||
} else { | ||
let res; | ||
while (!(res = it.next()).done) { | ||
const value = res.value; | ||
if (keyFn(value)) { | ||
return value; | ||
} | ||
} | ||
return void 0; | ||
} | ||
} | ||
function every(iterable, keyFn = identityPredicate) { | ||
@@ -393,3 +421,3 @@ for (const item of iterable) { | ||
} | ||
function* enumerate(iterable, start = 0) { | ||
function* enumerate2(iterable, start = 0) { | ||
let index = start; | ||
@@ -444,7 +472,7 @@ for (const value of iterable) { | ||
const it = iter(iterable); | ||
const start = it.next(); | ||
if (start.done) { | ||
const start = find(it); | ||
if (start === void 0) { | ||
return void 0; | ||
} else { | ||
return reduce3(it, reducer, start.value); | ||
return reduce3(it, reducer, start); | ||
} | ||
@@ -493,17 +521,2 @@ } | ||
} | ||
function find(iterable, keyFn) { | ||
if (keyFn === void 0) { | ||
for (const value of iterable) { | ||
return value; | ||
} | ||
return void 0; | ||
} else { | ||
for (const value of iterable) { | ||
if (keyFn(value)) { | ||
return value; | ||
} | ||
} | ||
return void 0; | ||
} | ||
} | ||
function first(iterable, keyFn) { | ||
@@ -527,3 +540,3 @@ return find(iterable, keyFn != null ? keyFn : isDefined); | ||
dropwhile, | ||
enumerate, | ||
enumerate2 as enumerate, | ||
every, | ||
@@ -530,0 +543,0 @@ filter, |
{ | ||
"name": "itertools", | ||
"version": "2.2.2", | ||
"version": "2.2.3", | ||
"description": "A JavaScript port of Python's awesome itertools standard library", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
187358
1545