Comparing version 0.1.6 to 0.1.7
@@ -37,3 +37,3 @@ { | ||
"func2/object." | ||
"func2/cache." | ||
], | ||
@@ -40,0 +40,0 @@ "console": "integratedTerminal", |
@@ -160,2 +160,17 @@ const it = require('../it'); | ||
async function* asItPartial(iter) { | ||
let cur; | ||
while (!(cur = await iter.next()).done) yield cur.value; | ||
} | ||
AsIt.value_(function partial(iter) { | ||
const partial = asItPartial(iter); | ||
return new AsIt(partial); | ||
}); | ||
AsIt_.init = function init(id, value) { | ||
this[id] = value; | ||
return this; | ||
}; | ||
AsIt.chain_(async function* save(iter, id) { | ||
@@ -162,0 +177,0 @@ for await (const item of iter) { |
@@ -1,2 +0,2 @@ | ||
const $ = require('../base'); | ||
const $ = require('../func'); | ||
const AsIt = require('./base'); | ||
@@ -214,2 +214,29 @@ | ||
test('AsIt_.partial: sequential iteration', async () => { | ||
const iter = new AsIt([1, 2, 3, 4]); | ||
const part = iter.partial(); | ||
let n = 2; | ||
for await (const item of part) if (!--n) break; | ||
expect(await asItArray(iter)).toEqual([3, 4]); | ||
}); | ||
test('AsIt_.partial: parallel iteration', async () => { | ||
const iter = new AsIt(async function*() {for (let i = 0; i < 4; i++) {await $.delayMsec(50); yield i;}} ()); | ||
const part = iter.partial(); | ||
let r1 = [], r2 = []; | ||
await Promise.all([ | ||
(async () => { for await (const item of part) r1.push(item); }) (), | ||
(async () => { await $.delayMsec(25); for await (const item of iter) r2.push(item); }) (), | ||
]); | ||
expect(r1).toEqual([0, 2]); | ||
expect(r2).toEqual([1, 3]); | ||
}); | ||
test('AsIt_.init: initial set var', async () => { | ||
const iter = new AsIt([1, 2, 3, 4]); | ||
iter.init('a', 2).init('b', 3); | ||
expect(iter.a).toBe(2); | ||
expect(iter.b).toBe(3); | ||
}); | ||
test('AsIt_.get, .set: get/set wrapped iterator', async () => { | ||
@@ -216,0 +243,0 @@ const iter = new AsIt([1, 2]); |
@@ -6,8 +6,11 @@ const AsIt = require('./realign'); | ||
AsIt.chain_(async function* race(iter, chunk = Infinity, ...args) { | ||
AsIt.chain_(async function* race(iter, chunk = Infinity, ...funcs) { | ||
const buf = $(); | ||
let latch = chunk; | ||
let latch = chunk - 1; | ||
let nBuf = 0; | ||
let idx = 0; | ||
const desc = {buf, ctx: this, chunk}; | ||
$._predicateFuncs(funcs); | ||
if (iter[Symbol.iterator]) { | ||
@@ -18,2 +21,5 @@ for (let item of iter) { | ||
for (const func of funcs) v = func.call(this, v, k, desc); | ||
buf[k] = v; | ||
if (latch) { | ||
@@ -27,5 +33,2 @@ latch--; | ||
} | ||
if (typeof v === 'function') v = v.call(this, ...args); | ||
buf[k] = v; | ||
} | ||
@@ -37,2 +40,5 @@ } else { | ||
for (const func of funcs) v = func.call(this, v, k, desc); | ||
buf[k] = v; | ||
if (latch) { | ||
@@ -46,5 +52,2 @@ latch--; | ||
} | ||
if (typeof v === 'function') v = v.call(this, ...args); | ||
buf[k] = v; | ||
} | ||
@@ -51,0 +54,0 @@ } |
const AsIt = require('./promise'); | ||
const Iter = require('../iter'); | ||
require('./make'); | ||
@@ -55,3 +54,3 @@ const $ = require('../func2/promise'); | ||
const top = await AsIt.from(src).race().toArray(); | ||
const top = await AsIt.from(src).map($.mapper).race().toArray(); | ||
@@ -64,3 +63,3 @@ expect(top).toEqual([ | ||
const top2 = await AsIt.from(src).race().toArray(); | ||
const top2 = await AsIt.from(src).map($.mapper).race().toArray(); | ||
@@ -73,1 +72,12 @@ expect(top2).toEqual([ | ||
}); | ||
test('AsIt_.race: internal mapping', async () => { | ||
const msec = AsIt.from([240, 160, 200, 1]); | ||
const arr = await msec.race(2, async (msec) => { | ||
await $.delayMsec(msec); | ||
return msec; | ||
}).map($.inValue).toArray(); | ||
expect(arr).toEqual([160, 240, 1, 200]); | ||
}); |
@@ -287,2 +287,24 @@ const {Readable} = require('stream'); | ||
const appendResultFuncs = new Map([ | ||
[Object, AsIt.appendObject.gen], | ||
[Array, AsIt.appendArray.gen], | ||
[Set, AsIt.appendSet.gen], | ||
[Map, AsIt.appendMap.gen], | ||
]); | ||
AsIt.chain_(function appendResult(iter, to, ...args) { | ||
return (appendResultFuncs.get($.getClass(to)) || AsIt.appendObject.gen).call(this, iter, to, ...args); | ||
}); | ||
const toResultFuncs = new Map([ | ||
[Object, AsIt.toObject], | ||
[Array, AsIt.toArray], | ||
[Set, AsIt.toSet], | ||
[Map, AsIt.toMap], | ||
]); | ||
AsIt.value_(function toResult(iter, to, ...args) { | ||
return (toResultFuncs.get($.getClass(to)) || AsIt.toObject).call(this, iter, to, ...args); | ||
}); | ||
module.exports = AsIt; |
@@ -376,1 +376,28 @@ const cr = require('crypto'); | ||
}); | ||
class Custom { | ||
constructor(config) { Object.assign(this, config); } | ||
} | ||
test('AsIt_.appendResult: ', async () => { | ||
const iter = AsIt.from([1, 2, 3, 4]); | ||
const array = []; | ||
const set = new Set(); | ||
const object = {}; | ||
const map = new Map(); | ||
const custom = new Custom(); | ||
await iter.appendResult(array).appendResult(set).appendResult(object, 1).appendResult(map, 1).appendResult(custom, 1).exec(); | ||
expect(array).toEqual([1, 2, 3, 4]); | ||
expect(Array.from(set)).toEqual([1, 2, 3, 4]); | ||
expect(object).toEqual({1: 1, 2: 1, 3: 1, 4: 1}); | ||
expect(map).toEqual(new Map([[1, 1], [2, 1], [3, 1], [4, 1]])); | ||
expect(custom).toEqual({1: 1, 2: 1, 3: 1, 4: 1}); | ||
}); | ||
test('AsIt_.toResult: ', async () => { | ||
expect(await AsIt.from([1, 2, 3, 4]).toResult([])).toEqual([1, 2, 3, 4]); | ||
expect(await AsIt.from([1, 2, 3, 4]).toResult(new Set())).toEqual(new Set([1, 2, 3, 4])); | ||
expect(await AsIt.from([1, 2, 3, 4]).toResult($(), 1)).toEqual({1: 1, 2: 1, 3: 1, 4: 1}); | ||
expect(await AsIt.from([1, 2, 3, 4]).toResult(new Map(), 1)).toEqual(new Map([[1, 1], [2, 1], [3, 1], [4, 1]])); | ||
expect(await AsIt.from([1, 2, 3, 4]).toResult(new Custom(), 1)).toEqual({1: 1, 2: 1, 3: 1, 4: 1}); | ||
}); |
@@ -31,2 +31,16 @@ const $ = require('../base'); | ||
func_(function ifNull_(func) { | ||
return function _ifNull(value, ...args) { | ||
if (value == null) return func.call(this, value, ...args); | ||
return value; | ||
}; | ||
}); | ||
func_(function defNull_(def) { | ||
return function _defNull(value) { | ||
if (value == null) return def; | ||
return value; | ||
}; | ||
}); | ||
func_(function trueMap() { | ||
@@ -339,2 +353,12 @@ return true; | ||
func_(function mapper(item, ...args) { | ||
if (typeof item === 'function') return item.call(this, ...args); | ||
if (item instanceof Array && typeof item[1] === 'function') { | ||
item[1] = item[1].call(this, item[0], ...args); | ||
} | ||
return item; | ||
}); | ||
$.stop = Symbol('$.stop'); | ||
@@ -341,0 +365,0 @@ $.pass = Symbol('$.pass'); |
@@ -27,2 +27,16 @@ const $ = require('./map'); | ||
test('$.ifNull_: call function if argument is nullish', () => { | ||
const def = $.ifNull_(() => 5); | ||
expect(def(undefined)).toBe(5); | ||
expect(def(null)).toBe(5); | ||
expect(def(3)).toBe(3); | ||
}); | ||
test('$.defNull_: return default value if argument is nullish', () => { | ||
const def = $.defNull_(5); | ||
expect(def(undefined)).toBe(5); | ||
expect(def(null)).toBe(5); | ||
expect(def(3)).toBe(3); | ||
}); | ||
test('$.not: map boolean not', () => { | ||
@@ -29,0 +43,0 @@ expect([1, undefined, 0, 'a', null, {a: 1}, [5]].map($.not)).toEqual([false, true, true, false, true, false, false]); |
@@ -73,2 +73,25 @@ const $ = require('./map'); | ||
func_(function defMap(map, key, type) { | ||
let ex = map.get(key); | ||
if (ex) return ex; | ||
if (typeof type === 'function') ex = new type(); | ||
else if (typeof type === 'object') ex = Object.create(type); | ||
else ex = type; | ||
map.set(key, ex); | ||
return ex; | ||
}); | ||
func_(function defsMap(map, key, type) { | ||
let ex = map.get(key); | ||
if (ex) { | ||
if (typeof type === 'function' && typeof ex === 'object' && ex instanceof type) return ex; | ||
if (typeof type === 'object' && typeof ex === 'object' && Object.getPrototypeOf(ex) === type) return ex; | ||
} | ||
if (typeof type === 'function') ex = new type(); | ||
else if (typeof type === 'object') ex = Object.create(type); | ||
else ex = type; | ||
map.set(key, ex); | ||
return ex; | ||
}); | ||
func_(function unset(obj, ...walk) { | ||
@@ -75,0 +98,0 @@ const key = walk.pop(); |
@@ -217,2 +217,56 @@ const $ = require('./object'); | ||
test('defMap: set sticky value', () => { | ||
const m = new Map(); | ||
$.defMap(m, 'a', 1); | ||
expect(m).toEqual(new Map([['a', 1]])); | ||
$.defMap(m, 'a', 2); | ||
expect(m).toEqual(new Map([['a', 1]])); | ||
}); | ||
test('defsMap: redefine value', () => { | ||
const m = new Map(); | ||
$.defsMap(m, 'a', 1); | ||
expect(m).toEqual(new Map([['a', 1]])); | ||
$.defsMap(m, 'a', 2); | ||
expect(m).toEqual(new Map([['a', 2]])); | ||
}); | ||
test('defMap: class', () => { | ||
const m = new Map(); | ||
$.defMap(m, 'a', Array).push(1); | ||
expect(m).toEqual(new Map([['a', [1]]])); | ||
$.defMap(m, 'a', Array).push(2); | ||
expect(m).toEqual(new Map([['a', [1, 2]]])); | ||
expect($.defMap(m, 'a', Set) instanceof Array).toBe(true); | ||
}); | ||
test('defsMap: class', () => { | ||
const m = new Map(); | ||
$.defsMap(m, 'a', Array).push(1); | ||
expect(m).toEqual(new Map([['a', [1]]])); | ||
$.defsMap(m, 'a', Array).push(2); | ||
expect(m).toEqual(new Map([['a', [1, 2]]])); | ||
$.defsMap(m, 'a', Set).add(3); | ||
expect(m).toEqual(new Map([['a', new Set([3])]])); | ||
}); | ||
test('defMap: prototype', () => { | ||
const m = new Map(); | ||
$.defMap(m, 'a', null).a = 1; | ||
expect(m).toEqual(new Map([['a', {a: 1}]])); | ||
$.defMap(m, 'a', null).b = 2; | ||
expect(m).toEqual(new Map([['a', {a: 1, b: 2}]])); | ||
expect(Object.getPrototypeOf($.defMap(m, 'a', Set))).toBe(null); | ||
}); | ||
test('defsMap: prototype', () => { | ||
const m = new Map(); | ||
$.defsMap(m, 'a', null).a = 1; | ||
expect(m).toEqual(new Map([['a', {a: 1}]])); | ||
$.defsMap(m, 'a', null).b = 2; | ||
expect(m).toEqual(new Map([['a', {a: 1, b: 2}]])); | ||
$.defsMap(m, 'a', Set).add(3); | ||
expect(m).toEqual(new Map([['a', new Set([3])]])); | ||
}); | ||
test('unset: delete object key in walk', () => { | ||
@@ -219,0 +273,0 @@ const obj = {a: {m: 4, x: [{z: {v: 7}}]}}; |
@@ -34,5 +34,4 @@ const $ = require('../base'); | ||
func_(function accDate(to, from) { | ||
const msec = parseInt(from); | ||
if (Number.isNaN(msec)) to.setTime(from); | ||
else to.setTime(to.getTime() + msec); | ||
if (Number.isInteger(from)) to.setTime(to.getTime() + from); | ||
else to.setTime(from == null ? new Date() : new Date(from)); | ||
return to; | ||
@@ -39,0 +38,0 @@ }); |
@@ -27,3 +27,3 @@ const $ = require('./acc'); | ||
const src = new Date('2020-02-01'); | ||
const acc = $.accumulate(src, new Date('2020-01-01'), 1000); | ||
const acc = $.accumulate(src, '2020-01-01', 1000); | ||
expect(acc).toBe(src); | ||
@@ -33,2 +33,9 @@ expect(acc).toEqual(new Date('2020-01-01T00:00:01Z')); | ||
test('$.accumulate: now Date', () => { | ||
const src = new Date('2020-02-01'); | ||
const acc = $.accumulate(src, null, 1000); | ||
expect(acc).toBe(src); | ||
expect(acc.constructor).toBe(Date); | ||
}); | ||
test('$.accumulate: other ctor', () => { | ||
@@ -35,0 +42,0 @@ const src = new SyntaxError(); |
@@ -79,16 +79,24 @@ const $ = require('../func'); | ||
function getChunkFromCache(func, chunk) { | ||
const {cache} = func; | ||
const {cache, type} = func; | ||
const isArray = type === Array; | ||
let res, toOut; | ||
if (!cache.map) { | ||
cache.map = new Map(); | ||
return {res: Array(chunk.length), need: new Set(chunk)}; | ||
if (!cache.map) cache.map = new Map(); | ||
const need = new Map(); | ||
const iter = Iter.entries(chunk instanceof Array ? Iter.getIter(chunk) : chunk); | ||
if (isArray) { | ||
iter.mapKey($.counter_(0)); | ||
res = chunk instanceof Array ? Array(chunk.length) : []; | ||
toOut = Iter.toObject; | ||
} else { | ||
res = ($.accInit.get(type) || $)(); | ||
toOut = Iter.toResult; | ||
} | ||
const res = Array(chunk.length); | ||
const need = new Set(); | ||
for (const [idx, value] of chunk.entries()) { | ||
for (const [key, value] of iter) { | ||
const cached = getFromCache(func, value); | ||
if (cached) res[idx] = cached.res; | ||
else need.add(value); | ||
if (cached) toOut([[key, cached.res]], res); | ||
else $.defMap(need, value, Set).add(key); | ||
} | ||
@@ -107,5 +115,6 @@ | ||
const {cache} = func; | ||
const mapped = await mapper.call(this, need, chunk, desc); | ||
Iter.entries(chunk).filter(([, v]) => need.has(v)).mapValue(mapped).toObject(res); | ||
Iter.from(mapped).call(([k, v]) => cache.map.set(k, v)).exec(); | ||
const mapped = await mapper.call(this, need.keys(), chunk, desc); | ||
const iter = Iter.from(need).mapKey(mapped).maps(([k, v]) => Iter.entries(v).mapValue($._(k))); | ||
if (func.type === Array) iter.toObject(res); else iter.toResult(res); | ||
Iter.from(mapped).toMap(cache.map); | ||
checkOver(func); | ||
@@ -119,5 +128,6 @@ return res; | ||
const {cache} = func; | ||
const mapped = mapper.call(this, need, chunk, desc); | ||
Iter.entries(chunk).filter(([, v]) => need.has(v)).mapValue(mapped).toObject(res); | ||
Iter.from(mapped).call(([k, v]) => cache.map.set(k, v)).exec(); | ||
const mapped = mapper.call(this, need.keys(), chunk, desc); | ||
const iter = Iter.from(need).mapKey(mapped).maps(([k, v]) => Iter.entries(v).mapValue($._(k))); | ||
if (func.type === Array) iter.toObject(res); else iter.toResult(res); | ||
Iter.from(mapped).toMap(cache.map); | ||
checkOver(func); | ||
@@ -163,5 +173,6 @@ return res; | ||
function toChunkCache(mapper, ctx) { | ||
function toChunkCache(type, mapper, ctx) { | ||
if (!mapper) mapper = emulChunkMapper(this.cacheMapper); | ||
const func = getChunkCacheFunc(mapper, {ctx}); | ||
func.type = type; | ||
func.cache = this.cache; | ||
@@ -172,3 +183,3 @@ func.dropCache = dropCache; | ||
func_(function cacheChunk_(size, mapper) { | ||
func_(function cacheChunk_(size, mapper, type) { | ||
if (typeof mapper !== 'function') throw new TypeError('mapper is not function'); | ||
@@ -178,2 +189,3 @@ if (!Number.isFinite(size) || size <= 0) size = $.defaultCacheSize; | ||
const func = getChunkCacheFunc(mapper, desc); | ||
func.type = type; | ||
func.cache = {size, map: null}; | ||
@@ -180,0 +192,0 @@ func.cacheMapper = mapper; |
const $ = require('./cache'); | ||
const Iter = require('../iter'); | ||
const AsIt = require('../iter'); | ||
const AsIt = require('../as-it'); | ||
//test=$.echo; | ||
test('$.cacheChunk_: Array -> Object', () => { | ||
const c = $.cacheChunk_(2, chunk => Iter.entries(chunk).map($.invert).mapValue(v => v + 1).toObject()); | ||
expect(c([1, 2, 3])).toEqual({1: 2, 2: 3, 3: 4}); | ||
}); | ||
test('$.cacheChunk_: Set -> Object', () => { | ||
const c = $.cacheChunk_(2, chunk => Iter.entries(chunk).map($.invert).mapValue(v => v + 1).toObject()); | ||
expect(c(new Set([1, 2, 3]))).toEqual({1: 2, 2: 3, 3: 4}); | ||
}); | ||
test('$.cacheChunk_: Object -> Object', () => { | ||
const c = $.cacheChunk_(2, chunk => Iter.entries(chunk).map($.invert).mapValue(v => v + 1).toObject()); | ||
expect(c({a: 1, b: 2, c: 3})).toEqual({a: 2, b: 3, c: 4}); | ||
}); | ||
test('$.cacheChunk_: Map -> Object', () => { | ||
const c = $.cacheChunk_(2, chunk => Iter.entries(chunk).map($.invert).mapValue(v => v + 1).toObject()); | ||
expect(c(new Map([['a', 1], ['b', 2], ['c', 3]]))).toEqual({a: 2, b: 3, c: 4}); | ||
}); | ||
test('$.cacheChunk_: Array -> Array', () => { | ||
const c = $.cacheChunk_(2, chunk => Iter.entries(chunk).map($.invert).mapValue(v => v + 1).toObject(), Array); | ||
expect(c([1, 2, 3])).toEqual([2, 3, 4]); | ||
}); | ||
test('$.cacheChunk_: Set -> Array', () => { | ||
const c = $.cacheChunk_(2, chunk => Iter.entries(chunk).map($.invert).mapValue(v => v + 1).toObject(), Array); | ||
expect(c(new Set([1, 2, 3]))).toEqual([2, 3, 4]); | ||
}); | ||
test('$.cacheChunk_: Map -> Array: ignore keys', () => { | ||
const c = $.cacheChunk_(2, chunk => Iter.entries(chunk).map($.invert).mapValue(v => v + 1).toObject(), Array); | ||
expect(c(new Map([['a', 1], ['b', 2], ['c', 3]]))).toEqual([2, 3, 4]); | ||
}); | ||
test('$.cacheChunk_: Array -> Map', () => { | ||
const c = $.cacheChunk_(2, chunk => Iter.entries(chunk).map($.invert).mapValue(v => v + 1).toObject(), Map); | ||
expect(c([1, 2, 3])).toEqual(new Map([[1, 2], [2, 3], [3, 4]])); | ||
}); | ||
test('$.cacheChunk_: Set -> Map', () => { | ||
const c = $.cacheChunk_(2, chunk => Iter.entries(chunk).map($.invert).mapValue(v => v + 1).toObject(), Map); | ||
expect(c(new Set([1, 2, 3]))).toEqual(new Map([[1, 2], [2, 3], [3, 4]])); | ||
}); | ||
test('$.cacheChunk_: Object -> Map', () => { | ||
const c = $.cacheChunk_(2, chunk => Iter.entries(chunk).map($.invert).mapValue(v => v + 1).toObject(), Map); | ||
expect(c({a: 1, b: 2, c: 3})).toEqual(new Map([['a', 2], ['b', 3], ['c', 4]])); | ||
}); | ||
test('$.cache_: no mapper', () => { | ||
@@ -58,3 +112,3 @@ expect(() => $.cache_(-1)).toThrow('mapper is not function'); | ||
test('$.cacheChunk_: sync', () => { | ||
const inc = $.cacheChunk_(2, vs => Iter.from(vs).map(2).mapValue($.inc).toMap()); | ||
const inc = $.cacheChunk_(2, vs => Iter.from(vs).map(2).mapValue($.inc).toMap(), Array); | ||
expect(inc([1, 2, 3])).toEqual([2, 3, 4]); | ||
@@ -71,3 +125,3 @@ expect(Array.from(inc.cache.map)).toEqual([[2, 3], [3, 4]]); | ||
test('$.cacheChunk_: async', async () => { | ||
const inc = $.cacheChunk_(2, async vs => await AsIt.from(vs).map(2).mapValue($.inc).toMap()); | ||
const inc = $.cacheChunk_(2, async vs => await AsIt.from(vs).map(2).mapValue($.inc).toMap(), Array); | ||
const inced = inc([1, 2, 3]); | ||
@@ -88,3 +142,3 @@ expect(inced instanceof Promise).toBe(true); | ||
inc(1); inc(3); inc(-1); | ||
const incs = inc.toChunkCache(); | ||
const incs = inc.toChunkCache(Array); | ||
expect(incs([2, 1])).toEqual([3, 2]); | ||
@@ -96,3 +150,3 @@ }); | ||
await inc(1); await inc(3); await inc(-1); | ||
const incs = inc.toChunkCache(); | ||
const incs = inc.toChunkCache(Array); | ||
expect(await incs([2, 1])).toEqual([3, 2]); | ||
@@ -99,0 +153,0 @@ }); |
@@ -98,2 +98,8 @@ const $ = require('../func'); | ||
func_(function bound_(func) { | ||
return function _bound(...args) { | ||
return func.bind(this, ...args); | ||
} | ||
}); | ||
//TODO: bindJob in ClAs | ||
@@ -100,0 +106,0 @@ |
@@ -48,2 +48,11 @@ const Emitter = require('events'); | ||
test('$.bound_: bound function mapping', () => { | ||
const res = []; | ||
const binder = $.bound_((a, b, c, d) => res.push(a, b, c, d)); | ||
const bound = binder(1, 2); | ||
expect(bound(3, 4)).toBe(4); | ||
expect(bound(5, 6)).toBe(8); | ||
expect(res).toEqual([1, 2, 3, 4, 1, 2, 5, 6]); | ||
}); | ||
test('$.all: ', async () => { | ||
@@ -50,0 +59,0 @@ const all = await $.all([1, Promise.resolve(2)]); |
@@ -153,4 +153,4 @@ const $ = require('./index'); | ||
test('Iter_.race: ', async () => { | ||
const msec = $.AsIt.from([60, 40, 20]); | ||
test('Iter_.race: external mapping', async () => { | ||
const msec = $.AsIt.from([240, 160, 200]); | ||
@@ -160,7 +160,21 @@ const arr = await msec.Iter.from(await msec.toArray()).map(async (msec) => { | ||
return msec; | ||
}).concat(async () => { $.delayMsec(30); return 30; }).race(2).map($.inValue).toArray(); | ||
}).concat([async () => { | ||
await $.delayMsec(1); | ||
return 1; | ||
}]).map($.mapper).race(2).map($.inValue).toArray(); | ||
expect(arr).toEqual([40, 20, 30, 60]); | ||
expect(arr).toEqual([160, 240, 1, 200]); | ||
}); | ||
test('Iter_.race: internal mapping', async () => { | ||
const msec = $.Iter.from([240, 160, 200, 1]); | ||
const arr = await msec.race(2, async (msec) => { | ||
await $.delayMsec(msec); | ||
return msec; | ||
}).map($.inValue).toArray(); | ||
expect(arr).toEqual([160, 240, 1, 200]); | ||
}); | ||
test('Iter_.map: _mappingFuncs', () => { | ||
@@ -167,0 +181,0 @@ const m = new Map([[1, 'a'], [2, 'b'], [3, 'c']]); |
@@ -135,2 +135,17 @@ const it = require('../it'); | ||
function* iterPartial(iter) { | ||
let cur; | ||
while (!(cur = iter.next()).done) yield cur.value; | ||
} | ||
Iter.value_(function partial(iter) { | ||
const partial = iterPartial(iter); | ||
return new Iter(partial); | ||
}); | ||
Iter_.init = function init(id, value) { | ||
this[id] = value; | ||
return this; | ||
}; | ||
Iter.chain_(function* save(iter, id) { | ||
@@ -137,0 +152,0 @@ for (const item of iter) { |
@@ -184,2 +184,28 @@ const $ = require('../base'); | ||
test('Iter_.partial: sequential iteration', () => { | ||
const iter = new Iter([1, 2, 3, 4]); | ||
const part = iter.partial(); | ||
let n = 2; | ||
for (const item of part) if (!--n) break; | ||
expect(Array.from(iter)).toEqual([3, 4]); | ||
}); | ||
test('Iter_.partial: parallel iteration', () => { | ||
const iter = new Iter([1, 2, 3, 4]); | ||
const part = iter.partial(); | ||
expect(part.read()).toEqual(1); | ||
expect(iter.read()).toEqual(2); | ||
expect(part.read()).toEqual(3); | ||
expect(iter.read()).toEqual(4); | ||
expect(part.read()).toEqual($.eof); | ||
expect(iter.read()).toEqual($.eof); | ||
}); | ||
test('Iter_.init: initial set var', () => { | ||
const iter = new Iter([1, 2, 3, 4]); | ||
iter.init('a', 2).init('b', 3); | ||
expect(iter.a).toBe(2); | ||
expect(iter.b).toBe(3); | ||
}); | ||
test('Iter_.get, .set: get/set wrapped iterator', () => { | ||
@@ -186,0 +212,0 @@ const iter = new Iter([1, 2]); |
@@ -276,2 +276,24 @@ const {Readable} = require('stream'); | ||
const appendResultFuncs = new Map([ | ||
[Object, Iter.appendObject.gen], | ||
[Array, Iter.appendArray.gen], | ||
[Set, Iter.appendSet.gen], | ||
[Map, Iter.appendMap.gen], | ||
]); | ||
Iter.chain_(function appendResult(iter, to, ...args) { | ||
return (appendResultFuncs.get($.getClass(to)) || Iter.appendObject.gen).call(this, iter, to, ...args); | ||
}); | ||
const toResultFuncs = new Map([ | ||
[Object, Iter.toObject], | ||
[Array, Iter.toArray], | ||
[Set, Iter.toSet], | ||
[Map, Iter.toMap], | ||
]); | ||
Iter.value_(function toResult(iter, to, ...args) { | ||
return (toResultFuncs.get($.getClass(to)) || Iter.toObject).call(this, iter, to, ...args); | ||
}); | ||
module.exports = Iter; |
@@ -354,1 +354,28 @@ const cr = require('crypto'); | ||
}); | ||
class Custom { | ||
constructor(config) { Object.assign(this, config); } | ||
} | ||
test('Iter_.appendResult: ', () => { | ||
const iter = Iter.from([1, 2, 3, 4]); | ||
const array = []; | ||
const set = new Set(); | ||
const object = {}; | ||
const map = new Map(); | ||
const custom = new Custom(); | ||
iter.appendResult(array).appendResult(set).appendResult(object, 1).appendResult(map, 1).appendResult(custom, 1).exec(); | ||
expect(array).toEqual([1, 2, 3, 4]); | ||
expect(Array.from(set)).toEqual([1, 2, 3, 4]); | ||
expect(object).toEqual({1: 1, 2: 1, 3: 1, 4: 1}); | ||
expect(map).toEqual(new Map([[1, 1], [2, 1], [3, 1], [4, 1]])); | ||
expect(custom).toEqual({1: 1, 2: 1, 3: 1, 4: 1}); | ||
}); | ||
test('Iter_.toResult: ', () => { | ||
expect(Iter.from([1, 2, 3, 4]).toResult([])).toEqual([1, 2, 3, 4]); | ||
expect(Iter.from([1, 2, 3, 4]).toResult(new Set())).toEqual(new Set([1, 2, 3, 4])); | ||
expect(Iter.from([1, 2, 3, 4]).toResult($(), 1)).toEqual({1: 1, 2: 1, 3: 1, 4: 1}); | ||
expect(Iter.from([1, 2, 3, 4]).toResult(new Map(), 1)).toEqual(new Map([[1, 1], [2, 1], [3, 1], [4, 1]])); | ||
expect(Iter.from([1, 2, 3, 4]).toResult(new Custom(), 1)).toEqual({1: 1, 2: 1, 3: 1, 4: 1}); | ||
}); |
{ | ||
"name": "asclasit", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "ASync CLasses + ASync ITerators", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
357041
10252