From cedd6b04f78f3baa71d57d0033f4532ba89886ab Mon Sep 17 00:00:00 2001 From: Vesa Karvonen Date: Wed, 15 Aug 2018 07:29:42 +0300 Subject: [PATCH] Allow traversals as predicates --- src/partial.lenses.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/partial.lenses.js b/src/partial.lenses.js index 1866e1c5..2da2ea90 100644 --- a/src/partial.lenses.js +++ b/src/partial.lenses.js @@ -195,6 +195,16 @@ const mumBy = ord => // +const predicateFn = x => x || void 0 + +const toPredicate = predicate => { + if (typeof predicate === 'function' && predicate.length < 4) return predicate + predicate = toFunction(predicate) + return (x, i) => predicate(x, i, Select, predicateFn) +} + +// + const traverseU = function traverse(C, xi2yC, t, s) { return toFunction(t)(s, void 0, C, xi2yC) } @@ -832,6 +842,7 @@ const disjointFwd = (process.env.NODE_ENV === 'production' const eitherU = (t, e) => function either(c) { + c = toPredicate(c) return function either(x, i, C, xi2yC) { return (c(x, i) ? t : e)(x, i, C, xi2yC) } @@ -1001,7 +1012,7 @@ export const condOf = (process.env.NODE_ENV === 'production' const os = Array(n + 1) for (let i = 0; i < n; ++i) { const c = arguments[i + 1] - ps[i] = c[0] + ps[i] = toPredicate(c[0]) os[i] = toFunction(c[1]) } os[n] = def @@ -1227,11 +1238,13 @@ export function query() { return r } -export const satisfying = p => - function satisfying(x, i, C, xi2yC) { +export const satisfying = p => { + p = toPredicate(p) + return function satisfying(x, i, C, xi2yC) { const rec = (x, i) => (p(x, i) ? xi2yC(x, i) : children(x, i, C, rec)) return rec(x, i) } +} export const leafs = satisfying( x => void 0 !== x && !I.isArray(x) && !I.isObject(x) @@ -1240,6 +1253,7 @@ export const leafs = satisfying( // Folds over traversals export const all = I.curry(function all(xi2b, t, s) { + xi2b = toPredicate(xi2b) return !getAsU( (x, i) => { if (!xi2b(x, i)) return true @@ -1252,6 +1266,7 @@ export const all = I.curry(function all(xi2b, t, s) { export const and = all(id) export const any = I.curry(function any(xi2b, t, s) { + xi2b = toPredicate(xi2b) return !!getAsU( (x, i) => { if (xi2b(x, i)) return true @@ -1284,6 +1299,7 @@ export const concatAs = mkTraverse(id, ConstantOf) export const concat = concatAs(id) export const countIf = I.curry(function countIf(p, t, s) { + p = toPredicate(p) return traverseU(Sum, (x, i) => (p(x, i) ? 1 : 0), t, s) }) @@ -1417,6 +1433,7 @@ export const minimumBy = mumBy(I.ltU) export const minimum = minimumBy(id) export const none = I.curry(function none(xi2b, t, s) { + xi2b = toPredicate(xi2b) return !getAsU( (x, i) => { if (xi2b(x, i)) return true @@ -1548,6 +1565,7 @@ export const filter = (process.env.NODE_ENV === 'production' ) ]) ))(function filter(xi2b) { + xi2b = toPredicate(xi2b) return function filter(xs, i, F, xi2yF) { let ts let fs = I.array0 @@ -1564,6 +1582,7 @@ export const filter = (process.env.NODE_ENV === 'production' }) export function find(xih2b) { + xih2b = toPredicate(xih2b) const hint = arguments.length > 1 ? arguments[1] : {hint: 0} return function find(xs, _i, F, xi2yF) { const ys = seemsArrayLike(xs) ? xs : ''