Comparing version 0.0.7 to 0.0.9
193
fx.js
@@ -1,2 +0,2 @@ | ||
// FxJS 0.0.7 | ||
// FxJS 0.0.9 | ||
export const | ||
@@ -12,4 +12,12 @@ identity = a => a, | ||
curry = f => | ||
(a, ..._) => _.length < 1 ? (..._) => f(a, ..._) : f(a, ..._); | ||
(a, ..._) => _.length < 1 ? (..._) => f(a, ..._) : f(a, ..._), | ||
log = console.log, | ||
go1 = (a, f) => a instanceof Promise ? a.then(f) : f(a), | ||
negate = f => (..._) => go1(f(..._), not), | ||
constant = a => _ => a; | ||
export const | ||
@@ -28,20 +36,16 @@ isString = a => typeof a == 'string', | ||
isUndefined = a => a === undefined; | ||
isUndefined = a => a === undefined, | ||
export const | ||
hasIter = coll => !!(coll && coll[Symbol.iterator]), | ||
is_undefined = isUndefined, | ||
alterIter = alter => coll => | ||
hasIter(coll) ? coll[Symbol.iterator]() : alter(coll), | ||
has = curry((k, obj) => obj.hasOwnProperty(k)), | ||
collIter = alterIter(Lvalues); | ||
hasIter = a => !!(a && a[Symbol.iterator]); | ||
export const go1 = (a, f) => a instanceof Promise ? a.then(f) : f(a); | ||
export const L = {}; | ||
function* Lvalues(obj) { | ||
function *_values(obj) { | ||
for (const k in obj) yield obj[k]; | ||
} | ||
L.values = Lvalues; | ||
L.values = a => hasIter(a) ? a[Symbol.iterator]() : _values(a); | ||
@@ -52,2 +56,6 @@ L.entries = function *(obj) { | ||
L.keys = function *(obj) { | ||
for (const k in obj) yield k; | ||
}; | ||
L.reverse = function *(arr) { | ||
@@ -59,3 +67,3 @@ var l = arr.length; | ||
L.tail = function(coll) { | ||
var iter = collIter(coll); | ||
var iter = L.values(coll); | ||
return go1(take(1, iter), _ => iter); | ||
@@ -65,3 +73,3 @@ }; | ||
L.headTail = L.head_tail = function(coll) { | ||
var iter = collIter(coll); | ||
var iter = L.values(coll); | ||
return go1(take(1, iter), ([head]) => [head, iter]); | ||
@@ -76,7 +84,7 @@ }; | ||
L.map = curry(function *(f, coll) { | ||
for (const a of coll) yield go1(a, f); | ||
for (const a of L.values(coll)) yield go1(a, f); | ||
}); | ||
L.filter = curry(function *(f, coll) { | ||
for (const a of collIter(coll)) { | ||
for (const a of L.values(coll)) { | ||
const b = go1(a, f); | ||
@@ -88,4 +96,4 @@ if (b instanceof Promise) yield Promise.all([a, b]).then(([a, b]) => b ? a : Promise.reject(nop)); | ||
L.entriesMap = L.eMap = curry(function *(f, coll) { | ||
for (const [k, a] of coll) yield go1(go1(a, f), b => [k, b]); | ||
L.entriesMap = L.esMap = L.es_map = curry(function *(f, coll) { | ||
for (const [k, a] of L.values(coll)) yield go1(go1(a, f), b => [k, b]); | ||
}); | ||
@@ -117,6 +125,6 @@ | ||
if (arguments.length == 2) { | ||
var iter = collIter(acc); | ||
var iter = L.values(acc); | ||
acc = iter.next().value; | ||
} else { | ||
iter = collIter(coll); | ||
iter = L.values(coll); | ||
} | ||
@@ -145,3 +153,3 @@ return function recur() { | ||
if (limit === 0) return []; | ||
var res = [], iter = collIter(coll); | ||
var res = [], iter = L.values(coll); | ||
return function recur() { | ||
@@ -164,2 +172,4 @@ let cur; | ||
take_all = takeAll, | ||
take1 = take(1); | ||
@@ -172,7 +182,18 @@ | ||
const baseCalls = (map, esMap) => (fs, ...args) => | ||
hasIter(fs) ? | ||
map(f => f(...args), fs) : | ||
object(esMap(f => f(...args), fs)); | ||
export const | ||
map = curry(pipe(L.map, takeAll)), | ||
pluck = curry((k, coll) => map(a => a[k], coll)); | ||
esMap = curry(pipe(L.eMap, takeAll)), | ||
entriesMap = esMap, es_map = esMap, | ||
pluck = curry((k, coll) => map(a => a[k], coll)), | ||
calls = baseCalls(map, esMap); | ||
export const | ||
@@ -214,4 +235,14 @@ filter = curry(pipe(L.filter, takeAll)), | ||
index_by = indexBy; | ||
index_by = indexBy, | ||
maxBy = curry((f, coll) => | ||
reduce((a, b) => f(a) > f(b) ? a : b, coll)), | ||
max = maxBy(identity), | ||
minBy = curry((f, coll) => | ||
reduce((a, b) => f(a) > f(b) ? b : a, coll)), | ||
min = maxBy(identity); | ||
function incSel(parent, k) { | ||
@@ -239,11 +270,25 @@ parent[k] ? parent[k]++ : parent[k] = 1; | ||
eMap = entryMap, | ||
eMap = entryMap, emap = eMap, | ||
entries = pipe(L.entries, takeAll); | ||
entries = Object.entries, | ||
values = Object.values, | ||
keys = Object.keys; | ||
const basePick = filter => curry((ks, obj) => go( | ||
obj, | ||
L.entries, | ||
filter(([k]) => ks.includes(k)), | ||
object | ||
)); | ||
export const | ||
pick = basePick(L.filter), | ||
omit = basePick(L.reject); | ||
const baseExtend = set => (obj, ...objs) => reduce(reduce(set), obj, L.map(entries, objs)); | ||
export const | ||
has = curry((k, obj) => obj.hasOwnProperty(k)), | ||
extend = baseExtend(set3), | ||
@@ -257,2 +302,4 @@ | ||
C.entriesMap = C.esMap = C.es_map = curry(pipe(L.esMap, _ => [..._], takeAll)); | ||
C.reduce = (f, coll, acc) => reduce(f, acc, [...coll]); | ||
@@ -276,3 +323,3 @@ | ||
C.takeAll = coll => C.take(Infinity, coll); | ||
C.takeAll = C.take_all = coll => C.take(Infinity, coll); | ||
@@ -289,2 +336,90 @@ C.take1 = C.take(1); | ||
C.every = curry(pipe(L.reject, C.take1, _ => _.length == 0)); | ||
C.every = curry(pipe(L.reject, C.take1, _ => _.length == 0)); | ||
C.calls = baseCalls(C.map, C.eMap); | ||
export const | ||
isMatch = curry((a, b) => | ||
typeof a == 'function' ? !!a(b) | ||
: | ||
isArray(a) && isArray(b) ? every(v => b.includes(v), a) | ||
: | ||
typeof b == 'object' ? every(([k, v]) => b[k] == v, L.entries(a)) | ||
: | ||
a instanceof RegExp ? b.match(a) | ||
: | ||
a == b | ||
), | ||
is_match = isMatch; | ||
export const | ||
findWhere = curry((w, coll) => find(isMatch(w), coll)), | ||
find_where = findWhere; | ||
function baseMatch(targets) { | ||
var cbs = []; | ||
function evl() { | ||
return go( | ||
targets, | ||
values, | ||
targets => | ||
go(cbs, | ||
find(pb => { return pb._case(...targets); }), | ||
pb => pb._body(...targets))); | ||
} | ||
function _case(f) { | ||
cbs.push({ _case: typeof f == 'function' ? pipe(...arguments) : isMatch(f) }); | ||
return _body; | ||
} | ||
_case.case = _case; | ||
function _body() { | ||
cbs[cbs.length-1]._body = pipe(...arguments); | ||
return _case; | ||
} | ||
_case.else = function() { | ||
_case(_=> true) (...arguments); | ||
return targets ? evl() : (...targets2) => ((targets = targets2), evl()); | ||
}; | ||
return _case; | ||
} | ||
export const match = (..._) => baseMatch(_); | ||
match.case = (..._) => baseMatch(null).case(..._); | ||
export const | ||
baseSel = sep => curry(function f(selector, acc) { | ||
return ( | ||
!selector ? | ||
acc | ||
: | ||
isArray(selector) ? | ||
reduce((acc, selector) => f(selector, acc), acc, selector) | ||
: | ||
typeof selector == 'object' || typeof selector == 'function' ? | ||
findWhere(selector, acc) | ||
: | ||
reduce( | ||
(acc, key, s = key[0]) => | ||
!acc ? acc : | ||
s == '#' ? findWhere({ id: key.substr(1) }, acc) : | ||
s == '[' || s == '{' ? findWhere(JSON.parse(key), acc) : | ||
acc[key], | ||
acc, | ||
selector.split(sep)) | ||
); | ||
}), | ||
sel = baseSel('.'); | ||
export const scat = curry((f, coll) => go( | ||
coll, | ||
L.map(f), | ||
reduce((a, b) => `${a}${b}`) | ||
)); |
{ | ||
"name": "fxjs2", | ||
"version": "0.0.7", | ||
"version": "0.0.9", | ||
"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
11846
296