Comparing version 0.1.7 to 0.1.8
@@ -49,3 +49,3 @@ const AsIt = require('./base'); | ||
if (!opts.to) opts.to = Object.create(null); | ||
for await (const item of AsIt.recentGroup.gen(iter, opts.to, limit, opts)); | ||
for await (const item of AsIt.recentGroup.gen.call(this, iter, opts.to, limit, opts)); | ||
return opts.to; | ||
@@ -92,3 +92,3 @@ }); | ||
if (!opts.to) opts.to = Object.create(null); | ||
for await (const item of AsIt.orderGroup.gen(iter, opts.to, limit, opts)); | ||
for await (const item of AsIt.orderGroup.gen.call(this, iter, opts.to, limit, opts)); | ||
return opts.to; | ||
@@ -95,0 +95,0 @@ }); |
const it = require('../it'); | ||
const Design = require('../design'); | ||
const wrapped = Symbol('$.AsIt.wrapped'); | ||
const wrapped = Symbol('_.AsIt.wrapped'); | ||
@@ -167,3 +167,4 @@ const AsIt = function(iter) { | ||
const partial = asItPartial(iter); | ||
return new AsIt(partial); | ||
const Class = typeof this === 'function' ? this : this.constructor; | ||
return new Class(partial); | ||
}); | ||
@@ -176,3 +177,3 @@ | ||
AsIt.chain_(async function* save(iter, id) { | ||
AsIt.chain_(async function* save(iter, id = 'saved') { | ||
for await (const item of iter) { | ||
@@ -184,3 +185,3 @@ this[id] = item; | ||
AsIt.chain_(async function* load(iter, id) { | ||
AsIt.chain_(async function* load(iter, id = 'saved') { | ||
for await (const item of iter) { | ||
@@ -187,0 +188,0 @@ yield this[id]; |
@@ -224,3 +224,3 @@ const $ = require('../func'); | ||
const iter = new AsIt(async function*() {for (let i = 0; i < 4; i++) {await $.delayMsec(50); yield i;}} ()); | ||
const part = iter.partial(); | ||
const part = AsIt.partial(iter); | ||
let r1 = [], r2 = []; | ||
@@ -227,0 +227,0 @@ await Promise.all([ |
const AsIt = require('./base'); | ||
const $ = require('../func'); | ||
const _ = require('../func'); | ||
async function* filterGen(iter, double, ...funcs) { | ||
const l = $._predicateFuncs(funcs); | ||
const l = _._predicateFuncs(funcs); | ||
@@ -15,44 +15,34 @@ if (!l) { | ||
let value, done; | ||
const resFunc = l === 1 ? funcs[0] : _.cascadeFuncAsync.bind(this, funcs); | ||
const desc = {iter, ctx: this}; | ||
let pass = false; | ||
const pass1 = double ? false : null; | ||
if (l === 1) { | ||
const func = funcs[0]; | ||
for await (const item of iter) { | ||
if (pass) { yield item; continue; } | ||
let v = await func.call(this, item, item, desc, pass1); | ||
if (v === $.stop) break; | ||
if (v === $.pass) { pass = true; yield item; continue; } | ||
if (v) yield item; | ||
if (!double) continue; | ||
v = await func.call(this, item, item, desc, true); | ||
if (v === $.stop) break; | ||
if (v === $.pass) { pass = true; continue; } | ||
} | ||
} else { | ||
for await (const item of iter) { | ||
if (pass) { yield item; continue; } | ||
let v = item; | ||
for (const func of funcs) { | ||
v = await func.call(this, v, item, desc, pass1); | ||
try { | ||
if (double < 0) { | ||
while ({value, done} = await iter.next(), !done) { | ||
yield value; | ||
const result2 = await resFunc.call(this, value, value, desc, -1); | ||
if (result2 === _.stop) return; | ||
if (result2 === _.pass) { done = true; yield* iter; return; } | ||
} | ||
if (v === $.stop) return; | ||
if (v === $.pass) { pass = true; yield item; continue; } | ||
if (v) yield item; | ||
if (!double) continue; | ||
v = item; | ||
for (const func of funcs) { | ||
v = await func.call(this, v, item, desc, true); | ||
} else if (double) { | ||
while ({value, done} = await iter.next(), !done) { | ||
const result = await resFunc.call(this, value, value, desc, false); | ||
if (result === _.stop) return; | ||
if (result) yield value; | ||
if (result === _.pass) { done = true; yield* iter; return; } | ||
const result2 = await resFunc.call(this, value, value, desc, true); | ||
if (result2 === _.stop) return; | ||
if (result2 === _.pass) { done = true; yield* iter; return; } | ||
} | ||
if (v === $.stop) return; | ||
if (v === $.pass) { pass = true; continue; } | ||
} else { | ||
while ({value, done} = await iter.next(), !done) { | ||
const result = await resFunc.call(this, value, value, desc, null); | ||
if (result === _.stop) return; | ||
if (result) yield value; | ||
if (result === _.pass) { done = true; yield* iter; return; } | ||
} | ||
} | ||
} finally { | ||
if (!done && iter.return) await iter.return(); | ||
} | ||
@@ -69,8 +59,12 @@ } | ||
AsIt.chain_(async function* pfilter(iter, ...funcs) { | ||
yield* filterGen.call(this, iter, -1, ...funcs); | ||
}); | ||
AsIt.chain_(async function* call(iter, ...funcs) { | ||
yield* filterGen.call(this, iter, false, ...funcs, $.true); | ||
yield* filterGen.call(this, iter, false, ...funcs, _.true); | ||
}); | ||
AsIt.chain_(async function* debug(iter, ...funcs) { | ||
$._predicateFuncs(funcs); | ||
_._predicateFuncs(funcs); | ||
@@ -81,3 +75,3 @@ funcs = funcs.map((func) => function (item) { | ||
yield* filterGen.call(this, iter, false, ...funcs, $.true); | ||
yield* filterGen.call(this, iter, false, ...funcs, _.true); | ||
}); | ||
@@ -89,14 +83,49 @@ | ||
async function* skipN(iter, n) { | ||
let done; | ||
if (n <= 0) { yield* iter; return; } | ||
try { | ||
while ({done} = await iter.next(), !done) { | ||
if (--n <= 0) { yield* iter; return; } | ||
} | ||
} finally { | ||
if (!done && iter.return) await iter.return(); | ||
} | ||
} | ||
AsIt.chain_(async function* skip(iter, ...funcs) { | ||
yield* filterGen.call(this, iter, false, ...funcs, $.cond_(false, $.pass)); | ||
if (funcs.length === 1 && typeof funcs[0] === 'number') { yield* skipN(iter, funcs[0]); return; } | ||
yield* filterGen.call(this, iter, false, ...funcs, _.condSkip); | ||
}); | ||
async function* takeN(iter, n) { | ||
let value, done; | ||
try { | ||
if (n <= 0) return; | ||
while ({value, done} = await iter.next(), !done) { | ||
yield value; | ||
if (--n <= 0) return; | ||
} | ||
} finally { | ||
if (!done && iter.return) await iter.return(); | ||
} | ||
} | ||
AsIt.chain_(async function* take(iter, ...funcs) { | ||
yield* filterGen.call(this, iter, false, ...funcs, $.cond_(true, $.stop)); | ||
if (funcs.length === 1 && typeof funcs[0] === 'number') { yield* takeN(iter, funcs[0]); return; } | ||
yield* filterGen.call(this, iter, false, ...funcs, _.condTake); | ||
}); | ||
AsIt.chain_(async function* takes(iter, ...funcs) { | ||
if (funcs.length === 1 && typeof funcs[0] === 'number') { yield* takeN(iter, funcs[0]); return; } | ||
yield* filterGen.call(this, iter, -1, ...funcs, _.condTake); | ||
}); | ||
AsIt.chain_(async function* stop(iter, ...funcs) { | ||
yield* filterGen.call(this, iter, true, ...funcs, $.cond_($.stop, true)); | ||
yield* filterGen.call(this, iter, true, ...funcs, _.condStop); | ||
}); | ||
module.exports = AsIt; |
const AsIt = require('./filter'); | ||
const $ = require('../func'); | ||
const _ = require('../func'); | ||
@@ -30,3 +30,3 @@ async function asItArray(iter) { | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.dfilter($.times_(5)); | ||
wrapped.dfilter(_.times_(5)); | ||
expect(await asItArray(wrapped)).toEqual(['a', '', 1]); | ||
@@ -37,3 +37,3 @@ }); | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.dfilter(v => v != null ? true : $.stop); | ||
wrapped.dfilter(v => v != null ? true : _.stop); | ||
expect(await asItArray(wrapped)).toEqual(['a', '', 1, 0]); | ||
@@ -44,3 +44,3 @@ }); | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.dfilter((v, a, d, p) => !p ? true : v != null ? true : $.stop); | ||
wrapped.dfilter((v, a, d, p) => !p ? true : v != null ? true : _.stop); | ||
expect(await asItArray(wrapped)).toEqual(['a', '', 1, 0, null]); | ||
@@ -51,3 +51,3 @@ }); | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.dfilter(v => v != null ? false : $.pass); | ||
wrapped.dfilter(v => v != null ? false : _.pass); | ||
expect(await asItArray(wrapped)).toEqual([null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
@@ -58,3 +58,3 @@ }); | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.dfilter((v, a, d, p) => !p ? true : v != null ? true : $.pass); | ||
wrapped.dfilter((v, a, d, p) => !p ? true : v != null ? true : _.pass); | ||
expect(await asItArray(wrapped)).toEqual(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
@@ -65,3 +65,3 @@ }); | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.dfilter($.echo, (v, a, d, p) => !p ? true : v != null ? true : $.pass); | ||
wrapped.dfilter(_.echo, (v, a, d, p) => !p ? true : v != null ? true : _.pass); | ||
expect(await asItArray(wrapped)).toEqual(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
@@ -111,2 +111,16 @@ }); | ||
test('AsIt_.skip: skip zero', async () => { | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.skip(0); | ||
expect(await asItArray(wrapped)).toEqual(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
}); | ||
test('AsIt_.skip, AsIt_.take: paging', async () => { | ||
const iter = AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
iter.return = () => ({done: true}); | ||
const wrapped = new AsIt(iter); | ||
wrapped.skip(5).take(2); | ||
expect(await asItArray(wrapped)).toEqual([NaN, {x: 1}]); | ||
}); | ||
test('AsIt_.take: empty', async () => { | ||
@@ -130,5 +144,49 @@ const wrapped = new AsIt(AsIt.getIter([1, 2, 3, 4, 5, 4, 3, 2, 1])); | ||
test('AsIt_.take: number', async () => { | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.take(2); | ||
expect(await asItArray(wrapped)).toEqual(['a', '']); | ||
}); | ||
test('AsIt_.take: zero', async () => { | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.take(0); | ||
expect(await asItArray(wrapped)).toEqual([]); | ||
}); | ||
test('AsIt_.takes: multi arg', async () => { | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.takes(o => o && o.x, v => !v); | ||
expect(await asItArray(wrapped)).toEqual(['a', '', 1, 0, null, NaN, {x: 1}]); | ||
}); | ||
test('AsIt_.takes: number', async () => { | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.takes(2); | ||
expect(await asItArray(wrapped)).toEqual(['a', '']); | ||
}); | ||
test('AsIt_.pfilter: takes', async () => { | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.pfilter(2, _.condTake); | ||
expect(await asItArray(wrapped)).toEqual(['a', '', 1]); | ||
}); | ||
test('AsIt_.pfilter: takes, take', async () => { | ||
const iter = AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
iter.return = () => ({done: true}); | ||
const wrapped = new AsIt(iter); | ||
wrapped.pfilter(2, _.condTake).take(2); | ||
expect(await asItArray(wrapped)).toEqual(['a', '']); | ||
}); | ||
test('AsIt_.pfilter: pass', async () => { | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.pfilter(() => _.pass); | ||
expect(await asItArray(wrapped)).toEqual(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
}); | ||
test('AsIt_.stop: one arg', async () => { | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.stop($.eq_(null)); | ||
wrapped.stop(_.eq_(null)); | ||
expect(await asItArray(wrapped)).toEqual(['a', '', 1, 0]); | ||
@@ -139,4 +197,4 @@ }); | ||
const wrapped = new AsIt(AsIt.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.stop($.times_(7), $.not); | ||
wrapped.stop(_.times_(7), _.not); | ||
expect(await asItArray(wrapped)).toEqual(['a', '', 1, 0]); | ||
}); |
@@ -34,6 +34,10 @@ const AsIt = require('./object'); | ||
for (let i = args.length - 1; i >= 0; i--) { | ||
yield* AsIt.from.gen(args[i]); | ||
yield* AsIt.from.gen.call(this, args[i]); | ||
} | ||
}); | ||
AsIt.make_(async function* repeat(from, times, strOk, ...args) { | ||
while (times--) yield* AsIt.getIter(from, strOk, ...args); | ||
}); | ||
class ForkBufferLimitExceededError extends Error { message = 'fork buffer limit exceeded'; } | ||
@@ -40,0 +44,0 @@ Object.assign(AsIt, {ForkBufferLimitExceededError}); |
@@ -133,1 +133,9 @@ const AsIt = require('./make'); | ||
}); | ||
test('AsIt.repeat: repeat iterable', async () => { | ||
expect(await asItArray(AsIt.repeat([1, 2, 3], 3))).toEqual([1, 2, 3, 1, 2, 3, 1, 2, 3]); | ||
}); | ||
test('AsIt.repeat: repeat characters', async () => { | ||
expect(await asItArray(AsIt.repeat('123', 3, true))).toEqual(['1', '2', '3', '1', '2', '3', '1', '2', '3']); | ||
}); |
124
as-it/map.js
const AsIt = require('./make'); | ||
const $ = require('../func/map'); | ||
const _ = require('../func/map'); | ||
AsIt.chain_(async function* map(iter, ...funcs) { | ||
const l = $._mappingFuncs(funcs, false); | ||
if (!l) return yield* iter; | ||
const l = _._mappingFuncs(funcs, false); | ||
if (!l) { yield* iter; return; } | ||
let value, done; | ||
const resFunc = l === 1 ? funcs[0] : _.cascadeFuncAsync.bind(this, funcs); | ||
const desc = {iter, ctx: this}; | ||
let idx = 0; | ||
let pass = false; | ||
if (l === 1) { | ||
const func = funcs[0]; | ||
for await (const item of iter) { | ||
if (pass) { yield item; continue; } | ||
const v = await func.call(this, item, idx, desc); | ||
if (v === $.stop) return; | ||
if (v === $.pass) { pass = true; yield item; continue; } | ||
yield v; | ||
idx++; | ||
try { | ||
while ({value, done} = await iter.next(), !done) { | ||
const result = await resFunc.call(this, value, idx, desc, value); | ||
if (result === _.stop) return; | ||
if (result === _.pass) { done = true; yield value; yield* iter; return; } | ||
yield result; | ||
++idx; | ||
} | ||
} else { | ||
for await (const item of iter) { | ||
if (pass) { yield item; continue; } | ||
let v = item; | ||
for (const func of funcs) { | ||
v = await func.call(this, v, idx, desc); | ||
} | ||
if (v === $.stop) return; | ||
if (v === $.pass) { pass = true; yield item; continue; } | ||
yield v; | ||
idx++; | ||
} | ||
} finally { | ||
if (!done && iter.return) await iter.return(); | ||
} | ||
@@ -41,23 +27,25 @@ }); | ||
AsIt.chain_(async function* maps(iter, ...funcs) { | ||
const l = $._mappingFuncs(funcs, true); | ||
if (!l) return yield* iter; | ||
const l = _._mappingFuncs(funcs, true); | ||
if (!l) { yield* iter; return; } | ||
let value, done; | ||
const resFunc = l === 1 ? funcs[0] : _.cascadeFuncAsync.bind(this, funcs); | ||
const desc = {iter, ctx: this}; | ||
let idx = 0; | ||
let pass = false; | ||
for await (const item of iter) { | ||
if (pass) { yield item; continue; } | ||
let v = item; | ||
try { | ||
while ({value, done} = await iter.next(), !done) { | ||
const result = await resFunc.call(this, value, idx, desc, value); | ||
if (result === _.stop) return; | ||
if (result === _.pass) { done = true; yield value; yield* iter; return; } | ||
for (const func of funcs) { | ||
v = await func.call(this, v, idx, desc, item); | ||
if (result != null) { | ||
const it = AsIt.getIter(result); | ||
if (it) yield* it; else yield result; | ||
} | ||
++idx; | ||
} | ||
if (v === $.stop) return; | ||
if (v === $.pass) { pass = true; yield item; continue; } | ||
idx++; | ||
if (v == null) continue; | ||
const it = AsIt.getIter(v); | ||
if (it) yield* it; else yield v; | ||
} finally { | ||
if (!done && iter.return) await iter.return(); | ||
} | ||
@@ -67,30 +55,28 @@ }); | ||
AsIt.chain_(async function* mapTo(iter, to, ...funcs) { | ||
const l = $._mappingFuncs(funcs, false); | ||
if (!l) return yield* iter; | ||
const l = _._mappingFuncs(funcs, false); | ||
if (!l) { yield* iter; return; } | ||
if (typeof to !== 'function') { | ||
if (to instanceof Array) to = $.to_(...to); | ||
else to = $.to_(to); | ||
if (to instanceof Array) to = _.to_(...to); | ||
else to = _.to_(to); | ||
} | ||
let value, done; | ||
const resFunc = l === 1 ? funcs[0] : _.cascadeFuncAsync.bind(this, funcs); | ||
const desc = {iter, ctx: this}; | ||
let idx = 0; | ||
let pass = false; | ||
for await (const item of iter) { | ||
if (pass) { yield item; continue; } | ||
let v = item; | ||
try { | ||
while ({value, done} = await iter.next(), !done) { | ||
const result = await resFunc.call(this, value, idx, desc, value); | ||
if (result === _.stop) return; | ||
if (result === _.pass) { done = true; yield value; yield* iter; return; } | ||
for (const func of funcs) { | ||
v = await func.call(this, v, idx, desc); | ||
const p = await to.call(this, value, idx, desc, value); | ||
if (p) p.ctx[p.key] = result; | ||
yield value; | ||
++idx; | ||
} | ||
if (v === $.stop) return; | ||
if (v === $.pass) { pass = true; yield item; continue; } | ||
const p = await to.call(this, item, idx, desc); | ||
if (p) p.ctx[p.key] = v; | ||
yield item; | ||
idx++; | ||
} finally { | ||
if (!done && iter.return) await iter.return(); | ||
} | ||
@@ -101,21 +87,21 @@ }); | ||
let ato, ain; | ||
if (at instanceof Array) { ato = $.to_(...at); ain = $.in_(...at); } | ||
else { ato = $.to_(at); ain = $.in_(at); } | ||
return AsIt.mapTo.gen(iter, ato, ain, ...funcs); | ||
if (at instanceof Array) { ato = _.to_(...at); ain = _.in_(...at); } | ||
else { ato = _.to_(at); ain = _.in_(at); } | ||
return AsIt.mapTo.gen.call(this, iter, ato, ain, ...funcs); | ||
}); | ||
AsIt.chain_(function mapKey(iter, ...funcs) { | ||
return AsIt.mapAt.gen(iter, 0, ...funcs); | ||
return AsIt.mapAt.gen.call(this, iter, 0, ...funcs); | ||
}); | ||
AsIt.chain_(function mapValue(iter, ...funcs) { | ||
return AsIt.mapAt.gen(iter, 1, ...funcs); | ||
return AsIt.mapAt.gen.call(this, iter, 1, ...funcs); | ||
}); | ||
AsIt.chain_(function mapKeys(iter, ...funcs) { | ||
return AsIt.mapTo.gen(iter, 0, ...funcs); | ||
return AsIt.mapTo.gen.call(this, iter, 0, ...funcs); | ||
}); | ||
AsIt.chain_(function mapValues(iter, ...funcs) { | ||
return AsIt.mapTo.gen(iter, 1, ...funcs); | ||
return AsIt.mapTo.gen.call(this, iter, 1, ...funcs); | ||
}); | ||
@@ -122,0 +108,0 @@ |
@@ -89,3 +89,5 @@ const AsIt = require('./map'); | ||
test('AsIt_.maps: stop: several functions', async () => { | ||
const wrapped = new AsIt(AsIt.getIter([4, 0, 8, 3, 1])); | ||
const iter = AsIt.getIter([4, 0, 8, 3, 1]); | ||
iter.return = () => ({done: true}); | ||
const wrapped = new AsIt(iter); | ||
wrapped.maps($.neg, v => v < -6 ? $.stop : [v, 0]); | ||
@@ -114,3 +116,5 @@ expect(await asItArray(wrapped)).toEqual([-4, 0, -0, 0]); | ||
test('AsIt_.mapTo: stop: several functions', async () => { | ||
const wrapped = new AsIt(AsIt.getIter([[4], [0], [8], [3], [1]])); | ||
const iter = AsIt.getIter([[4], [0], [8], [3], [1]]); | ||
iter.return = () => ({done: true}); | ||
const wrapped = new AsIt(iter); | ||
wrapped.mapTo(0, '0', $.neg, v => v < -6 ? $.stop : v); | ||
@@ -117,0 +121,0 @@ expect(await asItArray(wrapped)).toEqual([[-4], [-0]]); |
@@ -47,5 +47,6 @@ const {Readable} = require('stream'); | ||
AsIt.chain_(async function *omit(iter, to) { | ||
for await (const item of iter) { | ||
for await (let item of iter) { | ||
yield item; | ||
if (item instanceof Array) item = item[0]; | ||
delete to[item]; | ||
yield item; | ||
} | ||
@@ -203,6 +204,13 @@ }); | ||
let c = 0; | ||
for await (const item of iter) c++; | ||
for await (const item of iter) ++c; | ||
return c; | ||
}); | ||
AsIt.chain_(async function* countTo(iter, to, field = 'count') { | ||
for await (const item of iter) { | ||
yield item; | ||
++to[field]; | ||
} | ||
}); | ||
AsIt.value_(async function exec(iter) { | ||
@@ -246,3 +254,3 @@ for await (const item of iter); | ||
AsIt.value_(async function reduce(iter, func, def, out = {}) { | ||
const it = AsIt.reduceTo.gen(iter, func, def, out); | ||
const it = AsIt.reduceTo.gen.call(this, iter, func, def, out); | ||
for await (const item of it); | ||
@@ -249,0 +257,0 @@ return out.result; |
@@ -117,6 +117,6 @@ const cr = require('crypto'); | ||
test('AsIt_.omit: omit from object', async () => { | ||
const wrapped = new AsIt(['a', 'b', 'c'][Symbol.iterator]()); | ||
const wrapped = new AsIt(['a', 'b', ['c', 'd']][Symbol.iterator]()); | ||
const to = {a: 5, d: 8, x: 1, c: 2}; | ||
wrapped.omit(to); | ||
expect(await asItArray(wrapped)).toEqual(['a', 'b', 'c']); | ||
expect(await asItArray(wrapped)).toEqual(['a', 'b', ['c', 'd']]); | ||
expect(to).toEqual({d: 8, x: 1}); | ||
@@ -247,2 +247,10 @@ }); | ||
test('AsIt_.countTo: count iterator items on field', async () => { | ||
const result = {count: 0, next: 9}; | ||
const wrapped = new AsIt([2, 6, 7][Symbol.iterator]()); | ||
await wrapped.countTo(result).countTo(result, 'next').exec(); | ||
expect(result.count).toEqual(3); | ||
expect(result.next).toEqual(12); | ||
}); | ||
test('AsIt_.exec: execute iterator', async () => { | ||
@@ -249,0 +257,0 @@ const wrapped = new AsIt([2, 6, 7][Symbol.iterator]()); |
@@ -6,3 +6,3 @@ const it = require('./it'); | ||
const symbol = Symbol('$'); | ||
const life = Symbol('_.life'); | ||
@@ -23,3 +23,4 @@ function $(...args) { | ||
$.symbol = symbol; | ||
$.symbol = $.life = life; | ||
$.keepAlive = Symbol('_.keepAlive'); | ||
@@ -26,0 +27,0 @@ const func_ = function func_(func, name) { |
@@ -60,3 +60,9 @@ const $ = require('../base'); | ||
$.stop = Symbol('_.stop'); | ||
$.pass = Symbol('_.pass'); | ||
$.chunkEnds = $.cond_(-1, 0); | ||
$.condSkip = $.cond_(false, $.pass); | ||
$.condTake = $.cond_(true, $.stop); | ||
$.condStop = $.cond_($.stop, true); | ||
@@ -339,2 +345,18 @@ func_(function isNullish(value) { | ||
func_(function cascadeFunc(funcs, v, ...sfx) { | ||
for (const func of funcs) { | ||
v = func.call(this, v, ...sfx); | ||
} | ||
return v; | ||
}); | ||
func_(async function cascadeFuncAsync(funcs, v, ...sfx) { | ||
for (const func of funcs) { | ||
v = await func.call(this, v, ...sfx); | ||
} | ||
return v; | ||
}); | ||
const hasProtos = new Set([Map.prototype, WeakMap.prototype, Set.prototype, WeakSet.prototype]); | ||
@@ -364,5 +386,2 @@ | ||
$.stop = Symbol('$.stop'); | ||
$.pass = Symbol('$.pass'); | ||
module.exports = $; |
@@ -21,3 +21,3 @@ const $ = require('../base'); | ||
const iter = Iter.getIter(from); | ||
if (iter) for (const item of iter) for (const item of from) to.add(item); | ||
if (iter) for (const item of iter) to.add(item); | ||
else to.add(from); | ||
@@ -29,3 +29,3 @@ return to; | ||
const iter = Iter.getIter(from); | ||
if (iter) for (const item of iter) for (const item of from) to.set(item, true); | ||
if (iter) for (const item of iter) to.set(item, true); | ||
else to.set(from, true); | ||
@@ -32,0 +32,0 @@ return to; |
@@ -170,4 +170,2 @@ const $ = require('../func'); | ||
this.#sleepTimer = null; | ||
outer: for (const o of Iter.reverse.gen(this.#protos)) { | ||
@@ -417,3 +415,3 @@ let retr = this.wakeRetries, tries = 1; | ||
pingInterval: null, //TODO: | ||
keepAlive: null, //TODO: | ||
fatalErrors: [], //TODO: $_method_ -- idempotent method (retry on fatal error) | ||
@@ -420,0 +418,0 @@ |
@@ -50,3 +50,3 @@ const Iter = require('./base'); | ||
if (!opts.to) opts.to = Object.create(null); | ||
for (const item of Iter.recentGroup.gen(iter, opts.to, limit, opts)); | ||
for (const item of Iter.recentGroup.gen.call(this, iter, opts.to, limit, opts)); | ||
return opts.to; | ||
@@ -96,3 +96,3 @@ }); | ||
if (!opts.to) opts.to = Object.create(null); | ||
for (const item of Iter.orderGroup.gen(iter, opts.to, limit, opts)); | ||
for (const item of Iter.orderGroup.gen.call(this, iter, opts.to, limit, opts)); | ||
return opts.to; | ||
@@ -99,0 +99,0 @@ }); |
const it = require('../it'); | ||
const Design = require('../design'); | ||
const wrapped = Symbol('$.Iter.wrapped'); | ||
const wrapped = Symbol('_.Iter.wrapped'); | ||
@@ -142,3 +142,4 @@ const Iter = function(iter) { | ||
const partial = iterPartial(iter); | ||
return new Iter(partial); | ||
const Class = typeof this === 'function' ? this : this.constructor; | ||
return new Class(partial); | ||
}); | ||
@@ -145,0 +146,0 @@ |
@@ -194,3 +194,3 @@ const $ = require('../base'); | ||
const iter = new Iter([1, 2, 3, 4]); | ||
const part = iter.partial(); | ||
const part = Iter.partial(iter); | ||
expect(part.read()).toEqual(1); | ||
@@ -197,0 +197,0 @@ expect(iter.read()).toEqual(2); |
const Iter = require('./base'); | ||
const $ = require('../func'); | ||
const _ = require('../func'); | ||
function* filterGen(iter, double, ...funcs) { | ||
const l = $._predicateFuncs(funcs); | ||
const l = _._predicateFuncs(funcs); | ||
@@ -15,44 +15,34 @@ if (!l) { | ||
let value, done; | ||
const resFunc = l === 1 ? funcs[0] : _.cascadeFunc.bind(this, funcs); | ||
const desc = {iter, ctx: this}; | ||
let pass = false; | ||
const pass1 = double ? false : null; | ||
if (l === 1) { | ||
const func = funcs[0]; | ||
for (const item of iter) { | ||
if (pass) { yield item; continue; } | ||
let v = func.call(this, item, item, desc, pass1); | ||
if (v === $.stop) break; | ||
if (v === $.pass) { pass = true; yield item; continue; } | ||
if (v) yield item; | ||
if (!double) continue; | ||
v = func.call(this, item, item, desc, true); | ||
if (v === $.stop) break; | ||
if (v === $.pass) { pass = true; continue; } | ||
} | ||
} else { | ||
for (const item of iter) { | ||
if (pass) { yield item; continue; } | ||
let v = item; | ||
for (const func of funcs) { | ||
v = func.call(this, v, item, desc, pass1); | ||
try { | ||
if (double < 0) { | ||
while ({value, done} = iter.next(), !done) { | ||
yield value; | ||
const result2 = resFunc.call(this, value, value, desc, -1); | ||
if (result2 === _.stop) return; | ||
if (result2 === _.pass) { done = true; yield* iter; return; } | ||
} | ||
if (v === $.stop) break; | ||
if (v === $.pass) { pass = true; yield item; continue; } | ||
if (v) yield item; | ||
if (!double) continue; | ||
v = item; | ||
for (const func of funcs) { | ||
v = func.call(this, v, item, desc, true); | ||
} else if (double) { | ||
while ({value, done} = iter.next(), !done) { | ||
const result = resFunc.call(this, value, value, desc, false); | ||
if (result === _.stop) return; | ||
if (result) yield value; | ||
if (result === _.pass) { done = true; yield* iter; return; } | ||
const result2 = resFunc.call(this, value, value, desc, true); | ||
if (result2 === _.stop) return; | ||
if (result2 === _.pass) { done = true; yield* iter; return; } | ||
} | ||
if (v === $.stop) break; | ||
if (v === $.pass) { pass = true; continue; } | ||
} else { | ||
while ({value, done} = iter.next(), !done) { | ||
const result = resFunc.call(this, value, value, desc, null); | ||
if (result === _.stop) return; | ||
if (result) yield value; | ||
if (result === _.pass) { done = true; yield* iter; return; } | ||
} | ||
} | ||
} finally { | ||
if (!done && iter.return) iter.return(); | ||
} | ||
@@ -69,8 +59,12 @@ } | ||
Iter.chain_(function* pfilter(iter, ...funcs) { | ||
yield* filterGen.call(this, iter, -1, ...funcs); | ||
}); | ||
Iter.chain_(function* call(iter, ...funcs) { | ||
yield* filterGen.call(this, iter, false, ...funcs, $.true); | ||
yield* filterGen.call(this, iter, false, ...funcs, _.true); | ||
}); | ||
Iter.chain_(function* debug(iter, ...funcs) { | ||
$._predicateFuncs(funcs); | ||
_._predicateFuncs(funcs); | ||
@@ -81,3 +75,3 @@ funcs = funcs.map((func) => function (item) { | ||
yield* filterGen.call(this, iter, false, ...funcs, $.true); | ||
yield* filterGen.call(this, iter, false, ...funcs, _.true); | ||
}); | ||
@@ -89,14 +83,49 @@ | ||
function* skipN(iter, n) { | ||
let done; | ||
if (n <= 0) { yield* iter; return; } | ||
try { | ||
while ({done} = iter.next(), !done) { | ||
if (--n <= 0) { yield* iter; return; } | ||
} | ||
} finally { | ||
if (!done && iter.return) iter.return(); | ||
} | ||
} | ||
Iter.chain_(function* skip(iter, ...funcs) { | ||
yield* filterGen.call(this, iter, false, ...funcs, $.cond_(false, $.pass)); | ||
if (funcs.length === 1 && typeof funcs[0] === 'number') { yield* skipN(iter, funcs[0]); return; } | ||
yield* filterGen.call(this, iter, false, ...funcs, _.condSkip); | ||
}); | ||
function* takeN(iter, n) { | ||
let value, done; | ||
try { | ||
if (n <= 0) return; | ||
while ({value, done} = iter.next(), !done) { | ||
yield value; | ||
if (--n <= 0) return; | ||
} | ||
} finally { | ||
if (!done && iter.return) iter.return(); | ||
} | ||
} | ||
Iter.chain_(function* take(iter, ...funcs) { | ||
yield* filterGen.call(this, iter, false, ...funcs, $.cond_(true, $.stop)); | ||
if (funcs.length === 1 && typeof funcs[0] === 'number') { yield* takeN(iter, funcs[0]); return; } | ||
yield* filterGen.call(this, iter, false, ...funcs, _.condTake); | ||
}); | ||
Iter.chain_(function* takes(iter, ...funcs) { | ||
if (funcs.length === 1 && typeof funcs[0] === 'number') { yield* takeN(iter, funcs[0]); return; } | ||
yield* filterGen.call(this, iter, -1, ...funcs, _.condTake); | ||
}); | ||
Iter.chain_(function* stop(iter, ...funcs) { | ||
yield* filterGen.call(this, iter, true, ...funcs, $.cond_($.stop, true)); | ||
yield* filterGen.call(this, iter, true, ...funcs, _.condStop); | ||
}); | ||
module.exports = Iter; |
const Iter = require('./filter'); | ||
const $ = require('../func'); | ||
const _ = require('../func'); | ||
@@ -24,3 +24,3 @@ test('Iter_.filter: true filter', () => { | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.dfilter($.times_(5)); | ||
wrapped.dfilter(5); | ||
expect(Array.from(wrapped)).toEqual(['a', '', 1]); | ||
@@ -31,3 +31,3 @@ }); | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.dfilter(v => v != null ? true : $.stop); | ||
wrapped.dfilter(v => v != null ? true : _.stop); | ||
expect(Array.from(wrapped)).toEqual(['a', '', 1, 0]); | ||
@@ -38,3 +38,3 @@ }); | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.dfilter((v, a, d, p) => !p ? true : v != null ? true : $.stop); | ||
wrapped.dfilter((v, a, d, p) => !p ? true : v != null ? true : _.stop); | ||
expect(Array.from(wrapped)).toEqual(['a', '', 1, 0, null]); | ||
@@ -45,3 +45,3 @@ }); | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.dfilter(v => v != null ? false : $.pass); | ||
wrapped.dfilter(v => v != null ? false : _.pass); | ||
expect(Array.from(wrapped)).toEqual([null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
@@ -52,3 +52,3 @@ }); | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.dfilter((v, a, d, p) => !p ? true : v != null ? true : $.pass); | ||
wrapped.dfilter((v, a, d, p) => !p ? true : v != null ? true : _.pass); | ||
expect(Array.from(wrapped)).toEqual(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
@@ -59,3 +59,3 @@ }); | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.dfilter($.echo, (v, a, d, p) => !p ? true : v != null ? true : $.pass); | ||
wrapped.dfilter(_.echo, (v, a, d, p) => !p ? true : v != null ? true : _.pass); | ||
expect(Array.from(wrapped)).toEqual(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
@@ -83,3 +83,3 @@ }); | ||
const wr = new Iter(['test']); | ||
wr.dbglog('Iter.dbglog').debug($.dbglog_('$.dbglog_')); | ||
wr.dbglog('Iter.dbglog').debug(_.dbglog_('$.dbglog_')); | ||
res = Array.from(wr); | ||
@@ -106,2 +106,8 @@ } finally { | ||
test('Iter_.skip: skip zero', () => { | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.skip(0); | ||
expect(Array.from(wrapped)).toEqual(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
}); | ||
test('Iter_.take: empty', () => { | ||
@@ -125,5 +131,49 @@ const wrapped = new Iter(Iter.getIter([1, 2, 3, 4, 5, 4, 3, 2, 1])); | ||
test('Iter_.take: number', () => { | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.take(2); | ||
expect(Array.from(wrapped)).toEqual(['a', '']); | ||
}); | ||
test('Iter_.take: zero', () => { | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.take(0); | ||
expect(Array.from(wrapped)).toEqual([]); | ||
}); | ||
test('Iter_.takes: multi arg', () => { | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.takes(o => o && o.x, v => !v); | ||
expect(Array.from(wrapped)).toEqual(['a', '', 1, 0, null, NaN, {x: 1}]); | ||
}); | ||
test('Iter_.takes: number', () => { | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.takes(2); | ||
expect(Array.from(wrapped)).toEqual(['a', '']); | ||
}); | ||
test('Iter_.pfilter: takes', () => { | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.pfilter(2, _.condTake); | ||
expect(Array.from(wrapped)).toEqual(['a', '', 1]); | ||
}); | ||
test('Iter_.pfilter: takes, take', () => { | ||
const iter = Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
iter.return = () => ({done: true}); | ||
const wrapped = new Iter(iter); | ||
wrapped.pfilter(2, _.condTake).take(2); | ||
expect(Array.from(wrapped)).toEqual(['a', '']); | ||
}); | ||
test('Iter_.pfilter: pass', () => { | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.pfilter(() => _.pass); | ||
expect(Array.from(wrapped)).toEqual(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n]); | ||
}); | ||
test('Iter_.stop: one arg', () => { | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.stop($.eq_(null)); | ||
wrapped.stop(_.eq_(null)); | ||
expect(Array.from(wrapped)).toEqual(['a', '', 1, 0]); | ||
@@ -134,4 +184,4 @@ }); | ||
const wrapped = new Iter(Iter.getIter(['a', '', 1, 0, null, NaN, {x: 1}, [5, 1], false, 0n])); | ||
wrapped.stop($.times_(7), $.not); | ||
wrapped.stop(_.times_(7), _.not); | ||
expect(Array.from(wrapped)).toEqual(['a', '', 1, 0]); | ||
}); |
@@ -50,4 +50,8 @@ const Iter = require('./object'); | ||
Iter.make_(function* repeat(from, times, strOk, ...args) { | ||
while (times--) yield* Iter.getIter(from, strOk, ...args); | ||
}); | ||
Iter.chain_(function* reverse(iter) { | ||
if (!(iter instanceof Array)) iter = Array.from(iter); | ||
if (!('length' in iter)) iter = Array.from(iter); | ||
@@ -54,0 +58,0 @@ for (let i = iter.length - 1; i >= 0; i--) { |
@@ -70,2 +70,10 @@ const Iter = require('./make'); | ||
test('Iter.repeat: repeat iterable', () => { | ||
expect(Array.from(Iter.repeat([1, 2, 3], 3))).toEqual([1, 2, 3, 1, 2, 3, 1, 2, 3]); | ||
}); | ||
test('Iter.repeat: repeat characters', () => { | ||
expect(Array.from(Iter.repeat('123', 3, true))).toEqual(['1', '2', '3', '1', '2', '3', '1', '2', '3']); | ||
}); | ||
test('Iter.reverse: iterate backwards', () => { | ||
@@ -72,0 +80,0 @@ const src = [1, 2, 3, 4, 5]; |
123
iter/map.js
const Iter = require('./make'); | ||
const $ = require('../func/map'); | ||
const _ = require('../func/map'); | ||
Iter.chain_(function* map(iter, ...funcs) { | ||
const l = $._mappingFuncs(funcs, false); | ||
if (!l) return yield* iter; | ||
const l = _._mappingFuncs(funcs, false); | ||
if (!l) { yield* iter; return; } | ||
let value, done; | ||
const resFunc = l === 1 ? funcs[0] : _.cascadeFunc.bind(this, funcs); | ||
const desc = {iter, ctx: this}; | ||
let idx = 0; | ||
let pass = false; | ||
if (l === 1) { | ||
const func = funcs[0]; | ||
for (const item of iter) { | ||
if (pass) { yield item; continue; } | ||
const v = func.call(this, item, idx, desc, item); | ||
if (v === $.stop) return; | ||
if (v === $.pass) { pass = true; yield item; continue; } | ||
yield v; | ||
idx++; | ||
try { | ||
while ({value, done} = iter.next(), !done) { | ||
const result = resFunc.call(this, value, idx, desc, value); | ||
if (result === _.stop) return; | ||
if (result === _.pass) { done = true; yield value; yield* iter; return; } | ||
yield result; | ||
++idx; | ||
} | ||
} else { | ||
for (const item of iter) { | ||
if (pass) { yield item; continue; } | ||
let v = item; | ||
for (const func of funcs) { | ||
v = func.call(this, v, idx, desc, item); | ||
} | ||
if (v === $.stop) return; | ||
if (v === $.pass) { pass = true; yield item; continue; } | ||
yield v; | ||
idx++; | ||
} | ||
} finally { | ||
if (!done && iter.return) iter.return(); | ||
} | ||
@@ -41,23 +27,25 @@ }); | ||
Iter.chain_(function* maps(iter, ...funcs) { | ||
const l = $._mappingFuncs(funcs, true); | ||
if (!l) return yield* iter; | ||
const l = _._mappingFuncs(funcs, true); | ||
if (!l) { yield* iter; return; } | ||
let value, done; | ||
const resFunc = l === 1 ? funcs[0] : _.cascadeFunc.bind(this, funcs); | ||
const desc = {iter, ctx: this}; | ||
let idx = 0; | ||
let pass = false; | ||
for (const item of iter) { | ||
if (pass) { yield item; continue; } | ||
let v = item; | ||
try { | ||
while ({value, done} = iter.next(), !done) { | ||
const result = resFunc.call(this, value, idx, desc, value); | ||
if (result === _.stop) return; | ||
if (result === _.pass) { done = true; yield value; yield* iter; return; } | ||
for (const func of funcs) { | ||
v = func.call(this, v, idx, desc, item); | ||
if (result != null) { | ||
const it = Iter.getIter(result); | ||
if (it) yield* it; else yield result; | ||
} | ||
++idx; | ||
} | ||
if (v === $.stop) return; | ||
if (v === $.pass) { pass = true; yield item; continue; } | ||
idx++; | ||
if (v == null) continue; | ||
const it = Iter.getIter(v); | ||
if (it) yield* it; else yield v; | ||
} finally { | ||
if (!done && iter.return) iter.return(); | ||
} | ||
@@ -67,29 +55,28 @@ }); | ||
Iter.chain_(function* mapTo(iter, to, ...funcs) { | ||
const l = $._mappingFuncs(funcs, false); | ||
if (!l) return yield* iter; | ||
const l = _._mappingFuncs(funcs, false); | ||
if (!l) { yield* iter; return; } | ||
if (typeof to !== 'function') { | ||
if (to instanceof Array) to = $.to_(...to); | ||
else to = $.to_(to); | ||
if (to instanceof Array) to = _.to_(...to); | ||
else to = _.to_(to); | ||
} | ||
let value, done; | ||
const resFunc = l === 1 ? funcs[0] : _.cascadeFunc.bind(this, funcs); | ||
const desc = {iter, ctx: this}; | ||
let idx = 0; | ||
let pass = false; | ||
for (const item of iter) { | ||
if (pass) { yield item; continue; } | ||
let v = item; | ||
try { | ||
while ({value, done} = iter.next(), !done) { | ||
const result = resFunc.call(this, value, idx, desc, value); | ||
if (result === _.stop) return; | ||
if (result === _.pass) { done = true; yield value; yield* iter; return; } | ||
for (const func of funcs) { | ||
v = func.call(this, v, idx, desc, item); | ||
const p = to.call(this, value, idx, desc, value); | ||
if (p) p.ctx[p.key] = result; | ||
yield value; | ||
++idx; | ||
} | ||
if (v === $.stop) return; | ||
if (v === $.pass) { pass = true; yield item; continue; } | ||
const p = to.call(this, item, idx, desc, item); | ||
if (p) p.ctx[p.key] = v; | ||
yield item; | ||
idx++; | ||
} finally { | ||
if (!done && iter.return) iter.return(); | ||
} | ||
@@ -100,21 +87,21 @@ }); | ||
let ato, ain; | ||
if (at instanceof Array) { ato = $.to_(...at); ain = $.in_(...at); } | ||
else { ato = $.to_(at); ain = $.in_(at); } | ||
return Iter.mapTo.gen(iter, ato, ain, ...funcs); | ||
if (at instanceof Array) { ato = _.to_(...at); ain = _.in_(...at); } | ||
else { ato = _.to_(at); ain = _.in_(at); } | ||
return Iter.mapTo.gen.call(this, iter, ato, ain, ...funcs); | ||
}); | ||
Iter.chain_(function mapKey(iter, ...funcs) { | ||
return Iter.mapAt.gen(iter, 0, ...funcs); | ||
return Iter.mapAt.gen.call(this, iter, 0, ...funcs); | ||
}); | ||
Iter.chain_(function mapValue(iter, ...funcs) { | ||
return Iter.mapAt.gen(iter, 1, ...funcs); | ||
return Iter.mapAt.gen.call(this, iter, 1, ...funcs); | ||
}); | ||
Iter.chain_(function mapKeys(iter, ...funcs) { | ||
return Iter.mapTo.gen(iter, 0, ...funcs); | ||
return Iter.mapTo.gen.call(this, iter, 0, ...funcs); | ||
}); | ||
Iter.chain_(function mapValues(iter, ...funcs) { | ||
return Iter.mapTo.gen(iter, 1, ...funcs); | ||
return Iter.mapTo.gen.call(this, iter, 1, ...funcs); | ||
}); | ||
@@ -121,0 +108,0 @@ |
const Iter = require('./map'); | ||
const $ = require('../func'); | ||
const _ = require('../func'); | ||
@@ -36,3 +36,3 @@ test('Iter_.map: map iterator: echo', () => { | ||
const wrapped = new Iter(Iter.getIter([4, 0, 8, 3, 1])); | ||
wrapped.map(v => v > 6 ? $.stop : -v); | ||
wrapped.map(v => v > 6 ? _.stop : -v); | ||
expect(Array.from(wrapped)).toEqual([-4, -0]); | ||
@@ -43,3 +43,3 @@ }); | ||
const wrapped = new Iter(Iter.getIter([4, 0, 8, 3, 1])); | ||
wrapped.map(v => v > 6 ? $.pass : -v); | ||
wrapped.map(v => v > 6 ? _.pass : -v); | ||
expect(Array.from(wrapped)).toEqual([-4, -0, 8, 3, 1]); | ||
@@ -50,3 +50,3 @@ }); | ||
const wrapped = new Iter(Iter.getIter([4, 0, 8, 3, 1])); | ||
wrapped.map($.neg, v => v < -6 ? $.stop : v); | ||
wrapped.map(_.neg, v => v < -6 ? _.stop : v); | ||
expect(Array.from(wrapped)).toEqual([-4, -0]); | ||
@@ -57,3 +57,3 @@ }); | ||
const wrapped = new Iter(Iter.getIter([4, 0, 8, 3, 1])); | ||
wrapped.map($.neg, v => v < -6 ? $.pass : v); | ||
wrapped.map(_.neg, v => v < -6 ? _.pass : v); | ||
expect(Array.from(wrapped)).toEqual([-4, -0, 8, 3, 1]); | ||
@@ -105,4 +105,6 @@ }); | ||
test('Iter_.maps: stop: several functions', () => { | ||
const wrapped = new Iter(Iter.getIter([4, 0, 8, 3, 1])); | ||
wrapped.maps($.neg, v => v < -6 ? $.stop : [v, 0]); | ||
const iter = Iter.getIter([4, 0, 8, 3, 1]); | ||
iter.return = () => ({done: true}); | ||
const wrapped = new Iter(iter); | ||
wrapped.maps(_.neg, v => v < -6 ? _.stop : [v, 0]); | ||
expect(Array.from(wrapped)).toEqual([-4, 0, -0, 0]); | ||
@@ -113,3 +115,3 @@ }); | ||
const wrapped = new Iter(Iter.getIter([4, 0, 8, 3, 1])); | ||
wrapped.maps($.neg, v => v < -6 ? $.pass : [v, 0]); | ||
wrapped.maps(_.neg, v => v < -6 ? _.pass : [v, 0]); | ||
expect(Array.from(wrapped)).toEqual([-4, 0, -0, 0, 8, 3, 1]); | ||
@@ -131,4 +133,6 @@ }); | ||
test('Iter_.mapTo: stop: several functions', () => { | ||
const wrapped = new Iter(Iter.getIter([[4], [0], [8], [3], [1]])); | ||
wrapped.mapTo(0, '0', $.neg, v => v < -6 ? $.stop : v); | ||
const iter = Iter.getIter([[4], [0], [8], [3], [1]]); | ||
iter.return = () => ({done: true}); | ||
const wrapped = new Iter(iter); | ||
wrapped.mapTo(0, '0', _.neg, v => v < -6 ? _.stop : v); | ||
expect(Array.from(wrapped)).toEqual([[-4], [-0]]); | ||
@@ -139,3 +143,3 @@ }); | ||
const wrapped = new Iter(Iter.getIter([[4], [0], [8], [3], [1]])); | ||
wrapped.mapTo(0, '0', $.neg, v => v < -6 ? $.pass : v); | ||
wrapped.mapTo(0, '0', _.neg, v => v < -6 ? _.pass : v); | ||
expect(Array.from(wrapped)).toEqual([[-4], [-0], [8], [3], [1]]); | ||
@@ -142,0 +146,0 @@ }); |
@@ -49,5 +49,6 @@ const {Readable} = require('stream'); | ||
Iter.chain_(function *omit(iter, to) { | ||
for (const item of iter) { | ||
for (let item of iter) { | ||
yield item; | ||
if (item instanceof Array) item = item[0]; | ||
delete to[item]; | ||
yield item; | ||
} | ||
@@ -207,6 +208,13 @@ }); | ||
let c = 0; | ||
for (const item of iter) c++; | ||
for (const item of iter) ++c; | ||
return c; | ||
}); | ||
Iter.chain_(function* countTo(iter, to, field = 'count') { | ||
for (const item of iter) { | ||
yield item; | ||
++to[field]; | ||
} | ||
}); | ||
Iter.value_(function exec(iter) { | ||
@@ -250,3 +258,3 @@ for (const item of iter); | ||
Iter.value_(function reduce(iter, func, def, out = {}) { | ||
const it = Iter.reduceTo.gen(iter, func, def, out); | ||
const it = Iter.reduceTo.gen.call(this, iter, func, def, out); | ||
for (const item of it); | ||
@@ -253,0 +261,0 @@ return out.result; |
@@ -110,6 +110,6 @@ const cr = require('crypto'); | ||
test('Iter_.omit: omit from object', () => { | ||
const wrapped = new Iter(['a', 'b', 'c'][Symbol.iterator]()); | ||
const wrapped = new Iter(['a', 'b', ['c']][Symbol.iterator]()); | ||
const to = {a: 5, d: 8, x: 1, c: 2}; | ||
wrapped.omit(to); | ||
expect(Array.from(wrapped)).toEqual(['a', 'b', 'c']); | ||
expect(Array.from(wrapped)).toEqual(['a', 'b', ['c']]); | ||
expect(to).toEqual({d: 8, x: 1}); | ||
@@ -238,2 +238,10 @@ }); | ||
test('Iter_.countTo: count iterator items on field', () => { | ||
const result = {count: 0, next: 9}; | ||
const wrapped = new Iter([2, 6, 7][Symbol.iterator]()); | ||
wrapped.countTo(result).countTo(result, 'next').exec(); | ||
expect(result.count).toEqual(3); | ||
expect(result.next).toEqual(12); | ||
}); | ||
test('Iter_.exec: execute iterator', () => { | ||
@@ -240,0 +248,0 @@ const wrapped = new Iter([2, 6, 7][Symbol.iterator]()); |
{ | ||
"name": "asclasit", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "ASync CLasses + ASync ITerators", | ||
@@ -32,4 +32,4 @@ "main": "index.js", | ||
"devDependencies": { | ||
"jest": "^27.2.1" | ||
"jest": "^27.5.1" | ||
} | ||
} |
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
366476
10456