UNPKG

h5p-lib-controls

Version:

Library for adding user input to web applications

139 lines (130 loc) 2.39 kB
/** * Returns a curried version of a function * * @param {function} fn * * @public * * @return {function} */ export const curry = function(fn) { const arity = fn.length; return function f1() { const args = Array.prototype.slice.call(arguments, 0); if (args.length >= arity) { return fn.apply(null, args); } else { return function f2() { const args2 = Array.prototype.slice.call(arguments, 0); return f1.apply(null, args.concat(args2)); } } }; }; /** * Compose functions together, executing from right to left * * @param {function...} fns * * @function * @public * * @return {function} */ export const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))); /** * Applies a function to each element in an array * * @param {function} fn * @param {Array} arr * * @function * @public * * @return {function} */ export const forEach = curry(function (fn, arr) { arr.forEach(fn); }); /** * Maps a function to an array * * @param {function} fn * @param {Array} arr * * @function * @public * * @return {function} */ export const map = curry(function (fn, arr) { return arr.map(fn); }); /** * Applies a filter to an array * * @param {function} fn * @param {Array} arr * * @function * @public * * @return {function} */ export const filter = curry(function (fn, arr) { return arr.filter(fn); }); /** * Applies a some to an array * * @param {function} fn * @param {Array} arr * * @function * @public * * @return {function} */ export const some = curry(function (fn, arr) { return arr.some(fn); }); /** * Returns true if an array contains a value * * @param {*} value * @param {Array} arr * * @function * @public * * @return {function} */ export const contains = curry(function (value, arr) { return arr.indexOf(value) != -1; }); /** * Returns an array without the supplied values * * @param {Array} values * @param {Array} arr * * @function * @public * * @return {function} */ export const without = curry(function (values, arr) { return filter(value => !contains(value, values), arr) }); /** * Takes a string that is either 'true' or 'false' and returns the opposite * * @param {string} bool * * @public * @return {string} */ export const inverseBooleanString = function (bool) { return (bool !== 'true').toString(); };