extra-iterable
Advanced tools
Comparing version 2.0.29 to 2.0.30
507
index.js
/** | ||
* Breaks iterable into chunks of given size. | ||
* @param {Iterable} x an iterable | ||
* @param {number?} n chunk size | ||
* @param {number?} n chunk size (1) | ||
* @returns {Iterable<Array>} | ||
@@ -16,2 +16,8 @@ */ | ||
} | ||
/** | ||
* Compares two values. | ||
* @param {*} a a value | ||
* @param {*} b another value | ||
* @returns {number} a<b: -1, a=b: 0, a>b: 1 | ||
*/ | ||
function cmp(a, b) { | ||
@@ -49,14 +55,146 @@ return a<b? -1:(a>b? 1:0); | ||
/** | ||
* Compares two values. | ||
* @param {*} a a value | ||
* @param {*} b another value | ||
* @returns {number} a<b: -1, a=b: 0, a>b: 1 | ||
*/ | ||
function cmp4(a, b) { | ||
return a<b? -1:(a>b? 1:0); | ||
} | ||
const cmp5 = cmp4; | ||
/** | ||
* Counts occurrences of a value. | ||
* @param {Array} x an array | ||
* @param {*} v value | ||
* @param {function?} fn compare function (a, b) | ||
* @returns {number} occurrences | ||
*/ | ||
function count(x, v, fn=null) { | ||
var fn = fn||cmp5, n = 0; | ||
for(var u of x) | ||
if(fn(u, v)===0) n++; | ||
return n; | ||
} | ||
/** | ||
* Gives same value. | ||
* @param {*} v a value | ||
* @returns {*} v | ||
*/ | ||
function id(v) { | ||
return v; | ||
} | ||
/** | ||
* Counts occurrences of values. | ||
* @param {Array} x an array | ||
* @param {function?} fn map function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {Map<any, number>} occurrences | ||
*/ | ||
function countAllOn(x, fn=null, ths=null) { | ||
var fn = fn||id; | ||
var m = new Map(), i = -1; | ||
for(var v of x) { | ||
var v1 = fn.call(ths, v, i, x); | ||
m.set((m.get(v1)||0) + 1); | ||
} | ||
return m; | ||
} | ||
/** | ||
* Gives same value. | ||
* @param {*} v a value | ||
* @returns {*} v | ||
*/ | ||
function id8(v) { | ||
return v; | ||
} | ||
const id9 = id8; | ||
/** | ||
* Counts occurrences of a value. | ||
* @param {Array} x an array | ||
* @param {*} v value | ||
* @param {function?} fn map function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {number} occurrences | ||
*/ | ||
function countOn(x, v, fn=null, ths=null) { | ||
var fn = fn||id9, i = -1, n = 0; | ||
var v1 = fn.call(ths, v, 0, null); | ||
for(var u of x) { | ||
var u1 = fn.call(ths, u, ++i, x); | ||
if(u1===v1) n++; | ||
} | ||
return n; | ||
} | ||
/** | ||
* Gives values that cycle through an iterable. | ||
* @param {Iterable} x an iterable | ||
* @param {number} n number of values | ||
* @param {number?} n number of values (-1 => Inf) | ||
* @returns {Iterable} | ||
*/ | ||
function* cycle(x, n) { | ||
while(true) for(var v of x) { | ||
if(--n<0) break; | ||
yield v; | ||
function* cycle(x, n=-1) { | ||
w: while(true) { | ||
for(var v of x) { | ||
if(n--===0) break w; | ||
yield v; | ||
} | ||
} | ||
} | ||
/** | ||
* Gives values of an array not present in another. | ||
* @param {Iterable} x an iterable | ||
* @param {Iterable} y another iterable | ||
* @param {function?} fn compare function (a, b) | ||
* @returns {Iterable} | ||
*/ | ||
function* difference(x, y, fn=null) { | ||
var fn = fn||cmp; | ||
x: for(var u of x) { | ||
for(var v of y) | ||
if(fn(u, v)===0) continue x; | ||
yield u; | ||
} | ||
} | ||
/** | ||
* Gets unique set of values. | ||
* @param {Iterable} x an iterable | ||
* @param {function?} fn map function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {Set} | ||
*/ | ||
function uniques(x, fn=null, ths=null) { | ||
if(!fn) return new Set(x); | ||
var s = new Set(), i = -1; | ||
for(var v of x) | ||
s.add(fn.call(ths, v, ++i, x)); | ||
return s; | ||
} | ||
/** | ||
* Gives same value. | ||
* @param {*} v a value | ||
* @returns {*} v | ||
*/ | ||
function id13(v) { | ||
return v; | ||
} | ||
const id14 = id13; | ||
/** | ||
* Gives values of an array not present in another. | ||
* @param {Iterable} x an array | ||
* @param {Iterable} y another array | ||
* @param {function?} fn map function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {Iterable} | ||
*/ | ||
function* differenceOn(x, y, fn=null, ths=null) { | ||
var s = uniques(y, fn, ths); | ||
var fn = fn||id14, i = -1; | ||
for(var u of x) { | ||
var u1 = fn.call(ths, u, ++i, x); | ||
if(!s.has(u1)) yield u; | ||
} | ||
} | ||
/** | ||
* Checks if all values satisfy a test. | ||
@@ -167,2 +305,21 @@ * @param {Iterable} x an iterable | ||
/** | ||
* Keeps similar values together and in order. | ||
* @param {Iterable} x an iterable | ||
* @param {function?} fn map function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {Iterable<Array>} | ||
*/ | ||
function* groupOn(x, fn=null, ths=null) { | ||
var fn = fn||id; | ||
var a = [], i = -1; | ||
var u1 = fn.call(ths, x[0], 0, x); | ||
for(var v of x) { | ||
var v1 = fn.call(ths, v, ++i, x); | ||
if(u1===v1) a.push(v); | ||
else { yield a; a = [v]; } | ||
u1 = v1; | ||
} | ||
yield a; | ||
} | ||
/** | ||
* Gets first value. | ||
@@ -189,2 +346,100 @@ * @param {Iterable} x an iterable | ||
/** | ||
* Gives values of an iterable present in another. | ||
* @param {Iterable} x an iterable | ||
* @param {Iterable} y another iterable | ||
* @param {function?} fn compare function (a, b) | ||
* @returns {Iterable} | ||
*/ | ||
function* intersection(x, y, fn=null) { | ||
var fn = fn||cmp; | ||
x: for(var u of x) { | ||
for(var v of y) | ||
if(fn(u, v)===0) { yield u; continue x; } | ||
} | ||
} | ||
/** | ||
* Gives values of an iterable present in another. | ||
* @param {Iterable} x an iterable | ||
* @param {Iterable} y another iterable | ||
* @param {function?} fn map function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {Iterable} | ||
*/ | ||
function* intersectionOn(x, y, fn=null, ths=null) { | ||
var s = uniques(y, fn, ths); | ||
var fn = fn||id, i = -1; | ||
for(var u of x) { | ||
var u1 = fn.call(ths, u, ++i, x); | ||
if(s.has(u1)) yield u; | ||
} | ||
} | ||
/** | ||
* Compares two values. | ||
* @param {*} a a value | ||
* @param {*} b another value | ||
* @returns {number} a<b: -1, a=b: 0, a>b: 1 | ||
*/ | ||
function cmp29(a, b) { | ||
return a<b? -1:(a>b? 1:0); | ||
} | ||
const cmp30 = cmp29; | ||
/** | ||
* Checks if arrays have no value in common. | ||
* @param {Array} x an array | ||
* @param {Array} y another array | ||
* @param {function?} fn compare function (a, b) | ||
* @returns {boolean} true if disjoint | ||
*/ | ||
function isDisjoint(x, y, fn=null) { | ||
var fn = fn||cmp30; | ||
for(var v of y) { | ||
for(var u of x) | ||
if(fn(u, v)===0) return false; | ||
} | ||
return true; | ||
} | ||
/** | ||
* Gets unique set of values. | ||
* @param {Array} x an array | ||
* @param {function?} fn map function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {Set} | ||
*/ | ||
function uniques31(x, fn=null, ths=null) { | ||
if(!fn) return new Set(x); | ||
var s = new Set(), i = -1; | ||
for(var v of x) | ||
s.add(fn.call(ths, v, ++i, x)); | ||
return s; | ||
} | ||
/** | ||
* Gives same value. | ||
* @param {*} v a value | ||
* @returns {*} v | ||
*/ | ||
function id32(v) { | ||
return v; | ||
} | ||
const uniques33 = uniques31; | ||
const id33 = id32; | ||
/** | ||
* Checks if arrays have no value in common. | ||
* @param {Array} x an array | ||
* @param {Array} y another array | ||
* @param {function?} fn map function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {boolean} true if disjoint | ||
*/ | ||
function isDisjointOn(x, y, fn=null, ths=null) { | ||
var s = uniques33(x, fn, ths); | ||
var fn = fn||id33, i = -1; | ||
for(var v of y) { | ||
var v1 = fn.call(ths, v, ++i, y); | ||
if(s.has(v1)) return false; | ||
} | ||
return true; | ||
} | ||
/** | ||
* Checks if two iterables are equal. | ||
@@ -233,2 +488,195 @@ * @param {Iterable} x an iterable | ||
/** | ||
* Compares two values. | ||
* @param {*} a a value | ||
* @param {*} b another value | ||
* @returns {number} a<b: -1, a=b: 0, a>b: 1 | ||
*/ | ||
function cmp38(a, b) { | ||
return a<b? -1:(a>b? 1:0); | ||
} | ||
const cmp39 = cmp38; | ||
/** | ||
* Finds largest value. | ||
* @param {Array} x an array | ||
* @param {function?} fn compare function (a, b) | ||
* @returns {*} | ||
*/ | ||
function max(x, fn=null) { | ||
var fn = fn||cmp39, m = x[0]; | ||
for(var v of x) | ||
if(fn(v, m)>0) m = v; | ||
return m; | ||
} | ||
/** | ||
* Gives same value. | ||
* @param {*} v a value | ||
* @returns {*} v | ||
*/ | ||
function id40(v) { | ||
return v; | ||
} | ||
const id41 = id40; | ||
/** | ||
* Finds largest value. | ||
* @param {Array} x an array | ||
* @param {function?} fn map function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {*} | ||
*/ | ||
function maxOn(x, fn=null, ths=null) { | ||
var fn = fn||id41, i = -1; | ||
var mk = fn.call(ths, x[0], 0, x), mv = x[0]; | ||
for(var v of x) { | ||
var k = fn.call(ths, v, ++i, x); | ||
if(k>mk) { mk = k; mv = v; } | ||
} | ||
return mv; | ||
} | ||
/** | ||
* Compares two values. | ||
* @param {*} a a value | ||
* @param {*} b another value | ||
* @returns {number} a<b: -1, a=b: 0, a>b: 1 | ||
*/ | ||
function cmp42(a, b) { | ||
return a<b? -1:(a>b? 1:0); | ||
} | ||
const cmp43 = cmp42; | ||
/** | ||
* Finds smallest value. | ||
* @param {Array} x an array | ||
* @param {function?} fn compare function (a, b) | ||
* @returns {*} | ||
*/ | ||
function min(x, fn=null) { | ||
var fn = fn||cmp43, m = x[0]; | ||
for(var v of x) | ||
if(fn(v, m)<0) m = v; | ||
return m; | ||
} | ||
/** | ||
* Gives same value. | ||
* @param {*} v a value | ||
* @returns {*} v | ||
*/ | ||
function id44(v) { | ||
return v; | ||
} | ||
const id45 = id44; | ||
/** | ||
* Finds smallest value. | ||
* @param {Array} x an array | ||
* @param {function?} fn map function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {*} | ||
*/ | ||
function minOn(x, fn=null, ths=null) { | ||
var fn = fn||id45, i = -1; | ||
var mk = fn.call(ths, x[0], 0, x), mv = x[0]; | ||
for(var v of x) { | ||
var k = fn.call(ths, v, ++i, x); | ||
if(k<mk) { mk = k; mv = v; } | ||
} | ||
return mv; | ||
} | ||
/** | ||
* Breaks array into values, by test. | ||
* @param {Array} x an array | ||
* @param {function} fn test function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {Array<Array>} [[...satisfies], [...doesnt]] | ||
*/ | ||
function partition(x, fn, ths=null) { | ||
var t = [], f = [], i = -1; | ||
for(var v of x) { | ||
if(fn.call(ths, v, ++i, x)) t.push(v); | ||
else f.push(v); | ||
} | ||
return [t, f]; | ||
} | ||
/** | ||
* Gives same value. | ||
* @param {*} v a value | ||
* @returns {*} v | ||
*/ | ||
function id47(v) { | ||
return v; | ||
} | ||
const id48 = id47; | ||
/** | ||
* Breaks array into values, by map. | ||
* @param {Array} x an array | ||
* @param {function} fn map function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {Map<any, Array>} {key => [...values]} | ||
*/ | ||
function partitionOn(x, fn=null, ths=null) { | ||
var fn = fn||id48; | ||
var m = new Map(), i = -1; | ||
for(var v of x) { | ||
var v1 = fn.call(ths, v, ++i, x); | ||
if(!m.has(v1)) m.set(v1, []); | ||
m.get(v1).push(v); | ||
} | ||
return m; | ||
} | ||
/** | ||
* Compares two values. | ||
* @param {*} a a value | ||
* @param {*} b another value | ||
* @returns {number} a<b: -1, a=b: 0, a>b: 1 | ||
*/ | ||
function cmp49(a, b) { | ||
return a<b? -1:(a>b? 1:0); | ||
} | ||
const cmp50 = cmp49; | ||
/** | ||
* Finds smallest and largest values. | ||
* @param {Array} x an array | ||
* @param {function?} fn compare function (a, b) | ||
* @returns {Array} [min, max] | ||
*/ | ||
function range(x, fn=null) { | ||
var fn = fn||cmp50, m = x[0], n = m; | ||
for(var v of x) { | ||
if(fn(v, m)<0) m = v; | ||
if(fn(v, n)>0) n = v; | ||
} | ||
return [m, n]; | ||
} | ||
/** | ||
* Gives same value. | ||
* @param {*} v a value | ||
* @returns {*} v | ||
*/ | ||
function id51(v) { | ||
return v; | ||
} | ||
const id52 = id51; | ||
/** | ||
* Finds smallest and largest values. | ||
* @param {Array} x an array | ||
* @param {function?} fn map function (v, i, x) | ||
* @param {object?} ths this argument | ||
* @returns {Array} [min, max] | ||
*/ | ||
function rangeOn(x, fn=null, ths=null) { | ||
var fn = fn||id52, i = -1; | ||
var mk = fn.call(ths, x[0], 0, x), mv = x[0]; | ||
var nk = mk, nv = mv; | ||
for(var v of x) { | ||
var k = fn.call(ths, v, ++i, x); | ||
if(k<mk) { mk = k; mv = v; } | ||
if(k>nk) { nk = k; nv = v; } | ||
} | ||
return [mv, nv]; | ||
} | ||
/** | ||
* Reduces values to a single value. | ||
@@ -251,7 +699,7 @@ * @param {Iterable} x an iterable | ||
* @param {Iterable} x an iterable | ||
* @param {number} n times | ||
* @param {number} n times (-1 => Inf) | ||
* @returns {Iterable} | ||
*/ | ||
function* repeat(x, n) { | ||
for(; n>=0; n--) | ||
for(; n!==0; n--) | ||
yield* x; | ||
@@ -271,2 +719,15 @@ } | ||
/** | ||
* Gets part of an iterable. | ||
* @param {Iterable} x an iterable | ||
* @param {number?} i begin index (0) | ||
* @param {number?} I end index (end) | ||
*/ | ||
function* slice(x, i=0, I=Number.MAX_SAFE_INTEGER) { | ||
var j = -1; | ||
for(var v of x) { | ||
if(++j>=I) break; | ||
if(j>=i) yield v; | ||
} | ||
} | ||
/** | ||
* Checks if any value satisfies a test. | ||
@@ -309,2 +770,7 @@ * @param {Iterable} x an iterable | ||
} | ||
/** | ||
* Gives passed values as array. | ||
* @param {...any} vs values | ||
* @returns {Array} | ||
*/ | ||
function args(...vs) { | ||
@@ -333,3 +799,8 @@ return vs; | ||
exports.concat = concat; | ||
exports.count = count; | ||
exports.countAllOn = countAllOn; | ||
exports.countOn = countOn; | ||
exports.cycle = cycle; | ||
exports.difference = difference; | ||
exports.differenceOn = differenceOn; | ||
exports.every = every; | ||
@@ -342,6 +813,11 @@ exports.filter = filter; | ||
exports.group = group; | ||
exports.groupOn = groupOn; | ||
exports.head = head; | ||
exports.init = require('./init'); | ||
exports.init = init; | ||
exports.intersection = intersection; | ||
exports.intersectionOn = intersectionOn; | ||
exports.is = is; | ||
exports.isDisjoint = isDisjoint; | ||
exports.isDisjointOn = isDisjointOn; | ||
exports.isEqual = isEqual; | ||
exports.is = is; | ||
exports.isList = isList; | ||
@@ -351,5 +827,14 @@ exports.join = join; | ||
exports.map = map; | ||
exports.max = max; | ||
exports.maxOn = maxOn; | ||
exports.min = min; | ||
exports.minOn = minOn; | ||
exports.partition = partition; | ||
exports.partitionOn = partitionOn; | ||
exports.range = range; | ||
exports.rangeOn = rangeOn; | ||
exports.reduce = reduce; | ||
exports.repeat = require('./repeat'); | ||
exports.repeat = repeat; | ||
exports.size = size; | ||
exports.slice = slice; | ||
exports.some = some; | ||
@@ -356,0 +841,0 @@ exports.split = split; |
{ | ||
"name": "extra-iterable", | ||
"version": "2.0.29", | ||
"version": "2.0.30", | ||
"description": "Standard utility methods for Iterable.", | ||
@@ -30,6 +30,19 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@extra-array/count": "^2.4.4", | ||
"@extra-array/count-all-on": "^2.4.4", | ||
"@extra-array/count-on": "^2.4.4", | ||
"@extra-array/is-disjoint": "^2.4.4", | ||
"@extra-array/is-disjoint-on": "^2.4.4", | ||
"@extra-array/max": "^2.4.4", | ||
"@extra-array/max-on": "^2.4.4", | ||
"@extra-array/min": "^2.4.4", | ||
"@extra-array/min-on": "^2.4.4", | ||
"@extra-array/partition": "^2.4.4", | ||
"@extra-array/partition-on": "^2.4.4", | ||
"@extra-array/range": "^2.4.4", | ||
"@extra-array/range-on": "^2.4.4", | ||
"find-npm-prefix": "^1.0.2", | ||
"recast": "^0.18.7", | ||
"tempy": "^0.4.0" | ||
"tempy": "^0.5.0" | ||
} | ||
} |
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
24695
820
16