f-utility
Version:
functional utilities
133 lines (120 loc) • 4.99 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.propEq = exports.__propEq = exports.propIs = exports.__propIs = exports.pathEq = exports.__pathEq = exports.prop = exports.propOr = exports.__propOr = exports.path = exports.propSatisfies = exports.__propSatisfies = exports.pathSatisfies = exports.__pathSatisfies = exports.pathOr = exports.__pathOr = undefined;
var _katsuCurry = require("katsu-curry");
var _reduce = require("./reduce");
var _math = require("./math");
/**
* Grab a nested value from an object or return a default
* @method pathOr
* @param {any} def - a default value
* @param {string[]} lenses - a list of nested properties
* @param {any} input - an object to grab things from
* @returns {any} a nested value or default
* @example
* import {pathOr} from 'f-utility'
* pathOr(`default`, [`a`, `b`, `c`], {a: {b: {c: `actual`}}}) // `actual`
* pathOr(`default`, [`a`, `b`, `c`], {x: {y: {z: `actual`}}}) // `default`
*/
var __pathOr = exports.__pathOr = function __pathOr(def, lenses, input) {
return (0, _reduce.reduce)(function (focus, lens) {
return focus[lens] || def;
}, input, lenses);
};
var pathOr = exports.pathOr = (0, _katsuCurry.curry)(__pathOr);
/**
* Grab a property from an object and compare it with a given function
* @method pathSatisfies
* @param {function} equiv - a test function
* @param {string} pathTo - a nested property
* @param {any} input - an object to grab things from
* @returns {boolean} a truthy value
*/
var __pathSatisfies = exports.__pathSatisfies = function __pathSatisfies(equiv, pathTo, input) {
return (0, _katsuCurry.pipe)(path(pathTo), equiv, Boolean)(input);
};
var pathSatisfies = exports.pathSatisfies = (0, _katsuCurry.curry)(__pathSatisfies);
/**
* Grab a property from an object and compare it with a given function
* @method propSatisfies
* @param {function} equiv - a test function
* @param {string} propTo - a nested property
* @param {any} input - an object to grab things from
* @returns {boolean} a truthy value
*/
var __propSatisfies = exports.__propSatisfies = function __propSatisfies(equiv, propTo, input) {
return (0, _katsuCurry.pipe)(prop(propTo), equiv, Boolean)(input);
};
var propSatisfies = exports.propSatisfies = (0, _katsuCurry.curry)(__propSatisfies);
/**
* Grab a nested value from an object
* @method path
* @param {string[]} lenses - a list of nested properties
* @param {any} input - an object to grab things from
* @returns {any} a nested value or null
* @example
* import {path} from 'f-utility'
* pathOr([`a`, `b`, `c`], {a: {b: {c: `actual`}}}) // `actual`
* pathOr([`a`, `b`, `c`], {x: {y: {z: `actual`}}}) // null
*/
var path = exports.path = pathOr(null);
/**
* Grab a property from an object or return a default
* @method propOr
* @param {any} def - a default value
* @param {string} property - a property
* @param {any} input - an object to grab things from
* @returns {any} a property or default
* @example
* import {propOr} from 'f-utility'
* pathOr(`default`, `c`, {c: `actual`}) // `actual`
* pathOr(`default`, `c`, {z: `actual`}) // `default`
*/
var __propOr = exports.__propOr = function __propOr(def, property, input) {
return pathOr(def, [property], input);
};
var propOr = exports.propOr = (0, _katsuCurry.curry)(__propOr);
/**
* Grab a property from an object or return null
* @method prop
* @param {string} property - a property
* @param {any} input - an object to grab things from
* @returns {any} a property or null
* @example
* import {prop} from 'f-utility'
* path(`c`, {c: `actual`}) // `actual`
* path(`c`, {z: `actual`}) // null
*/
var prop = exports.prop = propOr(null);
/**
* Grab a property from an object and compare it with a given value via ===
* @method pathEq
* @param {strings[]} lenses - a property
* @param {any} equiv - equivalent value
* @param {any} input - an object to grab things from
* @returns {boolean} a truthy value
*/
var __pathEq = exports.__pathEq = function __pathEq(lenses, equiv, input) {
return pathSatisfies((0, _math.equals)(equiv), lenses, input);
};
var pathEq = exports.pathEq = (0, _katsuCurry.curry)(__pathEq);
var __propIs = exports.__propIs = function __propIs(type, property, input) {
return (0, _katsuCurry.pipe)(prop(property), function (val) {
return val != null && val.constructor === type || val instanceof type;
}, Boolean)(input);
};
var propIs = exports.propIs = (0, _katsuCurry.curry)(__propIs);
/**
* Grab a property from an object and compare it with a given value via ===
* @method propEq
* @param {string} property - a property
* @param {any} equiv - equivalent value
* @param {any} input - an object to grab things from
* @returns {boolean} a truthy value
*/
var __propEq = exports.__propEq = function __propEq(property, equiv, input) {
return pathSatisfies((0, _math.equals)(equiv), [property], input);
};
var propEq = exports.propEq = (0, _katsuCurry.curry)(__propEq);
;