Comparing version
@@ -31,42 +31,3 @@ /** | ||
* | ||
* Module Guide. | ||
* | ||
* 1. css (Deleight.css) (deleight/css) | ||
* | ||
* 2. dom (Deleight.dom) (deleight/dom) | ||
* 2.1. apply (Deleight.dom) (deleight/dom/apply) | ||
* 2.2. components (Deleight.dom) (deleight/dom/components) | ||
* 2.3. fragment (Deleight.dom) (deleight/dom/fragment) | ||
* 2.4. process (Deleight.dom) (deleight/dom/process) | ||
* | ||
* 3. function (Deleight.function) (deleight/function) | ||
* 3.1. Context (Deleight.function) (deleight/function/context) | ||
* 3.2. Return (Deleight.function) (deleight/function/return) | ||
* | ||
* 4. generators (Deleight.Generator) (deleight/generators) | ||
* | ||
* 5. lists (Deleight.List) (deleight/lists) | ||
* 5.1. Array (Deleight.List) (deleight/lists/array) | ||
* 5.2. Element (Deleight.List) (deleight/lists/element) | ||
* | ||
* 6. object (Deleight.object) (deleight/object) | ||
* 6.1. apply (Deleight.object) (deleight/object/apply) | ||
* 6.2. deepMember (Deleight.object) (deleight/object/deepmember) | ||
* 6.3. member (Deleight.object) (deleight/object/member) | ||
* 6.4. operations (Deleight.object) (deleight/object/operations) | ||
* 6.5. process (Deleight.object) (deleight/object/process) | ||
* 6.6. sharedMember (Deleight.object) (deleight/object/sharedmember) | ||
* | ||
* 7. Process (Deleight.process) (deleight/process) | ||
* | ||
* 8. proxies (Deleight.Proxy) (deleight/proxy) | ||
* 8.1. Alias (Deleight.Proxy) (deleight/proxy/alias) | ||
* 8.2. Scope (Deleight.Proxy) (deleight/proxy/scope) | ||
* 8.3. Selector (Deleight.Proxy) (deleight/proxy/selector) | ||
* 8.4. Wrapper (Deleight.Proxy) (deleight/proxy/wrapper) | ||
* | ||
* 9. template (Deleight.template) (deleight/template) | ||
* | ||
* | ||
* | ||
* @module | ||
@@ -73,0 +34,0 @@ * |
@@ -32,42 +32,3 @@ "use strict"; | ||
* | ||
* Module Guide. | ||
* | ||
* 1. css (Deleight.css) (deleight/css) | ||
* | ||
* 2. dom (Deleight.dom) (deleight/dom) | ||
* 2.1. apply (Deleight.dom) (deleight/dom/apply) | ||
* 2.2. components (Deleight.dom) (deleight/dom/components) | ||
* 2.3. fragment (Deleight.dom) (deleight/dom/fragment) | ||
* 2.4. process (Deleight.dom) (deleight/dom/process) | ||
* | ||
* 3. function (Deleight.function) (deleight/function) | ||
* 3.1. Context (Deleight.function) (deleight/function/context) | ||
* 3.2. Return (Deleight.function) (deleight/function/return) | ||
* | ||
* 4. generators (Deleight.Generator) (deleight/generators) | ||
* | ||
* 5. lists (Deleight.List) (deleight/lists) | ||
* 5.1. Array (Deleight.List) (deleight/lists/array) | ||
* 5.2. Element (Deleight.List) (deleight/lists/element) | ||
* | ||
* 6. object (Deleight.object) (deleight/object) | ||
* 6.1. apply (Deleight.object) (deleight/object/apply) | ||
* 6.2. deepMember (Deleight.object) (deleight/object/deepmember) | ||
* 6.3. member (Deleight.object) (deleight/object/member) | ||
* 6.4. operations (Deleight.object) (deleight/object/operations) | ||
* 6.5. process (Deleight.object) (deleight/object/process) | ||
* 6.6. sharedMember (Deleight.object) (deleight/object/sharedmember) | ||
* | ||
* 7. Process (Deleight.process) (deleight/process) | ||
* | ||
* 8. proxies (Deleight.Proxy) (deleight/proxy) | ||
* 8.1. Alias (Deleight.Proxy) (deleight/proxy/alias) | ||
* 8.2. Scope (Deleight.Proxy) (deleight/proxy/scope) | ||
* 8.3. Selector (Deleight.Proxy) (deleight/proxy/selector) | ||
* 8.4. Wrapper (Deleight.Proxy) (deleight/proxy/wrapper) | ||
* | ||
* 9. template (Deleight.template) (deleight/template) | ||
* | ||
* | ||
* | ||
* @module | ||
@@ -74,0 +35,0 @@ * |
/** | ||
* This module exports many useful generators for JS applications. | ||
* There are generators for most array methods like `map` and `filter`, | ||
* along many others drawn from other languages that make heavy use | ||
* along many those drawn from other languages that make heavy use | ||
* of generators, like Python. | ||
@@ -13,44 +13,30 @@ * | ||
* | ||
* API Table of Contents | ||
* | ||
* {@link range} | ||
* {@link forceIterator} | ||
* {@link forEach} | ||
* {@link forAsyncEach} | ||
* {@link forNext} | ||
* {@link next} | ||
* {@link items} | ||
* {@link random} | ||
* {@link repeat} | ||
* {@link map} | ||
* {@link filter} | ||
* {@link reduce} | ||
* {@link chain} | ||
* {@link zipFlat} | ||
* {@link zip} | ||
* (product: Still buggy. Not yet exported) | ||
* | ||
* | ||
* @module | ||
*/ | ||
/** | ||
* Perform the given action for all items of the iterable. | ||
* Performs the given action for all items of the iterable. | ||
* | ||
* @example | ||
* import { forEach } from 'deleight/generators'; | ||
* forEach(range(10), ()=> console.log(i)); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param action | ||
*/ | ||
export declare function forEach<T>(iter: Iterable<any>, action: (item: any, i?: number) => T): void; | ||
export declare function forEach<T>(it: Iterable<any>, action: (item: any, i?: number) => T): void; | ||
/** | ||
* Perform the given action for all items of the async iterable. | ||
* Performs the given action for all items of the async iterable. | ||
* | ||
* @example | ||
* import { forEachAsync } from 'deleight/generators'; | ||
* async function* asyncGen() { | ||
* for (let i of (range(10))) yield i; | ||
* } | ||
* forEachAsync(asyncGen(), ()=> console.log(i)); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param action | ||
*/ | ||
export declare function forAsyncEach<T>(iter: AsyncIterable<any>, action: (item: any, i?: number) => T): Promise<void>; | ||
export declare function forEachAsync<T>(it: AsyncIterable<any>, action: (item: any, i?: number) => T): Promise<void>; | ||
/** | ||
@@ -60,8 +46,9 @@ * Maps the values of the iterable to other values using the given mapper. | ||
* @example | ||
* import { map } from 'deleight/generators'; | ||
* map(range(10), i=> 2 * i); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param mapper | ||
*/ | ||
export declare function map<T>(iter: Iterable<any>, mapper: (item: any, i?: number) => T): Iterable<T>; | ||
export declare function map<T>(it: Iterable<any>, mapper: (item: any, i?: number) => T): Iterable<T>; | ||
/** | ||
@@ -71,41 +58,43 @@ * Filters the values of the iterable using the given test function. | ||
* @example | ||
* import { filter } from 'deleight/generators'; | ||
* filter(range(10), i=> 2 < i); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param test | ||
*/ | ||
export declare function filter<T>(iter: Iterable<any>, test: (item: any, i?: number) => boolean): Iterable<T>; | ||
export declare function filter<T>(it: Iterable<any>, test: (item: any, i?: number) => boolean): Iterable<T>; | ||
/** | ||
* Reduces the the iterable using the given reducer function. | ||
* Reduces the iterable using the given reducer function. | ||
* | ||
* @example | ||
* import { reduce } from 'deleight/generators'; | ||
* reduce(range(10), ((r, i) => r * i), 3); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param reducer | ||
*/ | ||
export declare function reduce<T>(iter: Iterable<any>, reducer: (value: any, item: any, i?: number) => boolean, value?: any): T; | ||
export declare function reduce<T>(it: Iterable<any>, reducer: (value: any, item: any, i?: number) => boolean, value?: any): T; | ||
/** | ||
* Chain multiple iterables... | ||
* Chains multiple iterables... | ||
* | ||
* @example | ||
* import { chain } from 'deleight/generators'; | ||
* for (let i of chain(range(10, range(15)))) { | ||
* for (let i of chain(range(10), range(15))) { | ||
* console.log(i); // 0, 1, 2, 3,...9, 0, 1, 2, ..., 14 | ||
* } | ||
* | ||
* @param {...any[]} args | ||
* @param its | ||
*/ | ||
export declare function chain<T>(...args: Iterable<any>[]): Iterable<T>; | ||
export declare function chain<T>(...its: Iterable<any>[]): Iterable<T>; | ||
/** | ||
* Forcs any iterable to become an iterator. Will throw | ||
* Forces any iterable or iterator to become an iterator. Will throw | ||
* if this is not possible. | ||
* | ||
* @example | ||
* import { iter } from 'deleight/generators'; | ||
* const it = iter([1, 2, 3, 4, 5]); | ||
* import { forceIterator } from 'deleight/generators'; | ||
* const it = forceIterator([1, 2, 3, 4, 5]); | ||
* | ||
* @param { any } it | ||
* @param it | ||
*/ | ||
export declare function forceIterator(it: any): Iterator<any>; | ||
export declare function forceIterator(it: Iterable<any> | Iterator<any>): Iterator<any>; | ||
/** | ||
@@ -133,6 +122,6 @@ * Fast and 'costless' range function for javascript based on generators. | ||
* | ||
* @param {any} arrayLike | ||
* @param {any} it | ||
* @param {Iterable<any>} index | ||
*/ | ||
export declare function items(arrayLike: any, index: Iterable<number>): Generator<any, void, unknown>; | ||
export declare function items(it: any, index: Iterable<number>): Generator<any, void, unknown>; | ||
/** | ||
@@ -148,30 +137,27 @@ * Returns a generator that yields first argument (`what`) the number of | ||
* | ||
* @param {Iterable<any>} what | ||
* @param {Iterable<any>} it | ||
* @param {number} [times] | ||
*/ | ||
export declare function repeat<T>(what: Iterable<T> | (() => Iterable<T>), times?: number, ...extras: any[]): Generator<any, void, unknown>; | ||
export declare function repeat<T>(it: Iterable<T> | (() => Iterable<T>), times?: number, ...extras: any[]): Generator<any, void, unknown>; | ||
/** | ||
* Returns an iterator over the next 'count' items of the iterator. | ||
* Returns a Generator over the next 'count' items of the iterable or iterator. | ||
* In most cases you will call this function with an iterator. | ||
* | ||
* Note that, for performance reasons, this only accepts an | ||
* iterator as the 'it' argument. You can convert any iterable to an | ||
* iterator using the {@link forceIterator} function as shown in the following | ||
* example. | ||
* | ||
* If a firstValue is specified, it will be yielded first. | ||
* | ||
* @example | ||
* import { next, iter } from 'deleight/generators'; | ||
* const it = iter([1, 'a', 2, 'b', 3, 'c', 4, 'd']); | ||
* import { next, forceIterator } from 'deleight/generators'; | ||
* const it = forceIterator([1, 'a', 2, 'b', 3, 'c', 4, 'd']); | ||
* const [num1, let1] = next(it, 2); // (1, 'a') | ||
* const [num2, let2] = next(it, 2); // (2, 'b') | ||
* | ||
* @param {Iterator<any>} it | ||
* @param {number} count | ||
* @param { any } [firstValue] | ||
* @param it | ||
* @param count | ||
* @param firstValue | ||
*/ | ||
export declare function next(it: Iterator<any>, count: number, firstValue?: any): Generator<any, void, unknown>; | ||
export declare function next(it: Iterable<any> | Iterator<any>, count: number, firstValue?: any): Generator<any, void, unknown>; | ||
/** | ||
* Returns an iterator of iterators over the next 'count' items of | ||
* the given iterable | ||
* Returns a generator of generators over the next 'count' items of | ||
* the given iterable. In other words, this function will partition the | ||
* input iterable with each partition containing `count` items. | ||
* | ||
@@ -186,44 +172,43 @@ * @example | ||
*/ | ||
export declare function forNext(it: Iterable<any>, count: number): Generator<any[], void, unknown>; | ||
export declare function forNext(it: Iterable<any>, count: number): Generator<Generator<any, void, unknown>, void, unknown>; | ||
/** | ||
* Returns an iterator over the items of all the input iterators, starting from | ||
* the zero index to the maximum index of the first argument. The | ||
* effective length of the iterator is the multiple of the length of thr smallest | ||
* iterator and the number of iterators (number of args). | ||
* Returns a generator over the items of all the input args (iterables), starting from | ||
* the zero index to the maximum index of the smallest arg. | ||
* | ||
* Can be used to join arrays in a way not supported by `concat`, `push`, etc. | ||
* To pass an array as an iterator, call array.values(). | ||
* The effective length of the generator is the multiple of the length of the smallest | ||
* arg and the number of args. | ||
* | ||
* @example | ||
* import { zipFlat } from 'deleight/generators'; | ||
* for (let i of zip(range(10, range(15)))) { | ||
* console.log(i); // (0, 0, 1, 1, 2, 2, .... till smallest iterable (10) is exhausted. | ||
* for (let i of zipFlat(range(10), range(15))) { | ||
* console.log(i); // (0, 0, 1, 1, 2, 2, .... till range(10) is exhausted. | ||
* } | ||
* | ||
* @param {...any[]} args | ||
* @param its | ||
*/ | ||
export declare function zipFlat(...args: any[]): Generator<any, void, unknown>; | ||
export declare function zipFlat(...its: Iterable<any>[]): Generator<any, void, unknown>; | ||
/** | ||
* Returns an iterator over the items of all the input iterators, starting from | ||
* the zero index to the maximum index of the first argument. The | ||
* effective length of the iterator is the multiple of the length of thr smallest | ||
* iterator and the number of iterators (number of args). | ||
* Returns a generator over the combined items of all the input args (iterables), | ||
* starting from the zero index to the maximum index of the smallest arg. | ||
* | ||
* Can be used to join arrays in a way not supported by `concat`, `push`, etc. | ||
* The effective length of the generator is the length of the smallest input | ||
* iterable. | ||
* | ||
* @example | ||
* import { zip } from 'deleight/generators'; | ||
* for (let i of zip(range(10, range(15)))) { | ||
* console.log(i); // (0, 0), (1, 1), (2, 2), .... till smallest iterable (10) is exhausted. | ||
* for (let i of zip(range(10), range(15))) { | ||
* console.log(i); // (0, 0), (1, 1), (2, 2), .... till range(10) is exhausted. | ||
* } | ||
* | ||
* @param {...any[]} args | ||
* @param its | ||
*/ | ||
export declare function zip(...args: any[]): Generator<any[], void, unknown>; | ||
export declare function zip(...its: Iterable<any>[]): Generator<Generator<any, void, unknown>, void, unknown>; | ||
/** | ||
* Returns an unordered/random iterator over the input array.. | ||
* Returns an unordered/random generator over the input itrable. | ||
* Note that this is forced to materialize the input before running. | ||
* | ||
* | ||
* @example | ||
* import { uItems } from 'deleight/generators'; | ||
* const unOrdered = uItems([1, 2, 3, 4]); // [4, 1, 3, 2] | ||
* import { random } from 'deleight/generators'; | ||
* const unOrdered = random([1, 2, 3, 4]); // probably [4, 1, 3, 2] | ||
* | ||
@@ -230,0 +215,0 @@ * @param it The iterable to get items from |
@@ -5,3 +5,3 @@ "use strict"; | ||
* There are generators for most array methods like `map` and `filter`, | ||
* along many others drawn from other languages that make heavy use | ||
* along many those drawn from other languages that make heavy use | ||
* of generators, like Python. | ||
@@ -15,38 +15,20 @@ * | ||
* | ||
* API Table of Contents | ||
* | ||
* {@link range} | ||
* {@link forceIterator} | ||
* {@link forEach} | ||
* {@link forAsyncEach} | ||
* {@link forNext} | ||
* {@link next} | ||
* {@link items} | ||
* {@link random} | ||
* {@link repeat} | ||
* {@link map} | ||
* {@link filter} | ||
* {@link reduce} | ||
* {@link chain} | ||
* {@link zipFlat} | ||
* {@link zip} | ||
* (product: Still buggy. Not yet exported) | ||
* | ||
* | ||
* @module | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.random = exports.zip = exports.zipFlat = exports.forNext = exports.next = exports.repeat = exports.items = exports.range = exports.forceIterator = exports.chain = exports.reduce = exports.filter = exports.map = exports.forAsyncEach = exports.forEach = void 0; | ||
exports.random = exports.zip = exports.zipFlat = exports.forNext = exports.next = exports.repeat = exports.items = exports.range = exports.forceIterator = exports.chain = exports.reduce = exports.filter = exports.map = exports.forEachAsync = exports.forEach = void 0; | ||
/** | ||
* Perform the given action for all items of the iterable. | ||
* Performs the given action for all items of the iterable. | ||
* | ||
* @example | ||
* import { forEach } from 'deleight/generators'; | ||
* forEach(range(10), ()=> console.log(i)); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param action | ||
*/ | ||
function forEach(iter, action) { | ||
function forEach(it, action) { | ||
let i = 0; | ||
for (let item of iter) | ||
for (let item of it) | ||
action(item, i++); | ||
@@ -56,16 +38,20 @@ } | ||
/** | ||
* Perform the given action for all items of the async iterable. | ||
* Performs the given action for all items of the async iterable. | ||
* | ||
* @example | ||
* import { forEachAsync } from 'deleight/generators'; | ||
* async function* asyncGen() { | ||
* for (let i of (range(10))) yield i; | ||
* } | ||
* forEachAsync(asyncGen(), ()=> console.log(i)); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param action | ||
*/ | ||
async function forAsyncEach(iter, action) { | ||
async function forEachAsync(it, action) { | ||
let i = 0; | ||
for await (let item of iter) | ||
for await (let item of it) | ||
action(item, i++); | ||
} | ||
exports.forAsyncEach = forAsyncEach; | ||
exports.forEachAsync = forEachAsync; | ||
/** | ||
@@ -75,10 +61,11 @@ * Maps the values of the iterable to other values using the given mapper. | ||
* @example | ||
* import { map } from 'deleight/generators'; | ||
* map(range(10), i=> 2 * i); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param mapper | ||
*/ | ||
function* map(iter, mapper) { | ||
function* map(it, mapper) { | ||
let i = 0; | ||
for (let item of iter) | ||
for (let item of it) | ||
yield mapper(item, i++); | ||
@@ -91,10 +78,11 @@ } | ||
* @example | ||
* import { filter } from 'deleight/generators'; | ||
* filter(range(10), i=> 2 < i); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param test | ||
*/ | ||
function* filter(iter, test) { | ||
function* filter(it, test) { | ||
let i = 0; | ||
for (let item of iter) | ||
for (let item of it) | ||
if (test(item, i++)) | ||
@@ -105,13 +93,14 @@ yield item; | ||
/** | ||
* Reduces the the iterable using the given reducer function. | ||
* Reduces the iterable using the given reducer function. | ||
* | ||
* @example | ||
* import { reduce } from 'deleight/generators'; | ||
* reduce(range(10), ((r, i) => r * i), 3); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param reducer | ||
*/ | ||
function reduce(iter, reducer, value) { | ||
function reduce(it, reducer, value) { | ||
let i = 0; | ||
for (let item of iter) | ||
for (let item of it) | ||
value = reducer(value, item, i++); | ||
@@ -122,15 +111,15 @@ return value; | ||
/** | ||
* Chain multiple iterables... | ||
* Chains multiple iterables... | ||
* | ||
* @example | ||
* import { chain } from 'deleight/generators'; | ||
* for (let i of chain(range(10, range(15)))) { | ||
* for (let i of chain(range(10), range(15))) { | ||
* console.log(i); // 0, 1, 2, 3,...9, 0, 1, 2, ..., 14 | ||
* } | ||
* | ||
* @param {...any[]} args | ||
* @param its | ||
*/ | ||
function* chain(...args) { | ||
function* chain(...its) { | ||
let item; | ||
for (let arg of args) { | ||
for (let arg of its) { | ||
for (item of arg) | ||
@@ -147,10 +136,10 @@ yield item; | ||
* | ||
* @param args | ||
* @param its | ||
*/ | ||
function* product(...args) { | ||
function* product(...its) { | ||
const breaker = Symbol(); | ||
const its = args.map(arg => repeat(arg, -1, breaker)); | ||
let c2 = args.length - 1, c1; | ||
const its2 = its.map(arg => repeat(arg, -1, breaker)); | ||
let c2 = its.length - 1, c1; | ||
let it, value; | ||
let item = its.map(it => it.next().value); | ||
let item = its2.map(it => it.next().value); | ||
for (let value of item) | ||
@@ -161,3 +150,3 @@ if (value === breaker) | ||
yield item; | ||
it = its[c2]; | ||
it = its2[c2]; | ||
while ((value = it.next().value) !== breaker) { | ||
@@ -169,3 +158,3 @@ item[c2] = value; | ||
do { | ||
it = its[--c1]; | ||
it = its2[--c1]; | ||
value = it.next().value; | ||
@@ -180,13 +169,13 @@ } while (value === breaker && c1 > 0); | ||
/** | ||
* Forcs any iterable to become an iterator. Will throw | ||
* Forces any iterable or iterator to become an iterator. Will throw | ||
* if this is not possible. | ||
* | ||
* @example | ||
* import { iter } from 'deleight/generators'; | ||
* const it = iter([1, 2, 3, 4, 5]); | ||
* import { forceIterator } from 'deleight/generators'; | ||
* const it = forceIterator([1, 2, 3, 4, 5]); | ||
* | ||
* @param { any } it | ||
* @param it | ||
*/ | ||
function forceIterator(it) { | ||
return (Reflect.get(it, 'next') instanceof Function) ? it : it[Symbol.iterator](); | ||
return it[Symbol.iterator]?.() || it; | ||
} | ||
@@ -226,8 +215,8 @@ exports.forceIterator = forceIterator; | ||
* | ||
* @param {any} arrayLike | ||
* @param {any} it | ||
* @param {Iterable<any>} index | ||
*/ | ||
function* items(arrayLike, index) { | ||
function* items(it, index) { | ||
for (let i of index) | ||
yield arrayLike[i]; | ||
yield it[i]; | ||
} | ||
@@ -245,9 +234,9 @@ exports.items = items; | ||
* | ||
* @param {Iterable<any>} what | ||
* @param {Iterable<any>} it | ||
* @param {number} [times] | ||
*/ | ||
function* repeat(what, times = -1, ...extras) { | ||
function* repeat(it, times = -1, ...extras) { | ||
let item; | ||
if (times !== 1 && !(what instanceof Array) && !(what instanceof Function)) { | ||
what = [...what]; | ||
if (times !== 1 && !(it instanceof Array) && !(it instanceof Function)) { | ||
it = [...it]; | ||
} | ||
@@ -257,3 +246,3 @@ let what2; | ||
while (true) { | ||
what2 = (what instanceof Function) ? what() : what; | ||
what2 = (it instanceof Function) ? it() : it; | ||
for (item of what2) | ||
@@ -265,3 +254,3 @@ yield item; | ||
for (let i = 0; i < times; i++) { | ||
what2 = (what instanceof Function) ? what() : what; | ||
what2 = (it instanceof Function) ? it() : it; | ||
for (item of what2) | ||
@@ -275,22 +264,19 @@ yield item; | ||
/** | ||
* Returns an iterator over the next 'count' items of the iterator. | ||
* Returns a Generator over the next 'count' items of the iterable or iterator. | ||
* In most cases you will call this function with an iterator. | ||
* | ||
* Note that, for performance reasons, this only accepts an | ||
* iterator as the 'it' argument. You can convert any iterable to an | ||
* iterator using the {@link forceIterator} function as shown in the following | ||
* example. | ||
* | ||
* If a firstValue is specified, it will be yielded first. | ||
* | ||
* @example | ||
* import { next, iter } from 'deleight/generators'; | ||
* const it = iter([1, 'a', 2, 'b', 3, 'c', 4, 'd']); | ||
* import { next, forceIterator } from 'deleight/generators'; | ||
* const it = forceIterator([1, 'a', 2, 'b', 3, 'c', 4, 'd']); | ||
* const [num1, let1] = next(it, 2); // (1, 'a') | ||
* const [num2, let2] = next(it, 2); // (2, 'b') | ||
* | ||
* @param {Iterator<any>} it | ||
* @param {number} count | ||
* @param { any } [firstValue] | ||
* @param it | ||
* @param count | ||
* @param firstValue | ||
*/ | ||
function* next(it, count, firstValue) { | ||
it = forceIterator(it); | ||
let count2 = count; | ||
@@ -306,4 +292,5 @@ if (firstValue) { | ||
/** | ||
* Returns an iterator of iterators over the next 'count' items of | ||
* the given iterable | ||
* Returns a generator of generators over the next 'count' items of | ||
* the given iterable. In other words, this function will partition the | ||
* input iterable with each partition containing `count` items. | ||
* | ||
@@ -322,3 +309,3 @@ * @example | ||
while (!nextItem.done) { | ||
yield [...next(it2, count, nextItem.value)]; | ||
yield next(it2, count, nextItem.value); | ||
nextItem = it2.next(); | ||
@@ -329,25 +316,23 @@ } | ||
/** | ||
* Returns an iterator over the items of all the input iterators, starting from | ||
* the zero index to the maximum index of the first argument. The | ||
* effective length of the iterator is the multiple of the length of thr smallest | ||
* iterator and the number of iterators (number of args). | ||
* Returns a generator over the items of all the input args (iterables), starting from | ||
* the zero index to the maximum index of the smallest arg. | ||
* | ||
* Can be used to join arrays in a way not supported by `concat`, `push`, etc. | ||
* To pass an array as an iterator, call array.values(). | ||
* The effective length of the generator is the multiple of the length of the smallest | ||
* arg and the number of args. | ||
* | ||
* @example | ||
* import { zipFlat } from 'deleight/generators'; | ||
* for (let i of zip(range(10, range(15)))) { | ||
* console.log(i); // (0, 0, 1, 1, 2, 2, .... till smallest iterable (10) is exhausted. | ||
* for (let i of zipFlat(range(10), range(15))) { | ||
* console.log(i); // (0, 0, 1, 1, 2, 2, .... till range(10) is exhausted. | ||
* } | ||
* | ||
* @param {...any[]} args | ||
* @param its | ||
*/ | ||
function* zipFlat(...args) { | ||
const count = args.length; | ||
args = args.map(arg => forceIterator(arg)); | ||
function* zipFlat(...its) { | ||
const count = its.length; | ||
const itArgs = its.map(arg => forceIterator(arg)); | ||
let i, nextItem; | ||
while (true) { | ||
for (i = 0; i < count; i++) { | ||
nextItem = args[i].next(); | ||
nextItem = itArgs[i].next(); | ||
if (nextItem.done) | ||
@@ -362,27 +347,28 @@ return; | ||
/** | ||
* Returns an iterator over the items of all the input iterators, starting from | ||
* the zero index to the maximum index of the first argument. The | ||
* effective length of the iterator is the multiple of the length of thr smallest | ||
* iterator and the number of iterators (number of args). | ||
* Returns a generator over the combined items of all the input args (iterables), | ||
* starting from the zero index to the maximum index of the smallest arg. | ||
* | ||
* Can be used to join arrays in a way not supported by `concat`, `push`, etc. | ||
* The effective length of the generator is the length of the smallest input | ||
* iterable. | ||
* | ||
* @example | ||
* import { zip } from 'deleight/generators'; | ||
* for (let i of zip(range(10, range(15)))) { | ||
* console.log(i); // (0, 0), (1, 1), (2, 2), .... till smallest iterable (10) is exhausted. | ||
* for (let i of zip(range(10), range(15))) { | ||
* console.log(i); // (0, 0), (1, 1), (2, 2), .... till range(10) is exhausted. | ||
* } | ||
* | ||
* @param {...any[]} args | ||
* @param its | ||
*/ | ||
function* zip(...args) { | ||
yield* forNext(zipFlat(...args), args.length); | ||
function* zip(...its) { | ||
yield* forNext(zipFlat(...its), its.length); | ||
} | ||
exports.zip = zip; | ||
/** | ||
* Returns an unordered/random iterator over the input array.. | ||
* Returns an unordered/random generator over the input itrable. | ||
* Note that this is forced to materialize the input before running. | ||
* | ||
* | ||
* @example | ||
* import { uItems } from 'deleight/generators'; | ||
* const unOrdered = uItems([1, 2, 3, 4]); // [4, 1, 3, 2] | ||
* import { random } from 'deleight/generators'; | ||
* const unOrdered = random([1, 2, 3, 4]); // probably [4, 1, 3, 2] | ||
* | ||
@@ -389,0 +375,0 @@ * @param it The iterable to get items from |
@@ -5,3 +5,3 @@ /** | ||
* with `Object.assign`. Hover the object created here does not allocate | ||
* new memory to hold all the properties and it 'contains' both the | ||
* new memory to hold all the properties and it 'contains' both | ||
* enumerable and the non-enumerable properties. | ||
@@ -8,0 +8,0 @@ * |
@@ -6,3 +6,3 @@ "use strict"; | ||
* with `Object.assign`. Hover the object created here does not allocate | ||
* new memory to hold all the properties and it 'contains' both the | ||
* new memory to hold all the properties and it 'contains' both | ||
* enumerable and the non-enumerable properties. | ||
@@ -9,0 +9,0 @@ * |
/** | ||
* Objects that select and manipulate elements when their properties or methods are accessed. | ||
* Objects that select and manipulate elements when their properties or | ||
* methods are accessed. | ||
* | ||
@@ -4,0 +5,0 @@ * @module |
"use strict"; | ||
/** | ||
* Objects that select and manipulate elements when their properties or methods are accessed. | ||
* Objects that select and manipulate elements when their properties or | ||
* methods are accessed. | ||
* | ||
@@ -5,0 +6,0 @@ * @module |
@@ -31,42 +31,3 @@ /** | ||
* | ||
* Module Guide. | ||
* | ||
* 1. css (Deleight.css) (deleight/css) | ||
* | ||
* 2. dom (Deleight.dom) (deleight/dom) | ||
* 2.1. apply (Deleight.dom) (deleight/dom/apply) | ||
* 2.2. components (Deleight.dom) (deleight/dom/components) | ||
* 2.3. fragment (Deleight.dom) (deleight/dom/fragment) | ||
* 2.4. process (Deleight.dom) (deleight/dom/process) | ||
* | ||
* 3. function (Deleight.function) (deleight/function) | ||
* 3.1. Context (Deleight.function) (deleight/function/context) | ||
* 3.2. Return (Deleight.function) (deleight/function/return) | ||
* | ||
* 4. generators (Deleight.Generator) (deleight/generators) | ||
* | ||
* 5. lists (Deleight.List) (deleight/lists) | ||
* 5.1. Array (Deleight.List) (deleight/lists/array) | ||
* 5.2. Element (Deleight.List) (deleight/lists/element) | ||
* | ||
* 6. object (Deleight.object) (deleight/object) | ||
* 6.1. apply (Deleight.object) (deleight/object/apply) | ||
* 6.2. deepMember (Deleight.object) (deleight/object/deepmember) | ||
* 6.3. member (Deleight.object) (deleight/object/member) | ||
* 6.4. operations (Deleight.object) (deleight/object/operations) | ||
* 6.5. process (Deleight.object) (deleight/object/process) | ||
* 6.6. sharedMember (Deleight.object) (deleight/object/sharedmember) | ||
* | ||
* 7. Process (Deleight.process) (deleight/process) | ||
* | ||
* 8. proxies (Deleight.Proxy) (deleight/proxy) | ||
* 8.1. Alias (Deleight.Proxy) (deleight/proxy/alias) | ||
* 8.2. Scope (Deleight.Proxy) (deleight/proxy/scope) | ||
* 8.3. Selector (Deleight.Proxy) (deleight/proxy/selector) | ||
* 8.4. Wrapper (Deleight.Proxy) (deleight/proxy/wrapper) | ||
* | ||
* 9. template (Deleight.template) (deleight/template) | ||
* | ||
* | ||
* | ||
* @module | ||
@@ -73,0 +34,0 @@ * |
@@ -31,42 +31,3 @@ /** | ||
* | ||
* Module Guide. | ||
* | ||
* 1. css (Deleight.css) (deleight/css) | ||
* | ||
* 2. dom (Deleight.dom) (deleight/dom) | ||
* 2.1. apply (Deleight.dom) (deleight/dom/apply) | ||
* 2.2. components (Deleight.dom) (deleight/dom/components) | ||
* 2.3. fragment (Deleight.dom) (deleight/dom/fragment) | ||
* 2.4. process (Deleight.dom) (deleight/dom/process) | ||
* | ||
* 3. function (Deleight.function) (deleight/function) | ||
* 3.1. Context (Deleight.function) (deleight/function/context) | ||
* 3.2. Return (Deleight.function) (deleight/function/return) | ||
* | ||
* 4. generators (Deleight.Generator) (deleight/generators) | ||
* | ||
* 5. lists (Deleight.List) (deleight/lists) | ||
* 5.1. Array (Deleight.List) (deleight/lists/array) | ||
* 5.2. Element (Deleight.List) (deleight/lists/element) | ||
* | ||
* 6. object (Deleight.object) (deleight/object) | ||
* 6.1. apply (Deleight.object) (deleight/object/apply) | ||
* 6.2. deepMember (Deleight.object) (deleight/object/deepmember) | ||
* 6.3. member (Deleight.object) (deleight/object/member) | ||
* 6.4. operations (Deleight.object) (deleight/object/operations) | ||
* 6.5. process (Deleight.object) (deleight/object/process) | ||
* 6.6. sharedMember (Deleight.object) (deleight/object/sharedmember) | ||
* | ||
* 7. Process (Deleight.process) (deleight/process) | ||
* | ||
* 8. proxies (Deleight.Proxy) (deleight/proxy) | ||
* 8.1. Alias (Deleight.Proxy) (deleight/proxy/alias) | ||
* 8.2. Scope (Deleight.Proxy) (deleight/proxy/scope) | ||
* 8.3. Selector (Deleight.Proxy) (deleight/proxy/selector) | ||
* 8.4. Wrapper (Deleight.Proxy) (deleight/proxy/wrapper) | ||
* | ||
* 9. template (Deleight.template) (deleight/template) | ||
* | ||
* | ||
* | ||
* @module | ||
@@ -73,0 +34,0 @@ * |
/** | ||
* This module exports many useful generators for JS applications. | ||
* There are generators for most array methods like `map` and `filter`, | ||
* along many others drawn from other languages that make heavy use | ||
* along many those drawn from other languages that make heavy use | ||
* of generators, like Python. | ||
@@ -13,44 +13,30 @@ * | ||
* | ||
* API Table of Contents | ||
* | ||
* {@link range} | ||
* {@link forceIterator} | ||
* {@link forEach} | ||
* {@link forAsyncEach} | ||
* {@link forNext} | ||
* {@link next} | ||
* {@link items} | ||
* {@link random} | ||
* {@link repeat} | ||
* {@link map} | ||
* {@link filter} | ||
* {@link reduce} | ||
* {@link chain} | ||
* {@link zipFlat} | ||
* {@link zip} | ||
* (product: Still buggy. Not yet exported) | ||
* | ||
* | ||
* @module | ||
*/ | ||
/** | ||
* Perform the given action for all items of the iterable. | ||
* Performs the given action for all items of the iterable. | ||
* | ||
* @example | ||
* import { forEach } from 'deleight/generators'; | ||
* forEach(range(10), ()=> console.log(i)); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param action | ||
*/ | ||
export declare function forEach<T>(iter: Iterable<any>, action: (item: any, i?: number) => T): void; | ||
export declare function forEach<T>(it: Iterable<any>, action: (item: any, i?: number) => T): void; | ||
/** | ||
* Perform the given action for all items of the async iterable. | ||
* Performs the given action for all items of the async iterable. | ||
* | ||
* @example | ||
* import { forEachAsync } from 'deleight/generators'; | ||
* async function* asyncGen() { | ||
* for (let i of (range(10))) yield i; | ||
* } | ||
* forEachAsync(asyncGen(), ()=> console.log(i)); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param action | ||
*/ | ||
export declare function forAsyncEach<T>(iter: AsyncIterable<any>, action: (item: any, i?: number) => T): Promise<void>; | ||
export declare function forEachAsync<T>(it: AsyncIterable<any>, action: (item: any, i?: number) => T): Promise<void>; | ||
/** | ||
@@ -60,8 +46,9 @@ * Maps the values of the iterable to other values using the given mapper. | ||
* @example | ||
* import { map } from 'deleight/generators'; | ||
* map(range(10), i=> 2 * i); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param mapper | ||
*/ | ||
export declare function map<T>(iter: Iterable<any>, mapper: (item: any, i?: number) => T): Iterable<T>; | ||
export declare function map<T>(it: Iterable<any>, mapper: (item: any, i?: number) => T): Iterable<T>; | ||
/** | ||
@@ -71,41 +58,43 @@ * Filters the values of the iterable using the given test function. | ||
* @example | ||
* import { filter } from 'deleight/generators'; | ||
* filter(range(10), i=> 2 < i); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param test | ||
*/ | ||
export declare function filter<T>(iter: Iterable<any>, test: (item: any, i?: number) => boolean): Iterable<T>; | ||
export declare function filter<T>(it: Iterable<any>, test: (item: any, i?: number) => boolean): Iterable<T>; | ||
/** | ||
* Reduces the the iterable using the given reducer function. | ||
* Reduces the iterable using the given reducer function. | ||
* | ||
* @example | ||
* import { reduce } from 'deleight/generators'; | ||
* reduce(range(10), ((r, i) => r * i), 3); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param reducer | ||
*/ | ||
export declare function reduce<T>(iter: Iterable<any>, reducer: (value: any, item: any, i?: number) => boolean, value?: any): T; | ||
export declare function reduce<T>(it: Iterable<any>, reducer: (value: any, item: any, i?: number) => boolean, value?: any): T; | ||
/** | ||
* Chain multiple iterables... | ||
* Chains multiple iterables... | ||
* | ||
* @example | ||
* import { chain } from 'deleight/generators'; | ||
* for (let i of chain(range(10, range(15)))) { | ||
* for (let i of chain(range(10), range(15))) { | ||
* console.log(i); // 0, 1, 2, 3,...9, 0, 1, 2, ..., 14 | ||
* } | ||
* | ||
* @param {...any[]} args | ||
* @param its | ||
*/ | ||
export declare function chain<T>(...args: Iterable<any>[]): Iterable<T>; | ||
export declare function chain<T>(...its: Iterable<any>[]): Iterable<T>; | ||
/** | ||
* Forcs any iterable to become an iterator. Will throw | ||
* Forces any iterable or iterator to become an iterator. Will throw | ||
* if this is not possible. | ||
* | ||
* @example | ||
* import { iter } from 'deleight/generators'; | ||
* const it = iter([1, 2, 3, 4, 5]); | ||
* import { forceIterator } from 'deleight/generators'; | ||
* const it = forceIterator([1, 2, 3, 4, 5]); | ||
* | ||
* @param { any } it | ||
* @param it | ||
*/ | ||
export declare function forceIterator(it: any): Iterator<any>; | ||
export declare function forceIterator(it: Iterable<any> | Iterator<any>): Iterator<any>; | ||
/** | ||
@@ -133,6 +122,6 @@ * Fast and 'costless' range function for javascript based on generators. | ||
* | ||
* @param {any} arrayLike | ||
* @param {any} it | ||
* @param {Iterable<any>} index | ||
*/ | ||
export declare function items(arrayLike: any, index: Iterable<number>): Generator<any, void, unknown>; | ||
export declare function items(it: any, index: Iterable<number>): Generator<any, void, unknown>; | ||
/** | ||
@@ -148,30 +137,27 @@ * Returns a generator that yields first argument (`what`) the number of | ||
* | ||
* @param {Iterable<any>} what | ||
* @param {Iterable<any>} it | ||
* @param {number} [times] | ||
*/ | ||
export declare function repeat<T>(what: Iterable<T> | (() => Iterable<T>), times?: number, ...extras: any[]): Generator<any, void, unknown>; | ||
export declare function repeat<T>(it: Iterable<T> | (() => Iterable<T>), times?: number, ...extras: any[]): Generator<any, void, unknown>; | ||
/** | ||
* Returns an iterator over the next 'count' items of the iterator. | ||
* Returns a Generator over the next 'count' items of the iterable or iterator. | ||
* In most cases you will call this function with an iterator. | ||
* | ||
* Note that, for performance reasons, this only accepts an | ||
* iterator as the 'it' argument. You can convert any iterable to an | ||
* iterator using the {@link forceIterator} function as shown in the following | ||
* example. | ||
* | ||
* If a firstValue is specified, it will be yielded first. | ||
* | ||
* @example | ||
* import { next, iter } from 'deleight/generators'; | ||
* const it = iter([1, 'a', 2, 'b', 3, 'c', 4, 'd']); | ||
* import { next, forceIterator } from 'deleight/generators'; | ||
* const it = forceIterator([1, 'a', 2, 'b', 3, 'c', 4, 'd']); | ||
* const [num1, let1] = next(it, 2); // (1, 'a') | ||
* const [num2, let2] = next(it, 2); // (2, 'b') | ||
* | ||
* @param {Iterator<any>} it | ||
* @param {number} count | ||
* @param { any } [firstValue] | ||
* @param it | ||
* @param count | ||
* @param firstValue | ||
*/ | ||
export declare function next(it: Iterator<any>, count: number, firstValue?: any): Generator<any, void, unknown>; | ||
export declare function next(it: Iterable<any> | Iterator<any>, count: number, firstValue?: any): Generator<any, void, unknown>; | ||
/** | ||
* Returns an iterator of iterators over the next 'count' items of | ||
* the given iterable | ||
* Returns a generator of generators over the next 'count' items of | ||
* the given iterable. In other words, this function will partition the | ||
* input iterable with each partition containing `count` items. | ||
* | ||
@@ -186,44 +172,43 @@ * @example | ||
*/ | ||
export declare function forNext(it: Iterable<any>, count: number): Generator<any[], void, unknown>; | ||
export declare function forNext(it: Iterable<any>, count: number): Generator<Generator<any, void, unknown>, void, unknown>; | ||
/** | ||
* Returns an iterator over the items of all the input iterators, starting from | ||
* the zero index to the maximum index of the first argument. The | ||
* effective length of the iterator is the multiple of the length of thr smallest | ||
* iterator and the number of iterators (number of args). | ||
* Returns a generator over the items of all the input args (iterables), starting from | ||
* the zero index to the maximum index of the smallest arg. | ||
* | ||
* Can be used to join arrays in a way not supported by `concat`, `push`, etc. | ||
* To pass an array as an iterator, call array.values(). | ||
* The effective length of the generator is the multiple of the length of the smallest | ||
* arg and the number of args. | ||
* | ||
* @example | ||
* import { zipFlat } from 'deleight/generators'; | ||
* for (let i of zip(range(10, range(15)))) { | ||
* console.log(i); // (0, 0, 1, 1, 2, 2, .... till smallest iterable (10) is exhausted. | ||
* for (let i of zipFlat(range(10), range(15))) { | ||
* console.log(i); // (0, 0, 1, 1, 2, 2, .... till range(10) is exhausted. | ||
* } | ||
* | ||
* @param {...any[]} args | ||
* @param its | ||
*/ | ||
export declare function zipFlat(...args: any[]): Generator<any, void, unknown>; | ||
export declare function zipFlat(...its: Iterable<any>[]): Generator<any, void, unknown>; | ||
/** | ||
* Returns an iterator over the items of all the input iterators, starting from | ||
* the zero index to the maximum index of the first argument. The | ||
* effective length of the iterator is the multiple of the length of thr smallest | ||
* iterator and the number of iterators (number of args). | ||
* Returns a generator over the combined items of all the input args (iterables), | ||
* starting from the zero index to the maximum index of the smallest arg. | ||
* | ||
* Can be used to join arrays in a way not supported by `concat`, `push`, etc. | ||
* The effective length of the generator is the length of the smallest input | ||
* iterable. | ||
* | ||
* @example | ||
* import { zip } from 'deleight/generators'; | ||
* for (let i of zip(range(10, range(15)))) { | ||
* console.log(i); // (0, 0), (1, 1), (2, 2), .... till smallest iterable (10) is exhausted. | ||
* for (let i of zip(range(10), range(15))) { | ||
* console.log(i); // (0, 0), (1, 1), (2, 2), .... till range(10) is exhausted. | ||
* } | ||
* | ||
* @param {...any[]} args | ||
* @param its | ||
*/ | ||
export declare function zip(...args: any[]): Generator<any[], void, unknown>; | ||
export declare function zip(...its: Iterable<any>[]): Generator<Generator<any, void, unknown>, void, unknown>; | ||
/** | ||
* Returns an unordered/random iterator over the input array.. | ||
* Returns an unordered/random generator over the input itrable. | ||
* Note that this is forced to materialize the input before running. | ||
* | ||
* | ||
* @example | ||
* import { uItems } from 'deleight/generators'; | ||
* const unOrdered = uItems([1, 2, 3, 4]); // [4, 1, 3, 2] | ||
* import { random } from 'deleight/generators'; | ||
* const unOrdered = random([1, 2, 3, 4]); // probably [4, 1, 3, 2] | ||
* | ||
@@ -230,0 +215,0 @@ * @param it The iterable to get items from |
/** | ||
* This module exports many useful generators for JS applications. | ||
* There are generators for most array methods like `map` and `filter`, | ||
* along many others drawn from other languages that make heavy use | ||
* along many those drawn from other languages that make heavy use | ||
* of generators, like Python. | ||
@@ -13,50 +13,36 @@ * | ||
* | ||
* API Table of Contents | ||
* | ||
* {@link range} | ||
* {@link forceIterator} | ||
* {@link forEach} | ||
* {@link forAsyncEach} | ||
* {@link forNext} | ||
* {@link next} | ||
* {@link items} | ||
* {@link random} | ||
* {@link repeat} | ||
* {@link map} | ||
* {@link filter} | ||
* {@link reduce} | ||
* {@link chain} | ||
* {@link zipFlat} | ||
* {@link zip} | ||
* (product: Still buggy. Not yet exported) | ||
* | ||
* | ||
* @module | ||
*/ | ||
/** | ||
* Perform the given action for all items of the iterable. | ||
* Performs the given action for all items of the iterable. | ||
* | ||
* @example | ||
* import { forEach } from 'deleight/generators'; | ||
* forEach(range(10), ()=> console.log(i)); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param action | ||
*/ | ||
export function forEach(iter, action) { | ||
export function forEach(it, action) { | ||
let i = 0; | ||
for (let item of iter) | ||
for (let item of it) | ||
action(item, i++); | ||
} | ||
/** | ||
* Perform the given action for all items of the async iterable. | ||
* Performs the given action for all items of the async iterable. | ||
* | ||
* @example | ||
* import { forEachAsync } from 'deleight/generators'; | ||
* async function* asyncGen() { | ||
* for (let i of (range(10))) yield i; | ||
* } | ||
* forEachAsync(asyncGen(), ()=> console.log(i)); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param action | ||
*/ | ||
export async function forAsyncEach(iter, action) { | ||
export async function forEachAsync(it, action) { | ||
let i = 0; | ||
for await (let item of iter) | ||
for await (let item of it) | ||
action(item, i++); | ||
@@ -68,10 +54,11 @@ } | ||
* @example | ||
* import { map } from 'deleight/generators'; | ||
* map(range(10), i=> 2 * i); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param mapper | ||
*/ | ||
export function* map(iter, mapper) { | ||
export function* map(it, mapper) { | ||
let i = 0; | ||
for (let item of iter) | ||
for (let item of it) | ||
yield mapper(item, i++); | ||
@@ -83,10 +70,11 @@ } | ||
* @example | ||
* import { filter } from 'deleight/generators'; | ||
* filter(range(10), i=> 2 < i); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param test | ||
*/ | ||
export function* filter(iter, test) { | ||
export function* filter(it, test) { | ||
let i = 0; | ||
for (let item of iter) | ||
for (let item of it) | ||
if (test(item, i++)) | ||
@@ -96,13 +84,14 @@ yield item; | ||
/** | ||
* Reduces the the iterable using the given reducer function. | ||
* Reduces the iterable using the given reducer function. | ||
* | ||
* @example | ||
* import { reduce } from 'deleight/generators'; | ||
* reduce(range(10), ((r, i) => r * i), 3); | ||
* | ||
* | ||
* @param iter | ||
* @param it | ||
* @param reducer | ||
*/ | ||
export function reduce(iter, reducer, value) { | ||
export function reduce(it, reducer, value) { | ||
let i = 0; | ||
for (let item of iter) | ||
for (let item of it) | ||
value = reducer(value, item, i++); | ||
@@ -112,15 +101,15 @@ return value; | ||
/** | ||
* Chain multiple iterables... | ||
* Chains multiple iterables... | ||
* | ||
* @example | ||
* import { chain } from 'deleight/generators'; | ||
* for (let i of chain(range(10, range(15)))) { | ||
* for (let i of chain(range(10), range(15))) { | ||
* console.log(i); // 0, 1, 2, 3,...9, 0, 1, 2, ..., 14 | ||
* } | ||
* | ||
* @param {...any[]} args | ||
* @param its | ||
*/ | ||
export function* chain(...args) { | ||
export function* chain(...its) { | ||
let item; | ||
for (let arg of args) { | ||
for (let arg of its) { | ||
for (item of arg) | ||
@@ -136,10 +125,10 @@ yield item; | ||
* | ||
* @param args | ||
* @param its | ||
*/ | ||
function* product(...args) { | ||
function* product(...its) { | ||
const breaker = Symbol(); | ||
const its = args.map(arg => repeat(arg, -1, breaker)); | ||
let c2 = args.length - 1, c1; | ||
const its2 = its.map(arg => repeat(arg, -1, breaker)); | ||
let c2 = its.length - 1, c1; | ||
let it, value; | ||
let item = its.map(it => it.next().value); | ||
let item = its2.map(it => it.next().value); | ||
for (let value of item) | ||
@@ -150,3 +139,3 @@ if (value === breaker) | ||
yield item; | ||
it = its[c2]; | ||
it = its2[c2]; | ||
while ((value = it.next().value) !== breaker) { | ||
@@ -158,3 +147,3 @@ item[c2] = value; | ||
do { | ||
it = its[--c1]; | ||
it = its2[--c1]; | ||
value = it.next().value; | ||
@@ -169,13 +158,13 @@ } while (value === breaker && c1 > 0); | ||
/** | ||
* Forcs any iterable to become an iterator. Will throw | ||
* Forces any iterable or iterator to become an iterator. Will throw | ||
* if this is not possible. | ||
* | ||
* @example | ||
* import { iter } from 'deleight/generators'; | ||
* const it = iter([1, 2, 3, 4, 5]); | ||
* import { forceIterator } from 'deleight/generators'; | ||
* const it = forceIterator([1, 2, 3, 4, 5]); | ||
* | ||
* @param { any } it | ||
* @param it | ||
*/ | ||
export function forceIterator(it) { | ||
return (Reflect.get(it, 'next') instanceof Function) ? it : it[Symbol.iterator](); | ||
return it[Symbol.iterator]?.() || it; | ||
} | ||
@@ -213,8 +202,8 @@ /** | ||
* | ||
* @param {any} arrayLike | ||
* @param {any} it | ||
* @param {Iterable<any>} index | ||
*/ | ||
export function* items(arrayLike, index) { | ||
export function* items(it, index) { | ||
for (let i of index) | ||
yield arrayLike[i]; | ||
yield it[i]; | ||
} | ||
@@ -231,9 +220,9 @@ /** | ||
* | ||
* @param {Iterable<any>} what | ||
* @param {Iterable<any>} it | ||
* @param {number} [times] | ||
*/ | ||
export function* repeat(what, times = -1, ...extras) { | ||
export function* repeat(it, times = -1, ...extras) { | ||
let item; | ||
if (times !== 1 && !(what instanceof Array) && !(what instanceof Function)) { | ||
what = [...what]; | ||
if (times !== 1 && !(it instanceof Array) && !(it instanceof Function)) { | ||
it = [...it]; | ||
} | ||
@@ -243,3 +232,3 @@ let what2; | ||
while (true) { | ||
what2 = (what instanceof Function) ? what() : what; | ||
what2 = (it instanceof Function) ? it() : it; | ||
for (item of what2) | ||
@@ -251,3 +240,3 @@ yield item; | ||
for (let i = 0; i < times; i++) { | ||
what2 = (what instanceof Function) ? what() : what; | ||
what2 = (it instanceof Function) ? it() : it; | ||
for (item of what2) | ||
@@ -260,22 +249,19 @@ yield item; | ||
/** | ||
* Returns an iterator over the next 'count' items of the iterator. | ||
* Returns a Generator over the next 'count' items of the iterable or iterator. | ||
* In most cases you will call this function with an iterator. | ||
* | ||
* Note that, for performance reasons, this only accepts an | ||
* iterator as the 'it' argument. You can convert any iterable to an | ||
* iterator using the {@link forceIterator} function as shown in the following | ||
* example. | ||
* | ||
* If a firstValue is specified, it will be yielded first. | ||
* | ||
* @example | ||
* import { next, iter } from 'deleight/generators'; | ||
* const it = iter([1, 'a', 2, 'b', 3, 'c', 4, 'd']); | ||
* import { next, forceIterator } from 'deleight/generators'; | ||
* const it = forceIterator([1, 'a', 2, 'b', 3, 'c', 4, 'd']); | ||
* const [num1, let1] = next(it, 2); // (1, 'a') | ||
* const [num2, let2] = next(it, 2); // (2, 'b') | ||
* | ||
* @param {Iterator<any>} it | ||
* @param {number} count | ||
* @param { any } [firstValue] | ||
* @param it | ||
* @param count | ||
* @param firstValue | ||
*/ | ||
export function* next(it, count, firstValue) { | ||
it = forceIterator(it); | ||
let count2 = count; | ||
@@ -290,4 +276,5 @@ if (firstValue) { | ||
/** | ||
* Returns an iterator of iterators over the next 'count' items of | ||
* the given iterable | ||
* Returns a generator of generators over the next 'count' items of | ||
* the given iterable. In other words, this function will partition the | ||
* input iterable with each partition containing `count` items. | ||
* | ||
@@ -306,3 +293,3 @@ * @example | ||
while (!nextItem.done) { | ||
yield [...next(it2, count, nextItem.value)]; | ||
yield next(it2, count, nextItem.value); | ||
nextItem = it2.next(); | ||
@@ -312,25 +299,23 @@ } | ||
/** | ||
* Returns an iterator over the items of all the input iterators, starting from | ||
* the zero index to the maximum index of the first argument. The | ||
* effective length of the iterator is the multiple of the length of thr smallest | ||
* iterator and the number of iterators (number of args). | ||
* Returns a generator over the items of all the input args (iterables), starting from | ||
* the zero index to the maximum index of the smallest arg. | ||
* | ||
* Can be used to join arrays in a way not supported by `concat`, `push`, etc. | ||
* To pass an array as an iterator, call array.values(). | ||
* The effective length of the generator is the multiple of the length of the smallest | ||
* arg and the number of args. | ||
* | ||
* @example | ||
* import { zipFlat } from 'deleight/generators'; | ||
* for (let i of zip(range(10, range(15)))) { | ||
* console.log(i); // (0, 0, 1, 1, 2, 2, .... till smallest iterable (10) is exhausted. | ||
* for (let i of zipFlat(range(10), range(15))) { | ||
* console.log(i); // (0, 0, 1, 1, 2, 2, .... till range(10) is exhausted. | ||
* } | ||
* | ||
* @param {...any[]} args | ||
* @param its | ||
*/ | ||
export function* zipFlat(...args) { | ||
const count = args.length; | ||
args = args.map(arg => forceIterator(arg)); | ||
export function* zipFlat(...its) { | ||
const count = its.length; | ||
const itArgs = its.map(arg => forceIterator(arg)); | ||
let i, nextItem; | ||
while (true) { | ||
for (i = 0; i < count; i++) { | ||
nextItem = args[i].next(); | ||
nextItem = itArgs[i].next(); | ||
if (nextItem.done) | ||
@@ -344,26 +329,27 @@ return; | ||
/** | ||
* Returns an iterator over the items of all the input iterators, starting from | ||
* the zero index to the maximum index of the first argument. The | ||
* effective length of the iterator is the multiple of the length of thr smallest | ||
* iterator and the number of iterators (number of args). | ||
* Returns a generator over the combined items of all the input args (iterables), | ||
* starting from the zero index to the maximum index of the smallest arg. | ||
* | ||
* Can be used to join arrays in a way not supported by `concat`, `push`, etc. | ||
* The effective length of the generator is the length of the smallest input | ||
* iterable. | ||
* | ||
* @example | ||
* import { zip } from 'deleight/generators'; | ||
* for (let i of zip(range(10, range(15)))) { | ||
* console.log(i); // (0, 0), (1, 1), (2, 2), .... till smallest iterable (10) is exhausted. | ||
* for (let i of zip(range(10), range(15))) { | ||
* console.log(i); // (0, 0), (1, 1), (2, 2), .... till range(10) is exhausted. | ||
* } | ||
* | ||
* @param {...any[]} args | ||
* @param its | ||
*/ | ||
export function* zip(...args) { | ||
yield* forNext(zipFlat(...args), args.length); | ||
export function* zip(...its) { | ||
yield* forNext(zipFlat(...its), its.length); | ||
} | ||
/** | ||
* Returns an unordered/random iterator over the input array.. | ||
* Returns an unordered/random generator over the input itrable. | ||
* Note that this is forced to materialize the input before running. | ||
* | ||
* | ||
* @example | ||
* import { uItems } from 'deleight/generators'; | ||
* const unOrdered = uItems([1, 2, 3, 4]); // [4, 1, 3, 2] | ||
* import { random } from 'deleight/generators'; | ||
* const unOrdered = random([1, 2, 3, 4]); // probably [4, 1, 3, 2] | ||
* | ||
@@ -370,0 +356,0 @@ * @param it The iterable to get items from |
@@ -5,3 +5,3 @@ /** | ||
* with `Object.assign`. Hover the object created here does not allocate | ||
* new memory to hold all the properties and it 'contains' both the | ||
* new memory to hold all the properties and it 'contains' both | ||
* enumerable and the non-enumerable properties. | ||
@@ -8,0 +8,0 @@ * |
@@ -5,3 +5,3 @@ /** | ||
* with `Object.assign`. Hover the object created here does not allocate | ||
* new memory to hold all the properties and it 'contains' both the | ||
* new memory to hold all the properties and it 'contains' both | ||
* enumerable and the non-enumerable properties. | ||
@@ -8,0 +8,0 @@ * |
/** | ||
* Objects that select and manipulate elements when their properties or methods are accessed. | ||
* Objects that select and manipulate elements when their properties or | ||
* methods are accessed. | ||
* | ||
@@ -4,0 +5,0 @@ * @module |
/** | ||
* Objects that select and manipulate elements when their properties or methods are accessed. | ||
* Objects that select and manipulate elements when their properties or | ||
* methods are accessed. | ||
* | ||
@@ -4,0 +5,0 @@ * @module |
{ | ||
"name": "deleight", | ||
"version": "5.0.4", | ||
"version": "5.1.0", | ||
"description": "A library with 9 modules for writing more expressive web applications with traditional HTML, CSS and JavaScript.", | ||
@@ -127,3 +127,3 @@ "type": "module", | ||
"compile-cjs": "npx tsc tsconfig.cjs.json", | ||
"build": "npx tsc && npx tsc --build tsconfig.cjs.json" | ||
"build": "npx tsc && npx tsc --build tsconfig.cjs.json && npx tsc --build tsconfig.test.json" | ||
}, | ||
@@ -130,0 +130,0 @@ "repository": { |
@@ -42,2 +42,3 @@ # Deleight | ||
**ESM** | ||
```js | ||
@@ -52,3 +53,4 @@ import { dom, Generator } from 'deleight'; | ||
```cjs | ||
**CommonJS** | ||
```js | ||
const { dom, Generator } = require('deleight'); | ||
@@ -65,3 +67,3 @@ const apply = dom.apply; | ||
You will find many examples (still adding) that show the code in action. Beyond this, you only need a bit of context about what a particular module entails so that you know when you need it and what to expect when you reach for it. You can learn a single function or class and benefit from that. There is no need to learn Deleight from the top down. Still an [API documentation](https://mksunny1.github.io/deleight-api-docs/main/modules/deleight.html) is available if you prefer. | ||
You will find many examples (still adding) that show the code in action. Beyond this, you only need a bit of context about what a particular module entails so that you know when you need it and what to expect when you reach for it. You can learn a single function or class and benefit from that. There is no need to learn Deleight from the top down. The API documentation is available [here](https://mksunny1.github.io/deleight-api-docs/main/modules/deleight.html). | ||
@@ -68,0 +70,0 @@ |
140
0.72%154
1.32%408245
-1.56%12014
-1.64%