Comparing version 0.0.10 to 0.0.12
138
fx.js
@@ -1,2 +0,2 @@ | ||
// FxJS 0.0.10 | ||
// FxJS 0.0.12 | ||
export const | ||
@@ -85,3 +85,3 @@ identity = a => a, | ||
const b = go1(a, f); | ||
if (b instanceof Promise) yield Promise.all([a, b]).then(([a, b]) => b ? a : Promise.reject(nop)); | ||
if (b instanceof Promise) yield b.then(b => b ? a : Promise.reject(nop)); | ||
else if (b) yield a; | ||
@@ -101,2 +101,18 @@ } | ||
L.flatten = function *(iter) { | ||
for (const a of iter) { | ||
if (hasIter(a)) yield *a; | ||
else yield a; | ||
} | ||
}; | ||
L.deepFlatten = function *f(iter) { | ||
for (const a of iter) { | ||
if (hasIter(a)) yield *f(a); | ||
else yield a; | ||
} | ||
}; | ||
L.flatMap = curry((f, iter) => L.flatten(L.map(f, iter))); | ||
export const | ||
@@ -117,20 +133,20 @@ call = (f, a) => f(a), | ||
const reduceF = (acc, a, f) => | ||
a instanceof Promise ? | ||
a.then(a => f(acc, a), e => e == nop ? acc : Promise.reject(e)) : | ||
f(acc, a); | ||
export const | ||
reduce = curry(function(f, acc, coll) { | ||
if (arguments.length == 2) { | ||
var iter = L.values(acc); | ||
acc = iter.next().value; | ||
} else { | ||
iter = L.values(coll); | ||
} | ||
return function recur() { | ||
reduce = curry(function(f, acc, iter) { | ||
if (arguments.length == 2) return reduce(f, head(iter = L.values(acc)), iter); | ||
iter = L.values(iter); | ||
return go1(acc, function recur(acc) { | ||
let cur; | ||
while (!(cur = iter.next()).done) { | ||
const a = cur.value, acc_ = acc; | ||
acc = go1(a, a => go1(acc_, acc => f(acc, a))); | ||
if (acc instanceof Promise) | ||
return (acc = acc.catch(e => e == nop ? acc_ : Promise.reject(e))).then(recur); | ||
acc = reduceF(acc, cur.value, f); | ||
if (acc instanceof Promise) return acc.then(recur); | ||
} | ||
return acc; | ||
} (); | ||
}); | ||
}), | ||
@@ -148,5 +164,6 @@ | ||
export const take = curry(function(limit, coll) { | ||
if (limit === 0) return []; | ||
var res = [], iter = L.values(coll); | ||
export const take = curry((l, iter) => { | ||
if (l === 0) return []; | ||
let res = []; | ||
iter = L.values(iter); | ||
return function recur() { | ||
@@ -156,7 +173,9 @@ let cur; | ||
const a = cur.value; | ||
if (a instanceof Promise) return a | ||
.then(a => (res.push(a), res).length == limit ? res : recur()) | ||
.catch(e => e == nop ? recur() : Promise.reject(e)); | ||
if (a instanceof Promise) { | ||
return a | ||
.then(a => (res.push(a), res).length == l ? res : recur()) | ||
.catch(e => e == nop ? recur() : Promise.reject(e)); | ||
} | ||
res.push(a); | ||
if (res.length == limit) return res; | ||
if (res.length == l) return res; | ||
} | ||
@@ -168,3 +187,3 @@ return res; | ||
export const | ||
takeAll = coll => take(Infinity, coll), | ||
takeAll = take(Infinity), | ||
@@ -176,3 +195,3 @@ take_all = takeAll, | ||
export const | ||
head = pipe(take1, ([a]) => a), | ||
head = iter => go1(take1(iter), ([h]) => h), | ||
@@ -203,2 +222,9 @@ tail = coll => takeAll(L.tail(coll)); | ||
export const | ||
flatten = pipe(L.flatten, takeAll), | ||
deepFlatten = pipe(L.deepFlatten, takeAll), | ||
flatMap = curry(pipe(L.map, flatten)); | ||
export const | ||
uniqueBy = curry((f, coll) => { | ||
@@ -217,2 +243,4 @@ const s = new Set(); | ||
unique_by = uniqueBy, | ||
unique = uniqueBy(a => a), | ||
@@ -240,2 +268,4 @@ | ||
max_by = maxBy, | ||
max = maxBy(identity), | ||
@@ -246,2 +276,4 @@ | ||
min_by = minBy, | ||
min = maxBy(identity); | ||
@@ -303,26 +335,19 @@ | ||
C.map = curry(pipe(L.map, _ => [..._], takeAll)); | ||
const catchNoop = ([...arr]) => | ||
(arr.forEach(a => a instanceof Promise ? a.catch(noop) : a), arr); | ||
C.entriesMap = C.esMap = C.es_map = curry(pipe(L.esMap, _ => [..._], takeAll)); | ||
C.reduce = curry((f, acc, iter) => iter ? | ||
reduce(f, acc, catchNoop(iter)) : | ||
reduce(f, catchNoop(acc))); | ||
C.reduce = (f, coll, acc) => reduce(f, acc, [...coll]); | ||
C.take = curry((l, iter) => take(l, catchNoop(iter))); | ||
C.take = curry((limit, coll) => limit === 0 ? [] : new Promise(function(resolve) { | ||
var res = []; | ||
var i = -1, j = -1, resolved; | ||
for (const a of coll) { | ||
++i; | ||
Promise.resolve(a).then(a => { | ||
if (resolved) return; | ||
res.push(a); | ||
if (res.length == limit || i == ++j) { | ||
resolved = true; | ||
resolve(res); | ||
} | ||
}).catch(e => e != nop && Promise.reject(e)); | ||
} | ||
})); | ||
C.takeAll = C.take(Infinity); | ||
C.takeAll = C.take_all = coll => C.take(Infinity, coll); | ||
C.map = curry(pipe(L.map, C.takeAll)); | ||
C.filter = curry(pipe(L.filter, C.takeAll)); | ||
C.entriesMap = C.esMap = C.es_map = curry(pipe(L.esMap, C.takeAll)); | ||
C.take1 = C.take(1); | ||
@@ -340,3 +365,3 @@ | ||
C.calls = baseCalls(C.map, C.eMap); | ||
C.calls = baseCalls(C.map, C.esMap); | ||
@@ -424,2 +449,25 @@ export const | ||
export const scat = curry((f, coll) => | ||
reduce((a, b) => `${a}${b}`, '', L.map(f, coll))); | ||
reduce((a, b) => `${a}${b}`, '', L.map(f, coll))); | ||
const arrComparator = (arr) => (a, b) => { | ||
let i = -1; | ||
while (++i < arr.length) { | ||
const ai = a[arr[i]], bi = b[arr[i]]; | ||
if (ai === bi) continue; | ||
return ai < bi ? -1 : 1; | ||
} | ||
return 0; | ||
}; | ||
const baseSortBy = (left, right) => curry(function sortBy(f, arr) { | ||
return isArray(f) ? sortBy(arrComparator(f), arr) : | ||
typeof f == 'string' ? sortBy(a => a[f], arr) : | ||
f.length == 2 ? [...arr].sort(right == -1 ? pipe(f, n => n * -1) : f) : | ||
[...arr].sort((a, b, fa = f(a), fb = f(b)) => fa == fb ? 0 : fa < fb ? left : right) | ||
}); | ||
export const | ||
sortBy = baseSortBy(-1, 1), | ||
sortByDesc = baseSortBy(1, -1), | ||
sort = sortBy(identity), | ||
sortDesc = sortByDesc(identity); |
{ | ||
"name": "fxjs2", | ||
"version": "0.0.10", | ||
"version": "0.0.12", | ||
"description": "Functional Extensions for Javascript", | ||
@@ -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
13766
329