@ts-common/iterator
Advanced tools
Comparing version 0.0.45 to 0.1.0
import { Tuple2 } from "@ts-common/tuple"; | ||
export interface IteratorResult<T> { | ||
readonly done: boolean; | ||
readonly value: T; | ||
} | ||
export interface Iterator<T> { | ||
readonly next: () => IteratorResult<T>; | ||
} | ||
export interface Iterable<T> { | ||
readonly [Symbol.iterator]: () => Iterator<T>; | ||
} | ||
export declare const iterable: <T>(createIterator: () => Iterator<T>) => Iterable<T>; | ||
@@ -3,0 +13,0 @@ export declare type Entry<T> = Tuple2<number, T>; |
@@ -6,59 +6,47 @@ "use strict"; | ||
exports.entry = tuple_1.tuple2; | ||
exports.entries = (input) => { | ||
function* iterator() { | ||
// tslint:disable-next-line:no-if-statement | ||
if (input === undefined) { | ||
return; | ||
} | ||
let index = 0; | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
for (const value of input) { | ||
yield exports.entry(index, value); | ||
/* tslint:disable-next-line:no-expression-statement */ | ||
++index; | ||
} | ||
exports.entries = (input) => exports.iterable(function* () { | ||
// tslint:disable-next-line:no-if-statement | ||
if (input === undefined) { | ||
return; | ||
} | ||
return exports.iterable(iterator); | ||
}; | ||
exports.map = (input, func) => { | ||
function* iterator() { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
for (const [index, value] of exports.entries(input)) { | ||
yield func(value, index); | ||
} | ||
let index = 0; | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
for (const value of input) { | ||
yield exports.entry(index, value); | ||
/* tslint:disable-next-line:no-expression-statement */ | ||
++index; | ||
} | ||
return exports.iterable(iterator); | ||
}; | ||
}); | ||
exports.map = (input, func) => exports.iterable(function* () { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
for (const [index, value] of exports.entries(input)) { | ||
yield func(value, index); | ||
} | ||
}); | ||
exports.drop = (input, n = 1) => exports.filter(input, (_, i) => n <= i); | ||
exports.flatten = (input) => { | ||
function* iterator() { | ||
exports.flatten = (input) => exports.iterable(function* () { | ||
// tslint:disable-next-line:no-if-statement | ||
if (input === undefined) { | ||
return; | ||
} | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
for (const v of input) { | ||
// tslint:disable-next-line:no-if-statement | ||
if (input === undefined) { | ||
return; | ||
if (v !== undefined) { | ||
yield* v; | ||
} | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
for (const v of input) { | ||
// tslint:disable-next-line:no-if-statement | ||
if (v !== undefined) { | ||
yield* v; | ||
} | ||
} | ||
} | ||
return exports.iterable(iterator); | ||
}; | ||
}); | ||
// tslint:disable-next-line:readonly-array | ||
exports.concat = (...input) => exports.flatten(input); | ||
exports.takeWhile = (input, func) => { | ||
function* iterator() { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
for (const [index, value] of exports.entries(input)) { | ||
/* tslint:disable-next-line:no-if-statement */ | ||
if (!func(value, index)) { | ||
return; | ||
} | ||
yield value; | ||
exports.takeWhile = (input, func) => exports.iterable(function* () { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
for (const [index, value] of exports.entries(input)) { | ||
/* tslint:disable-next-line:no-if-statement */ | ||
if (!func(value, index)) { | ||
return; | ||
} | ||
yield value; | ||
} | ||
return exports.iterable(iterator); | ||
}; | ||
}); | ||
exports.take = (input, n = 1) => exports.takeWhile(input, (_, i) => i < n); | ||
@@ -79,11 +67,8 @@ exports.find = (input, func) => { | ||
exports.filter = (input, func) => exports.flatMap(input, (v, i) => func(v, i) ? [v] : []); | ||
const infinite = () => { | ||
function* iterator() { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
while (true) { | ||
yield; | ||
} | ||
const infinite = () => exports.iterable(function* () { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
while (true) { | ||
yield; | ||
} | ||
return exports.iterable(iterator); | ||
}; | ||
}); | ||
exports.generate = (func, count) => exports.map(exports.takeWhile(infinite(), (_, i) => i !== count), (_, i) => func(i)); | ||
@@ -111,23 +96,20 @@ exports.repeat = (v, count) => exports.generate(() => v, count); | ||
/* tslint:disable-next-line:readonly-array */ | ||
exports.zip = (...inputs) => { | ||
function* iterator() { | ||
const iterators = inputs.map(i => i === undefined ? [][Symbol.iterator]() : i[Symbol.iterator]()); | ||
exports.zip = (...inputs) => exports.iterable(function* () { | ||
const iterators = inputs.map(i => i === undefined ? [][Symbol.iterator]() : i[Symbol.iterator]()); | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
while (true) { | ||
const result = new Array(inputs.length); | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
while (true) { | ||
const result = new Array(inputs.length); | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
for (const [index, it] of exports.entries(iterators)) { | ||
const v = it.next(); | ||
/* tslint:disable-next-line:no-if-statement */ | ||
if (v.done) { | ||
return; | ||
} | ||
/* tslint:disable-next-line:no-object-mutation no-expression-statement */ | ||
result[index] = v.value; | ||
for (const [index, it] of exports.entries(iterators)) { | ||
const v = it.next(); | ||
/* tslint:disable-next-line:no-if-statement */ | ||
if (v.done) { | ||
return; | ||
} | ||
yield result; | ||
/* tslint:disable-next-line:no-object-mutation no-expression-statement */ | ||
result[index] = v.value; | ||
} | ||
yield result; | ||
} | ||
return exports.iterable(iterator); | ||
}; | ||
}); | ||
// TypeScript gives an error in case if type of a and type of b are different | ||
@@ -134,0 +116,0 @@ exports.isStrictEqual = (a, b) => a === b; |
{ | ||
"name": "@ts-common/iterator", | ||
"version": "0.0.45", | ||
"version": "0.1.0", | ||
"description": "Iterator library for JavaScript and TypeScript", | ||
@@ -49,3 +49,3 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/chai": "^4.1.6", | ||
"@types/chai": "^4.1.7", | ||
"@types/mocha": "^5.2.5", | ||
@@ -57,8 +57,8 @@ "chai": "^4.2.0", | ||
"tslint": "^5.11.0", | ||
"tslint-immutable": "^4.8.0", | ||
"typescript": "^3.1.3" | ||
"tslint-immutable": "^4.9.1", | ||
"typescript": "^3.1.6" | ||
}, | ||
"dependencies": { | ||
"@ts-common/tuple": "^0.0.0" | ||
"@ts-common/tuple": "^0.0.2" | ||
} | ||
} |
import { Tuple2, tuple2 } from "@ts-common/tuple" | ||
export interface IteratorResult<T> { | ||
readonly done: boolean; | ||
readonly value: T; | ||
} | ||
export interface Iterator<T> { | ||
readonly next: () => IteratorResult<T>; | ||
} | ||
export interface Iterable<T> { | ||
readonly [Symbol.iterator]: () => Iterator<T>; | ||
} | ||
export const iterable = <T>(createIterator: () => Iterator<T>): Iterable<T> => | ||
@@ -10,4 +23,4 @@ ({ [Symbol.iterator]: createIterator }) | ||
export const entries = <T>(input: Iterable<T>|undefined): Iterable<Entry<T>> => { | ||
function *iterator(): Iterator<Entry<T>> { | ||
export const entries = <T>(input: Iterable<T>|undefined): Iterable<Entry<T>> => | ||
iterable(function *(): Iterator<Entry<T>> { | ||
// tslint:disable-next-line:no-if-statement | ||
@@ -24,5 +37,3 @@ if (input === undefined) { | ||
} | ||
} | ||
return iterable(iterator) | ||
} | ||
}) | ||
@@ -32,4 +43,4 @@ export const map = <T, I>( | ||
func: (v: I, i: number) => T, | ||
): Iterable<T> => { | ||
function *iterator(): Iterator<T> { | ||
): Iterable<T> => | ||
iterable(function *(): Iterator<T> { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
@@ -39,5 +50,3 @@ for (const [index, value] of entries(input)) { | ||
} | ||
} | ||
return iterable(iterator) | ||
} | ||
}) | ||
@@ -47,4 +56,4 @@ export const drop = <T>(input: Iterable<T>|undefined, n: number = 1): Iterable<T> => | ||
export const flatten = <T>(input: Iterable<Iterable<T>|undefined>|undefined): Iterable<T> => { | ||
function *iterator(): Iterator<T> { | ||
export const flatten = <T>(input: Iterable<Iterable<T>|undefined>|undefined): Iterable<T> => | ||
iterable(function *(): Iterator<T> { | ||
// tslint:disable-next-line:no-if-statement | ||
@@ -61,5 +70,3 @@ if (input === undefined) { | ||
} | ||
} | ||
return iterable(iterator) | ||
} | ||
}) | ||
@@ -73,4 +80,4 @@ // tslint:disable-next-line:readonly-array | ||
func: (v: T, i: number) => boolean, | ||
): Iterable<T> => { | ||
function *iterator(): Iterator<T> { | ||
): Iterable<T> => | ||
iterable(function *(): Iterator<T> { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
@@ -84,5 +91,3 @@ for (const [index, value] of entries(input)) { | ||
} | ||
} | ||
return iterable(iterator) | ||
} | ||
}) | ||
@@ -127,9 +132,7 @@ export const take = <T>(input: Iterable<T>|undefined, n: number = 1) => | ||
const infinite = (): Iterable<void> => { | ||
function *iterator(): Iterator<void> { | ||
const infinite = (): Iterable<void> => | ||
iterable(function *(): Iterator<void> { | ||
/* tslint:disable-next-line:no-loop-statement */ | ||
while (true) { yield } | ||
} | ||
return iterable(iterator) | ||
} | ||
}) | ||
@@ -163,3 +166,4 @@ export const generate = <T>(func: (i: number) => T, count?: number): Iterable<T> => | ||
(a, b, i) => a !== undefined ? func(a, b, i) : b, | ||
undefined) | ||
undefined, | ||
) | ||
@@ -195,4 +199,4 @@ export const last = <T>(input: Iterable<T>|undefined): T|undefined => | ||
/* tslint:disable-next-line:readonly-array */ | ||
export const zip = <T>(...inputs: Array<Iterable<T>|undefined>): Iterable<ReadonlyArray<T>> => { | ||
function *iterator(): Iterator<ReadonlyArray<T>> { | ||
export const zip = <T>(...inputs: Array<Iterable<T>|undefined>): Iterable<ReadonlyArray<T>> => | ||
iterable(function *(): Iterator<ReadonlyArray<T>> { | ||
const iterators = inputs.map( | ||
@@ -216,5 +220,3 @@ i => i === undefined ? [][Symbol.iterator]() : i[Symbol.iterator](), | ||
} | ||
} | ||
return iterable(iterator) | ||
} | ||
}) | ||
@@ -221,0 +223,0 @@ // TypeScript gives an error in case if type of a and type of b are different |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40163
416
+ Added@ts-common/tuple@0.0.2(transitive)
- Removed@ts-common/tuple@0.0.0(transitive)
Updated@ts-common/tuple@^0.0.2