Comparing version 5.1.2 to 6.0.0
'use strict'; | ||
/** | ||
* Delete the element at the specified index, creating a new array, | ||
* or returning the array as is if the index is out of bounds | ||
* | ||
* @param number The index of the element to remove. | ||
* @returns Returns a function that expects the `array` to modify. | ||
*/ | ||
function deleteAt(index) { | ||
return function (as) { | ||
if (!as[index]) return as; | ||
var b = as.slice(0, index); | ||
var a = as.slice(index + 1); | ||
return [].concat(b, a); | ||
}; | ||
} | ||
/** | ||
* Insert an element at the specified index, creating a new array, | ||
* or returning the array as is if the index is out of bounds | ||
* | ||
* @param number The index of the element to add. | ||
* @param T The item to add. | ||
* @returns Returns a function that expects the `array` to modify. | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const result = insertAt(1, 10)(nums) | ||
* // result === [1, 10, 2, 3, 4] | ||
* | ||
*/ | ||
function insertAt(index, a) { | ||
return function (as) { | ||
if (!as[index]) return as; | ||
var b = as.slice(0, index); | ||
var at = as.slice(index); | ||
return [].concat(b, [a], at); | ||
}; | ||
} | ||
/** | ||
* Read a value at a particular index from an array | ||
* | ||
* @param number The index of the element to read. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
* | ||
*/ | ||
function lookupAt(index) { | ||
return function (as) { | ||
return as[index]; | ||
}; | ||
} | ||
/** | ||
* Apply a function to the element at the specified index, creating a new array, | ||
* or returning the array as is if the index is out of bounds. | ||
* | ||
* @param number The index of the element to modify. | ||
* @param function A function that takes the element to modify as an argument. | ||
* @returns Returns a function that expects the `array` to modify. | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2] | ||
* const result = modifyAt(1, n => n * 2)(words) | ||
* // result === [1, 4] | ||
*/ | ||
function modifyAt(index, f) { | ||
return function (as) { | ||
if (!as[index]) return as; | ||
var b = as.slice(0, index); | ||
var at = as.slice(index + 1); | ||
var upd = f(as[index]); | ||
return [].concat(b, [upd], at); | ||
}; | ||
} | ||
/** | ||
* Change the element at the specified index, creating a new array, | ||
* or returning the array as us if the index is out of bounds | ||
* | ||
* @param number The index of the element to replace. | ||
* @param T The item that will take over. | ||
* @returns Returns a function that expects the `array` to modify. | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2] | ||
* const result = updateAt(1, 10)(nums) | ||
* // result === [1, 10] | ||
*/ | ||
function updateAt(index, a) { | ||
return function (as) { | ||
if (!as[index]) return as; | ||
var b = as.slice(0, index); | ||
var at = as.slice(index + 1); | ||
return [].concat(b, [a], at); | ||
}; | ||
} | ||
/** | ||
* Combines two arrays. | ||
* | ||
* @param array | ||
* @returns Returns a function that expects the `array` array to concatenate. | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2] | ||
* const otherNums = [3, 4] | ||
* const result = concat(nums)(otherNums) | ||
* // result === [1, 2, 3, 4] | ||
*/ | ||
function concat(as) { | ||
return function (bs) { | ||
return as.concat(bs); | ||
}; | ||
} | ||
/** | ||
* Determines whether all the members of an array satisfy the specified test. | ||
* | ||
* @param callbackfn A function that accepts one argument. The every method calls the callbackfn function for each element in the array until the callbackfn returns a value which is coercible to the Boolean value false, or until the end of the array. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
* | ||
*/ | ||
function every(predicate) { | ||
return function (as) { | ||
return as.every(predicate); | ||
}; | ||
} | ||
/** | ||
* Returns the elements of an array that meet the condition | ||
* specified in a callback function. | ||
* | ||
* @param callbackfn The function invoked per iteration. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
*/ | ||
function filter(f) { | ||
return function (as) { | ||
return as.filter(f); | ||
}; | ||
} | ||
/** | ||
* Returns the elements of an array that meet the condition | ||
* specified in a callback function and refines type. | ||
* | ||
* @param callbackfn The function invoked per iteration. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
*/ | ||
function filterTypeGuard(f) { | ||
return function (as) { | ||
return as.filter(f); | ||
}; | ||
} | ||
/** | ||
* Returns the value of the first element in the array where predicate is true, | ||
* and undefined otherwise. | ||
* | ||
* @param callbackfn The function that get called once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, immediately returns that element value. Otherwise returns undefined. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
*/ | ||
function find(f) { | ||
return function (as) { | ||
return as.find(f); | ||
}; | ||
} | ||
/** | ||
* Returns the index of the first element in the array where predicate is true, | ||
* and -1 otherwise. | ||
* | ||
* @param callbackfn The function that get called once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, immediately returns that element index. Otherwise returns undefined. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
*/ | ||
function findIndex(f) { | ||
return function (as) { | ||
return as.findIndex(f); | ||
}; | ||
} | ||
/** | ||
* Determines whether an array includes a certain element, | ||
* returning true or false as appropriate. | ||
* | ||
* @param searchElement The element to search for. | ||
* @param fromIndex A second optional argument to indicate the position in this array at which to begin searching for searchElement. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
* | ||
*/ | ||
function includes(a, fromIndex) { | ||
return function (as) { | ||
return as.includes(a, fromIndex); | ||
}; | ||
} | ||
/** | ||
* Calls a defined callback function on each element of an array, | ||
* and returns an array that contains the results. | ||
* | ||
* @param callbackfn The function invoked per iteration. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
*/ | ||
function map(f) { | ||
return function (as) { | ||
return as.map(f); | ||
}; | ||
} | ||
/** | ||
* Calls the specified callback function for all the elements in an array. | ||
* The return value of the callback function is the accumulated result, | ||
* and is provided as an argument in the next call to the callback function. | ||
* | ||
* @param callbackfn The function invoked per iteration. | ||
* @param initialValue The initial value. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
* | ||
* @example | ||
* | ||
* const words = ['one', 'two'] | ||
* const wordsLength = reduce<string, number>((acc, cur) => acc + cur.length, 0)(words) | ||
*/ | ||
function reduce(f, initial) { | ||
return function (as) { | ||
return as.reduce(function (acc, cur) { | ||
return f(acc, cur); | ||
}, initial); | ||
}; | ||
} | ||
/** | ||
* Reverses the elements in an Array. | ||
* | ||
* @param array | ||
* | ||
*/ | ||
function reverse(as) { | ||
var cp = as.slice(); | ||
return cp.reverse(); | ||
} | ||
/** | ||
* Determines whether the specified callback function returns true for any element of an array. | ||
* | ||
* @param callbackfn A function that accepts one argument. The some method calls the callbackfn function for each element in the array until the callbackfn returns a value which is coercible to the Boolean value true, or until the end of the array. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
* | ||
*/ | ||
function some(predicate) { | ||
return function (as) { | ||
return as.some(predicate); | ||
}; | ||
} | ||
/** | ||
* Append an element to the end of an array, creating a new array | ||
* | ||
* @param T item to append | ||
* @returns Returns a function that expects the `array` to append to | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2] | ||
* const result = append(3)(nums) | ||
* // result === [1, 2, 3] | ||
*/ | ||
function append(a) { | ||
return function (bs) { | ||
var copy = bs.slice(); | ||
copy.push(a); | ||
return copy; | ||
}; | ||
} | ||
/** | ||
* Creates an array with all falsy values removed. | ||
* | ||
* The values false, null, "", undefined, and NaN are considered falsy. | ||
* @param array The array to compact | ||
*/ | ||
function compact(as) { | ||
return as.filter(function (a) { | ||
if (typeof a === 'number' && a === 0) return true;else return Boolean(a); | ||
}); | ||
} | ||
/** | ||
* Finds the set (i.e. no duplicates) of all elements in the first list | ||
* not contained in the second list. | ||
* | ||
* @param array The array to inspect | ||
* @returns Returns a function that expects the `array` with the values | ||
* to exclude | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const otherdNums = [3, 2] | ||
* const result = difference(nums)(otherNums) | ||
* // result === [1, 4] | ||
*/ | ||
function difference(as) { | ||
return function (bs) { | ||
var s = new Set(bs); | ||
return as.filter(function (a) { | ||
return !s.has(a); | ||
}); | ||
}; | ||
} | ||
/** | ||
* Finds the set (i.e. no duplicates) of all elements in the first list | ||
* not contained in the second list. | ||
* Duplication is determined according to the value returned by applying | ||
* the supplied predicate to two list elements. | ||
* | ||
* @param function predicate | ||
* @param array The array to inspect | ||
* @returns Returns a function that expects the other `array` | ||
* with the values to exclude | ||
* | ||
* @example | ||
* | ||
* const items = [{ label: 'one', id: 1 }, { label: 'two', id: 2 }] | ||
* const otherItems = [{ label: 'three', id: 3 }, { label: 'one', id: 1 }] | ||
* const result = differenceBy(i => i.id, items)(otherItem) | ||
* // result === [{ label: 'two', id: 2}] | ||
*/ | ||
function differenceBy(f, as) { | ||
return function (bs) { | ||
var s = new Set(bs.map(f)); | ||
return as.filter(function (a) { | ||
return !s.has(f(a)); | ||
}); | ||
}; | ||
} | ||
/** | ||
* Drop a number of elements from the start of an array, creating a new array. | ||
* | ||
* @param number The number of elements to take | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const result = drop(2)(nums) | ||
* // result === [3, 4] | ||
*/ | ||
function drop(n) { | ||
return function (as) { | ||
return n < 0 ? as : as.slice(n); | ||
}; | ||
} | ||
/** | ||
* Drop a number of elements from the end of an array, creating a new array. | ||
* | ||
* @param number The number of elements to take | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const result = dropRigth(1)(nums) | ||
* // result === [1, 2, 3] | ||
*/ | ||
function dropRight(n) { | ||
return function (as) { | ||
return n < 1 ? as : as.slice(0, -n); | ||
}; | ||
} | ||
/** | ||
* Removes one level of nesting. | ||
* | ||
* @param array | ||
*/ | ||
function flatten(as) { | ||
var _Array$prototype; | ||
return (_Array$prototype = Array.prototype).concat.apply(_Array$prototype, as); | ||
} | ||
/** | ||
* Creates an object composed of keys generated from the results of running | ||
* each element of collection thru iteratee. | ||
* | ||
* The corresponding value of each key is an array of elements responsible | ||
* for generating the key. The iteratee is invoked with one argument: (item). | ||
* | ||
* @param function A function that returns the specific key | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const items = [{ name: 'ONE', id: 1 }, { name: 'TWO', id: 2 }] | ||
* const result = groupBy(i => i.name.toLowerCase())(items) | ||
* // result === { one: [{ name: 'one', id: 1], two: [{ name: 'two', id: 2] } | ||
*/ | ||
function groupBy(makeKey) { | ||
return function (as) { | ||
return as.reduce(function (acc, cur, _, __, k) { | ||
if (k === void 0) { | ||
k = makeKey(cur); | ||
} | ||
return (acc[k] || (acc[k] = [])).push(cur), acc; | ||
}, {}); | ||
}; | ||
} | ||
/** | ||
* Get the first element in an array, or `undefined` if the array is empty. | ||
* | ||
* @param array | ||
*/ | ||
function head(as) { | ||
return as[0]; | ||
} | ||
/** | ||
* Combines two lists into a set (i.e. no duplicates) composed | ||
* of those elements common to both lists. | ||
* | ||
* @param array The array to inspect | ||
* @returns Returns a function that expects the other `array` | ||
* to inspect | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const otherNums = [5, 6, 2, 1] | ||
* const result = intersection(nums)(otherNums) | ||
* // result === [1, 2] | ||
*/ | ||
function intersection(as) { | ||
return function (bs) { | ||
var s = new Set(bs); | ||
return as.filter(function (a) { | ||
return s.has(a); | ||
}); | ||
}; | ||
} | ||
/** | ||
* Combines two lists into a set (i.e. no duplicates) composed | ||
* of those elements common to both lists. | ||
* Duplication is determined according to the value returned by applying | ||
* the supplied predicate to two list elements. | ||
* | ||
* @param function predicate | ||
* @param array the array to inspect | ||
* @returns Returns a function that expects the other `array` to inspect | ||
* | ||
* @example | ||
* | ||
* const items = [{ label: 'one', id: 1 }, { label: 'two', id: 2 }] | ||
* const otherItems = [{ label: 'one', id: 3 }, { label: 'one', id: 1 }] | ||
* const result = intersectionBy(i => i.id, items)(otherItems) | ||
* // result === [{ label: 'one', id: 1 }] | ||
*/ | ||
function intersectionBy(f, as) { | ||
return function (bs) { | ||
var s = new Set(bs.map(f)); | ||
return as.filter(function (a) { | ||
return s.has(f(a)); | ||
}); | ||
}; | ||
} | ||
/** | ||
* Get the last element in an array, or `undefined` if the array is empty. | ||
* | ||
* @param array | ||
*/ | ||
function last(as) { | ||
return as[as.length - 1]; | ||
} | ||
/** | ||
* Sort of like flatMap. | ||
* | ||
* Calls a defined callback function on each element of every array | ||
* nested inside another one. | ||
* Then, flattens the result with depth 1. | ||
* | ||
* @param callbackfn The function invoked per iteration | ||
* @returns Returns a function that expects an `array` (`T[][]`) to iterate over | ||
* | ||
* @example | ||
* | ||
* const nestedNums = [[1, 2], [3, 4]] | ||
* const result = flatMap<number, number>(i => i * 2)(nestedNums) | ||
* // result === [2, 4, 6, 8] | ||
*/ | ||
function nestedMap(f) { | ||
return function (as) { | ||
var _Array$prototype2; | ||
return (_Array$prototype2 = Array.prototype).concat.apply(_Array$prototype2, as).map(f); | ||
}; | ||
} | ||
/** | ||
* Attaches an element to the front of an array, creating a new array. | ||
* | ||
* @param T item to prepend | ||
* @returns Returns a function that expects the `array` to prepend to | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2] | ||
* const result = prepend(3)(nums) | ||
* // result === [3, 1, 2] | ||
*/ | ||
function prepend(a) { | ||
return function (bs) { | ||
var copy = bs.slice(); | ||
copy.unshift(a); | ||
return copy; | ||
}; | ||
} | ||
/** | ||
* Attaches an array to the front of another array, creating a new array. | ||
* | ||
* @param array original array | ||
* @returns Returns a function that expects the `array` that will be attached to the original array | ||
* | ||
* @example | ||
* | ||
* const original = [1, 2] | ||
* const other = [3, 4] | ||
* const result = prependArr(original)(other) | ||
* // result ==== [3, 4, 1, 2] | ||
*/ | ||
function prependAll(as) { | ||
return function (bs) { | ||
return bs.concat(as); | ||
}; | ||
} | ||
/** | ||
* Create an array containing a range of integers, including both endpoints | ||
* | ||
* @param number start | ||
* @param number end | ||
* | ||
* @example | ||
* | ||
* const result = range(2, 4) | ||
* // result === [2, 3, 4] | ||
*/ | ||
function range(start, end) { | ||
if (start < 0 || end < 0) return []; | ||
return Array.from(Array(end + 1).keys()).slice(start); | ||
} | ||
/** | ||
* Sorts a list according to a list of iteratees. | ||
* | ||
* @param sortRecords `{ by: (a: T) => string | number; reverse?: boolean }[]` | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const items = [{ label: 'one', value: 1 }, { label: 'two', value: 2 }] | ||
* const sort = sortBy([{ by: i => i.label }, { by: i => i.value, reverse: true }]) | ||
* const result = sort(items) | ||
*/ | ||
function sortBy(cs) { | ||
return function (as) { | ||
var cp = as.slice(); | ||
return cp.sort(function (a, b) { | ||
for (var i = 0; i < cs.length; i++) { | ||
var _cs$i = cs[i], | ||
by = _cs$i.by, | ||
_cs$i$reverse = _cs$i.reverse, | ||
reverse = _cs$i$reverse === void 0 ? false : _cs$i$reverse; | ||
if (by(a) < by(b)) return reverse ? 1 : -1; | ||
if (by(a) > by(b)) return reverse ? -1 : 1; | ||
} | ||
return 0; | ||
}); | ||
}; | ||
} | ||
/** | ||
* Keep only a number of elements from the start of an array, creating a new array. | ||
* | ||
* @param number The number of elements to take | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const result = take(2)(nums) | ||
* // result === [1, 2] | ||
*/ | ||
function take(n) { | ||
return function (as) { | ||
return n < 0 ? as : as.slice(0, n); | ||
}; | ||
} | ||
/** | ||
* Keep only a number of elements from the end of an array, creating a new array. | ||
* | ||
* @param number The number of elements to take | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const result = takeRight(1)(nums) | ||
* // result === [4] | ||
*/ | ||
function takeRight(n) { | ||
return function (as) { | ||
return n < 0 || n > as.length ? as : as.slice(as.length - n); | ||
}; | ||
} | ||
/** | ||
* Creates a duplicate-free version of an array | ||
* | ||
* @param array | ||
*/ | ||
function uniq(as) { | ||
return Array.from(new Set(as)); | ||
} | ||
/** | ||
* Creates a duplicate-free version of an array, | ||
* with uniqueness determined by specific key. | ||
* | ||
* @param function A function that returns the specific key | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const items = [{ label: 'one', value: 1 }, { label: 'two', value: 1 }] | ||
* const result = uniqBy(i => i.value)(items) | ||
* // result === [{ label: 'one', value: 1 }] | ||
*/ | ||
function uniqBy(makeKey) { | ||
return function (as) { | ||
var bs = []; | ||
var keys = []; | ||
for (var i = 0; i < as.length; i++) { | ||
var k = makeKey(as[i]); | ||
if (keys.indexOf(k) < 0) { | ||
keys.push(k); | ||
bs.push(as[i]); | ||
} | ||
} | ||
return bs; | ||
}; | ||
} | ||
/** | ||
* Apply a function to pairs of elements at the same index in two arrays, | ||
* collecting the results in a new array. If one input array is short, excess elements | ||
* of the longer array are discarded. | ||
* | ||
* | ||
* @param function A function to combine grouped values | ||
* @returns Returns a function that expects the two `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const words = ['one', 'two', 'three'] | ||
* const nums = [1, 2] | ||
* const result = zipWith((w, n) => w + n)(words, num) | ||
* // result === ['one1', 'two2'] | ||
*/ | ||
function zipWih(f) { | ||
return function (t, u) { | ||
var zip = []; | ||
var length = Math.min(t.length, u.length); | ||
for (var i = 0; i < length; i++) { | ||
zip[i] = f(t[i], u[i]); | ||
} | ||
return zip; | ||
}; | ||
} | ||
var index = ({ | ||
__proto__: null, | ||
deleteAt: deleteAt, | ||
insertAt: insertAt, | ||
lookupAt: lookupAt, | ||
modifyAt: modifyAt, | ||
updateAt: updateAt, | ||
concat: concat, | ||
every: every, | ||
filter: filter, | ||
filterTypeGuard: filterTypeGuard, | ||
find: find, | ||
findIndex: findIndex, | ||
includes: includes, | ||
map: map, | ||
reduce: reduce, | ||
reverse: reverse, | ||
some: some, | ||
append: append, | ||
compact: compact, | ||
difference: difference, | ||
differenceBy: differenceBy, | ||
drop: drop, | ||
dropRight: dropRight, | ||
flatten: flatten, | ||
groupBy: groupBy, | ||
head: head, | ||
intersection: intersection, | ||
intersectionBy: intersectionBy, | ||
last: last, | ||
nestedMap: nestedMap, | ||
prepend: prepend, | ||
prependAll: prependAll, | ||
sortBy: sortBy, | ||
take: take, | ||
takeRight: takeRight, | ||
range: range, | ||
uniq: uniq, | ||
uniqBy: uniqBy, | ||
zipWih: zipWih | ||
}); | ||
/** | ||
* This function returns undefined. | ||
* | ||
*/ | ||
function noop() { | ||
return undefined; | ||
} | ||
/** | ||
* Runs the given function with the supplied value, then returns the value. | ||
* Useful for debugging when inside a `pipe` or `compose` function. | ||
* | ||
* @example | ||
* | ||
* pipe( | ||
* filter(w => w !== 'one'), | ||
* tap(console.log), // ['two'] | ||
* map(w => w.toUpperCase()) | ||
* )(['one', 'two']) | ||
* | ||
*/ | ||
function tap(f) { | ||
return function (a) { | ||
f(a); | ||
return a; | ||
}; | ||
} | ||
var index$1 = ({ | ||
__proto__: null, | ||
noop: noop, | ||
tap: tap | ||
}); | ||
/** | ||
* Returns a partial copy of an object omitting the keys specified. | ||
* | ||
* @example | ||
* | ||
* const item = { | ||
* label: 'ten', | ||
* id: 10, | ||
* isCool: true | ||
* } | ||
* | ||
* const updatedItem = omitKeys(item, 'label', 'isCool') | ||
* // updatedItem === { id: 10 } | ||
* | ||
*/ | ||
var omitKeys = function omitKeys(obj) { | ||
var ret = {}; | ||
var key; | ||
for (var _len = arguments.length, keys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
keys[_key - 1] = arguments[_key]; | ||
} | ||
for (key in obj) { | ||
if (!keys.includes(key)) { | ||
ret[key] = obj[key]; | ||
} | ||
} | ||
return ret; | ||
}; | ||
/** | ||
* Preserves the type of the array returned by Object.keys | ||
* | ||
* @example | ||
* | ||
* const item = { | ||
* label: 'ten', | ||
* id: 10, | ||
* isCool: true | ||
* } | ||
* | ||
* const keys = objectKeys(item) | ||
* // const keys: ("label" | "id" | "isCool")[] | ||
* | ||
*/ | ||
function objectKeys(obj) { | ||
return Object.keys(obj); | ||
} | ||
var index$2 = ({ | ||
__proto__: null, | ||
objectKeys: objectKeys, | ||
omitKeys: omitKeys | ||
}); | ||
/** | ||
* Check whether the value is defined or not. | ||
* | ||
*/ | ||
function isDefined(a) { | ||
return !(a === undefined || a === null); | ||
} | ||
/** | ||
* Check whether the value is empty. | ||
* | ||
* empty means `""`, `[]` or `{}`. | ||
* `undefined` and `null` will be considered empty as well. | ||
* | ||
*/ | ||
function isEmpty(a) { | ||
if (isArray(a)) return !a.length; | ||
if (isObject(a)) return !Object.keys(a).length; | ||
if (typeof a === 'string') return !a.length; | ||
if (typeof a === 'number' || typeof a === 'boolean') return false;else return !a; | ||
} | ||
/** | ||
* Check if object contains key. | ||
* | ||
*/ | ||
function hasKey(k) { | ||
return function (o) { | ||
return o.hasOwnProperty(k); | ||
}; | ||
} | ||
/* Helpers | ||
========================================================================== */ | ||
/** | ||
* Check if value is of type `[]`. | ||
* | ||
*/ | ||
function isArray(x) { | ||
return !!x && x.constructor === Array; | ||
} | ||
/** | ||
* Check if value is of type `{}`. | ||
* | ||
*/ | ||
function isObject(x) { | ||
return !!x && x.constructor === Object; | ||
} | ||
var index$3 = ({ | ||
__proto__: null, | ||
hasKey: hasKey, | ||
isDefined: isDefined, | ||
isEmpty: isEmpty | ||
}); | ||
function err(e) { | ||
@@ -1030,8 +132,4 @@ return { | ||
exports.array = index; | ||
exports.err = err; | ||
exports.func = index$1; | ||
exports.object = index$2; | ||
exports.ok = ok; | ||
exports.predicate = index$3; | ||
exports.result = result; | ||
@@ -1038,0 +136,0 @@ exports.when = when; |
@@ -1,2 +0,2 @@ | ||
"use strict";var n={__proto__:null,objectKeys:function(n){return Object.keys(n)},omitKeys:function(n){for(var r,t={},e=arguments.length,u=new Array(e>1?e-1:0),o=1;o<e;o++)u[o-1]=arguments[o];for(r in n)u.includes(r)||(t[r]=n[r]);return t}},r={__proto__:null,hasKey:function(n){return function(r){return r.hasOwnProperty(n)}},isDefined:function(n){return!(null==n)},isEmpty:function(n){return(r=n)&&r.constructor===Array?!n.length:function(n){return!!n&&n.constructor===Object}(n)?!Object.keys(n).length:"string"==typeof n?!n.length:"number"!=typeof n&&"boolean"!=typeof n&&!n;var r}};function t(n){var r={};function e(n){return null==n}return r.map=function(r){return e(n)?t(void 0):t(r(n))},r.filter=function(r){return!e(n)&&r(n)?t(n):t(void 0)},r.getOrElse=function(r){return e(n)?r:n},r.get=function(){return n},r.fold=function(r,t){return e(n)?r():t(n)},r}exports.array={__proto__:null,deleteAt:function(n){return function(r){if(!r[n])return r;var t=r.slice(0,n),e=r.slice(n+1);return[].concat(t,e)}},insertAt:function(n,r){return function(t){if(!t[n])return t;var e=t.slice(0,n),u=t.slice(n);return[].concat(e,[r],u)}},lookupAt:function(n){return function(r){return r[n]}},modifyAt:function(n,r){return function(t){if(!t[n])return t;var e=t.slice(0,n),u=t.slice(n+1),o=r(t[n]);return[].concat(e,[o],u)}},updateAt:function(n,r){return function(t){if(!t[n])return t;var e=t.slice(0,n),u=t.slice(n+1);return[].concat(e,[r],u)}},concat:function(n){return function(r){return n.concat(r)}},every:function(n){return function(r){return r.every(n)}},filter:function(n){return function(r){return r.filter(n)}},filterTypeGuard:function(n){return function(r){return r.filter(n)}},find:function(n){return function(r){return r.find(n)}},findIndex:function(n){return function(r){return r.findIndex(n)}},includes:function(n,r){return function(t){return t.includes(n,r)}},map:function(n){return function(r){return r.map(n)}},reduce:function(n,r){return function(t){return t.reduce((function(r,t){return n(r,t)}),r)}},reverse:function(n){return n.slice().reverse()},some:function(n){return function(r){return r.some(n)}},append:function(n){return function(r){var t=r.slice();return t.push(n),t}},compact:function(n){return n.filter((function(n){return"number"==typeof n&&0===n||Boolean(n)}))},difference:function(n){return function(r){var t=new Set(r);return n.filter((function(n){return!t.has(n)}))}},differenceBy:function(n,r){return function(t){var e=new Set(t.map(n));return r.filter((function(r){return!e.has(n(r))}))}},drop:function(n){return function(r){return n<0?r:r.slice(n)}},dropRight:function(n){return function(r){return n<1?r:r.slice(0,-n)}},flatten:function(n){var r;return(r=Array.prototype).concat.apply(r,n)},groupBy:function(n){return function(r){return r.reduce((function(r,t,e,u,o){return void 0===o&&(o=n(t)),(r[o]||(r[o]=[])).push(t),r}),{})}},head:function(n){return n[0]},intersection:function(n){return function(r){var t=new Set(r);return n.filter((function(n){return t.has(n)}))}},intersectionBy:function(n,r){return function(t){var e=new Set(t.map(n));return r.filter((function(r){return e.has(n(r))}))}},last:function(n){return n[n.length-1]},nestedMap:function(n){return function(r){var t;return(t=Array.prototype).concat.apply(t,r).map(n)}},prepend:function(n){return function(r){var t=r.slice();return t.unshift(n),t}},prependAll:function(n){return function(r){return r.concat(n)}},sortBy:function(n){return function(r){return r.slice().sort((function(r,t){for(var e=0;e<n.length;e++){var u=n[e],o=u.by,i=u.reverse,c=void 0!==i&&i;if(o(r)<o(t))return c?1:-1;if(o(r)>o(t))return c?-1:1}return 0}))}},take:function(n){return function(r){return n<0?r:r.slice(0,n)}},takeRight:function(n){return function(r){return n<0||n>r.length?r:r.slice(r.length-n)}},range:function(n,r){return n<0||r<0?[]:Array.from(Array(r+1).keys()).slice(n)},uniq:function(n){return Array.from(new Set(n))},uniqBy:function(n){return function(r){for(var t=[],e=[],u=0;u<r.length;u++){var o=n(r[u]);e.indexOf(o)<0&&(e.push(o),t.push(r[u]))}return t}},zipWih:function(n){return function(r,t){for(var e=[],u=Math.min(r.length,t.length),o=0;o<u;o++)e[o]=n(r[o],t[o]);return e}}},exports.err=function(n){return{_tag_:"Err",err:n}},exports.func={__proto__:null,noop:function(){},tap:function(n){return function(r){return n(r),r}}},exports.object=n,exports.ok=function(n){return{_tag_:"Ok",ok:n}},exports.predicate=r,exports.result=function(n){return{fold:function(r,t){return function(n){switch(n._tag_){case"Err":return!0;case"Ok":return!1}}(n)?r(n.err):t(n.ok)}}},exports.when=t,exports.whenAll=function(n){return n.some((function(n){return null==n}))?t(void 0):t(n)}; | ||
"use strict";function r(n){var t={};function e(r){return null==r}return t.map=function(t){return e(n)?r(void 0):r(t(n))},t.filter=function(t){return!e(n)&&t(n)?r(n):r(void 0)},t.getOrElse=function(r){return e(n)?r:n},t.get=function(){return n},t.fold=function(r,t){return e(n)?r():t(n)},t}exports.err=function(r){return{_tag_:"Err",err:r}},exports.ok=function(r){return{_tag_:"Ok",ok:r}},exports.result=function(r){return{fold:function(n,t){return function(r){switch(r._tag_){case"Err":return!0;case"Ok":return!1}}(r)?n(r.err):t(r.ok)}}},exports.when=r,exports.whenAll=function(n){return n.some((function(r){return null==r}))?r(void 0):r(n)}; | ||
//# sourceMappingURL=acd-utils.cjs.production.min.js.map |
@@ -1,899 +0,1 @@ | ||
/** | ||
* Delete the element at the specified index, creating a new array, | ||
* or returning the array as is if the index is out of bounds | ||
* | ||
* @param number The index of the element to remove. | ||
* @returns Returns a function that expects the `array` to modify. | ||
*/ | ||
function deleteAt(index) { | ||
return function (as) { | ||
if (!as[index]) return as; | ||
var b = as.slice(0, index); | ||
var a = as.slice(index + 1); | ||
return [].concat(b, a); | ||
}; | ||
} | ||
/** | ||
* Insert an element at the specified index, creating a new array, | ||
* or returning the array as is if the index is out of bounds | ||
* | ||
* @param number The index of the element to add. | ||
* @param T The item to add. | ||
* @returns Returns a function that expects the `array` to modify. | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const result = insertAt(1, 10)(nums) | ||
* // result === [1, 10, 2, 3, 4] | ||
* | ||
*/ | ||
function insertAt(index, a) { | ||
return function (as) { | ||
if (!as[index]) return as; | ||
var b = as.slice(0, index); | ||
var at = as.slice(index); | ||
return [].concat(b, [a], at); | ||
}; | ||
} | ||
/** | ||
* Read a value at a particular index from an array | ||
* | ||
* @param number The index of the element to read. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
* | ||
*/ | ||
function lookupAt(index) { | ||
return function (as) { | ||
return as[index]; | ||
}; | ||
} | ||
/** | ||
* Apply a function to the element at the specified index, creating a new array, | ||
* or returning the array as is if the index is out of bounds. | ||
* | ||
* @param number The index of the element to modify. | ||
* @param function A function that takes the element to modify as an argument. | ||
* @returns Returns a function that expects the `array` to modify. | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2] | ||
* const result = modifyAt(1, n => n * 2)(words) | ||
* // result === [1, 4] | ||
*/ | ||
function modifyAt(index, f) { | ||
return function (as) { | ||
if (!as[index]) return as; | ||
var b = as.slice(0, index); | ||
var at = as.slice(index + 1); | ||
var upd = f(as[index]); | ||
return [].concat(b, [upd], at); | ||
}; | ||
} | ||
/** | ||
* Change the element at the specified index, creating a new array, | ||
* or returning the array as us if the index is out of bounds | ||
* | ||
* @param number The index of the element to replace. | ||
* @param T The item that will take over. | ||
* @returns Returns a function that expects the `array` to modify. | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2] | ||
* const result = updateAt(1, 10)(nums) | ||
* // result === [1, 10] | ||
*/ | ||
function updateAt(index, a) { | ||
return function (as) { | ||
if (!as[index]) return as; | ||
var b = as.slice(0, index); | ||
var at = as.slice(index + 1); | ||
return [].concat(b, [a], at); | ||
}; | ||
} | ||
/** | ||
* Combines two arrays. | ||
* | ||
* @param array | ||
* @returns Returns a function that expects the `array` array to concatenate. | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2] | ||
* const otherNums = [3, 4] | ||
* const result = concat(nums)(otherNums) | ||
* // result === [1, 2, 3, 4] | ||
*/ | ||
function concat(as) { | ||
return function (bs) { | ||
return as.concat(bs); | ||
}; | ||
} | ||
/** | ||
* Determines whether all the members of an array satisfy the specified test. | ||
* | ||
* @param callbackfn A function that accepts one argument. The every method calls the callbackfn function for each element in the array until the callbackfn returns a value which is coercible to the Boolean value false, or until the end of the array. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
* | ||
*/ | ||
function every(predicate) { | ||
return function (as) { | ||
return as.every(predicate); | ||
}; | ||
} | ||
/** | ||
* Returns the elements of an array that meet the condition | ||
* specified in a callback function. | ||
* | ||
* @param callbackfn The function invoked per iteration. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
*/ | ||
function filter(f) { | ||
return function (as) { | ||
return as.filter(f); | ||
}; | ||
} | ||
/** | ||
* Returns the elements of an array that meet the condition | ||
* specified in a callback function and refines type. | ||
* | ||
* @param callbackfn The function invoked per iteration. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
*/ | ||
function filterTypeGuard(f) { | ||
return function (as) { | ||
return as.filter(f); | ||
}; | ||
} | ||
/** | ||
* Returns the value of the first element in the array where predicate is true, | ||
* and undefined otherwise. | ||
* | ||
* @param callbackfn The function that get called once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, immediately returns that element value. Otherwise returns undefined. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
*/ | ||
function find(f) { | ||
return function (as) { | ||
return as.find(f); | ||
}; | ||
} | ||
/** | ||
* Returns the index of the first element in the array where predicate is true, | ||
* and -1 otherwise. | ||
* | ||
* @param callbackfn The function that get called once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, immediately returns that element index. Otherwise returns undefined. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
*/ | ||
function findIndex(f) { | ||
return function (as) { | ||
return as.findIndex(f); | ||
}; | ||
} | ||
/** | ||
* Determines whether an array includes a certain element, | ||
* returning true or false as appropriate. | ||
* | ||
* @param searchElement The element to search for. | ||
* @param fromIndex A second optional argument to indicate the position in this array at which to begin searching for searchElement. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
* | ||
*/ | ||
function includes(a, fromIndex) { | ||
return function (as) { | ||
return as.includes(a, fromIndex); | ||
}; | ||
} | ||
/** | ||
* Calls a defined callback function on each element of an array, | ||
* and returns an array that contains the results. | ||
* | ||
* @param callbackfn The function invoked per iteration. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
*/ | ||
function map(f) { | ||
return function (as) { | ||
return as.map(f); | ||
}; | ||
} | ||
/** | ||
* Calls the specified callback function for all the elements in an array. | ||
* The return value of the callback function is the accumulated result, | ||
* and is provided as an argument in the next call to the callback function. | ||
* | ||
* @param callbackfn The function invoked per iteration. | ||
* @param initialValue The initial value. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
* | ||
* @example | ||
* | ||
* const words = ['one', 'two'] | ||
* const wordsLength = reduce<string, number>((acc, cur) => acc + cur.length, 0)(words) | ||
*/ | ||
function reduce(f, initial) { | ||
return function (as) { | ||
return as.reduce(function (acc, cur) { | ||
return f(acc, cur); | ||
}, initial); | ||
}; | ||
} | ||
/** | ||
* Reverses the elements in an Array. | ||
* | ||
* @param array | ||
* | ||
*/ | ||
function reverse(as) { | ||
var cp = as.slice(); | ||
return cp.reverse(); | ||
} | ||
/** | ||
* Determines whether the specified callback function returns true for any element of an array. | ||
* | ||
* @param callbackfn A function that accepts one argument. The some method calls the callbackfn function for each element in the array until the callbackfn returns a value which is coercible to the Boolean value true, or until the end of the array. | ||
* @returns Returns a function that expects the `array` to iterate over. | ||
* | ||
*/ | ||
function some(predicate) { | ||
return function (as) { | ||
return as.some(predicate); | ||
}; | ||
} | ||
/** | ||
* Append an element to the end of an array, creating a new array | ||
* | ||
* @param T item to append | ||
* @returns Returns a function that expects the `array` to append to | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2] | ||
* const result = append(3)(nums) | ||
* // result === [1, 2, 3] | ||
*/ | ||
function append(a) { | ||
return function (bs) { | ||
var copy = bs.slice(); | ||
copy.push(a); | ||
return copy; | ||
}; | ||
} | ||
/** | ||
* Creates an array with all falsy values removed. | ||
* | ||
* The values false, null, "", undefined, and NaN are considered falsy. | ||
* @param array The array to compact | ||
*/ | ||
function compact(as) { | ||
return as.filter(function (a) { | ||
if (typeof a === 'number' && a === 0) return true;else return Boolean(a); | ||
}); | ||
} | ||
/** | ||
* Finds the set (i.e. no duplicates) of all elements in the first list | ||
* not contained in the second list. | ||
* | ||
* @param array The array to inspect | ||
* @returns Returns a function that expects the `array` with the values | ||
* to exclude | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const otherdNums = [3, 2] | ||
* const result = difference(nums)(otherNums) | ||
* // result === [1, 4] | ||
*/ | ||
function difference(as) { | ||
return function (bs) { | ||
var s = new Set(bs); | ||
return as.filter(function (a) { | ||
return !s.has(a); | ||
}); | ||
}; | ||
} | ||
/** | ||
* Finds the set (i.e. no duplicates) of all elements in the first list | ||
* not contained in the second list. | ||
* Duplication is determined according to the value returned by applying | ||
* the supplied predicate to two list elements. | ||
* | ||
* @param function predicate | ||
* @param array The array to inspect | ||
* @returns Returns a function that expects the other `array` | ||
* with the values to exclude | ||
* | ||
* @example | ||
* | ||
* const items = [{ label: 'one', id: 1 }, { label: 'two', id: 2 }] | ||
* const otherItems = [{ label: 'three', id: 3 }, { label: 'one', id: 1 }] | ||
* const result = differenceBy(i => i.id, items)(otherItem) | ||
* // result === [{ label: 'two', id: 2}] | ||
*/ | ||
function differenceBy(f, as) { | ||
return function (bs) { | ||
var s = new Set(bs.map(f)); | ||
return as.filter(function (a) { | ||
return !s.has(f(a)); | ||
}); | ||
}; | ||
} | ||
/** | ||
* Drop a number of elements from the start of an array, creating a new array. | ||
* | ||
* @param number The number of elements to take | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const result = drop(2)(nums) | ||
* // result === [3, 4] | ||
*/ | ||
function drop(n) { | ||
return function (as) { | ||
return n < 0 ? as : as.slice(n); | ||
}; | ||
} | ||
/** | ||
* Drop a number of elements from the end of an array, creating a new array. | ||
* | ||
* @param number The number of elements to take | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const result = dropRigth(1)(nums) | ||
* // result === [1, 2, 3] | ||
*/ | ||
function dropRight(n) { | ||
return function (as) { | ||
return n < 1 ? as : as.slice(0, -n); | ||
}; | ||
} | ||
/** | ||
* Removes one level of nesting. | ||
* | ||
* @param array | ||
*/ | ||
function flatten(as) { | ||
var _Array$prototype; | ||
return (_Array$prototype = Array.prototype).concat.apply(_Array$prototype, as); | ||
} | ||
/** | ||
* Creates an object composed of keys generated from the results of running | ||
* each element of collection thru iteratee. | ||
* | ||
* The corresponding value of each key is an array of elements responsible | ||
* for generating the key. The iteratee is invoked with one argument: (item). | ||
* | ||
* @param function A function that returns the specific key | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const items = [{ name: 'ONE', id: 1 }, { name: 'TWO', id: 2 }] | ||
* const result = groupBy(i => i.name.toLowerCase())(items) | ||
* // result === { one: [{ name: 'one', id: 1], two: [{ name: 'two', id: 2] } | ||
*/ | ||
function groupBy(makeKey) { | ||
return function (as) { | ||
return as.reduce(function (acc, cur, _, __, k) { | ||
if (k === void 0) { | ||
k = makeKey(cur); | ||
} | ||
return (acc[k] || (acc[k] = [])).push(cur), acc; | ||
}, {}); | ||
}; | ||
} | ||
/** | ||
* Get the first element in an array, or `undefined` if the array is empty. | ||
* | ||
* @param array | ||
*/ | ||
function head(as) { | ||
return as[0]; | ||
} | ||
/** | ||
* Combines two lists into a set (i.e. no duplicates) composed | ||
* of those elements common to both lists. | ||
* | ||
* @param array The array to inspect | ||
* @returns Returns a function that expects the other `array` | ||
* to inspect | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const otherNums = [5, 6, 2, 1] | ||
* const result = intersection(nums)(otherNums) | ||
* // result === [1, 2] | ||
*/ | ||
function intersection(as) { | ||
return function (bs) { | ||
var s = new Set(bs); | ||
return as.filter(function (a) { | ||
return s.has(a); | ||
}); | ||
}; | ||
} | ||
/** | ||
* Combines two lists into a set (i.e. no duplicates) composed | ||
* of those elements common to both lists. | ||
* Duplication is determined according to the value returned by applying | ||
* the supplied predicate to two list elements. | ||
* | ||
* @param function predicate | ||
* @param array the array to inspect | ||
* @returns Returns a function that expects the other `array` to inspect | ||
* | ||
* @example | ||
* | ||
* const items = [{ label: 'one', id: 1 }, { label: 'two', id: 2 }] | ||
* const otherItems = [{ label: 'one', id: 3 }, { label: 'one', id: 1 }] | ||
* const result = intersectionBy(i => i.id, items)(otherItems) | ||
* // result === [{ label: 'one', id: 1 }] | ||
*/ | ||
function intersectionBy(f, as) { | ||
return function (bs) { | ||
var s = new Set(bs.map(f)); | ||
return as.filter(function (a) { | ||
return s.has(f(a)); | ||
}); | ||
}; | ||
} | ||
/** | ||
* Get the last element in an array, or `undefined` if the array is empty. | ||
* | ||
* @param array | ||
*/ | ||
function last(as) { | ||
return as[as.length - 1]; | ||
} | ||
/** | ||
* Sort of like flatMap. | ||
* | ||
* Calls a defined callback function on each element of every array | ||
* nested inside another one. | ||
* Then, flattens the result with depth 1. | ||
* | ||
* @param callbackfn The function invoked per iteration | ||
* @returns Returns a function that expects an `array` (`T[][]`) to iterate over | ||
* | ||
* @example | ||
* | ||
* const nestedNums = [[1, 2], [3, 4]] | ||
* const result = flatMap<number, number>(i => i * 2)(nestedNums) | ||
* // result === [2, 4, 6, 8] | ||
*/ | ||
function nestedMap(f) { | ||
return function (as) { | ||
var _Array$prototype2; | ||
return (_Array$prototype2 = Array.prototype).concat.apply(_Array$prototype2, as).map(f); | ||
}; | ||
} | ||
/** | ||
* Attaches an element to the front of an array, creating a new array. | ||
* | ||
* @param T item to prepend | ||
* @returns Returns a function that expects the `array` to prepend to | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2] | ||
* const result = prepend(3)(nums) | ||
* // result === [3, 1, 2] | ||
*/ | ||
function prepend(a) { | ||
return function (bs) { | ||
var copy = bs.slice(); | ||
copy.unshift(a); | ||
return copy; | ||
}; | ||
} | ||
/** | ||
* Attaches an array to the front of another array, creating a new array. | ||
* | ||
* @param array original array | ||
* @returns Returns a function that expects the `array` that will be attached to the original array | ||
* | ||
* @example | ||
* | ||
* const original = [1, 2] | ||
* const other = [3, 4] | ||
* const result = prependArr(original)(other) | ||
* // result ==== [3, 4, 1, 2] | ||
*/ | ||
function prependAll(as) { | ||
return function (bs) { | ||
return bs.concat(as); | ||
}; | ||
} | ||
/** | ||
* Create an array containing a range of integers, including both endpoints | ||
* | ||
* @param number start | ||
* @param number end | ||
* | ||
* @example | ||
* | ||
* const result = range(2, 4) | ||
* // result === [2, 3, 4] | ||
*/ | ||
function range(start, end) { | ||
if (start < 0 || end < 0) return []; | ||
return Array.from(Array(end + 1).keys()).slice(start); | ||
} | ||
/** | ||
* Sorts a list according to a list of iteratees. | ||
* | ||
* @param sortRecords `{ by: (a: T) => string | number; reverse?: boolean }[]` | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const items = [{ label: 'one', value: 1 }, { label: 'two', value: 2 }] | ||
* const sort = sortBy([{ by: i => i.label }, { by: i => i.value, reverse: true }]) | ||
* const result = sort(items) | ||
*/ | ||
function sortBy(cs) { | ||
return function (as) { | ||
var cp = as.slice(); | ||
return cp.sort(function (a, b) { | ||
for (var i = 0; i < cs.length; i++) { | ||
var _cs$i = cs[i], | ||
by = _cs$i.by, | ||
_cs$i$reverse = _cs$i.reverse, | ||
reverse = _cs$i$reverse === void 0 ? false : _cs$i$reverse; | ||
if (by(a) < by(b)) return reverse ? 1 : -1; | ||
if (by(a) > by(b)) return reverse ? -1 : 1; | ||
} | ||
return 0; | ||
}); | ||
}; | ||
} | ||
/** | ||
* Keep only a number of elements from the start of an array, creating a new array. | ||
* | ||
* @param number The number of elements to take | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const result = take(2)(nums) | ||
* // result === [1, 2] | ||
*/ | ||
function take(n) { | ||
return function (as) { | ||
return n < 0 ? as : as.slice(0, n); | ||
}; | ||
} | ||
/** | ||
* Keep only a number of elements from the end of an array, creating a new array. | ||
* | ||
* @param number The number of elements to take | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const nums = [1, 2, 3, 4] | ||
* const result = takeRight(1)(nums) | ||
* // result === [4] | ||
*/ | ||
function takeRight(n) { | ||
return function (as) { | ||
return n < 0 || n > as.length ? as : as.slice(as.length - n); | ||
}; | ||
} | ||
/** | ||
* Creates a duplicate-free version of an array | ||
* | ||
* @param array | ||
*/ | ||
function uniq(as) { | ||
return Array.from(new Set(as)); | ||
} | ||
/** | ||
* Creates a duplicate-free version of an array, | ||
* with uniqueness determined by specific key. | ||
* | ||
* @param function A function that returns the specific key | ||
* @returns Returns a function that expects the `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const items = [{ label: 'one', value: 1 }, { label: 'two', value: 1 }] | ||
* const result = uniqBy(i => i.value)(items) | ||
* // result === [{ label: 'one', value: 1 }] | ||
*/ | ||
function uniqBy(makeKey) { | ||
return function (as) { | ||
var bs = []; | ||
var keys = []; | ||
for (var i = 0; i < as.length; i++) { | ||
var k = makeKey(as[i]); | ||
if (keys.indexOf(k) < 0) { | ||
keys.push(k); | ||
bs.push(as[i]); | ||
} | ||
} | ||
return bs; | ||
}; | ||
} | ||
/** | ||
* Apply a function to pairs of elements at the same index in two arrays, | ||
* collecting the results in a new array. If one input array is short, excess elements | ||
* of the longer array are discarded. | ||
* | ||
* | ||
* @param function A function to combine grouped values | ||
* @returns Returns a function that expects the two `array` to iterate over | ||
* | ||
* @example | ||
* | ||
* const words = ['one', 'two', 'three'] | ||
* const nums = [1, 2] | ||
* const result = zipWith((w, n) => w + n)(words, num) | ||
* // result === ['one1', 'two2'] | ||
*/ | ||
function zipWih(f) { | ||
return function (t, u) { | ||
var zip = []; | ||
var length = Math.min(t.length, u.length); | ||
for (var i = 0; i < length; i++) { | ||
zip[i] = f(t[i], u[i]); | ||
} | ||
return zip; | ||
}; | ||
} | ||
var index = ({ | ||
__proto__: null, | ||
deleteAt: deleteAt, | ||
insertAt: insertAt, | ||
lookupAt: lookupAt, | ||
modifyAt: modifyAt, | ||
updateAt: updateAt, | ||
concat: concat, | ||
every: every, | ||
filter: filter, | ||
filterTypeGuard: filterTypeGuard, | ||
find: find, | ||
findIndex: findIndex, | ||
includes: includes, | ||
map: map, | ||
reduce: reduce, | ||
reverse: reverse, | ||
some: some, | ||
append: append, | ||
compact: compact, | ||
difference: difference, | ||
differenceBy: differenceBy, | ||
drop: drop, | ||
dropRight: dropRight, | ||
flatten: flatten, | ||
groupBy: groupBy, | ||
head: head, | ||
intersection: intersection, | ||
intersectionBy: intersectionBy, | ||
last: last, | ||
nestedMap: nestedMap, | ||
prepend: prepend, | ||
prependAll: prependAll, | ||
sortBy: sortBy, | ||
take: take, | ||
takeRight: takeRight, | ||
range: range, | ||
uniq: uniq, | ||
uniqBy: uniqBy, | ||
zipWih: zipWih | ||
}); | ||
/** | ||
* This function returns undefined. | ||
* | ||
*/ | ||
function noop() { | ||
return undefined; | ||
} | ||
/** | ||
* Runs the given function with the supplied value, then returns the value. | ||
* Useful for debugging when inside a `pipe` or `compose` function. | ||
* | ||
* @example | ||
* | ||
* pipe( | ||
* filter(w => w !== 'one'), | ||
* tap(console.log), // ['two'] | ||
* map(w => w.toUpperCase()) | ||
* )(['one', 'two']) | ||
* | ||
*/ | ||
function tap(f) { | ||
return function (a) { | ||
f(a); | ||
return a; | ||
}; | ||
} | ||
var index$1 = ({ | ||
__proto__: null, | ||
noop: noop, | ||
tap: tap | ||
}); | ||
/** | ||
* Returns a partial copy of an object omitting the keys specified. | ||
* | ||
* @example | ||
* | ||
* const item = { | ||
* label: 'ten', | ||
* id: 10, | ||
* isCool: true | ||
* } | ||
* | ||
* const updatedItem = omitKeys(item, 'label', 'isCool') | ||
* // updatedItem === { id: 10 } | ||
* | ||
*/ | ||
var omitKeys = function omitKeys(obj) { | ||
var ret = {}; | ||
var key; | ||
for (var _len = arguments.length, keys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
keys[_key - 1] = arguments[_key]; | ||
} | ||
for (key in obj) { | ||
if (!keys.includes(key)) { | ||
ret[key] = obj[key]; | ||
} | ||
} | ||
return ret; | ||
}; | ||
/** | ||
* Preserves the type of the array returned by Object.keys | ||
* | ||
* @example | ||
* | ||
* const item = { | ||
* label: 'ten', | ||
* id: 10, | ||
* isCool: true | ||
* } | ||
* | ||
* const keys = objectKeys(item) | ||
* // const keys: ("label" | "id" | "isCool")[] | ||
* | ||
*/ | ||
function objectKeys(obj) { | ||
return Object.keys(obj); | ||
} | ||
var index$2 = ({ | ||
__proto__: null, | ||
objectKeys: objectKeys, | ||
omitKeys: omitKeys | ||
}); | ||
/** | ||
* Check whether the value is defined or not. | ||
* | ||
*/ | ||
function isDefined(a) { | ||
return !(a === undefined || a === null); | ||
} | ||
/** | ||
* Check whether the value is empty. | ||
* | ||
* empty means `""`, `[]` or `{}`. | ||
* `undefined` and `null` will be considered empty as well. | ||
* | ||
*/ | ||
function isEmpty(a) { | ||
if (isArray(a)) return !a.length; | ||
if (isObject(a)) return !Object.keys(a).length; | ||
if (typeof a === 'string') return !a.length; | ||
if (typeof a === 'number' || typeof a === 'boolean') return false;else return !a; | ||
} | ||
/** | ||
* Check if object contains key. | ||
* | ||
*/ | ||
function hasKey(k) { | ||
return function (o) { | ||
return o.hasOwnProperty(k); | ||
}; | ||
} | ||
/* Helpers | ||
========================================================================== */ | ||
/** | ||
* Check if value is of type `[]`. | ||
* | ||
*/ | ||
function isArray(x) { | ||
return !!x && x.constructor === Array; | ||
} | ||
/** | ||
* Check if value is of type `{}`. | ||
* | ||
*/ | ||
function isObject(x) { | ||
return !!x && x.constructor === Object; | ||
} | ||
var index$3 = ({ | ||
__proto__: null, | ||
hasKey: hasKey, | ||
isDefined: isDefined, | ||
isEmpty: isEmpty | ||
}); | ||
function err(e) { | ||
@@ -1028,3 +130,3 @@ return { | ||
export { index as array, err, index$1 as func, index$2 as object, ok, index$3 as predicate, result, when, whenAll }; | ||
export { err, ok, result, when, whenAll }; | ||
//# sourceMappingURL=acd-utils.esm.js.map |
@@ -1,12 +0,4 @@ | ||
import * as array from './array'; | ||
import * as func from './function'; | ||
import * as object from './object'; | ||
import * as predicate from './predicate'; | ||
import { result, err, ok, Result } from './result/result'; | ||
import { when, whenAll } from './when/when'; | ||
export { array }; | ||
export { func }; | ||
export { object }; | ||
export { predicate }; | ||
export { err, ok, result, Result }; | ||
export { when, whenAll }; |
{ | ||
"name": "acd-utils", | ||
"repository": "github:acd02/utils", | ||
"version": "5.1.2", | ||
"version": "6.0.0", | ||
"license": "ISC", | ||
@@ -6,0 +6,0 @@ "author": "acd02", |
477
README.md
@@ -13,479 +13,7 @@ # Utils | ||
Then you can import each module individually: | ||
Then you can import these: | ||
- [array](#Array) | ||
- [function](#Function) | ||
- [object](#Object) | ||
- [predicate](#Predicate) | ||
- [result](#Result) | ||
- [when](#When) | ||
## Array | ||
`import { array } from 'acd-utils'` | ||
### Helpers: | ||
--- | ||
#### `append` | ||
Append an element to the end of an array, creating a new array. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2] | ||
const result = append(3)(nums) | ||
// result === [1, 2, 3] | ||
``` | ||
#### `compact` | ||
Creates an array with all falsy values removed. | ||
_example_ | ||
```typescript | ||
const items = [1, 2, 0, '', undefined, 'a'] | ||
const result = compact(items) | ||
// result === [1, 2, 0, 'a'] | ||
``` | ||
#### `difference` | ||
Finds the set (i.e. no duplicates) of all elements in the first list not contained in the second list. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const otherdNums = [3, 2] | ||
const result = difference(nums)(otherNums) | ||
// result === [1, 4] | ||
``` | ||
#### `differenceBy` | ||
Finds the set (i.e. no duplicates) of all elements in the first list not contained in the second list. | ||
Duplication is determined according to the value returned by applying the supplied predicate to two list elements. | ||
_example_ | ||
```typescript | ||
const items = [ | ||
{ label: 'one', id: 1 }, | ||
{ label: 'two', id: 2 }, | ||
] | ||
const otherItems = [ | ||
{ label: 'three', id: 3 }, | ||
{ label: 'one', id: 1 }, | ||
] | ||
const result = differenceBy(i => i.id, items)(otherItem) | ||
// result === [{ label: 'two', id: 2}] | ||
``` | ||
#### `drop` | ||
Drop a number of elements from the start of an array, creating a new array. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const result = drop(2)(nums) | ||
// result === [3, 4] | ||
``` | ||
#### `dropRight` | ||
Drop a number of elements from the end of an array, creating a new array. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const result = dropRigth(1)(nums) | ||
// result === [1, 2, 3] | ||
``` | ||
#### `flatten` | ||
Removes one level of nesting. | ||
_example_ | ||
```typescript | ||
const nums = [[1, 2], [3, 4]] | ||
const result = flatten((nums) | ||
// result === [1, 2, 3, 4] | ||
``` | ||
#### `groupBy` | ||
Creates an object composed of keys generated from the results of running | ||
each element of collection thru iteratee. | ||
The corresponding value of each key is an array of elements responsible | ||
for generating the key. The iteratee is invoked with one argument: (item). | ||
_example_ | ||
```typescript | ||
const items = [ | ||
{ name: 'ONE', id: 1 }, | ||
{ name: 'TWO', id: 2 }, | ||
] | ||
const result = groupBy(i => i.name.toLowerCase())(items) | ||
// result === { one: [{ name: 'one', id: 1], two: [{ name: 'two', id: 2] } | ||
``` | ||
#### `head` | ||
Get the first element in an array, or `undefined` if the array is empty. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const result = head(items) | ||
// result === 1 | ||
``` | ||
#### `intersection` | ||
Combines two lists into a set (i.e. no duplicates) composed of those elements common to both lists. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const otherNums = [5, 6, 2, 1] | ||
const result = intersection(nums)(otherNums) | ||
// result === [1, 2] | ||
``` | ||
#### `intersectionBy` | ||
Combines two lists into a set (i.e. no duplicates) composed of those elements common to both lists. | ||
Duplication is determined according to the value returned by applying the supplied predicate to two list elements. | ||
_example_ | ||
```typescript | ||
const items = [ | ||
{ label: 'one', id: 1 }, | ||
{ label: 'two', id: 2 }, | ||
] | ||
const otherItems = [ | ||
{ label: 'one', id: 3 }, | ||
{ label: 'one', id: 1 }, | ||
] | ||
const result = intersectionBy(i => i.id, items)(otherItems) | ||
// result === [{ label: 'one', id: 1 }] | ||
``` | ||
#### `last` | ||
Get the last element in an array, or `undefined` if the array is empty. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const result = last(4) | ||
// result === 1 | ||
``` | ||
#### `nestedMap` | ||
Sort of like flatMap. | ||
Calls a defined callback function on each element of every array nested inside another one. | ||
Then, flattens the result with depth 1. | ||
_example_ | ||
```typescript | ||
const nestedNums = [ | ||
[1, 2], | ||
[3, 4], | ||
] | ||
const result = flatMap<number, number>(i => i * 2)(nestedNums) | ||
// result === [2, 4, 6, 8] | ||
``` | ||
#### `preprend` | ||
Attaches an element to the front of an array, creating a new array. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2] | ||
const result = prepend(3)(nums) | ||
// result === [3, 1, 2] | ||
``` | ||
#### `preprendAll` | ||
Attaches an array to the front of another array, creating a new array. | ||
_example_ | ||
```typescript | ||
const original = [1, 2] | ||
const other = [3, 4] | ||
const result = prependArr(original)(other) | ||
// result ==== [3, 4, 1, 2] | ||
``` | ||
#### `sortBy` | ||
Sorts a list according to a list of comparators. | ||
_example_ | ||
```typescript | ||
const items = [ | ||
{ label: 'one', value: 1 }, | ||
{ label: 'three', value: 3 }, | ||
{ label: 'two', value: 2 }, | ||
{ label: 'z', value: 1 }, | ||
] | ||
const sort = sortBy([{ by: i => i.value }, { by: i => i.label, reverse: true }]) | ||
const result = sort(items) | ||
// result ==== [{ label: 'z', value: 1 }, { label: 'one', value: 1 }, { label: 'two', value: 2 }, { label: 'three', value: 3 }] | ||
``` | ||
#### `take` | ||
Keep only a number of elements from the start of an array, creating a new array. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const result = take(2)(nums) | ||
// result === [1, 2] | ||
``` | ||
#### `takeRight` | ||
Keep only a number of elements from the end of an array, creating a new array. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const result = takeRight(1)(nums) | ||
// result === [4] | ||
``` | ||
#### `range` | ||
Create an array containing a range of integers, including both endpoints | ||
_example_ | ||
```typescript | ||
const result = range(2, 4) | ||
// result === [2, 3, 4] | ||
``` | ||
#### `uniq` | ||
Creates a duplicate-free version of an array. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 1] | ||
const result = uniq(nums) | ||
// result === [1, 2, 3] | ||
``` | ||
#### `uniqBy` | ||
Creates a duplicate-free version of an array, with uniqueness determined by specific key. | ||
_example_ | ||
```typescript | ||
const items = [ | ||
{ label: 'one', value: 1 }, | ||
{ label: 'two', value: 1 }, | ||
] | ||
const result = uniqBy(i => i.value)(items) | ||
// result === [{ label: 'one', value: 1 }] | ||
``` | ||
#### `zipWith` | ||
Apply a function to pairs of elements at the same index in two arrays, collecting the results in a new array. | ||
If one input array is short, excess elements of the longer array are discarded. | ||
_example_ | ||
```typescript | ||
const words = ['one', 'two', 'three'] | ||
const nums = [1, 2] | ||
const result = zipWith((w, n) => w + n)(words, num) | ||
// result === ['one1', 'two2'] | ||
``` | ||
### Crud like operations: | ||
--- | ||
#### `deleteAt` | ||
Delete the element at the specified index, creating a new array, or returning the array as is if the index is out of bounds. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const result = deleteAt(2)(nums) | ||
// result === [1, 2, 4] | ||
``` | ||
#### `insertAt` | ||
Insert an element at the specified index, creating a new array, or returning the array as is if the index is out of bounds. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const result = insertAt(2, 10)(nums) | ||
// result === [1, 2, 10, 3, 4] | ||
``` | ||
#### `lookupAt` | ||
Read a value at a particular index from an array. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const result = lookupAt(2)(nums) | ||
// result === 3 | ||
``` | ||
#### `modifyAt` | ||
Apply a function to the element at the specified index, creating a new array, or returning the array as is if the index is out of bounds. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const result = modifyAt(2, n => n * 2)(nums) | ||
// result === [1, 2, 6, 4] | ||
``` | ||
#### `updateAt` | ||
Change the element at the specified index, creating a new array, or returning the array as us if the index is out of bounds. | ||
_example_ | ||
```typescript | ||
const nums = [1, 2, 3, 4] | ||
const result = updateAt(2, 20)(nums) | ||
// result === [1, 2, 20, 4] | ||
``` | ||
### Native (curried and iteratee-first data-last) array methods: | ||
--- | ||
- concat | ||
- every | ||
- filter | ||
- _filterTypeGuard_ | ||
- includes | ||
- map | ||
- reduce | ||
- some | ||
## Function | ||
`import { func } from 'acd-utils'` | ||
#### `noop` | ||
This function always returns `undefined`. | ||
#### `tap` | ||
Runs the given function with the supplied value, then returns the value. | ||
Useful for debugging when inside a `pipe` or `compose` function.his function always returns `undefined`. | ||
_example_ | ||
```typescript | ||
pipe( | ||
filter(w => w !== 'one'), | ||
tap(console.log), // ['two'] | ||
map(w => w.toUpperCase()), | ||
)(['one', 'two']) | ||
``` | ||
## Object | ||
`import { object } from 'acd-utils'` | ||
#### `objectKeys` | ||
Preserves the type of the array returned by `Object.keys` | ||
_example_ | ||
```typescript | ||
const item = { | ||
label: 'ten', | ||
id: 10, | ||
isCool: true, | ||
} | ||
const keys = objectKeys(item) | ||
// const keys: ("label" | "id" | "isCool")[] | ||
``` | ||
#### `omitKeys` | ||
Returns a partial copy of an object omitting the keys specified. | ||
_example_: | ||
```typescript | ||
const item = { | ||
label: 'ten', | ||
id: 10, | ||
isCool: true, | ||
} | ||
const updatedItem = omitKeys(item, 'label', 'isCool') | ||
// updatedItem === { id: 10 } | ||
``` | ||
## Predicate | ||
`import { predicate } from 'acd-utils'` | ||
#### `isDefined` | ||
Check whether the value is defined or not | ||
#### `isEmpty` | ||
Check if the value is empty. | ||
empty means `""`, `[]` or `{}`. | ||
`undefined` or `null` will also be considered as empty. | ||
## Result | ||
@@ -660,5 +188,4 @@ | ||
This collection was inspired by: | ||
This project was inspired by: | ||
- [lodash](https://lodash.com/) | ||
- [fp-ts](https://github.com/gcanti/fp-ts) | ||
@@ -665,0 +192,0 @@ - [space-lift](https://github.com/AlexGalays/spacelift) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
47583
13
405
195
1