extra-iterable
Advanced tools
Comparing version 1.0.6 to 1.0.7
647
index.js
@@ -1,2 +0,59 @@ | ||
// 0. iterable-all (all) | ||
function is(v) { | ||
return v!=null && typeof v[Symbol.iterator]==='function'; | ||
} | ||
function isCollection(a) { | ||
return is(a) && typeof a!=='string'; | ||
} | ||
function equal(i1, i2) { | ||
if(i1===i2) return true; | ||
var f2 = i2[Symbol.iterator](); | ||
for(var v1 of i1) { | ||
var s2 = f2.next(); | ||
if(v1!=s2.value || s2.done) return false; | ||
} | ||
return true; | ||
} | ||
function size(itr) { | ||
var z = 0; | ||
if(itr.size!=null) return itr.size; | ||
if(itr.length!=null) return itr.length; | ||
for(var v of itr) | ||
z++; | ||
return z; | ||
} | ||
function first(itr, idx=0) { | ||
var i = -1; | ||
for(var v of itr) | ||
if(++i===idx) return v; | ||
} | ||
function last(itr, idx=-1) { | ||
var i = -1, a = []; | ||
for(var v of itr) | ||
a[++i % idx] = v; | ||
return a[++i % idx]; | ||
} | ||
function middle(itr, idx=0) { | ||
return idx<0? last(itr, idx):first(itr, idx); | ||
} | ||
function keys(itr) { | ||
var i = 0, z = []; | ||
for(var v of itr) | ||
z.push(i++); | ||
return z; | ||
} | ||
function entries(itr) { | ||
var z = [], i = 0; | ||
for(var v of itr) | ||
z.push([i++, v]); | ||
return z; | ||
} | ||
function any(itr, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
if(v) return true; | ||
} | ||
return false; | ||
} | ||
function all(itr, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
@@ -10,5 +67,16 @@ var i = -1; | ||
return true; | ||
}; | ||
// 1. iterable-any (any) | ||
function any(itr, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
} | ||
function from(a) { | ||
if(a==null || typeof a[Symbol.iterator]==='function') return a; | ||
return typeof a==='object'? Object.entries(a):[a]; | ||
} | ||
function startsWith(itr, val) { | ||
for(var v of itr) | ||
return v===val; | ||
} | ||
function endsWith(itr, val) { | ||
for(var v of itr); | ||
return v===val; | ||
} | ||
function indexOf(itr, val, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
@@ -18,19 +86,25 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
if(v) return true; | ||
if(v===val) return i; | ||
} | ||
return false; | ||
}; | ||
// 2. iterable-average (average) | ||
function average(itr, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1, s = 0, n = 0; | ||
return -1; | ||
} | ||
function indicesOf(itr, val, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
s += v; | ||
++n; | ||
if(v===val) z[z0++] = i; | ||
} | ||
return n>0? s/n:0; | ||
}; | ||
// 3. iterable-compact (compact) | ||
function compact(itr, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
return z; | ||
} | ||
function lastIndexOf(itr, val, bgn=Number.MAX_SAFE_INTEGER, end=-1) { | ||
var i = -1, z = -1; | ||
for(var v of itr) { | ||
if(++i<=end) continue; | ||
if(i>bgn) break; | ||
if(v===val) z = i; | ||
} | ||
return z; | ||
} | ||
function includes(itr, val, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
@@ -40,16 +114,6 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
if(v) z[z0++] = v; | ||
if(v===val) return true; | ||
} | ||
return z; | ||
}; | ||
// 4. iterable-concat (concat) | ||
function concat() { | ||
var z = [], z0 = 0; | ||
for(var i=0, I=arguments.length; i<I; i++) { | ||
for(var v of arguments[i]) | ||
z[z0++] = v; | ||
} | ||
return z; | ||
}; | ||
// 5. iterable-contains (contains) | ||
return false; | ||
} | ||
function contains(i1, i2) { | ||
@@ -66,46 +130,13 @@ var a2 = Array.from(i2); | ||
return false; | ||
}; | ||
// 6. iterable-endswith (endsWith) | ||
function endsWith(itr, val) { | ||
for(var v of itr); | ||
return v===val; | ||
}; | ||
// 7. iterable-entries (entries) | ||
function entries(itr) { | ||
var z = [], i = 0; | ||
for(var v of itr) | ||
z.push([i++, v]); | ||
return z; | ||
}; | ||
// 8. iterable-equal (equal) | ||
function equal(i1, i2) { | ||
if(i1===i2) return true; | ||
var f2 = i2[Symbol.iterator](); | ||
for(var v1 of i1) { | ||
var s2 = f2.next(); | ||
if(v1!=s2.value || s2.done) return false; | ||
} | ||
return true; | ||
}; | ||
// 9. iterable-euclideandistance (euclideanDistance) | ||
function euclideanDistance(i1, i2) { | ||
var I2 = i2[Symbol.iterator](), z = 0; | ||
for(var v1 of i1) { | ||
var v2 = I2.next().value; | ||
z += (v1-v2)*(v1-v2); | ||
} | ||
return Math.sqrt(z); | ||
}; | ||
// 10. iterable-everyof (everyOf) | ||
function everyOf(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
} | ||
function join(itr, sep=',', bgn=0, end=Number.MAX_SAFE_INTEGER, z='') { | ||
var i = -1, zl0 = z.length; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
if(!fn.call(ths, v, i, itr)) return false; | ||
z += v+sep; | ||
} | ||
return true; | ||
}; | ||
// 11. iterable-filter (filter) | ||
function filter(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
return z.length>zl0? z.substring(0, z.length-sep.length):z; | ||
} | ||
function pick(itr, idx, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1; | ||
@@ -115,17 +146,22 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
if(fn.call(ths, v, i, itr)) z[z0++] = v; | ||
var ix = idx.indexOf(i); | ||
if(ix>=0) z[z0+ix] = v; | ||
} | ||
return z; | ||
}; | ||
// 12. iterable-find (find) | ||
function find(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
} | ||
function slice(itr, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1, z0 = z0<0? z.length+z0:z0; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
if(fn.call(ths, v, i, itr)) return v; | ||
z[z0++] = v; | ||
} | ||
}; | ||
// 13. iterable-findall (findAll) | ||
function findAll(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
return z; | ||
} | ||
function pickAs(itr, idx, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
if(idx==null) return slice(itr, bgn, end, z, z0); | ||
if(Array.isArray(idx)) return pick(itr, idx, bgn, end, z, z0); | ||
return pick(itr, [idx], bgn, end, z, z0)[z0]; | ||
} | ||
function compact(itr, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1; | ||
@@ -135,8 +171,7 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
if(fn.call(ths, v, i, itr)) z[z0++] = v; | ||
if(v) z[z0++] = v; | ||
} | ||
return z; | ||
}; | ||
// 14. iterable-findallindices (findAllIndices) | ||
function findAllIndices(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
} | ||
function without(itr, rmv, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1; | ||
@@ -146,30 +181,21 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
if(fn.call(ths, v, i, itr)) z[z0++] = i; | ||
if(!rmv.has(v)) z[z0++] = v; | ||
} | ||
return z; | ||
}; | ||
// 15. iterable-findindex (findIndex) | ||
function findIndex(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
if(fn.call(ths, v, i, itr)) return i; | ||
} | ||
function concat() { | ||
var z = [], z0 = 0; | ||
for(var i=0, I=arguments.length; i<I; i++) { | ||
for(var v of arguments[i]) | ||
z[z0++] = v; | ||
} | ||
return -1; | ||
}; | ||
// 16. iterable-first (first) | ||
function first(itr, idx=0) { | ||
var i = -1; | ||
for(var v of itr) | ||
if(++i===idx) return v; | ||
}; | ||
// 17. iterable-flatten (flatten) | ||
function flattenAll20(itr, lvl, z, z0) { | ||
return z; | ||
} | ||
function flattenAll(itr, lvl, z, z0) { | ||
for(var v of itr) { | ||
if(lvl!==0 && is(v)) z0 = flattenAll20(v, lvl-1, z, z0); | ||
if(lvl!==0 && is(v)) z0 = flattenAll(v, lvl-1, z, z0); | ||
else z[z0++] = v; | ||
} | ||
return z0; | ||
}; | ||
} | ||
function flatten(itr, lvl=-1, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
@@ -180,33 +206,20 @@ var i = -1; | ||
if(i>=end) break; | ||
if(lvl!==0 && is(v)) z0 = flattenAll20(v, lvl-1, z, z0); | ||
if(lvl!==0 && is(v)) z0 = flattenAll(v, lvl-1, z, z0); | ||
else z[z0++] = v; | ||
} | ||
return z; | ||
}; | ||
// 18. iterable-foreach (forEach) | ||
function forEach(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
} | ||
function zip(itr, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1, a = 0; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
fn.call(ths, v, i, itr); | ||
var b = z0-1; | ||
for(var u of v) | ||
(z[++b] = z[b]||[])[a] = u; | ||
a++; | ||
} | ||
}; | ||
// 19. iterable-from (from) | ||
function from(a) { | ||
if(a==null || typeof a[Symbol.iterator]==='function') return a; | ||
return typeof a==='object'? Object.entries(a):[a]; | ||
}; | ||
// 20. iterable-hammingdistance (hammingDistance) | ||
function hammingDistance(i1, i2) { | ||
var I2 = i2[Symbol.iterator](), z = 0; | ||
for(var v1 of i1) { | ||
V2 = I2.next(); | ||
if(V2.done) return NaN; | ||
if(v1!==V2.value) z++; | ||
} | ||
return I2.next().done? z:NaN; | ||
}; | ||
// 21. iterable-includes (includes) | ||
function includes(itr, val, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
return z; | ||
} | ||
function replaceTo(itr, val, rep, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1; | ||
@@ -216,8 +229,7 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
if(v===val) return true; | ||
z[z0++] = v===val? rep:v; | ||
} | ||
return false; | ||
}; | ||
// 22. iterable-indexof (indexOf) | ||
function indexOf(itr, val, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
return z; | ||
} | ||
function mapReplace(itr, map, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1; | ||
@@ -227,8 +239,7 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
if(v===val) return i; | ||
z[z0++] = map.has(v)? map.get(v):v; | ||
} | ||
return -1; | ||
}; | ||
// 23. iterable-indicesof (indicesOf) | ||
function indicesOf(itr, val, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
return z; | ||
} | ||
function objectReplace(itr, obj, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1; | ||
@@ -238,50 +249,56 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
if(v===val) z[z0++] = i; | ||
z[z0++] = obj[v]!==undefined? obj[v]:v; | ||
} | ||
return z; | ||
}; | ||
// 24. iterable-is (is) | ||
function is(v) { | ||
return v!=null && typeof v[Symbol.iterator]==='function'; | ||
}; | ||
// 25. iterable-iscollection (isCollection) | ||
function isCollection(a) { | ||
return is(a) && typeof a!=='string'; | ||
}; | ||
// 26. iterable-join (join) | ||
function join(itr, sep=',', bgn=0, end=Number.MAX_SAFE_INTEGER, z='') { | ||
var i = -1, zl0 = z.length; | ||
} | ||
function reverseTo(arr, bgn=0, end=arr.length, z=[], z0=z.length) { | ||
for(var i=end-1; i>=bgn; i--) | ||
z[z0++] = arr[i]; | ||
return z; | ||
} | ||
const arrayReverse = reverseTo; | ||
function reverse(itr, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1, z00 = z0; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
z += v+sep; | ||
z[z0++] = v; | ||
} | ||
return z.length>zl0? z.substring(0, z.length-sep.length):z; | ||
}; | ||
// 27. iterable-keys (keys) | ||
function keys(itr) { | ||
var i = 0, z = []; | ||
for(var v of itr) | ||
z.push(i++); | ||
return z; | ||
}; | ||
// 28. iterable-last (last) | ||
function last(itr, idx=-1) { | ||
var i = -1, a = []; | ||
for(var v of itr) | ||
a[++i % idx] = v; | ||
return a[++i % idx]; | ||
}; | ||
// 29. iterable-lastindexof (lastIndexOf) | ||
function lastIndexOf(itr, val, bgn=Number.MAX_SAFE_INTEGER, end=-1) { | ||
var i = -1, z = -1; | ||
return arrayReverse(z, z00, z0); | ||
} | ||
function split(itr, sep=',', lim=Number.MAX_SAFE_INTEGER, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1, zx = [], n = 0; | ||
for(var v of itr) { | ||
if(++i<=end) continue; | ||
if(i>bgn) break; | ||
if(v===val) z = i; | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
if(n<lim && v!==sep) zx.push(v); | ||
else if(zx.length) { z[z0++] = zx; zx = []; n++; } | ||
} | ||
if(zx.length) z[z0++] = zx; | ||
return z; | ||
}; | ||
// 30. iterable-mapreplace (mapReplace) | ||
function mapReplace(itr, map, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
} | ||
function copyTo(arr, bgn=0, end=arr.length, z=[], z0=z.length, z1=z0+(end-bgn)) { | ||
if(z1>z.length) z.length = z1; | ||
if(arr===z) return z.copyWithin(z0, bgn, end); | ||
for(var i=bgn; i<end; i++) | ||
z[z0++] = arr[i]; | ||
return z; | ||
} | ||
function sliceTo(arr, bgn=0, end=arr.length, z=[], z0=z.length) { | ||
bgn = bgn<0? arr.length+bgn:bgn; | ||
end = end<0? arr.length+end:end; | ||
end = end>arr.length? arr.length:end; | ||
z0 = z0<0? z.length+z0:z0; | ||
return copyTo(arr, bgn, end, z, z0); | ||
} | ||
const arraySlice = sliceTo; | ||
function repeat(itr, n=1, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var iv = slice(itr, bgn, end); | ||
for(var i=0; i<n; i++) { | ||
arraySlice(iv, 0, iv.length, z, z0); | ||
z0 += iv.length; | ||
} | ||
return z; | ||
} | ||
function forEach(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
@@ -291,8 +308,6 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
z[z0++] = map.has(v)? map.get(v):v; | ||
fn.call(ths, v, i, itr); | ||
} | ||
return z; | ||
}; | ||
// 31. iterable-mapto (mapTo) | ||
function mapTo(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
} | ||
function someOf(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
@@ -302,33 +317,24 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
z[z0++] = fn.call(ths, v, i, itr); | ||
if(fn.call(ths, v, i, itr)) return true; | ||
} | ||
return z; | ||
}; | ||
// 32. iterable-max (max) | ||
function max(itr, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1, z = -Infinity; | ||
return false; | ||
} | ||
function everyOf(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
z = v>z? v:z; | ||
if(!fn.call(ths, v, i, itr)) return false; | ||
} | ||
return z; | ||
}; | ||
// 33. iterable-middle (middle) | ||
const last36 = require('iterable-last'); | ||
function middle(itr, idx=0) { | ||
return idx<0? last36(itr, idx):first(itr, idx); | ||
}; | ||
// 34. iterable-min (min) | ||
function min(itr, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var z = Infinity; | ||
return true; | ||
} | ||
function find(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
z = v<z? v:z; | ||
if(fn.call(ths, v, i, itr)) return v; | ||
} | ||
return z; | ||
}; | ||
// 35. iterable-objectreplace (objectReplace) | ||
function objectReplace(itr, obj, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
} | ||
function findIndex(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
@@ -338,8 +344,16 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
z[z0++] = obj[v]!==undefined? obj[v]:v; | ||
if(fn.call(ths, v, i, itr)) return i; | ||
} | ||
return -1; | ||
} | ||
function findAll(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
if(fn.call(ths, v, i, itr)) z[z0++] = v; | ||
} | ||
return z; | ||
}; | ||
// 36. iterable-pick (pick) | ||
function pick(itr, idx, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
} | ||
function findAllIndices(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1; | ||
@@ -349,15 +363,6 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
var ix = idx.indexOf(i); | ||
if(ix>=0) z[z0+ix] = v; | ||
if(fn.call(ths, v, i, itr)) z[z0++] = i; | ||
} | ||
return z; | ||
}; | ||
// 37. iterable-pickas (pickAs) | ||
const pick40 = require('iterable-pick'); | ||
function pickAs(itr, idx, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
if(idx==null) return slice(itr, bgn, end, z, z0); | ||
if(Array.isArray(idx)) return pick40(itr, idx, bgn, end, z, z0); | ||
return pick40(itr, [idx], bgn, end, z, z0)[z0]; | ||
}; | ||
// 38. iterable-reduce (reduce) | ||
} | ||
function reduce(itr, fn, acc, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
@@ -371,15 +376,4 @@ var i = -1; | ||
return acc; | ||
}; | ||
// 39. iterable-repeat (repeat) | ||
const sliceTo42 = require('array-sliceto'); | ||
function repeat(itr, n=1, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var iv = slice(itr, bgn, end); | ||
for(var i=0; i<n; i++) { | ||
sliceTo42(iv, 0, iv.length, z, z0); | ||
z0 += iv.length; | ||
} | ||
return z; | ||
}; | ||
// 40. iterable-replaceto (replaceTo) | ||
function replaceTo(itr, val, rep, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
} | ||
function filter(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1; | ||
@@ -389,63 +383,33 @@ for(var v of itr) { | ||
if(i>=end) break; | ||
z[z0++] = v===val? rep:v; | ||
if(fn.call(ths, v, i, itr)) z[z0++] = v; | ||
} | ||
return z; | ||
}; | ||
// 41. iterable-reverse (reverse) | ||
function reverse(itr, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1, z00 = z0; | ||
} | ||
function mapTo(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
z[z0++] = v; | ||
z[z0++] = fn.call(ths, v, i, itr); | ||
} | ||
return reverseOf1(z, z00, z0); | ||
}; | ||
// 42. iterable-size (size) | ||
function size(itr) { | ||
var z = 0; | ||
if(itr.size!=null) return itr.size; | ||
if(itr.length!=null) return itr.length; | ||
for(var v of itr) | ||
z++; | ||
return z; | ||
}; | ||
// 43. iterable-slice (slice) | ||
function slice(itr, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1, z0 = z0<0? z.length+z0:z0; | ||
} | ||
function max(itr, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1, z = -Infinity; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
z[z0++] = v; | ||
z = v>z? v:z; | ||
} | ||
return z; | ||
}; | ||
// 44. iterable-someof (someOf) | ||
function someOf(itr, fn, ths, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1; | ||
} | ||
function min(itr, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var z = Infinity; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
if(fn.call(ths, v, i, itr)) return true; | ||
z = v<z? v:z; | ||
} | ||
return false; | ||
}; | ||
// 45. iterable-split (split) | ||
function split(itr, sep=',', lim=Number.MAX_SAFE_INTEGER, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1, zx = [], n = 0; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
if(n<lim && v!==sep) zx.push(v); | ||
else if(zx.length) { z[z0++] = zx; zx = []; n++; } | ||
} | ||
if(zx.length) z[z0++] = zx; | ||
return z; | ||
}; | ||
// 46. iterable-startswith (startsWith) | ||
function startsWith(itr, val) { | ||
for(var v of itr) | ||
return v===val; | ||
}; | ||
// 47. iterable-sum (sum) | ||
} | ||
function sum(itr, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
@@ -459,76 +423,93 @@ var i = -1, z = 0; | ||
return z; | ||
}; | ||
// 48. iterable-without (without) | ||
function without(itr, rmv, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1; | ||
} | ||
function average(itr, bgn=0, end=Number.MAX_SAFE_INTEGER) { | ||
var i = -1, s = 0, n = 0; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
if(!rmv.has(v)) z[z0++] = v; | ||
s += v; | ||
++n; | ||
} | ||
return z; | ||
}; | ||
// 49. iterable-zip (zip) | ||
function zip(itr, bgn=0, end=Number.MAX_SAFE_INTEGER, z=[], z0=z.length) { | ||
var i = -1, a = 0; | ||
for(var v of itr) { | ||
if(++i<bgn) continue; | ||
if(i>=end) break; | ||
var b = z0-1; | ||
for(var u of v) | ||
(z[++b] = z[b]||[])[a] = u; | ||
a++; | ||
return n>0? s/n:0; | ||
} | ||
function hammingDistance(i1, i2) { | ||
var I2 = i2[Symbol.iterator](), z = 0; | ||
for(var v1 of i1) { | ||
V2 = I2.next(); | ||
if(V2.done) return NaN; | ||
if(v1!==V2.value) z++; | ||
} | ||
return z; | ||
}; | ||
exports.all = all; | ||
exports.any = any; | ||
exports.average = average; | ||
exports.compact = compact; | ||
exports.concat = concat; | ||
exports.contains = contains; | ||
exports.endsWith = endsWith; | ||
exports.entries = entries; | ||
return I2.next().done? z:NaN; | ||
} | ||
function euclideanDistance(i1, i2) { | ||
var I2 = i2[Symbol.iterator](), z = 0; | ||
for(var v1 of i1) { | ||
var v2 = I2.next().value; | ||
z += (v1-v2)*(v1-v2); | ||
} | ||
return Math.sqrt(z); | ||
} | ||
// 1. Datatype methods | ||
exports.is = is; | ||
exports.isCollection = isCollection; | ||
// 2. About methods | ||
exports.equal = equal; | ||
exports.euclideanDistance = euclideanDistance; | ||
exports.every = everyOf; | ||
exports.filter = filter; | ||
exports.find = find; | ||
exports.findAll = findAll; | ||
exports.findAllIndices = findAllIndices; | ||
exports.findIndex = findIndex; | ||
exports.size = size; | ||
exports.first = first; | ||
exports.flatten = flatten; | ||
exports.forEach = forEach; | ||
exports.middle = middle; | ||
exports.last = last; | ||
exports.keys = keys; | ||
exports.entries = entries; | ||
exports.any = any; | ||
exports.all = all; | ||
// 3. Generate methods | ||
exports.from = from; | ||
exports.hammingDistance = hammingDistance; | ||
exports.includes = includes; | ||
// 4. Search methods | ||
exports.startsWith = startsWith; | ||
exports.endsWith = endsWith; | ||
exports.indexOf = indexOf; | ||
exports.indicesOf = indicesOf; | ||
exports.is = is; | ||
exports.isCollection = isCollection; | ||
exports.lastIndexOf = lastIndexOf; | ||
exports.includes = includes; | ||
exports.contains = contains; | ||
// 4. Transform methods | ||
exports.join = join; | ||
exports.keys = keys; | ||
exports.last = last; | ||
exports.lastIndexOf = lastIndexOf; | ||
exports.mapReplace = mapReplace; | ||
exports.map = mapTo; | ||
exports.max = max; | ||
exports.middle = middle; | ||
exports.min = min; | ||
exports.objectReplace = objectReplace; | ||
exports.pick = pick; | ||
exports.pickAs = pickAs; | ||
exports.reduce = reduce; | ||
exports.repeat = repeat; | ||
exports.slice = slice; | ||
exports.compact = compact; | ||
exports.without = without; | ||
exports.concat = concat; | ||
exports.flatten = flatten; | ||
exports.zip = zip; | ||
exports.unzip = exports.zip; | ||
exports.replace = replaceTo; | ||
exports.mapReplace = mapReplace; | ||
exports.objectReplace = objectReplace; | ||
exports.reverse = reverse; | ||
exports.size = size; | ||
exports.slice = slice; | ||
exports.split = split; | ||
exports.repeat = repeat; | ||
// 5. Functional methods | ||
exports.forEach = forEach; | ||
exports.some = someOf; | ||
exports.split = split; | ||
exports.startsWith = startsWith; | ||
exports.every = everyOf; | ||
exports.find = find; | ||
exports.findIndex = findIndex; | ||
exports.findAll = findAll; | ||
exports.findAllIndices = findAllIndices; | ||
exports.reduce = reduce; | ||
exports.filter = filter; | ||
exports.map = mapTo; | ||
// 5. Evaluate methods | ||
exports.max = max; | ||
exports.min = min; | ||
exports.sum = sum; | ||
exports.without = without; | ||
exports.zip = zip; | ||
exports.unzip = zip; | ||
exports.average = average; | ||
exports.hammingDistance = hammingDistance; | ||
exports.euclideanDistance = euclideanDistance; |
{ | ||
"name": "extra-iterable", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Standard utility methods for Iterable.", | ||
@@ -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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
18743
491
1