UNPKG

pragmatic-fp-ts

Version:

Opinionated functional programming library with easy use in mind

131 lines (130 loc) 4.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.transformObj = exports.intoObjM = exports.transformList = exports.insertM = exports.compact = exports.find = exports.uniqBy = exports.uniq = exports.insert = exports.drop = exports.take = exports.filter = exports.flatten = exports.flatMap = exports.map = exports.consM = exports.conjM = void 0; const main_1 = require("./main"); // Mutating conj! Expects empty new array input const conjM = (coll, elem) => { coll.push(elem); return coll; }; exports.conjM = conjM; // Mutating cons! Expects empty new array input const consM = (coll, elem) => { coll.unshift(elem); return coll; }; exports.consM = consM; const map = (fn) => (reducing) => (coll, input) => reducing(coll, fn(input)); exports.map = map; const flatMap = (fn) => (reducing) => (coll, input) => { const mapped = fn(input); return (0, main_1.isArray)(mapped) ? (0, main_1.reduce)(reducing, coll, mapped) : reducing(coll, mapped); }; exports.flatMap = flatMap; const flatten = (reducing) => (coll, input) => ((0, main_1.isArray)(input) ? (0, main_1.reduce)(reducing, coll, input) : reducing(coll, input)); exports.flatten = flatten; const filter = (pred) => (reducing) => (coll, input) => (pred(input) ? reducing(coll, input) : coll); exports.filter = filter; const take = (n) => (reducing) => { let i = 0; return (coll, input) => { return i++ < n ? reducing(coll, input) : coll; }; }; exports.take = take; const drop = (n) => (reducing) => { let i = 0; return (coll, input) => { return i++ < n ? coll : reducing(coll, input); }; }; exports.drop = drop; const insert = (elem) => (reducing) => { let inserted = false; return (coll, input) => { if (inserted) return reducing(coll, input); else { inserted = true; return reducing(reducing(coll, elem), input); } }; }; exports.insert = insert; const uniq = (reducing) => { const set = new Set(); return (coll, input) => { if (set.has(input)) return coll; else { set.add(input); return reducing(coll, input); } }; }; exports.uniq = uniq; const uniqBy = (toKey) => (reducing) => { const set = new Set(); return (coll, input) => { const key = toKey(input); if (set.has(key)) return coll; else { set.add(key); return reducing(coll, input); } }; }; exports.uniqBy = uniqBy; const find = (pred) => (reducing) => { let found = false; return (coll, input) => { if (!found && pred(input)) return reducing(coll, input); else return coll; }; }; exports.find = find; exports.compact = (0, exports.filter)((x) => x !== null && x !== undefined && (typeof x === "number" ? !isNaN(x) : true) && !(0, main_1.isNothing)(x) && !(0, main_1.isLeft)(x)); const insertM = (inOrder, arr, elem) => { let l = 0; let r = arr.length; while (r > l) { const m = (l + r) >> 1; // Math.floor(r + l / 2); if (inOrder(elem, arr[m])) r = m; else l = m + 1; } arr.splice(l, 0, elem); return arr; }; exports.insertM = insertM; function transformList(...args) { if (!Array.isArray(args[args.length - 1])) return (coll_) => transformList(...args, coll_); const xforms = (0, main_1.butLast)(args); const coll = (0, main_1.last)(args); const xform = (0, main_1.flow)(...xforms.reverse()); return (0, main_1.reduce)(xform(exports.conjM), [], coll); } exports.transformList = transformList; const intoObjM = (coll, [k, v]) => { coll[k] = v; return coll; }; exports.intoObjM = intoObjM; function transformObj(...args) { if ((0, main_1.isFunction)(args[args.length - 1])) return (coll_) => transformObj(...args, coll_); const xforms = (0, main_1.butLast)(args); const coll = (0, main_1.last)(args); const xform = (0, main_1.flow)(...xforms.reverse()); return (0, main_1.reduce)(xform(exports.intoObjM), {}, Object.entries(coll)); } exports.transformObj = transformObj;