@livechat/data-utils
Advanced tools
Comparing version 0.2.17 to 1.0.0
@@ -0,1 +1,7 @@ | ||
/** | ||
* adds two numbers | ||
* @example | ||
* add(1, 2) | ||
* // returns 3 | ||
*/ | ||
function add(first, second) { | ||
@@ -6,3 +12,9 @@ return first + second; | ||
var _ref = {}, | ||
hasOwnProperty = _ref.hasOwnProperty; | ||
hasOwnProperty = _ref.hasOwnProperty; | ||
/** | ||
* returns true or false depending if the provided property is present inside the provided object | ||
* @example | ||
* hasOwn('a', { a: 1, b: 2 }) | ||
* // returns true | ||
*/ | ||
function hasOwn(prop, obj) { | ||
@@ -12,3 +24,11 @@ return hasOwnProperty.call(obj, prop); | ||
/** | ||
* assigns properties of one or more source objects onto target object | ||
* @example | ||
* assign({ a: 1, b: 2 }, { b: 4, c: 5 }) | ||
* // returns { a: 1, b: 4, c: 5 } | ||
*/ | ||
function assign() { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
// eslint-disable-next-line no-func-assign | ||
@@ -19,3 +39,2 @@ assign = Object.assign || function (target) { | ||
} | ||
sources.forEach(function (source) { | ||
@@ -30,13 +49,24 @@ for (var key in source) { | ||
}; | ||
return assign.apply(void 0, arguments); | ||
} | ||
var capitalizeFirstLetter = function capitalizeFirstLetter(text) { | ||
/** | ||
* capitalizes first letter of string | ||
* @example | ||
* capitalizeFirstLetter('hello') | ||
* // returns 'Hello' | ||
*/ | ||
function capitalizeFirstLetter(text) { | ||
return text.charAt(0).toUpperCase() + text.slice(1); | ||
}; | ||
} | ||
/** | ||
* returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level | ||
* @example | ||
* const arr = [{ a: [1, 2] }, { a: [3, 4] }, { a: [5, 6] }] | ||
* flatMap(el => el.a, arr) | ||
* // returns [1, 2, 3, 4, 5, 6] | ||
*/ | ||
function flatMap(iteratee, arr) { | ||
var _ref; | ||
return (_ref = []).concat.apply(_ref, arr.map(iteratee)); | ||
@@ -51,18 +81,43 @@ } | ||
} | ||
return flatMap(fn, listOrFn); | ||
} | ||
/** | ||
* chunks provided array to specified size chunks | ||
* @example | ||
* chunk([1, 2, 3, 4], 2) | ||
* // returns [[1, 2], [3, 4]] | ||
*/ | ||
function chunk(arr, number) { | ||
if (number <= 0) { | ||
return []; | ||
} | ||
var chunked = []; | ||
for (var i = 0; i < arr.length; i += number) { | ||
chunked.push(arr.slice(i, i + number)); | ||
} | ||
return chunked; | ||
} | ||
var isArray = Array.isArray; | ||
/** | ||
* determines whether provided value is an array | ||
* @example | ||
* isArray([1, 2]) | ||
* // returns true | ||
* isArray('hello') | ||
* // returns false | ||
*/ | ||
function isArray(arr) { | ||
return Array.isArray(arr); | ||
} | ||
/** | ||
* returns true or false depending if the provided value is an object | ||
* @example | ||
* isObject({ a: 1 }) | ||
* // returns true | ||
* isObject([1, 2]) | ||
* // returns false | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
function isObject(obj) { | ||
@@ -72,4 +127,29 @@ return typeof obj === 'object' && obj !== null && !isArray(obj); | ||
/** | ||
* returns an array of the provided object keys | ||
* @example | ||
* keys({ a: 1, b: 2, c: 3 }) | ||
* // returns ['a', 'b', 'c'] | ||
*/ | ||
function keys(obj) { | ||
if ('keys' in Object && typeof Object.keys === 'function') { | ||
return Object.keys(obj); | ||
} | ||
var keysArray = []; | ||
for (var property in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, property)) { | ||
keysArray.push(property); | ||
} | ||
} | ||
return keysArray; | ||
} | ||
/** | ||
* maps values of the provided object | ||
* @example | ||
* mapValues(val => val.toUpperCase(), { a: 'foo', b: 'bar' }) | ||
* // returns { a: 'FOO', b: 'BAR' } | ||
*/ | ||
function mapValues(mapper, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
acc[key] = mapper(obj[key]); | ||
@@ -80,2 +160,8 @@ return acc; | ||
/** | ||
* returns an exact clone of the provided value with a new reference | ||
* @example | ||
* cloneDeep({ a: 1, b: 2 }) | ||
* // returns { a: 1, b: 2 } | ||
*/ | ||
function cloneDeep(value) { | ||
@@ -85,7 +171,5 @@ if (isArray(value)) { | ||
} | ||
if (isObject(value)) { | ||
return mapValues(cloneDeep, value); | ||
} | ||
return value; | ||
@@ -96,3 +180,4 @@ } | ||
// TODO: possibly could be rewritten using short-ish regexp | ||
var preserveCamelCase = function preserveCamelCase(input) { | ||
function preserveCamelCase(input) { | ||
var text = input; | ||
@@ -102,6 +187,4 @@ var isLastCharLower = false; | ||
var isLastLastCharUpper = false; | ||
for (var index = 0; index < text.length; index++) { | ||
var _char = text[index]; | ||
if (isLastCharLower && /[a-zA-Z]/.test(_char) && _char.toUpperCase() === _char) { | ||
@@ -124,27 +207,26 @@ text = text.slice(0, index) + '-' + text.slice(index); | ||
} | ||
return text; | ||
}; | ||
} | ||
/** | ||
* converts provided string to camel case | ||
* @example | ||
* camelCase('hello_world') | ||
* // returns 'helloWorld' | ||
*/ | ||
function camelCase(input) { | ||
var text = input.trim(); | ||
if (text.length === 0) { | ||
return ''; | ||
} | ||
if (text.length === 1) { | ||
return text.toLowerCase(); | ||
} | ||
if (/^[a-z\d]+$/.test(text)) { | ||
return text; | ||
} | ||
var hasUpperCase = text !== text.toLowerCase(); | ||
if (hasUpperCase) { | ||
text = preserveCamelCase(text); | ||
} | ||
text = text.replace(/^[_.\- ]+/, '').toLowerCase().replace(/[_.\- ]+(\w|$)/g, function (match, p1) { | ||
@@ -161,7 +243,5 @@ return p1.toUpperCase(); | ||
var value = collection[key]; | ||
if (value !== null && value !== undefined && !Number.isNaN(value)) { | ||
result[key] = value; | ||
} | ||
return result; | ||
@@ -171,2 +251,12 @@ }, {}); | ||
/** | ||
* returns a composed value from the provided arguments | ||
* @example | ||
* const f3 = (a: string) => `f3(${a})` | ||
* const f2 = (a: string) => `f2(${a})` | ||
* const f1 = (a: string) => `f1(${a})` | ||
* compose(f3, f2, f1)('arg') | ||
* // returns 'f3(f2(f1(arg)))' | ||
*/ | ||
function compose() { | ||
@@ -176,3 +266,2 @@ for (var _len = arguments.length, funcs = new Array(_len), _key = 0; _key < _len; _key++) { | ||
} | ||
return funcs.reduce(function (composed, next) { | ||
@@ -185,2 +274,7 @@ return function () { | ||
/** | ||
* delays the execution of a function until a certain period of time has passed without the function being called again | ||
* @example | ||
* debounce(1000, someFunction) | ||
*/ | ||
function debounce(ms, fn) { | ||
@@ -191,7 +285,5 @@ // actual return type of setTimeout differs per platform (browser vs node) | ||
clearTimeout(timeoutId); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
timeoutId = setTimeout.apply(void 0, [fn, ms].concat(args)); | ||
@@ -201,3 +293,9 @@ }; | ||
function mapKeys(mapper, obj) { | ||
/** | ||
* recurses through an object and transforms its keys – and the keys of any nested objects – based on the provided key mapping function | ||
* @example | ||
* deepMapKeys(key => `test_${key}`, { a: 1, b: { c: 2 } }) | ||
* // returns { test_a: 1, test_b: { test_c: 2 } } | ||
*/ | ||
function deepMapKeys(mapper, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
@@ -207,11 +305,9 @@ if (obj[key] && typeof obj[key] === 'object') { | ||
acc[mapper(key)] = obj[key].map(function (el) { | ||
return mapKeys(mapper, el); | ||
return deepMapKeys(mapper, el); | ||
}); | ||
return acc; | ||
} | ||
acc[mapper(key)] = mapKeys(mapper, obj[key]); | ||
acc[mapper(key)] = deepMapKeys(mapper, obj[key]); | ||
return acc; | ||
} | ||
acc[mapper(key)] = obj[key]; | ||
@@ -222,2 +318,8 @@ return acc; | ||
/** | ||
* deep merges provided objects by assigning the sources onto the target object passed as the first argument | ||
* @example | ||
* deepMerge({ a: 1, b: { c: 2 } }, { b: { d: 3 } }) | ||
* // returns { a: 1, b: { c: 2, d: 3 } } | ||
*/ | ||
function deepMerge(target) { | ||
@@ -227,6 +329,4 @@ for (var _len = arguments.length, sources = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
} | ||
if (!sources.length) return target; | ||
var source = sources.shift(); | ||
if (isObject(target) && isObject(source)) { | ||
@@ -236,3 +336,2 @@ for (var key in source) { | ||
var _Object$assign; | ||
if (!target[key]) Object.assign(target, (_Object$assign = {}, _Object$assign[key] = {}, _Object$assign)); | ||
@@ -242,3 +341,2 @@ deepMerge(target[key], source[key]); | ||
var _Object$assign2; | ||
Object.assign(target, (_Object$assign2 = {}, _Object$assign2[key] = source[key], _Object$assign2)); | ||
@@ -248,6 +346,11 @@ } | ||
} | ||
return deepMerge.apply(void 0, [target].concat(sources)); | ||
} | ||
/** | ||
* returns true or false depending if the provided value is null or undefined | ||
* @example | ||
* isNil(null) | ||
* // returns true | ||
*/ | ||
function isNil(value) { | ||
@@ -257,2 +360,11 @@ return value === null || value === undefined; | ||
/** | ||
* returns a function that invoked with a null or undefined values returns the provided default | ||
* @example | ||
* const defaulter = defaultTo('default') | ||
* defaulter('hello') | ||
* // returns 'hello' | ||
* defaulter(null) | ||
* // returns 'default' | ||
*/ | ||
function defaultTo(defaultValue) { | ||
@@ -264,2 +376,23 @@ return function (value) { | ||
/** | ||
* returns an array of keys of the object passed as the first argument that are not present, or that are present, but have different values, in object passed as the second argument | ||
* @example | ||
* diffKeys({ a: 1, b: 2, c: 3 }, { c: 3, d: 4 }) | ||
* // returns ['a', 'b'] | ||
*/ | ||
function diffKeys(obj, comparable) { | ||
return keys(obj).reduce(function (diffKeys, key) { | ||
if (obj[key] !== comparable[key]) { | ||
diffKeys.push(key); | ||
} | ||
return diffKeys; | ||
}, []); | ||
} | ||
/** | ||
* drops specific amount of elements of the provided array starting at the beginning | ||
* @example | ||
* drop(2, [1, 2, 3, 4]) | ||
* // returns [3, 4] | ||
*/ | ||
function drop(count, arr) { | ||
@@ -269,2 +402,8 @@ return arr.slice(count); | ||
/** | ||
* drops specific amount of elements of the provided array starting at the end | ||
* @example | ||
* dropRight(2, [1, 2, 3, 4]) | ||
* // returns [1, 2] | ||
*/ | ||
function dropRight(count, arr) { | ||
@@ -274,2 +413,10 @@ return arr.slice(0, -count); | ||
/** | ||
* returns an array if an array was provided, or a new array if provided value was not an array | ||
* @example | ||
* ensureArray([1, 2]) | ||
* // returns [1, 2] | ||
* ensureArray('test') | ||
* // returns ['test'] | ||
*/ | ||
function ensureArray(maybeArr) { | ||
@@ -279,4 +426,10 @@ return isArray(maybeArr) ? maybeArr : [maybeArr]; | ||
/** | ||
* returns an array of entries of the provided object | ||
* @example | ||
* entries({ a: 1, b: 2 }) | ||
* // returns [['a', 1], ['b', 2]] | ||
*/ | ||
function entries(obj) { | ||
return Object.keys(obj).map(function (key) { | ||
return keys(obj).map(function (key) { | ||
return [key, obj[key]]; | ||
@@ -286,6 +439,11 @@ }); | ||
/** | ||
* returns the first element of the provided array that returns true for the predicate function, or undefined if the element was not found | ||
* @example | ||
* find(el => el === 2, [1, 2, 3]) | ||
* // returns 2 | ||
*/ | ||
function find(predicate, arr) { | ||
for (var index = 0; index < arr.length; index++) { | ||
var element = arr[index]; | ||
if (predicate(element)) { | ||
@@ -297,2 +455,10 @@ return element; | ||
/** | ||
* returns the index of the element of the provided array that returns true for the predicate function, or -1 if the element was not found | ||
* @example | ||
* findIndex(el => el === 2, [1, 2, 3]) | ||
* // returns 1 | ||
* findIndex(el => el === 5, [1, 2, 3]) | ||
* // returns -1 | ||
*/ | ||
function findIndex(predicate, arr) { | ||
@@ -304,12 +470,25 @@ for (var index = 0; index < arr.length; index++) { | ||
} | ||
return -1; | ||
} | ||
/** | ||
* returns the key of the provided object that returns true for the predicate function, or undefined if the key was not found | ||
* @example | ||
* findKey(el => el === 1, { a: 1, b: { c: 2 } }) | ||
* // returns 'a' | ||
* findKey(el => el === 2, { a: 1, b: { c: 2 } }) | ||
* // returns undefined | ||
*/ | ||
function findKey(predicate, obj) { | ||
return find(function (key) { | ||
return predicate(obj[key]); | ||
}, Object.keys(obj)); | ||
}, keys(obj)); | ||
} | ||
/** | ||
* returns the last element of the provided array that returns true for the predicate function, or undefined if the element was not found | ||
* @example | ||
* findLast(el => el % 2 === 0, [1, 2, 3, 4]) | ||
* // returns 4 | ||
*/ | ||
function findLast(predicate, arr) { | ||
@@ -323,2 +502,8 @@ for (var index = arr.length - 1; index >= 0; index--) { | ||
/** | ||
* returns the index of the last element of the provided array that returns true for the predicate function, starting from the specified index element to the begining of the array, or -1 if the element was not found | ||
* @example | ||
* findLastIndexFrom(el => el % 2 === 0, 3, [1, 2, 3, 4, 5, 6]) | ||
* // returns 3 | ||
*/ | ||
function findLastIndexFrom(predicate, startIndex, arr) { | ||
@@ -330,6 +515,11 @@ for (var index = startIndex; index >= 0; index--) { | ||
} | ||
return -1; | ||
} | ||
/** | ||
* returns the index of the last element of the provided array that returns true for the predicate function, or -1 if the element was not found | ||
* @example | ||
* findLastIndex(el => el % 2 === 0, [1, 2, 3, 4]) | ||
* // returns 3 | ||
*/ | ||
function findLastIndex(predicate, arr) { | ||
@@ -339,12 +529,25 @@ return findLastIndexFrom(predicate, arr.length - 1, arr); | ||
/** | ||
* returns an array of requested length filled with provided value | ||
* @example | ||
* filledArray(3, 1) | ||
* // returns [1, 1, 1] | ||
*/ | ||
function filledArray(length, value) { | ||
if (length <= 0) { | ||
return []; | ||
} | ||
var arr = []; | ||
while (length--) { | ||
arr.push(value); | ||
} | ||
return arr; | ||
} | ||
/** | ||
* returns the provided value | ||
* @example | ||
* identity('hello') | ||
* // returns 'hello' | ||
*/ | ||
function identity(value) { | ||
@@ -354,2 +557,10 @@ return value; | ||
/** | ||
* flattens the provided array by one level | ||
* @example | ||
* flatten([1, 2, [3, 4]]) | ||
* // returns [1, 2, 3, 4] | ||
* flatten([1, 2, [3, [4, 5]], 6]) | ||
* // returns [1, 2, 3, [4, 5], 6] | ||
*/ | ||
function flatten(arr) { | ||
@@ -359,4 +570,12 @@ return flatMap(identity, arr); | ||
/** | ||
* invokes the provided callback for each of the provided object fields | ||
* @example | ||
* const obj = { a: 1, b: 2, c: 3 } | ||
* const arr: string[] = [] | ||
* forOwn(el => arr.push(el.toString()), obj) | ||
* // arr equals ['1', '2', '3'] | ||
*/ | ||
function forOwn(callback, obj) { | ||
return Object.keys(obj).forEach(function (key) { | ||
return keys(obj).forEach(function (key) { | ||
callback(obj[key], key); | ||
@@ -366,6 +585,13 @@ }); | ||
/** | ||
* returns an object constructed from the provided array of key, value pairs' | ||
* @example | ||
* const arr = [['a', 1], ['b', 2], ['c', 3]] | ||
* fromPairs(arr) | ||
* // returns { a: 1, b: 2, c: 3 } | ||
*/ | ||
function fromPairs(pairs) { | ||
return pairs.reduce(function (obj, _ref) { | ||
var key = _ref[0], | ||
value = _ref[1]; | ||
value = _ref[1]; | ||
obj[key] = value; | ||
@@ -376,2 +602,8 @@ return obj; | ||
/** | ||
* generates a random id | ||
* @example | ||
* generateRandomId() | ||
* // returns 'd1rjknhhch8' | ||
*/ | ||
function generateRandomId() { | ||
@@ -381,2 +613,8 @@ return Math.random().toString(36).substring(2); | ||
/** | ||
* generates an unique id based on the provided object keys | ||
* @example | ||
* generateUniqueId({ xuvarw8cao: 1, b837g2nba1d: 2 }) | ||
* // returns 'd1rjknhhch8' | ||
*/ | ||
function generateUniqueId(map) { | ||
@@ -387,3 +625,11 @@ var id = generateRandomId(); | ||
// based on https://github.com/developit/dlv/blob/d7ec976d12665f1c25dec2acf955dfc2e8757a9c/index.js | ||
/** | ||
* returns the value from the specififed path from the provided object | ||
* @example | ||
* const obj = { a: { b: [1, 2, 3] } } | ||
* get('a.b.1', obj) | ||
* // returns 2 | ||
* get(['a', 'b', '1'], obj) | ||
* // returns 2 | ||
*/ | ||
function get(propPath, obj) { | ||
@@ -393,10 +639,17 @@ var arrPath = typeof propPath === 'string' ? propPath.split('.') : propPath; | ||
var result = obj; | ||
while (result && pathPartIndex < arrPath.length) { | ||
result = result[arrPath[pathPartIndex++]]; | ||
} | ||
return result; | ||
} | ||
/** | ||
* returns the value from the specififed path from the provided object or the default value if the path element was not found | ||
* @example | ||
* const obj = { a: { b: [1, 2, 3] } } | ||
* getOr(null, 'a.b.1', obj) | ||
* // returns 2 | ||
* getOr(null, 'a.c', obj) | ||
* // returns null | ||
*/ | ||
function getOr(defaultValue, prop, obj) { | ||
@@ -407,2 +660,19 @@ var propValue = get(prop, obj); | ||
/** | ||
* groups values from the provided array or object based on the result of the provided mapper function | ||
* @example | ||
* const arr = [ | ||
* { a: 1, b: 2 }, | ||
* { a: 2, b: 4 }, | ||
* { a: 3, b: 6 }, | ||
* ] | ||
* groupBy(el => (el.a % 2 === 0 ? 'even' : 'odd'), arr) | ||
* // returns { | ||
* // odd: [ | ||
* // { a: 1, b: 2 }, | ||
* // { a: 3, b: 6 }, | ||
* // ], | ||
* // even: [{ a: 2, b: 4 }], | ||
* // } | ||
*/ | ||
function groupBy(mapper, collection) { | ||
@@ -418,4 +688,14 @@ return Object.keys(collection).reduce(function (grouped, key) { | ||
/** | ||
* groups values from the provided object based on the result of the provided key mapping function | ||
* @example | ||
* const obj = { a: 1, b: 2, c: 3 } | ||
* groupKeys(el => (el === 'a' ? 'aaa' : 'rest'), obj) | ||
* // returns { | ||
* // aaa: { a: 1 }, | ||
* // rest: { b: 2, c: 3 }, | ||
* // } | ||
*/ | ||
function groupKeys(mapper, obj) { | ||
return Object.keys(obj).reduce(function (grouped, key) { | ||
return keys(obj).reduce(function (grouped, key) { | ||
var groupKey = mapper(key); | ||
@@ -428,2 +708,11 @@ grouped[groupKey] = grouped[groupKey] || {}; | ||
/** | ||
* returns true or false depending if the provided value is present inside the provided array or string | ||
* @example | ||
* includes('a', ['a', 'b', 'c']) | ||
* // returns true | ||
* includes('d', 'abc') | ||
* // returns false | ||
*/ | ||
function includes(value, arrOrStr) { | ||
@@ -433,2 +722,8 @@ return arrOrStr.indexOf(value) !== -1; | ||
/** | ||
* returns true or false depending if the provided value is an empty object or an empty array | ||
* @example | ||
* isEmpty({}) | ||
* // returns true | ||
*/ | ||
function isEmpty(collection) { | ||
@@ -438,14 +733,43 @@ return (isArray(collection) ? collection : Object.keys(collection)).length === 0; | ||
function isFalsy (value) { | ||
/** | ||
* returns true or false depending if the provided value is falsy | ||
* @example | ||
* isFalsy(0) | ||
* // returns true | ||
*/ | ||
function isFalsy(value) { | ||
return !value; | ||
} | ||
function isTruthy (value) { | ||
/** | ||
* returns true or false depending if the provided value is truthy | ||
* @example | ||
* isTruthy(1) | ||
* // returns true | ||
*/ | ||
function isTruthy(value) { | ||
return !!value; | ||
} | ||
var isPromise = function isPromise(promise) { | ||
/** | ||
* returns true or false depending if the provided value is a Promise | ||
* @example | ||
* isPromise(new Promise(res => res(null))) | ||
* // returns true | ||
*/ | ||
function isPromise(promise) { | ||
return !!promise && typeof promise.then === 'function'; | ||
}; | ||
} | ||
/** | ||
* constructs an object from the provided array of objects, grouped by the value of the specified key | ||
* @example | ||
* const arr = [ | ||
* { a: 'foo', b: 'bar' }, | ||
* { a: 'foo', b: 'baz' }, | ||
* { a: 'test', b: 'bab' }, | ||
* ] | ||
* keyBy('a', arr) | ||
* // returns { foo: { a: 'foo', b: 'baz' }, test: { a: 'test', b: 'bab' } } | ||
*/ | ||
function keyBy(prop, arr) { | ||
@@ -458,22 +782,24 @@ return arr.reduce(function (acc, el) { | ||
function keys(obj) { | ||
var keysArray = []; | ||
for (var property in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, property)) { | ||
keysArray.push(property); | ||
} | ||
} | ||
return keysArray; | ||
} | ||
// TODO: this should return `T | undefined` to match native behavior | ||
/** | ||
* returns the last element of the provided array or undefined when array is empty | ||
* @example | ||
* last([1, 2, 3]) | ||
* // returns 3 | ||
*/ | ||
function last(arr) { | ||
return arr.length > 0 ? arr[arr.length - 1] : null; | ||
return arr.length > 0 ? arr[arr.length - 1] : undefined; | ||
} | ||
function mapKeys$1(mapper, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
acc[mapper(key)] = obj[key]; | ||
/** | ||
* maps keys of the provided object | ||
* @example | ||
* mapKeys(key => key.toUpperCase(), { a: 1, b: 2 }) | ||
* // returns { A: 1, B: 2 } | ||
*/ | ||
function mapKeys(mapper, obj) { | ||
return keys(obj).reduce(function (acc, key) { | ||
Object.defineProperty(acc, mapper(key), { | ||
value: obj[key], | ||
enumerable: true | ||
}); | ||
return acc; | ||
@@ -483,4 +809,10 @@ }, {}); | ||
/** | ||
* maps values and keys of the provided object | ||
* @example | ||
* mapValuesIndexed((val, key) => `${key}-${val}`, { a: 1, b: 2 }) | ||
* // returns { a: 'a-1', b: 'b-2' } | ||
*/ | ||
function mapValuesIndexed(iteratee, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
acc[key] = iteratee(obj[key], key); | ||
@@ -491,2 +823,8 @@ return acc; | ||
/** | ||
* deep merges the two provided objects | ||
* @example | ||
* merge({ a: 1 }, { b: 2 }) | ||
* // returns { a: 1, b: 2 } | ||
*/ | ||
function merge(objA, objB) { | ||
@@ -496,34 +834,11 @@ if (isEmpty(objB)) { | ||
} | ||
var result = {}; | ||
forOwn(function (value, key) { | ||
if (hasOwn(key, objB)) { | ||
if (isObject(objA[key]) && isObject(objB[key])) { | ||
result[key] = merge(objA[key], objB[key]); | ||
} else if (isArray(objA[key]) && isArray(objB[key])) { | ||
var length = Math.max(objA[key].length, objB[key].length); | ||
result[key] = new Array(length); | ||
for (var i = 0; i < length; i++) { | ||
if (i in objB[key]) { | ||
result[key][i] = objB[key][i]; | ||
} else if (i in objA[key]) { | ||
result[key][i] = objA[key][i]; | ||
} | ||
} | ||
} else { | ||
result[key] = objB[key]; | ||
} | ||
} else { | ||
result[key] = objA[key]; | ||
} | ||
}, objA); | ||
forOwn(function (value, key) { | ||
if (!hasOwn(key, result)) { | ||
result[key] = objB[key]; | ||
} | ||
}, objB); | ||
return result; | ||
return deepMerge({}, objA, objB); | ||
} | ||
/** | ||
* deep merges all provided objects | ||
* @example | ||
* mergeAll({ a: 1 }, { b: 2 }, { c: 3 }) | ||
* // returns { a: 1, b: 2, c: 3 } | ||
*/ | ||
function mergeAll(objs) { | ||
@@ -533,5 +848,4 @@ if (objs.length === 0) { | ||
} | ||
var first = objs[0], | ||
rest = objs.slice(1); | ||
rest = objs.slice(1); | ||
return rest.reduce(function (merged, obj) { | ||
@@ -542,2 +856,9 @@ return merge(merged, obj); | ||
/** | ||
* memoizes the provided function so it returns cached results but based on the provided key resolver | ||
* @example | ||
* const memoizedFunc = memoizeWith(key => (key === 'a' ? key : 'bar'), (val: string) => ({ current: val })) | ||
* memoizedFunc('a') === memoizedFunc('a') // true | ||
* memoizedFunc('b') === memoizedFunc('c') // true | ||
*/ | ||
function memoizeWith(keyResolver, func) { | ||
@@ -547,7 +868,5 @@ var cache = {}; | ||
var key = keyResolver.apply(void 0, arguments); | ||
if (hasOwn(key, cache)) { | ||
return cache[key]; | ||
} | ||
var value = func.apply(void 0, arguments); | ||
@@ -560,2 +879,10 @@ cache[key] = value; | ||
// TODO: technically this should accept AnyFunction but it doesn't type check currently with that for some reason | ||
/** | ||
* memoizes the provided function so it returns cached results when invoked with the same arguments | ||
* @example | ||
* const memoizedFunc = memoize((val: number) => ({ current: val }) | ||
* memoizedFunc(3) === memoizedFunc(3) | ||
* // returns true | ||
*/ | ||
function memoize(func) { | ||
@@ -565,2 +892,15 @@ return memoizeWith(identity, func); | ||
/** | ||
* memoizes the provided function so it returns cached results but only with one cache key | ||
* @example | ||
* const memoizedFunc = memoizeOne((val: number) => ({ current: val }) | ||
* | ||
* const resultFor1 = memoizedFunc(1) // { current: 1 } | ||
* resultFor1 === memoizedFunc(1) // true | ||
* resultFor1 === memoizedFunc(1) // true | ||
* | ||
* const resultFor2 = memoizedFunc(2) // { current: 2 } | ||
* resultFor2 === memoizedFunc(2) // true | ||
* resultFor1 === memoizedFunc(1) // false | ||
*/ | ||
function memoizeOne(fn) { | ||
@@ -574,3 +914,2 @@ var called = false; | ||
} | ||
called = true; | ||
@@ -583,7 +922,18 @@ cacheKey = arguments.length <= 0 ? undefined : arguments[0]; | ||
/** | ||
* does literally nothing | ||
* @example | ||
* somethingAsyncAndDangerous().catch(noop) | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
function noop() {} | ||
/** | ||
* returns an array of values of the provided object | ||
* @example | ||
* values({ a: 1, b: 2, c: 3 }) | ||
* // returns [1, 2, 3] | ||
*/ | ||
function values(obj) { | ||
return Object.keys(obj).map(function (key) { | ||
return keys(obj).map(function (key) { | ||
return obj[key]; | ||
@@ -593,2 +943,13 @@ }); | ||
/** | ||
* sorts values of the provided object or array based on the provided mapping function or the provided prop string | ||
* @example | ||
* const obj = { | ||
* a: { chats: 1 }, | ||
* b: { chats: 3 }, | ||
* c: { chats: 2 }, | ||
* } | ||
* numericSortBy(el => el.chats * -1, obj) | ||
* // returns [{ chats: 3 }, { chats: 2 }, { chats: 1 }] | ||
*/ | ||
function numericSortBy(propOrMapper, collection) { | ||
@@ -603,8 +964,13 @@ var mapper = typeof propOrMapper === 'function' ? propOrMapper : function (element) { | ||
/** | ||
* returns an object the same as the provided one but without the fields omitted by the predicate function | ||
* @example | ||
* omitByIndexed((val, key) => key === 'b', { a: 1, b: 2, c: 3 }) | ||
* // returns { a: 1, c: 3 } | ||
*/ | ||
function omitByIndexed(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
if (!predicate(obj[key], key)) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
@@ -614,2 +980,8 @@ }, {}); | ||
/** | ||
* returns the provided object but with specified keys omitted | ||
* @example | ||
* omit(['a'], { a: 1, b: 2 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function omit(keys, obj) { | ||
@@ -621,8 +993,13 @@ return omitByIndexed(function (_, key) { | ||
/** | ||
* returns an object same as the provided one but without the fields omitted by the predicate function | ||
* @example | ||
* omitBy(el => el % 2 === 0, { a: 1, b: 2, c: 3 }) | ||
* // returns { a: 1, c: 3 } | ||
*/ | ||
function omitBy(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
if (!predicate(obj[key])) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
@@ -632,2 +1009,12 @@ }, {}); | ||
/** | ||
* returns a particular function to be executed only a single time | ||
* @example | ||
* const results = [1, 2, 3, 4, 5] | ||
* const getResult = once(() => results.pop()) | ||
* getResult() // 5 | ||
* getResult() // 5 | ||
* getResult() // 5 | ||
* | ||
*/ | ||
function once(fn) { | ||
@@ -640,3 +1027,2 @@ var called = false; | ||
} | ||
called = true; | ||
@@ -647,2 +1033,9 @@ return result = fn.apply(void 0, arguments); | ||
/** | ||
* returns a function that called invokes the provided array of functions with the call arguments | ||
* @example | ||
* const func = over([val => val + 1, val => val * 10, Boolean]) | ||
* func(1) | ||
* // returns [2, 10, true] | ||
*/ | ||
function over(fns) { | ||
@@ -653,3 +1046,2 @@ return function () { | ||
} | ||
return fns.map(function (fn) { | ||
@@ -661,2 +1053,8 @@ return fn.apply(void 0, args); | ||
/** | ||
* returns the specified amount of the provided array elements starting from the end | ||
* @example | ||
* takeLast(2, ['a', 'b', 'c', 'd']) | ||
* // returns ['c', 'd'] | ||
*/ | ||
function takeLast(count, arr) { | ||
@@ -666,2 +1064,9 @@ return arr.slice(-count); | ||
/** | ||
* returns a function that called invokes the provided arguments transformers | ||
* @example | ||
* const func = overArgs((a, b, c) => [a, b, c], [n => n * n, Boolean]) | ||
* func(2, 0, 1) | ||
* // returns [4, false, 1] | ||
*/ | ||
function overArgs(fn, transformers) { | ||
@@ -672,3 +1077,2 @@ return function () { | ||
} | ||
var transformed = transformers.map(function (transform, index) { | ||
@@ -681,2 +1085,8 @@ return transform(args[index]); | ||
/** | ||
* returns a tuple from the two provided values | ||
* @example | ||
* pair('a', 'b') | ||
* // returns ['a', 'b'] | ||
*/ | ||
function pair(a, b) { | ||
@@ -686,4 +1096,10 @@ return [a, b]; | ||
/** | ||
* returns a tuple of valid and invalid parts of the object as defined in validation function passed in the first argument | ||
* @example | ||
* partitionObject(el => el % 2 === 0, { a: 1, b: 2, c: 3 }) | ||
* // returns [{ b: 2 }, { a: 1, c: 3 }] | ||
*/ | ||
function partitionObject(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
acc[predicate(obj[key]) ? 0 : 1][key] = obj[key]; | ||
@@ -694,2 +1110,8 @@ return acc; | ||
/** | ||
* picks specified properties from the object | ||
* @example | ||
* pick(['b'], { a: 1, b: 2 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function pick(props, obj) { | ||
@@ -702,8 +1124,13 @@ return props.reduce(function (acc, prop) { | ||
/** | ||
* returns an object same as the provided one but with the fields picked by the predicate function | ||
* @example | ||
* pickBy(el => el % 2 === 0, { a: 1, b: 2, c: 3 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function pickBy(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
if (predicate(obj[key])) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
@@ -713,8 +1140,13 @@ }, {}); | ||
/** | ||
* returns an object same as the provided one but with the fields picked by the predicate function | ||
* @example | ||
* pickByIndexed((val, key) => key === 'b', { a: 1, b: 2, c: 3 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function pickByIndexed(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
if (predicate(obj[key], key)) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
@@ -724,2 +1156,8 @@ }, {}); | ||
/** | ||
* picks specified props only if they exist on the provided object | ||
* @example | ||
* pickOwn(['b'], { a: 1, b: 2 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function pickOwn(props, obj) { | ||
@@ -730,3 +1168,2 @@ return props.reduce(function (acc, prop) { | ||
} | ||
return acc; | ||
@@ -736,2 +1173,8 @@ }, {}); | ||
/** | ||
* returns a random integer number between the specified min and max values | ||
* @example | ||
* randomInt(0, 10) | ||
* // returns 7 | ||
*/ | ||
function randomInt(min, max) { | ||
@@ -741,13 +1184,23 @@ return min + Math.floor(Math.random() * (max - min + 1)); | ||
/** | ||
* returns an array filled with numbers in an ascending order to the provided max value | ||
* @example | ||
* range(5) | ||
* // returns [0, 1, 2, 3, 4, 5] | ||
*/ | ||
function range(max) { | ||
var arr = []; | ||
var counter = 0; | ||
while (counter <= max) { | ||
arr.push(counter++); | ||
} | ||
return arr; | ||
} | ||
/** | ||
* returns an array with elements that return false for the provided predicate function | ||
* @example | ||
* reject(el => el % 2 === 0, [1, 2, 3, 4, 5]) | ||
* // returns [1, 3, 5] | ||
*/ | ||
function reject(predicate, arr) { | ||
@@ -759,2 +1212,8 @@ return arr.filter(function (element) { | ||
/** | ||
* returns the provided array without the specified index element | ||
* @example | ||
* removeAt(2, [1, 2, 3, 4, 5]) | ||
* // returns [1, 2, 4, 5] | ||
*/ | ||
function removeAt(index, arr) { | ||
@@ -767,6 +1226,5 @@ var copy = [].concat(arr); | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
_extends = Object.assign ? Object.assign.bind() : function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
@@ -778,9 +1236,6 @@ if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
@@ -794,43 +1249,40 @@ if (!o) return; | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _createForOfIteratorHelperLoose(o, allowArrayLike) { | ||
var it; | ||
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
return function () { | ||
if (i >= o.length) return { | ||
done: true | ||
}; | ||
return { | ||
done: false, | ||
value: o[i++] | ||
}; | ||
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; | ||
if (it) return (it = it.call(o)).next.bind(it); | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
return function () { | ||
if (i >= o.length) return { | ||
done: true | ||
}; | ||
} | ||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
return { | ||
done: false, | ||
value: o[i++] | ||
}; | ||
}; | ||
} | ||
it = o[Symbol.iterator](); | ||
return it.next.bind(it); | ||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
/** | ||
* sets the property of the provided object specified by the provided path in form of a string or an array of keys | ||
* @example | ||
* const obj = { a: 1, b: { c: 2 } } | ||
* set('b.d', 3, obj) | ||
* // returns { a: 1, b: { c: 2, d: 3 } } | ||
* set(['b', 'd'], 3, obj) | ||
* // returns { a: 1, b: { c: 2, d: 3 } } | ||
*/ | ||
function set(_keys, val, obj) { | ||
var _extends2; | ||
var keys = _keys.split ? _keys.split('.') : _keys; | ||
var keys = isArray(_keys) ? _keys : _keys.split('.'); | ||
var index = keys[0]; | ||
var finalVal = val; | ||
if (keys.length > 1) { | ||
@@ -841,3 +1293,2 @@ // eslint-disable-next-line eqeqeq | ||
} | ||
return _extends({}, obj, (_extends2 = {}, _extends2[index] = finalVal, _extends2)); | ||
@@ -847,23 +1298,26 @@ } | ||
// https://github.com/reactjs/react-redux/blob/5d792a283554cff3d2f54fad1be1f79cbcab33fe/src/utils/shallowEqual.js | ||
function is(first, second) { | ||
if (first === second) { | ||
return first !== 0 || second !== 0 || 1 / first === 1 / second; | ||
} // eslint-disable-next-line no-self-compare | ||
} | ||
// eslint-disable-next-line no-self-compare | ||
return first !== first && second !== second; | ||
} | ||
/** | ||
* returns true if the provided values are shallow equal | ||
* @example | ||
* shallowEqual({ a: 1 }, { a: 1 }) | ||
* // returns true | ||
* shallowEqual({ a: { b: 1 } }, { a: { b: 1 } }) | ||
* // returns false | ||
*/ | ||
function shallowEqual(objA, objB) { | ||
if (is(objA, objB)) return true; | ||
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) { | ||
return false; | ||
} | ||
var keysA = Object.keys(objA); | ||
var keysB = Object.keys(objB); | ||
if (keysA.length !== keysB.length) return false; | ||
for (var index = 0; index < keysA.length; index++) { | ||
@@ -874,29 +1328,37 @@ if (!hasOwn(keysA[index], objB) || !is(objA[keysA[index]], objB[keysA[index]])) { | ||
} | ||
return true; | ||
} | ||
var shortenLongText = function shortenLongText(limit, text) { | ||
/** | ||
* shortens the provided string by the specified amount | ||
* @example | ||
* shortenLongText(5, 'Lorem ipsum dolor') | ||
* // returns 'Lorem...' | ||
*/ | ||
function shortenLongText(limit, text) { | ||
if (text.length <= limit) { | ||
return text; | ||
} | ||
var words = text.split(' '); | ||
var sentence = []; | ||
var totalLength = 0; | ||
for (var _iterator = _createForOfIteratorHelperLoose(words), _step; !(_step = _iterator()).done;) { | ||
var word = _step.value; | ||
if (totalLength + word.length > limit) { | ||
break; | ||
} | ||
totalLength += word.length + 1; | ||
sentence.push(word); | ||
} | ||
return sentence.join(' ') + "..."; | ||
}; | ||
} | ||
/** | ||
* returns a sign of the provided number or NaN if value different than number was provided | ||
* @example | ||
* sign(10) | ||
* // returns 1 | ||
* sign(-8) | ||
* // returns -1 | ||
*/ | ||
function sign(value) { | ||
@@ -906,19 +1368,20 @@ if (typeof value !== 'number' || isNaN(value)) { | ||
} | ||
if (value === 0) { | ||
if (Object.is(value, 0)) { | ||
return 0; | ||
} // eslint-disable-next-line no-compare-neg-zero | ||
if (value === -0) { | ||
} | ||
if (Object.is(value, -0)) { | ||
return -0; | ||
} | ||
return value > 0 ? 1 : -1; | ||
} | ||
/** | ||
* returns an array with the provided array values shuffled | ||
* @example | ||
* shuffle([1, 2, 3, 4]) | ||
* // returns [4, 1, 3, 2] | ||
*/ | ||
function shuffle(arr) { | ||
var copy = arr.slice(); | ||
var lastIndex = arr.length - 1; | ||
for (var i = 0; i < arr.length; i++) { | ||
@@ -930,6 +1393,11 @@ var randomIndex = randomInt(i, lastIndex); | ||
} | ||
return copy; | ||
} | ||
/** | ||
* returns the diff object from the provided object and slice comparison | ||
* @example | ||
* sliceDiff({ a: 1, b: 10, g: 3 }, { a: 1, b: 2, c: 3 }) | ||
* // returns { b: 10, g: 3 } | ||
*/ | ||
function sliceDiff(slice, obj) { | ||
@@ -943,2 +1411,9 @@ var picked = pickByIndexed(function (value, key) { | ||
// TODO: this could be written a lot better | ||
/** | ||
* snake cases the provided string | ||
* @example | ||
* snakeCase('helloWorld') | ||
* // returns 'hello_world' | ||
*/ | ||
function snakeCase(str) { | ||
@@ -953,2 +1428,8 @@ var snakeCased = str.replace(/[A-Z]|([-_ ]+)/g, function (match) { | ||
/** | ||
* returns true if some of the array values is truthy | ||
* @example | ||
* someAreTruthy([0, 1, 2]) | ||
* // returns true | ||
*/ | ||
function someAreTruthy(arr) { | ||
@@ -958,2 +1439,11 @@ return arr.some(identity); | ||
/** | ||
* splits an array or a string at the given index | ||
* @example | ||
* splitAt(2, [1, 2, 3, 4, 5]) | ||
* // returns [[1, 2], [3, 4, 5]] | ||
* splitAt(2, 'foobar') | ||
* // returns ['fo', 'obar'] | ||
*/ | ||
function splitAt(splitPoint, arrOrStr) { | ||
@@ -963,12 +1453,14 @@ return [arrOrStr.slice(0, splitPoint), arrOrStr.slice(splitPoint, arrOrStr.length)]; | ||
/** | ||
* returns a tuple with the provided array splited on an element that returned true for the provided function, iterating from the right side | ||
* @example | ||
* splitRightWhenAccum((el, acc) => [el % 2 === 0, acc], [], [1, 2, 3, 4]) | ||
* // returns [[1, 2, 3], [4]] | ||
*/ | ||
function splitRightWhenAccum(fn, acc, arr) { | ||
var result = false; | ||
for (var index = arr.length; index > 0; index--) { | ||
var _fn = fn(arr[index - 1], acc); | ||
result = _fn[0]; | ||
acc = _fn[1]; | ||
if (result) { | ||
@@ -978,6 +1470,12 @@ return splitAt(index - 1, arr); | ||
} | ||
return [[], arr]; | ||
} | ||
/** | ||
* returns a function that called with an array of values calls the provided function with the spreaded arguments from the array arguments | ||
* @example | ||
* const func = spread((a, b, c) => a + b + c) | ||
* func([1, 2, 3]) | ||
* // returns 6 | ||
*/ | ||
function spread(fn) { | ||
@@ -989,2 +1487,8 @@ return function (args) { | ||
/** | ||
* returns a sum of all the provided array number values | ||
* @example | ||
* sum([1, 2, 3]) | ||
* // returns 6 | ||
*/ | ||
function sum(numbers) { | ||
@@ -994,2 +1498,8 @@ return numbers.reduce(add, 0); | ||
/** | ||
* returns the specified amount of the provided array elements starting from the beginning | ||
* @example | ||
* take(2, ['a', 'b', 'c', 'd']) | ||
* // returns ['a', 'b'] | ||
*/ | ||
function take(count, arr) { | ||
@@ -999,2 +1509,8 @@ return arr.slice(0, count); | ||
/** | ||
* takes elements while `predicate` returns true starting from the right from the specified index | ||
* @example | ||
* takeRightWhileFrom(el => el > 2, 3, [1, 2, 3, 4, 5]) | ||
* // returns [3, 4] | ||
*/ | ||
function takeRightWhileFrom(predicate, startIndex, arr) { | ||
@@ -1007,2 +1523,8 @@ var endIndex = findLastIndexFrom(function (element) { | ||
/** | ||
* takes elements while `predicate` returns true | ||
* @example | ||
* takeRightWhile(el => el > 2, [1, 2, 3, 4, 5]) | ||
* // returns [3, 4] | ||
*/ | ||
function takeRightWhile(predicate, arr) { | ||
@@ -1012,6 +1534,12 @@ return takeRightWhileFrom(predicate, arr.length - 1, arr); | ||
/** | ||
* ensures that a function is executed at a fixed interval, so that it is not called too frequently | ||
* @example | ||
* const updatePreview = () => { ... } | ||
* const throttledUpdatePreview = throttle(updatePreview, 500); | ||
* inputField.addEventListener('input', throttledUpdatePreview); | ||
*/ | ||
function throttle(ms, fn) { | ||
var lastCall = Date.now() - 2 * ms; | ||
var timerId; | ||
var invoke = function invoke() { | ||
@@ -1021,14 +1549,10 @@ lastCall = Date.now(); | ||
}; | ||
var cancel = function cancel() { | ||
return clearTimeout(timerId); | ||
}; | ||
var throttled = function throttled() { | ||
var now = Date.now(); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
if (now - lastCall >= ms) { | ||
@@ -1038,7 +1562,5 @@ invoke.apply(void 0, args); | ||
} | ||
cancel(); | ||
timerId = setTimeout.apply(void 0, [invoke, lastCall - now + ms].concat(args)); | ||
}; | ||
throttled.cancel = cancel; | ||
@@ -1048,2 +1570,8 @@ return throttled; | ||
/** | ||
* converts values that are iterable to an array | ||
* @example | ||
* toArray('hello') | ||
* // returns ['h', 'e', 'l', 'l', 'o'] | ||
*/ | ||
function toArray(arrayLike) { | ||
@@ -1053,16 +1581,34 @@ return Array.prototype.slice.call(arrayLike); | ||
var toFixedNumber = function toFixedNumber(num, digits) { | ||
/** | ||
* returns the provided number with specified amount of decimal digits | ||
* @example | ||
* toFixedNumber(1.2345, 2) | ||
* // returns 1.23 | ||
*/ | ||
function toFixedNumber(num, digits) { | ||
return Number(num.toFixed(digits)); | ||
}; | ||
} | ||
var toPairs = function toPairs(obj) { | ||
return Object.keys(obj).map(function (key) { | ||
/** | ||
* returns an array of derived from the provided object key-value tuples | ||
* @example | ||
* toPairs({ a: 1, b: 2 }) | ||
* // returns [['a', 1], ['b', 2]] | ||
*/ | ||
function toPairs(obj) { | ||
return keys(obj).map(function (key) { | ||
return [key, obj[key]]; | ||
}); | ||
}; | ||
} | ||
/** | ||
* ensures that a function is executed at a fixed interval, so that it is not called too frequently | ||
* @example | ||
* const updatePreview = () => { ... } | ||
* const throttledUpdatePreview = trailingThrottle(updatePreview, 500); | ||
* inputField.addEventListener('input', throttledUpdatePreview); | ||
*/ | ||
function trailingThrottle(ms, fn) { | ||
var lastCall = Date.now() - 2 * ms; | ||
var trailingId; | ||
var invoke = function invoke() { | ||
@@ -1072,23 +1618,16 @@ lastCall = Date.now(); | ||
}; | ||
var cancel = function cancel() { | ||
return clearTimeout(trailingId); | ||
}; | ||
var throttled = function throttled() { | ||
var now = Date.now(); | ||
if (now - lastCall >= ms) { | ||
lastCall = Date.now(); | ||
} | ||
cancel(); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
trailingId = setTimeout.apply(void 0, [invoke, lastCall - now + ms].concat(args)); | ||
}; | ||
throttled.cancel = cancel; | ||
@@ -1098,2 +1637,9 @@ return throttled; | ||
/** | ||
* ensures that a function is executed at a fixed interval, so that it is not called too frequently | ||
* @example | ||
* const updatePreview = () => { ... } | ||
* const throttledUpdatePreview = leadingThrottle(updatePreview, 500); | ||
* inputField.addEventListener('input', throttledUpdatePreview); | ||
*/ | ||
function leadingThrottle(ms, fn) { | ||
@@ -1103,3 +1649,2 @@ var lastCall = 0; | ||
var now = Date.now(); | ||
if (now - lastCall >= ms) { | ||
@@ -1112,11 +1657,27 @@ lastCall = Date.now(); | ||
var stringCompare = function stringCompare(strA, strB) { | ||
/** | ||
* returns 0 for matching strings | ||
* return -1 for string with lower char code at some position | ||
* return 1 for string with greater char code at some position | ||
* @example | ||
* stringCompare('abc', 'abc') | ||
* // returns 0 | ||
* stringCompare('abc', 'abd') | ||
* // returns -1 | ||
*/ | ||
function stringCompare(strA, strB) { | ||
if (strA === strB) { | ||
return 0; | ||
} | ||
return strA < strB ? -1 : 1; | ||
}; | ||
} | ||
var leadingWhiteSpace = /^\s+/; | ||
/** | ||
* trims the beginning whitespaces from the provided string | ||
* @example | ||
* trimStart(' hello') | ||
* // returns 'hello' | ||
*/ | ||
function trimStart(str) { | ||
@@ -1127,2 +1688,9 @@ return str.replace(leadingWhiteSpace, ''); | ||
var trailingWhiteSpace = /\s+$/; | ||
/** | ||
* trims the end whitespaces from the provided string | ||
* @example | ||
* trimEnd('hello ') | ||
* // returns 'hello' | ||
*/ | ||
function trimEnd(str) { | ||
@@ -1132,2 +1700,8 @@ return str.replace(trailingWhiteSpace, ''); | ||
/** | ||
* returns the provided string repeated the specified amount of times | ||
* @example | ||
* repeat(3, 'test') | ||
* // returns 'testtesttest' | ||
*/ | ||
function repeat(count, text) { | ||
@@ -1137,2 +1711,8 @@ return Array(count + 1).join(text); | ||
/** | ||
* returns an array with all the duplicates from the provided array removed based on the iteratee function | ||
* @example | ||
* uniqBy(el => el.toString(), [1, '1', 2, '3', 3]) | ||
* // returns [1, 2, '3'] | ||
*/ | ||
function uniqBy(iteratee, arr) { | ||
@@ -1143,3 +1723,2 @@ // with polyfills this could be just: return Array.from(new Set(arr.map(iteratee))) | ||
var key = iteratee(element); | ||
if (seen.indexOf(key) === -1) { | ||
@@ -1149,3 +1728,2 @@ seen.push(key); | ||
} | ||
return false; | ||
@@ -1155,2 +1733,8 @@ }); | ||
/** | ||
* returns an array with all the duplicates from the provided array removed | ||
* @example | ||
* uniq([1, 1, 2, 3, 3]) | ||
* // returns [1, 2, 3] | ||
*/ | ||
function uniq(arr) { | ||
@@ -1160,2 +1744,8 @@ return uniqBy(identity, arr); | ||
/** | ||
* updates the provided array with the provided value on the specified index | ||
* @example | ||
* update(2, 3, [1, 2, 5]) | ||
* // returns [1, 2, 3] | ||
*/ | ||
function update(index, newElement, arr) { | ||
@@ -1165,2 +1755,8 @@ return [].concat(arr.slice(0, index), [newElement], arr.slice(index + 1, arr.length)); | ||
/** | ||
* returns an array without the specified elements from the provided array | ||
* @example | ||
* without([2, 4], [1, 2, 3, 4, 5]) | ||
* // returns [1, 3, 5] | ||
*/ | ||
function without(removed, arr) { | ||
@@ -1172,2 +1768,8 @@ return arr.filter(function (element) { | ||
/** | ||
* returns an array of tuples of certain index elements from two provided arrays based on the zipper function | ||
* @example | ||
* zipWith((a, b) => [a * 2, b.toString()], [1, 2, 3], [10, 20, 30]) | ||
* // returns [[2, '10'], [4, '20'], [6, '30']] | ||
*/ | ||
function zipWith(zipper, arrayA, arrayB) { | ||
@@ -1179,2 +1781,8 @@ return arrayA.map(function (elementA, index) { | ||
/** | ||
* returns an array of tuples of certain index elements from two provided arrays | ||
* @example | ||
* zip([1, 2, 3], [10, 20, 30]) | ||
* // returns [[1, 10], [2, 20], [3, 30]] | ||
*/ | ||
function zip(arrayA, arrayB) { | ||
@@ -1184,2 +1792,2 @@ return zipWith(pair, arrayA, arrayB); | ||
export { add, assign, camelCase, capitalizeFirstLetter, chain, chunk, cloneDeep, compact, compose, debounce, mapKeys as deepMapKeys, deepMerge, defaultTo, drop, dropRight, ensureArray, entries, filledArray, find, findIndex, findKey, findLast, findLastIndex, findLastIndexFrom, flatMap, flatten, forOwn, fromPairs, generateRandomId, generateUniqueId, get, getOr, groupBy, groupKeys, hasOwn, identity, includes, isArray, isEmpty, isFalsy, isNil, isObject, isPromise, isTruthy, keyBy, keys, last, leadingThrottle, mapKeys$1 as mapKeys, mapValues, mapValuesIndexed, memoize, memoizeOne, memoizeWith, merge, mergeAll, noop, numericSortBy, omit, omitBy, omitByIndexed, once, over, overArgs, pair, partitionObject, pick, pickBy, pickByIndexed, pickOwn, randomInt, range, reject, removeAt, repeat, set, shallowEqual, shortenLongText, shuffle, sign, sliceDiff, snakeCase, someAreTruthy, splitAt, splitRightWhenAccum, spread, stringCompare, sum, take, takeLast, takeRightWhile, takeRightWhileFrom, throttle, toArray, toFixedNumber, toPairs, trailingThrottle, trimEnd, trimStart, uniq, uniqBy, update, values, without, zip, zipWith }; | ||
export { add, assign, camelCase, capitalizeFirstLetter, chain, chunk, cloneDeep, compact, compose, debounce, deepMapKeys, deepMerge, defaultTo, diffKeys, drop, dropRight, ensureArray, entries, filledArray, find, findIndex, findKey, findLast, findLastIndex, findLastIndexFrom, flatMap, flatten, forOwn, fromPairs, generateRandomId, generateUniqueId, get, getOr, groupBy, groupKeys, hasOwn, identity, includes, isArray, isEmpty, isFalsy, isNil, isObject, isPromise, isTruthy, keyBy, keys, last, leadingThrottle, mapKeys, mapValues, mapValuesIndexed, memoize, memoizeOne, memoizeWith, merge, mergeAll, noop, numericSortBy, omit, omitBy, omitByIndexed, once, over, overArgs, pair, partitionObject, pick, pickBy, pickByIndexed, pickOwn, randomInt, range, reject, removeAt, repeat, set, shallowEqual, shortenLongText, shuffle, sign, sliceDiff, snakeCase, someAreTruthy, splitAt, splitRightWhenAccum, spread, stringCompare, sum, take, takeLast, takeRightWhile, takeRightWhileFrom, throttle, toArray, toFixedNumber, toPairs, trailingThrottle, trimEnd, trimStart, uniq, uniqBy, update, values, without, zip, zipWith }; |
@@ -5,2 +5,8 @@ 'use strict'; | ||
/** | ||
* adds two numbers | ||
* @example | ||
* add(1, 2) | ||
* // returns 3 | ||
*/ | ||
function add(first, second) { | ||
@@ -11,3 +17,9 @@ return first + second; | ||
var _ref = {}, | ||
hasOwnProperty = _ref.hasOwnProperty; | ||
hasOwnProperty = _ref.hasOwnProperty; | ||
/** | ||
* returns true or false depending if the provided property is present inside the provided object | ||
* @example | ||
* hasOwn('a', { a: 1, b: 2 }) | ||
* // returns true | ||
*/ | ||
function hasOwn(prop, obj) { | ||
@@ -17,3 +29,11 @@ return hasOwnProperty.call(obj, prop); | ||
/** | ||
* assigns properties of one or more source objects onto target object | ||
* @example | ||
* assign({ a: 1, b: 2 }, { b: 4, c: 5 }) | ||
* // returns { a: 1, b: 4, c: 5 } | ||
*/ | ||
function assign() { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
// eslint-disable-next-line no-func-assign | ||
@@ -24,3 +44,2 @@ assign = Object.assign || function (target) { | ||
} | ||
sources.forEach(function (source) { | ||
@@ -35,13 +54,24 @@ for (var key in source) { | ||
}; | ||
return assign.apply(void 0, arguments); | ||
} | ||
var capitalizeFirstLetter = function capitalizeFirstLetter(text) { | ||
/** | ||
* capitalizes first letter of string | ||
* @example | ||
* capitalizeFirstLetter('hello') | ||
* // returns 'Hello' | ||
*/ | ||
function capitalizeFirstLetter(text) { | ||
return text.charAt(0).toUpperCase() + text.slice(1); | ||
}; | ||
} | ||
/** | ||
* returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level | ||
* @example | ||
* const arr = [{ a: [1, 2] }, { a: [3, 4] }, { a: [5, 6] }] | ||
* flatMap(el => el.a, arr) | ||
* // returns [1, 2, 3, 4, 5, 6] | ||
*/ | ||
function flatMap(iteratee, arr) { | ||
var _ref; | ||
return (_ref = []).concat.apply(_ref, arr.map(iteratee)); | ||
@@ -56,18 +86,43 @@ } | ||
} | ||
return flatMap(fn, listOrFn); | ||
} | ||
/** | ||
* chunks provided array to specified size chunks | ||
* @example | ||
* chunk([1, 2, 3, 4], 2) | ||
* // returns [[1, 2], [3, 4]] | ||
*/ | ||
function chunk(arr, number) { | ||
if (number <= 0) { | ||
return []; | ||
} | ||
var chunked = []; | ||
for (var i = 0; i < arr.length; i += number) { | ||
chunked.push(arr.slice(i, i + number)); | ||
} | ||
return chunked; | ||
} | ||
var isArray = Array.isArray; | ||
/** | ||
* determines whether provided value is an array | ||
* @example | ||
* isArray([1, 2]) | ||
* // returns true | ||
* isArray('hello') | ||
* // returns false | ||
*/ | ||
function isArray(arr) { | ||
return Array.isArray(arr); | ||
} | ||
/** | ||
* returns true or false depending if the provided value is an object | ||
* @example | ||
* isObject({ a: 1 }) | ||
* // returns true | ||
* isObject([1, 2]) | ||
* // returns false | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
function isObject(obj) { | ||
@@ -77,4 +132,29 @@ return typeof obj === 'object' && obj !== null && !isArray(obj); | ||
/** | ||
* returns an array of the provided object keys | ||
* @example | ||
* keys({ a: 1, b: 2, c: 3 }) | ||
* // returns ['a', 'b', 'c'] | ||
*/ | ||
function keys(obj) { | ||
if ('keys' in Object && typeof Object.keys === 'function') { | ||
return Object.keys(obj); | ||
} | ||
var keysArray = []; | ||
for (var property in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, property)) { | ||
keysArray.push(property); | ||
} | ||
} | ||
return keysArray; | ||
} | ||
/** | ||
* maps values of the provided object | ||
* @example | ||
* mapValues(val => val.toUpperCase(), { a: 'foo', b: 'bar' }) | ||
* // returns { a: 'FOO', b: 'BAR' } | ||
*/ | ||
function mapValues(mapper, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
acc[key] = mapper(obj[key]); | ||
@@ -85,2 +165,8 @@ return acc; | ||
/** | ||
* returns an exact clone of the provided value with a new reference | ||
* @example | ||
* cloneDeep({ a: 1, b: 2 }) | ||
* // returns { a: 1, b: 2 } | ||
*/ | ||
function cloneDeep(value) { | ||
@@ -90,7 +176,5 @@ if (isArray(value)) { | ||
} | ||
if (isObject(value)) { | ||
return mapValues(cloneDeep, value); | ||
} | ||
return value; | ||
@@ -101,3 +185,4 @@ } | ||
// TODO: possibly could be rewritten using short-ish regexp | ||
var preserveCamelCase = function preserveCamelCase(input) { | ||
function preserveCamelCase(input) { | ||
var text = input; | ||
@@ -107,6 +192,4 @@ var isLastCharLower = false; | ||
var isLastLastCharUpper = false; | ||
for (var index = 0; index < text.length; index++) { | ||
var _char = text[index]; | ||
if (isLastCharLower && /[a-zA-Z]/.test(_char) && _char.toUpperCase() === _char) { | ||
@@ -129,27 +212,26 @@ text = text.slice(0, index) + '-' + text.slice(index); | ||
} | ||
return text; | ||
}; | ||
} | ||
/** | ||
* converts provided string to camel case | ||
* @example | ||
* camelCase('hello_world') | ||
* // returns 'helloWorld' | ||
*/ | ||
function camelCase(input) { | ||
var text = input.trim(); | ||
if (text.length === 0) { | ||
return ''; | ||
} | ||
if (text.length === 1) { | ||
return text.toLowerCase(); | ||
} | ||
if (/^[a-z\d]+$/.test(text)) { | ||
return text; | ||
} | ||
var hasUpperCase = text !== text.toLowerCase(); | ||
if (hasUpperCase) { | ||
text = preserveCamelCase(text); | ||
} | ||
text = text.replace(/^[_.\- ]+/, '').toLowerCase().replace(/[_.\- ]+(\w|$)/g, function (match, p1) { | ||
@@ -166,7 +248,5 @@ return p1.toUpperCase(); | ||
var value = collection[key]; | ||
if (value !== null && value !== undefined && !Number.isNaN(value)) { | ||
result[key] = value; | ||
} | ||
return result; | ||
@@ -176,2 +256,12 @@ }, {}); | ||
/** | ||
* returns a composed value from the provided arguments | ||
* @example | ||
* const f3 = (a: string) => `f3(${a})` | ||
* const f2 = (a: string) => `f2(${a})` | ||
* const f1 = (a: string) => `f1(${a})` | ||
* compose(f3, f2, f1)('arg') | ||
* // returns 'f3(f2(f1(arg)))' | ||
*/ | ||
function compose() { | ||
@@ -181,3 +271,2 @@ for (var _len = arguments.length, funcs = new Array(_len), _key = 0; _key < _len; _key++) { | ||
} | ||
return funcs.reduce(function (composed, next) { | ||
@@ -190,2 +279,7 @@ return function () { | ||
/** | ||
* delays the execution of a function until a certain period of time has passed without the function being called again | ||
* @example | ||
* debounce(1000, someFunction) | ||
*/ | ||
function debounce(ms, fn) { | ||
@@ -196,7 +290,5 @@ // actual return type of setTimeout differs per platform (browser vs node) | ||
clearTimeout(timeoutId); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
timeoutId = setTimeout.apply(void 0, [fn, ms].concat(args)); | ||
@@ -206,3 +298,9 @@ }; | ||
function mapKeys(mapper, obj) { | ||
/** | ||
* recurses through an object and transforms its keys – and the keys of any nested objects – based on the provided key mapping function | ||
* @example | ||
* deepMapKeys(key => `test_${key}`, { a: 1, b: { c: 2 } }) | ||
* // returns { test_a: 1, test_b: { test_c: 2 } } | ||
*/ | ||
function deepMapKeys(mapper, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
@@ -212,11 +310,9 @@ if (obj[key] && typeof obj[key] === 'object') { | ||
acc[mapper(key)] = obj[key].map(function (el) { | ||
return mapKeys(mapper, el); | ||
return deepMapKeys(mapper, el); | ||
}); | ||
return acc; | ||
} | ||
acc[mapper(key)] = mapKeys(mapper, obj[key]); | ||
acc[mapper(key)] = deepMapKeys(mapper, obj[key]); | ||
return acc; | ||
} | ||
acc[mapper(key)] = obj[key]; | ||
@@ -227,2 +323,8 @@ return acc; | ||
/** | ||
* deep merges provided objects by assigning the sources onto the target object passed as the first argument | ||
* @example | ||
* deepMerge({ a: 1, b: { c: 2 } }, { b: { d: 3 } }) | ||
* // returns { a: 1, b: { c: 2, d: 3 } } | ||
*/ | ||
function deepMerge(target) { | ||
@@ -232,6 +334,4 @@ for (var _len = arguments.length, sources = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
} | ||
if (!sources.length) return target; | ||
var source = sources.shift(); | ||
if (isObject(target) && isObject(source)) { | ||
@@ -241,3 +341,2 @@ for (var key in source) { | ||
var _Object$assign; | ||
if (!target[key]) Object.assign(target, (_Object$assign = {}, _Object$assign[key] = {}, _Object$assign)); | ||
@@ -247,3 +346,2 @@ deepMerge(target[key], source[key]); | ||
var _Object$assign2; | ||
Object.assign(target, (_Object$assign2 = {}, _Object$assign2[key] = source[key], _Object$assign2)); | ||
@@ -253,6 +351,11 @@ } | ||
} | ||
return deepMerge.apply(void 0, [target].concat(sources)); | ||
} | ||
/** | ||
* returns true or false depending if the provided value is null or undefined | ||
* @example | ||
* isNil(null) | ||
* // returns true | ||
*/ | ||
function isNil(value) { | ||
@@ -262,2 +365,11 @@ return value === null || value === undefined; | ||
/** | ||
* returns a function that invoked with a null or undefined values returns the provided default | ||
* @example | ||
* const defaulter = defaultTo('default') | ||
* defaulter('hello') | ||
* // returns 'hello' | ||
* defaulter(null) | ||
* // returns 'default' | ||
*/ | ||
function defaultTo(defaultValue) { | ||
@@ -269,2 +381,23 @@ return function (value) { | ||
/** | ||
* returns an array of keys of the object passed as the first argument that are not present, or that are present, but have different values, in object passed as the second argument | ||
* @example | ||
* diffKeys({ a: 1, b: 2, c: 3 }, { c: 3, d: 4 }) | ||
* // returns ['a', 'b'] | ||
*/ | ||
function diffKeys(obj, comparable) { | ||
return keys(obj).reduce(function (diffKeys, key) { | ||
if (obj[key] !== comparable[key]) { | ||
diffKeys.push(key); | ||
} | ||
return diffKeys; | ||
}, []); | ||
} | ||
/** | ||
* drops specific amount of elements of the provided array starting at the beginning | ||
* @example | ||
* drop(2, [1, 2, 3, 4]) | ||
* // returns [3, 4] | ||
*/ | ||
function drop(count, arr) { | ||
@@ -274,2 +407,8 @@ return arr.slice(count); | ||
/** | ||
* drops specific amount of elements of the provided array starting at the end | ||
* @example | ||
* dropRight(2, [1, 2, 3, 4]) | ||
* // returns [1, 2] | ||
*/ | ||
function dropRight(count, arr) { | ||
@@ -279,2 +418,10 @@ return arr.slice(0, -count); | ||
/** | ||
* returns an array if an array was provided, or a new array if provided value was not an array | ||
* @example | ||
* ensureArray([1, 2]) | ||
* // returns [1, 2] | ||
* ensureArray('test') | ||
* // returns ['test'] | ||
*/ | ||
function ensureArray(maybeArr) { | ||
@@ -284,4 +431,10 @@ return isArray(maybeArr) ? maybeArr : [maybeArr]; | ||
/** | ||
* returns an array of entries of the provided object | ||
* @example | ||
* entries({ a: 1, b: 2 }) | ||
* // returns [['a', 1], ['b', 2]] | ||
*/ | ||
function entries(obj) { | ||
return Object.keys(obj).map(function (key) { | ||
return keys(obj).map(function (key) { | ||
return [key, obj[key]]; | ||
@@ -291,6 +444,11 @@ }); | ||
/** | ||
* returns the first element of the provided array that returns true for the predicate function, or undefined if the element was not found | ||
* @example | ||
* find(el => el === 2, [1, 2, 3]) | ||
* // returns 2 | ||
*/ | ||
function find(predicate, arr) { | ||
for (var index = 0; index < arr.length; index++) { | ||
var element = arr[index]; | ||
if (predicate(element)) { | ||
@@ -302,2 +460,10 @@ return element; | ||
/** | ||
* returns the index of the element of the provided array that returns true for the predicate function, or -1 if the element was not found | ||
* @example | ||
* findIndex(el => el === 2, [1, 2, 3]) | ||
* // returns 1 | ||
* findIndex(el => el === 5, [1, 2, 3]) | ||
* // returns -1 | ||
*/ | ||
function findIndex(predicate, arr) { | ||
@@ -309,12 +475,25 @@ for (var index = 0; index < arr.length; index++) { | ||
} | ||
return -1; | ||
} | ||
/** | ||
* returns the key of the provided object that returns true for the predicate function, or undefined if the key was not found | ||
* @example | ||
* findKey(el => el === 1, { a: 1, b: { c: 2 } }) | ||
* // returns 'a' | ||
* findKey(el => el === 2, { a: 1, b: { c: 2 } }) | ||
* // returns undefined | ||
*/ | ||
function findKey(predicate, obj) { | ||
return find(function (key) { | ||
return predicate(obj[key]); | ||
}, Object.keys(obj)); | ||
}, keys(obj)); | ||
} | ||
/** | ||
* returns the last element of the provided array that returns true for the predicate function, or undefined if the element was not found | ||
* @example | ||
* findLast(el => el % 2 === 0, [1, 2, 3, 4]) | ||
* // returns 4 | ||
*/ | ||
function findLast(predicate, arr) { | ||
@@ -328,2 +507,8 @@ for (var index = arr.length - 1; index >= 0; index--) { | ||
/** | ||
* returns the index of the last element of the provided array that returns true for the predicate function, starting from the specified index element to the begining of the array, or -1 if the element was not found | ||
* @example | ||
* findLastIndexFrom(el => el % 2 === 0, 3, [1, 2, 3, 4, 5, 6]) | ||
* // returns 3 | ||
*/ | ||
function findLastIndexFrom(predicate, startIndex, arr) { | ||
@@ -335,6 +520,11 @@ for (var index = startIndex; index >= 0; index--) { | ||
} | ||
return -1; | ||
} | ||
/** | ||
* returns the index of the last element of the provided array that returns true for the predicate function, or -1 if the element was not found | ||
* @example | ||
* findLastIndex(el => el % 2 === 0, [1, 2, 3, 4]) | ||
* // returns 3 | ||
*/ | ||
function findLastIndex(predicate, arr) { | ||
@@ -344,12 +534,25 @@ return findLastIndexFrom(predicate, arr.length - 1, arr); | ||
/** | ||
* returns an array of requested length filled with provided value | ||
* @example | ||
* filledArray(3, 1) | ||
* // returns [1, 1, 1] | ||
*/ | ||
function filledArray(length, value) { | ||
if (length <= 0) { | ||
return []; | ||
} | ||
var arr = []; | ||
while (length--) { | ||
arr.push(value); | ||
} | ||
return arr; | ||
} | ||
/** | ||
* returns the provided value | ||
* @example | ||
* identity('hello') | ||
* // returns 'hello' | ||
*/ | ||
function identity(value) { | ||
@@ -359,2 +562,10 @@ return value; | ||
/** | ||
* flattens the provided array by one level | ||
* @example | ||
* flatten([1, 2, [3, 4]]) | ||
* // returns [1, 2, 3, 4] | ||
* flatten([1, 2, [3, [4, 5]], 6]) | ||
* // returns [1, 2, 3, [4, 5], 6] | ||
*/ | ||
function flatten(arr) { | ||
@@ -364,4 +575,12 @@ return flatMap(identity, arr); | ||
/** | ||
* invokes the provided callback for each of the provided object fields | ||
* @example | ||
* const obj = { a: 1, b: 2, c: 3 } | ||
* const arr: string[] = [] | ||
* forOwn(el => arr.push(el.toString()), obj) | ||
* // arr equals ['1', '2', '3'] | ||
*/ | ||
function forOwn(callback, obj) { | ||
return Object.keys(obj).forEach(function (key) { | ||
return keys(obj).forEach(function (key) { | ||
callback(obj[key], key); | ||
@@ -371,6 +590,13 @@ }); | ||
/** | ||
* returns an object constructed from the provided array of key, value pairs' | ||
* @example | ||
* const arr = [['a', 1], ['b', 2], ['c', 3]] | ||
* fromPairs(arr) | ||
* // returns { a: 1, b: 2, c: 3 } | ||
*/ | ||
function fromPairs(pairs) { | ||
return pairs.reduce(function (obj, _ref) { | ||
var key = _ref[0], | ||
value = _ref[1]; | ||
value = _ref[1]; | ||
obj[key] = value; | ||
@@ -381,2 +607,8 @@ return obj; | ||
/** | ||
* generates a random id | ||
* @example | ||
* generateRandomId() | ||
* // returns 'd1rjknhhch8' | ||
*/ | ||
function generateRandomId() { | ||
@@ -386,2 +618,8 @@ return Math.random().toString(36).substring(2); | ||
/** | ||
* generates an unique id based on the provided object keys | ||
* @example | ||
* generateUniqueId({ xuvarw8cao: 1, b837g2nba1d: 2 }) | ||
* // returns 'd1rjknhhch8' | ||
*/ | ||
function generateUniqueId(map) { | ||
@@ -392,3 +630,11 @@ var id = generateRandomId(); | ||
// based on https://github.com/developit/dlv/blob/d7ec976d12665f1c25dec2acf955dfc2e8757a9c/index.js | ||
/** | ||
* returns the value from the specififed path from the provided object | ||
* @example | ||
* const obj = { a: { b: [1, 2, 3] } } | ||
* get('a.b.1', obj) | ||
* // returns 2 | ||
* get(['a', 'b', '1'], obj) | ||
* // returns 2 | ||
*/ | ||
function get(propPath, obj) { | ||
@@ -398,10 +644,17 @@ var arrPath = typeof propPath === 'string' ? propPath.split('.') : propPath; | ||
var result = obj; | ||
while (result && pathPartIndex < arrPath.length) { | ||
result = result[arrPath[pathPartIndex++]]; | ||
} | ||
return result; | ||
} | ||
/** | ||
* returns the value from the specififed path from the provided object or the default value if the path element was not found | ||
* @example | ||
* const obj = { a: { b: [1, 2, 3] } } | ||
* getOr(null, 'a.b.1', obj) | ||
* // returns 2 | ||
* getOr(null, 'a.c', obj) | ||
* // returns null | ||
*/ | ||
function getOr(defaultValue, prop, obj) { | ||
@@ -412,2 +665,19 @@ var propValue = get(prop, obj); | ||
/** | ||
* groups values from the provided array or object based on the result of the provided mapper function | ||
* @example | ||
* const arr = [ | ||
* { a: 1, b: 2 }, | ||
* { a: 2, b: 4 }, | ||
* { a: 3, b: 6 }, | ||
* ] | ||
* groupBy(el => (el.a % 2 === 0 ? 'even' : 'odd'), arr) | ||
* // returns { | ||
* // odd: [ | ||
* // { a: 1, b: 2 }, | ||
* // { a: 3, b: 6 }, | ||
* // ], | ||
* // even: [{ a: 2, b: 4 }], | ||
* // } | ||
*/ | ||
function groupBy(mapper, collection) { | ||
@@ -423,4 +693,14 @@ return Object.keys(collection).reduce(function (grouped, key) { | ||
/** | ||
* groups values from the provided object based on the result of the provided key mapping function | ||
* @example | ||
* const obj = { a: 1, b: 2, c: 3 } | ||
* groupKeys(el => (el === 'a' ? 'aaa' : 'rest'), obj) | ||
* // returns { | ||
* // aaa: { a: 1 }, | ||
* // rest: { b: 2, c: 3 }, | ||
* // } | ||
*/ | ||
function groupKeys(mapper, obj) { | ||
return Object.keys(obj).reduce(function (grouped, key) { | ||
return keys(obj).reduce(function (grouped, key) { | ||
var groupKey = mapper(key); | ||
@@ -433,2 +713,11 @@ grouped[groupKey] = grouped[groupKey] || {}; | ||
/** | ||
* returns true or false depending if the provided value is present inside the provided array or string | ||
* @example | ||
* includes('a', ['a', 'b', 'c']) | ||
* // returns true | ||
* includes('d', 'abc') | ||
* // returns false | ||
*/ | ||
function includes(value, arrOrStr) { | ||
@@ -438,2 +727,8 @@ return arrOrStr.indexOf(value) !== -1; | ||
/** | ||
* returns true or false depending if the provided value is an empty object or an empty array | ||
* @example | ||
* isEmpty({}) | ||
* // returns true | ||
*/ | ||
function isEmpty(collection) { | ||
@@ -443,14 +738,43 @@ return (isArray(collection) ? collection : Object.keys(collection)).length === 0; | ||
function isFalsy (value) { | ||
/** | ||
* returns true or false depending if the provided value is falsy | ||
* @example | ||
* isFalsy(0) | ||
* // returns true | ||
*/ | ||
function isFalsy(value) { | ||
return !value; | ||
} | ||
function isTruthy (value) { | ||
/** | ||
* returns true or false depending if the provided value is truthy | ||
* @example | ||
* isTruthy(1) | ||
* // returns true | ||
*/ | ||
function isTruthy(value) { | ||
return !!value; | ||
} | ||
var isPromise = function isPromise(promise) { | ||
/** | ||
* returns true or false depending if the provided value is a Promise | ||
* @example | ||
* isPromise(new Promise(res => res(null))) | ||
* // returns true | ||
*/ | ||
function isPromise(promise) { | ||
return !!promise && typeof promise.then === 'function'; | ||
}; | ||
} | ||
/** | ||
* constructs an object from the provided array of objects, grouped by the value of the specified key | ||
* @example | ||
* const arr = [ | ||
* { a: 'foo', b: 'bar' }, | ||
* { a: 'foo', b: 'baz' }, | ||
* { a: 'test', b: 'bab' }, | ||
* ] | ||
* keyBy('a', arr) | ||
* // returns { foo: { a: 'foo', b: 'baz' }, test: { a: 'test', b: 'bab' } } | ||
*/ | ||
function keyBy(prop, arr) { | ||
@@ -463,22 +787,24 @@ return arr.reduce(function (acc, el) { | ||
function keys(obj) { | ||
var keysArray = []; | ||
for (var property in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, property)) { | ||
keysArray.push(property); | ||
} | ||
} | ||
return keysArray; | ||
} | ||
// TODO: this should return `T | undefined` to match native behavior | ||
/** | ||
* returns the last element of the provided array or undefined when array is empty | ||
* @example | ||
* last([1, 2, 3]) | ||
* // returns 3 | ||
*/ | ||
function last(arr) { | ||
return arr.length > 0 ? arr[arr.length - 1] : null; | ||
return arr.length > 0 ? arr[arr.length - 1] : undefined; | ||
} | ||
function mapKeys$1(mapper, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
acc[mapper(key)] = obj[key]; | ||
/** | ||
* maps keys of the provided object | ||
* @example | ||
* mapKeys(key => key.toUpperCase(), { a: 1, b: 2 }) | ||
* // returns { A: 1, B: 2 } | ||
*/ | ||
function mapKeys(mapper, obj) { | ||
return keys(obj).reduce(function (acc, key) { | ||
Object.defineProperty(acc, mapper(key), { | ||
value: obj[key], | ||
enumerable: true | ||
}); | ||
return acc; | ||
@@ -488,4 +814,10 @@ }, {}); | ||
/** | ||
* maps values and keys of the provided object | ||
* @example | ||
* mapValuesIndexed((val, key) => `${key}-${val}`, { a: 1, b: 2 }) | ||
* // returns { a: 'a-1', b: 'b-2' } | ||
*/ | ||
function mapValuesIndexed(iteratee, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
acc[key] = iteratee(obj[key], key); | ||
@@ -496,2 +828,8 @@ return acc; | ||
/** | ||
* deep merges the two provided objects | ||
* @example | ||
* merge({ a: 1 }, { b: 2 }) | ||
* // returns { a: 1, b: 2 } | ||
*/ | ||
function merge(objA, objB) { | ||
@@ -501,34 +839,11 @@ if (isEmpty(objB)) { | ||
} | ||
var result = {}; | ||
forOwn(function (value, key) { | ||
if (hasOwn(key, objB)) { | ||
if (isObject(objA[key]) && isObject(objB[key])) { | ||
result[key] = merge(objA[key], objB[key]); | ||
} else if (isArray(objA[key]) && isArray(objB[key])) { | ||
var length = Math.max(objA[key].length, objB[key].length); | ||
result[key] = new Array(length); | ||
for (var i = 0; i < length; i++) { | ||
if (i in objB[key]) { | ||
result[key][i] = objB[key][i]; | ||
} else if (i in objA[key]) { | ||
result[key][i] = objA[key][i]; | ||
} | ||
} | ||
} else { | ||
result[key] = objB[key]; | ||
} | ||
} else { | ||
result[key] = objA[key]; | ||
} | ||
}, objA); | ||
forOwn(function (value, key) { | ||
if (!hasOwn(key, result)) { | ||
result[key] = objB[key]; | ||
} | ||
}, objB); | ||
return result; | ||
return deepMerge({}, objA, objB); | ||
} | ||
/** | ||
* deep merges all provided objects | ||
* @example | ||
* mergeAll({ a: 1 }, { b: 2 }, { c: 3 }) | ||
* // returns { a: 1, b: 2, c: 3 } | ||
*/ | ||
function mergeAll(objs) { | ||
@@ -538,5 +853,4 @@ if (objs.length === 0) { | ||
} | ||
var first = objs[0], | ||
rest = objs.slice(1); | ||
rest = objs.slice(1); | ||
return rest.reduce(function (merged, obj) { | ||
@@ -547,2 +861,9 @@ return merge(merged, obj); | ||
/** | ||
* memoizes the provided function so it returns cached results but based on the provided key resolver | ||
* @example | ||
* const memoizedFunc = memoizeWith(key => (key === 'a' ? key : 'bar'), (val: string) => ({ current: val })) | ||
* memoizedFunc('a') === memoizedFunc('a') // true | ||
* memoizedFunc('b') === memoizedFunc('c') // true | ||
*/ | ||
function memoizeWith(keyResolver, func) { | ||
@@ -552,7 +873,5 @@ var cache = {}; | ||
var key = keyResolver.apply(void 0, arguments); | ||
if (hasOwn(key, cache)) { | ||
return cache[key]; | ||
} | ||
var value = func.apply(void 0, arguments); | ||
@@ -565,2 +884,10 @@ cache[key] = value; | ||
// TODO: technically this should accept AnyFunction but it doesn't type check currently with that for some reason | ||
/** | ||
* memoizes the provided function so it returns cached results when invoked with the same arguments | ||
* @example | ||
* const memoizedFunc = memoize((val: number) => ({ current: val }) | ||
* memoizedFunc(3) === memoizedFunc(3) | ||
* // returns true | ||
*/ | ||
function memoize(func) { | ||
@@ -570,2 +897,15 @@ return memoizeWith(identity, func); | ||
/** | ||
* memoizes the provided function so it returns cached results but only with one cache key | ||
* @example | ||
* const memoizedFunc = memoizeOne((val: number) => ({ current: val }) | ||
* | ||
* const resultFor1 = memoizedFunc(1) // { current: 1 } | ||
* resultFor1 === memoizedFunc(1) // true | ||
* resultFor1 === memoizedFunc(1) // true | ||
* | ||
* const resultFor2 = memoizedFunc(2) // { current: 2 } | ||
* resultFor2 === memoizedFunc(2) // true | ||
* resultFor1 === memoizedFunc(1) // false | ||
*/ | ||
function memoizeOne(fn) { | ||
@@ -579,3 +919,2 @@ var called = false; | ||
} | ||
called = true; | ||
@@ -588,7 +927,18 @@ cacheKey = arguments.length <= 0 ? undefined : arguments[0]; | ||
/** | ||
* does literally nothing | ||
* @example | ||
* somethingAsyncAndDangerous().catch(noop) | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
function noop() {} | ||
/** | ||
* returns an array of values of the provided object | ||
* @example | ||
* values({ a: 1, b: 2, c: 3 }) | ||
* // returns [1, 2, 3] | ||
*/ | ||
function values(obj) { | ||
return Object.keys(obj).map(function (key) { | ||
return keys(obj).map(function (key) { | ||
return obj[key]; | ||
@@ -598,2 +948,13 @@ }); | ||
/** | ||
* sorts values of the provided object or array based on the provided mapping function or the provided prop string | ||
* @example | ||
* const obj = { | ||
* a: { chats: 1 }, | ||
* b: { chats: 3 }, | ||
* c: { chats: 2 }, | ||
* } | ||
* numericSortBy(el => el.chats * -1, obj) | ||
* // returns [{ chats: 3 }, { chats: 2 }, { chats: 1 }] | ||
*/ | ||
function numericSortBy(propOrMapper, collection) { | ||
@@ -608,8 +969,13 @@ var mapper = typeof propOrMapper === 'function' ? propOrMapper : function (element) { | ||
/** | ||
* returns an object the same as the provided one but without the fields omitted by the predicate function | ||
* @example | ||
* omitByIndexed((val, key) => key === 'b', { a: 1, b: 2, c: 3 }) | ||
* // returns { a: 1, c: 3 } | ||
*/ | ||
function omitByIndexed(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
if (!predicate(obj[key], key)) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
@@ -619,2 +985,8 @@ }, {}); | ||
/** | ||
* returns the provided object but with specified keys omitted | ||
* @example | ||
* omit(['a'], { a: 1, b: 2 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function omit(keys, obj) { | ||
@@ -626,8 +998,13 @@ return omitByIndexed(function (_, key) { | ||
/** | ||
* returns an object same as the provided one but without the fields omitted by the predicate function | ||
* @example | ||
* omitBy(el => el % 2 === 0, { a: 1, b: 2, c: 3 }) | ||
* // returns { a: 1, c: 3 } | ||
*/ | ||
function omitBy(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
if (!predicate(obj[key])) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
@@ -637,2 +1014,12 @@ }, {}); | ||
/** | ||
* returns a particular function to be executed only a single time | ||
* @example | ||
* const results = [1, 2, 3, 4, 5] | ||
* const getResult = once(() => results.pop()) | ||
* getResult() // 5 | ||
* getResult() // 5 | ||
* getResult() // 5 | ||
* | ||
*/ | ||
function once(fn) { | ||
@@ -645,3 +1032,2 @@ var called = false; | ||
} | ||
called = true; | ||
@@ -652,2 +1038,9 @@ return result = fn.apply(void 0, arguments); | ||
/** | ||
* returns a function that called invokes the provided array of functions with the call arguments | ||
* @example | ||
* const func = over([val => val + 1, val => val * 10, Boolean]) | ||
* func(1) | ||
* // returns [2, 10, true] | ||
*/ | ||
function over(fns) { | ||
@@ -658,3 +1051,2 @@ return function () { | ||
} | ||
return fns.map(function (fn) { | ||
@@ -666,2 +1058,8 @@ return fn.apply(void 0, args); | ||
/** | ||
* returns the specified amount of the provided array elements starting from the end | ||
* @example | ||
* takeLast(2, ['a', 'b', 'c', 'd']) | ||
* // returns ['c', 'd'] | ||
*/ | ||
function takeLast(count, arr) { | ||
@@ -671,2 +1069,9 @@ return arr.slice(-count); | ||
/** | ||
* returns a function that called invokes the provided arguments transformers | ||
* @example | ||
* const func = overArgs((a, b, c) => [a, b, c], [n => n * n, Boolean]) | ||
* func(2, 0, 1) | ||
* // returns [4, false, 1] | ||
*/ | ||
function overArgs(fn, transformers) { | ||
@@ -677,3 +1082,2 @@ return function () { | ||
} | ||
var transformed = transformers.map(function (transform, index) { | ||
@@ -686,2 +1090,8 @@ return transform(args[index]); | ||
/** | ||
* returns a tuple from the two provided values | ||
* @example | ||
* pair('a', 'b') | ||
* // returns ['a', 'b'] | ||
*/ | ||
function pair(a, b) { | ||
@@ -691,4 +1101,10 @@ return [a, b]; | ||
/** | ||
* returns a tuple of valid and invalid parts of the object as defined in validation function passed in the first argument | ||
* @example | ||
* partitionObject(el => el % 2 === 0, { a: 1, b: 2, c: 3 }) | ||
* // returns [{ b: 2 }, { a: 1, c: 3 }] | ||
*/ | ||
function partitionObject(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
acc[predicate(obj[key]) ? 0 : 1][key] = obj[key]; | ||
@@ -699,2 +1115,8 @@ return acc; | ||
/** | ||
* picks specified properties from the object | ||
* @example | ||
* pick(['b'], { a: 1, b: 2 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function pick(props, obj) { | ||
@@ -707,8 +1129,13 @@ return props.reduce(function (acc, prop) { | ||
/** | ||
* returns an object same as the provided one but with the fields picked by the predicate function | ||
* @example | ||
* pickBy(el => el % 2 === 0, { a: 1, b: 2, c: 3 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function pickBy(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
if (predicate(obj[key])) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
@@ -718,8 +1145,13 @@ }, {}); | ||
/** | ||
* returns an object same as the provided one but with the fields picked by the predicate function | ||
* @example | ||
* pickByIndexed((val, key) => key === 'b', { a: 1, b: 2, c: 3 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function pickByIndexed(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
if (predicate(obj[key], key)) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
@@ -729,2 +1161,8 @@ }, {}); | ||
/** | ||
* picks specified props only if they exist on the provided object | ||
* @example | ||
* pickOwn(['b'], { a: 1, b: 2 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function pickOwn(props, obj) { | ||
@@ -735,3 +1173,2 @@ return props.reduce(function (acc, prop) { | ||
} | ||
return acc; | ||
@@ -741,2 +1178,8 @@ }, {}); | ||
/** | ||
* returns a random integer number between the specified min and max values | ||
* @example | ||
* randomInt(0, 10) | ||
* // returns 7 | ||
*/ | ||
function randomInt(min, max) { | ||
@@ -746,13 +1189,23 @@ return min + Math.floor(Math.random() * (max - min + 1)); | ||
/** | ||
* returns an array filled with numbers in an ascending order to the provided max value | ||
* @example | ||
* range(5) | ||
* // returns [0, 1, 2, 3, 4, 5] | ||
*/ | ||
function range(max) { | ||
var arr = []; | ||
var counter = 0; | ||
while (counter <= max) { | ||
arr.push(counter++); | ||
} | ||
return arr; | ||
} | ||
/** | ||
* returns an array with elements that return false for the provided predicate function | ||
* @example | ||
* reject(el => el % 2 === 0, [1, 2, 3, 4, 5]) | ||
* // returns [1, 3, 5] | ||
*/ | ||
function reject(predicate, arr) { | ||
@@ -764,2 +1217,8 @@ return arr.filter(function (element) { | ||
/** | ||
* returns the provided array without the specified index element | ||
* @example | ||
* removeAt(2, [1, 2, 3, 4, 5]) | ||
* // returns [1, 2, 4, 5] | ||
*/ | ||
function removeAt(index, arr) { | ||
@@ -772,6 +1231,5 @@ var copy = [].concat(arr); | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
_extends = Object.assign ? Object.assign.bind() : function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
@@ -783,9 +1241,6 @@ if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
@@ -799,43 +1254,40 @@ if (!o) return; | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _createForOfIteratorHelperLoose(o, allowArrayLike) { | ||
var it; | ||
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
return function () { | ||
if (i >= o.length) return { | ||
done: true | ||
}; | ||
return { | ||
done: false, | ||
value: o[i++] | ||
}; | ||
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; | ||
if (it) return (it = it.call(o)).next.bind(it); | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
return function () { | ||
if (i >= o.length) return { | ||
done: true | ||
}; | ||
} | ||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
return { | ||
done: false, | ||
value: o[i++] | ||
}; | ||
}; | ||
} | ||
it = o[Symbol.iterator](); | ||
return it.next.bind(it); | ||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
/** | ||
* sets the property of the provided object specified by the provided path in form of a string or an array of keys | ||
* @example | ||
* const obj = { a: 1, b: { c: 2 } } | ||
* set('b.d', 3, obj) | ||
* // returns { a: 1, b: { c: 2, d: 3 } } | ||
* set(['b', 'd'], 3, obj) | ||
* // returns { a: 1, b: { c: 2, d: 3 } } | ||
*/ | ||
function set(_keys, val, obj) { | ||
var _extends2; | ||
var keys = _keys.split ? _keys.split('.') : _keys; | ||
var keys = isArray(_keys) ? _keys : _keys.split('.'); | ||
var index = keys[0]; | ||
var finalVal = val; | ||
if (keys.length > 1) { | ||
@@ -846,3 +1298,2 @@ // eslint-disable-next-line eqeqeq | ||
} | ||
return _extends({}, obj, (_extends2 = {}, _extends2[index] = finalVal, _extends2)); | ||
@@ -852,23 +1303,26 @@ } | ||
// https://github.com/reactjs/react-redux/blob/5d792a283554cff3d2f54fad1be1f79cbcab33fe/src/utils/shallowEqual.js | ||
function is(first, second) { | ||
if (first === second) { | ||
return first !== 0 || second !== 0 || 1 / first === 1 / second; | ||
} // eslint-disable-next-line no-self-compare | ||
} | ||
// eslint-disable-next-line no-self-compare | ||
return first !== first && second !== second; | ||
} | ||
/** | ||
* returns true if the provided values are shallow equal | ||
* @example | ||
* shallowEqual({ a: 1 }, { a: 1 }) | ||
* // returns true | ||
* shallowEqual({ a: { b: 1 } }, { a: { b: 1 } }) | ||
* // returns false | ||
*/ | ||
function shallowEqual(objA, objB) { | ||
if (is(objA, objB)) return true; | ||
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) { | ||
return false; | ||
} | ||
var keysA = Object.keys(objA); | ||
var keysB = Object.keys(objB); | ||
if (keysA.length !== keysB.length) return false; | ||
for (var index = 0; index < keysA.length; index++) { | ||
@@ -879,29 +1333,37 @@ if (!hasOwn(keysA[index], objB) || !is(objA[keysA[index]], objB[keysA[index]])) { | ||
} | ||
return true; | ||
} | ||
var shortenLongText = function shortenLongText(limit, text) { | ||
/** | ||
* shortens the provided string by the specified amount | ||
* @example | ||
* shortenLongText(5, 'Lorem ipsum dolor') | ||
* // returns 'Lorem...' | ||
*/ | ||
function shortenLongText(limit, text) { | ||
if (text.length <= limit) { | ||
return text; | ||
} | ||
var words = text.split(' '); | ||
var sentence = []; | ||
var totalLength = 0; | ||
for (var _iterator = _createForOfIteratorHelperLoose(words), _step; !(_step = _iterator()).done;) { | ||
var word = _step.value; | ||
if (totalLength + word.length > limit) { | ||
break; | ||
} | ||
totalLength += word.length + 1; | ||
sentence.push(word); | ||
} | ||
return sentence.join(' ') + "..."; | ||
}; | ||
} | ||
/** | ||
* returns a sign of the provided number or NaN if value different than number was provided | ||
* @example | ||
* sign(10) | ||
* // returns 1 | ||
* sign(-8) | ||
* // returns -1 | ||
*/ | ||
function sign(value) { | ||
@@ -911,19 +1373,20 @@ if (typeof value !== 'number' || isNaN(value)) { | ||
} | ||
if (value === 0) { | ||
if (Object.is(value, 0)) { | ||
return 0; | ||
} // eslint-disable-next-line no-compare-neg-zero | ||
if (value === -0) { | ||
} | ||
if (Object.is(value, -0)) { | ||
return -0; | ||
} | ||
return value > 0 ? 1 : -1; | ||
} | ||
/** | ||
* returns an array with the provided array values shuffled | ||
* @example | ||
* shuffle([1, 2, 3, 4]) | ||
* // returns [4, 1, 3, 2] | ||
*/ | ||
function shuffle(arr) { | ||
var copy = arr.slice(); | ||
var lastIndex = arr.length - 1; | ||
for (var i = 0; i < arr.length; i++) { | ||
@@ -935,6 +1398,11 @@ var randomIndex = randomInt(i, lastIndex); | ||
} | ||
return copy; | ||
} | ||
/** | ||
* returns the diff object from the provided object and slice comparison | ||
* @example | ||
* sliceDiff({ a: 1, b: 10, g: 3 }, { a: 1, b: 2, c: 3 }) | ||
* // returns { b: 10, g: 3 } | ||
*/ | ||
function sliceDiff(slice, obj) { | ||
@@ -948,2 +1416,9 @@ var picked = pickByIndexed(function (value, key) { | ||
// TODO: this could be written a lot better | ||
/** | ||
* snake cases the provided string | ||
* @example | ||
* snakeCase('helloWorld') | ||
* // returns 'hello_world' | ||
*/ | ||
function snakeCase(str) { | ||
@@ -958,2 +1433,8 @@ var snakeCased = str.replace(/[A-Z]|([-_ ]+)/g, function (match) { | ||
/** | ||
* returns true if some of the array values is truthy | ||
* @example | ||
* someAreTruthy([0, 1, 2]) | ||
* // returns true | ||
*/ | ||
function someAreTruthy(arr) { | ||
@@ -963,2 +1444,11 @@ return arr.some(identity); | ||
/** | ||
* splits an array or a string at the given index | ||
* @example | ||
* splitAt(2, [1, 2, 3, 4, 5]) | ||
* // returns [[1, 2], [3, 4, 5]] | ||
* splitAt(2, 'foobar') | ||
* // returns ['fo', 'obar'] | ||
*/ | ||
function splitAt(splitPoint, arrOrStr) { | ||
@@ -968,12 +1458,14 @@ return [arrOrStr.slice(0, splitPoint), arrOrStr.slice(splitPoint, arrOrStr.length)]; | ||
/** | ||
* returns a tuple with the provided array splited on an element that returned true for the provided function, iterating from the right side | ||
* @example | ||
* splitRightWhenAccum((el, acc) => [el % 2 === 0, acc], [], [1, 2, 3, 4]) | ||
* // returns [[1, 2, 3], [4]] | ||
*/ | ||
function splitRightWhenAccum(fn, acc, arr) { | ||
var result = false; | ||
for (var index = arr.length; index > 0; index--) { | ||
var _fn = fn(arr[index - 1], acc); | ||
result = _fn[0]; | ||
acc = _fn[1]; | ||
if (result) { | ||
@@ -983,6 +1475,12 @@ return splitAt(index - 1, arr); | ||
} | ||
return [[], arr]; | ||
} | ||
/** | ||
* returns a function that called with an array of values calls the provided function with the spreaded arguments from the array arguments | ||
* @example | ||
* const func = spread((a, b, c) => a + b + c) | ||
* func([1, 2, 3]) | ||
* // returns 6 | ||
*/ | ||
function spread(fn) { | ||
@@ -994,2 +1492,8 @@ return function (args) { | ||
/** | ||
* returns a sum of all the provided array number values | ||
* @example | ||
* sum([1, 2, 3]) | ||
* // returns 6 | ||
*/ | ||
function sum(numbers) { | ||
@@ -999,2 +1503,8 @@ return numbers.reduce(add, 0); | ||
/** | ||
* returns the specified amount of the provided array elements starting from the beginning | ||
* @example | ||
* take(2, ['a', 'b', 'c', 'd']) | ||
* // returns ['a', 'b'] | ||
*/ | ||
function take(count, arr) { | ||
@@ -1004,2 +1514,8 @@ return arr.slice(0, count); | ||
/** | ||
* takes elements while `predicate` returns true starting from the right from the specified index | ||
* @example | ||
* takeRightWhileFrom(el => el > 2, 3, [1, 2, 3, 4, 5]) | ||
* // returns [3, 4] | ||
*/ | ||
function takeRightWhileFrom(predicate, startIndex, arr) { | ||
@@ -1012,2 +1528,8 @@ var endIndex = findLastIndexFrom(function (element) { | ||
/** | ||
* takes elements while `predicate` returns true | ||
* @example | ||
* takeRightWhile(el => el > 2, [1, 2, 3, 4, 5]) | ||
* // returns [3, 4] | ||
*/ | ||
function takeRightWhile(predicate, arr) { | ||
@@ -1017,6 +1539,12 @@ return takeRightWhileFrom(predicate, arr.length - 1, arr); | ||
/** | ||
* ensures that a function is executed at a fixed interval, so that it is not called too frequently | ||
* @example | ||
* const updatePreview = () => { ... } | ||
* const throttledUpdatePreview = throttle(updatePreview, 500); | ||
* inputField.addEventListener('input', throttledUpdatePreview); | ||
*/ | ||
function throttle(ms, fn) { | ||
var lastCall = Date.now() - 2 * ms; | ||
var timerId; | ||
var invoke = function invoke() { | ||
@@ -1026,14 +1554,10 @@ lastCall = Date.now(); | ||
}; | ||
var cancel = function cancel() { | ||
return clearTimeout(timerId); | ||
}; | ||
var throttled = function throttled() { | ||
var now = Date.now(); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
if (now - lastCall >= ms) { | ||
@@ -1043,7 +1567,5 @@ invoke.apply(void 0, args); | ||
} | ||
cancel(); | ||
timerId = setTimeout.apply(void 0, [invoke, lastCall - now + ms].concat(args)); | ||
}; | ||
throttled.cancel = cancel; | ||
@@ -1053,2 +1575,8 @@ return throttled; | ||
/** | ||
* converts values that are iterable to an array | ||
* @example | ||
* toArray('hello') | ||
* // returns ['h', 'e', 'l', 'l', 'o'] | ||
*/ | ||
function toArray(arrayLike) { | ||
@@ -1058,16 +1586,34 @@ return Array.prototype.slice.call(arrayLike); | ||
var toFixedNumber = function toFixedNumber(num, digits) { | ||
/** | ||
* returns the provided number with specified amount of decimal digits | ||
* @example | ||
* toFixedNumber(1.2345, 2) | ||
* // returns 1.23 | ||
*/ | ||
function toFixedNumber(num, digits) { | ||
return Number(num.toFixed(digits)); | ||
}; | ||
} | ||
var toPairs = function toPairs(obj) { | ||
return Object.keys(obj).map(function (key) { | ||
/** | ||
* returns an array of derived from the provided object key-value tuples | ||
* @example | ||
* toPairs({ a: 1, b: 2 }) | ||
* // returns [['a', 1], ['b', 2]] | ||
*/ | ||
function toPairs(obj) { | ||
return keys(obj).map(function (key) { | ||
return [key, obj[key]]; | ||
}); | ||
}; | ||
} | ||
/** | ||
* ensures that a function is executed at a fixed interval, so that it is not called too frequently | ||
* @example | ||
* const updatePreview = () => { ... } | ||
* const throttledUpdatePreview = trailingThrottle(updatePreview, 500); | ||
* inputField.addEventListener('input', throttledUpdatePreview); | ||
*/ | ||
function trailingThrottle(ms, fn) { | ||
var lastCall = Date.now() - 2 * ms; | ||
var trailingId; | ||
var invoke = function invoke() { | ||
@@ -1077,23 +1623,16 @@ lastCall = Date.now(); | ||
}; | ||
var cancel = function cancel() { | ||
return clearTimeout(trailingId); | ||
}; | ||
var throttled = function throttled() { | ||
var now = Date.now(); | ||
if (now - lastCall >= ms) { | ||
lastCall = Date.now(); | ||
} | ||
cancel(); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
trailingId = setTimeout.apply(void 0, [invoke, lastCall - now + ms].concat(args)); | ||
}; | ||
throttled.cancel = cancel; | ||
@@ -1103,2 +1642,9 @@ return throttled; | ||
/** | ||
* ensures that a function is executed at a fixed interval, so that it is not called too frequently | ||
* @example | ||
* const updatePreview = () => { ... } | ||
* const throttledUpdatePreview = leadingThrottle(updatePreview, 500); | ||
* inputField.addEventListener('input', throttledUpdatePreview); | ||
*/ | ||
function leadingThrottle(ms, fn) { | ||
@@ -1108,3 +1654,2 @@ var lastCall = 0; | ||
var now = Date.now(); | ||
if (now - lastCall >= ms) { | ||
@@ -1117,11 +1662,27 @@ lastCall = Date.now(); | ||
var stringCompare = function stringCompare(strA, strB) { | ||
/** | ||
* returns 0 for matching strings | ||
* return -1 for string with lower char code at some position | ||
* return 1 for string with greater char code at some position | ||
* @example | ||
* stringCompare('abc', 'abc') | ||
* // returns 0 | ||
* stringCompare('abc', 'abd') | ||
* // returns -1 | ||
*/ | ||
function stringCompare(strA, strB) { | ||
if (strA === strB) { | ||
return 0; | ||
} | ||
return strA < strB ? -1 : 1; | ||
}; | ||
} | ||
var leadingWhiteSpace = /^\s+/; | ||
/** | ||
* trims the beginning whitespaces from the provided string | ||
* @example | ||
* trimStart(' hello') | ||
* // returns 'hello' | ||
*/ | ||
function trimStart(str) { | ||
@@ -1132,2 +1693,9 @@ return str.replace(leadingWhiteSpace, ''); | ||
var trailingWhiteSpace = /\s+$/; | ||
/** | ||
* trims the end whitespaces from the provided string | ||
* @example | ||
* trimEnd('hello ') | ||
* // returns 'hello' | ||
*/ | ||
function trimEnd(str) { | ||
@@ -1137,2 +1705,8 @@ return str.replace(trailingWhiteSpace, ''); | ||
/** | ||
* returns the provided string repeated the specified amount of times | ||
* @example | ||
* repeat(3, 'test') | ||
* // returns 'testtesttest' | ||
*/ | ||
function repeat(count, text) { | ||
@@ -1142,2 +1716,8 @@ return Array(count + 1).join(text); | ||
/** | ||
* returns an array with all the duplicates from the provided array removed based on the iteratee function | ||
* @example | ||
* uniqBy(el => el.toString(), [1, '1', 2, '3', 3]) | ||
* // returns [1, 2, '3'] | ||
*/ | ||
function uniqBy(iteratee, arr) { | ||
@@ -1148,3 +1728,2 @@ // with polyfills this could be just: return Array.from(new Set(arr.map(iteratee))) | ||
var key = iteratee(element); | ||
if (seen.indexOf(key) === -1) { | ||
@@ -1154,3 +1733,2 @@ seen.push(key); | ||
} | ||
return false; | ||
@@ -1160,2 +1738,8 @@ }); | ||
/** | ||
* returns an array with all the duplicates from the provided array removed | ||
* @example | ||
* uniq([1, 1, 2, 3, 3]) | ||
* // returns [1, 2, 3] | ||
*/ | ||
function uniq(arr) { | ||
@@ -1165,2 +1749,8 @@ return uniqBy(identity, arr); | ||
/** | ||
* updates the provided array with the provided value on the specified index | ||
* @example | ||
* update(2, 3, [1, 2, 5]) | ||
* // returns [1, 2, 3] | ||
*/ | ||
function update(index, newElement, arr) { | ||
@@ -1170,2 +1760,8 @@ return [].concat(arr.slice(0, index), [newElement], arr.slice(index + 1, arr.length)); | ||
/** | ||
* returns an array without the specified elements from the provided array | ||
* @example | ||
* without([2, 4], [1, 2, 3, 4, 5]) | ||
* // returns [1, 3, 5] | ||
*/ | ||
function without(removed, arr) { | ||
@@ -1177,2 +1773,8 @@ return arr.filter(function (element) { | ||
/** | ||
* returns an array of tuples of certain index elements from two provided arrays based on the zipper function | ||
* @example | ||
* zipWith((a, b) => [a * 2, b.toString()], [1, 2, 3], [10, 20, 30]) | ||
* // returns [[2, '10'], [4, '20'], [6, '30']] | ||
*/ | ||
function zipWith(zipper, arrayA, arrayB) { | ||
@@ -1184,2 +1786,8 @@ return arrayA.map(function (elementA, index) { | ||
/** | ||
* returns an array of tuples of certain index elements from two provided arrays | ||
* @example | ||
* zip([1, 2, 3], [10, 20, 30]) | ||
* // returns [[1, 10], [2, 20], [3, 30]] | ||
*/ | ||
function zip(arrayA, arrayB) { | ||
@@ -1199,5 +1807,6 @@ return zipWith(pair, arrayA, arrayB); | ||
exports.debounce = debounce; | ||
exports.deepMapKeys = mapKeys; | ||
exports.deepMapKeys = deepMapKeys; | ||
exports.deepMerge = deepMerge; | ||
exports.defaultTo = defaultTo; | ||
exports.diffKeys = diffKeys; | ||
exports.drop = drop; | ||
@@ -1238,3 +1847,3 @@ exports.dropRight = dropRight; | ||
exports.leadingThrottle = leadingThrottle; | ||
exports.mapKeys = mapKeys$1; | ||
exports.mapKeys = mapKeys; | ||
exports.mapValues = mapValues; | ||
@@ -1241,0 +1850,0 @@ exports.mapValuesIndexed = mapValuesIndexed; |
@@ -7,2 +7,8 @@ (function (global, factory) { | ||
/** | ||
* adds two numbers | ||
* @example | ||
* add(1, 2) | ||
* // returns 3 | ||
*/ | ||
function add(first, second) { | ||
@@ -13,3 +19,9 @@ return first + second; | ||
var _ref = {}, | ||
hasOwnProperty = _ref.hasOwnProperty; | ||
hasOwnProperty = _ref.hasOwnProperty; | ||
/** | ||
* returns true or false depending if the provided property is present inside the provided object | ||
* @example | ||
* hasOwn('a', { a: 1, b: 2 }) | ||
* // returns true | ||
*/ | ||
function hasOwn(prop, obj) { | ||
@@ -19,3 +31,11 @@ return hasOwnProperty.call(obj, prop); | ||
/** | ||
* assigns properties of one or more source objects onto target object | ||
* @example | ||
* assign({ a: 1, b: 2 }, { b: 4, c: 5 }) | ||
* // returns { a: 1, b: 4, c: 5 } | ||
*/ | ||
function assign() { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
// eslint-disable-next-line no-func-assign | ||
@@ -26,3 +46,2 @@ assign = Object.assign || function (target) { | ||
} | ||
sources.forEach(function (source) { | ||
@@ -37,13 +56,24 @@ for (var key in source) { | ||
}; | ||
return assign.apply(void 0, arguments); | ||
} | ||
var capitalizeFirstLetter = function capitalizeFirstLetter(text) { | ||
/** | ||
* capitalizes first letter of string | ||
* @example | ||
* capitalizeFirstLetter('hello') | ||
* // returns 'Hello' | ||
*/ | ||
function capitalizeFirstLetter(text) { | ||
return text.charAt(0).toUpperCase() + text.slice(1); | ||
}; | ||
} | ||
/** | ||
* returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level | ||
* @example | ||
* const arr = [{ a: [1, 2] }, { a: [3, 4] }, { a: [5, 6] }] | ||
* flatMap(el => el.a, arr) | ||
* // returns [1, 2, 3, 4, 5, 6] | ||
*/ | ||
function flatMap(iteratee, arr) { | ||
var _ref; | ||
return (_ref = []).concat.apply(_ref, arr.map(iteratee)); | ||
@@ -58,18 +88,43 @@ } | ||
} | ||
return flatMap(fn, listOrFn); | ||
} | ||
/** | ||
* chunks provided array to specified size chunks | ||
* @example | ||
* chunk([1, 2, 3, 4], 2) | ||
* // returns [[1, 2], [3, 4]] | ||
*/ | ||
function chunk(arr, number) { | ||
if (number <= 0) { | ||
return []; | ||
} | ||
var chunked = []; | ||
for (var i = 0; i < arr.length; i += number) { | ||
chunked.push(arr.slice(i, i + number)); | ||
} | ||
return chunked; | ||
} | ||
var isArray = Array.isArray; | ||
/** | ||
* determines whether provided value is an array | ||
* @example | ||
* isArray([1, 2]) | ||
* // returns true | ||
* isArray('hello') | ||
* // returns false | ||
*/ | ||
function isArray(arr) { | ||
return Array.isArray(arr); | ||
} | ||
/** | ||
* returns true or false depending if the provided value is an object | ||
* @example | ||
* isObject({ a: 1 }) | ||
* // returns true | ||
* isObject([1, 2]) | ||
* // returns false | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
function isObject(obj) { | ||
@@ -79,4 +134,29 @@ return typeof obj === 'object' && obj !== null && !isArray(obj); | ||
/** | ||
* returns an array of the provided object keys | ||
* @example | ||
* keys({ a: 1, b: 2, c: 3 }) | ||
* // returns ['a', 'b', 'c'] | ||
*/ | ||
function keys(obj) { | ||
if ('keys' in Object && typeof Object.keys === 'function') { | ||
return Object.keys(obj); | ||
} | ||
var keysArray = []; | ||
for (var property in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, property)) { | ||
keysArray.push(property); | ||
} | ||
} | ||
return keysArray; | ||
} | ||
/** | ||
* maps values of the provided object | ||
* @example | ||
* mapValues(val => val.toUpperCase(), { a: 'foo', b: 'bar' }) | ||
* // returns { a: 'FOO', b: 'BAR' } | ||
*/ | ||
function mapValues(mapper, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
acc[key] = mapper(obj[key]); | ||
@@ -87,2 +167,8 @@ return acc; | ||
/** | ||
* returns an exact clone of the provided value with a new reference | ||
* @example | ||
* cloneDeep({ a: 1, b: 2 }) | ||
* // returns { a: 1, b: 2 } | ||
*/ | ||
function cloneDeep(value) { | ||
@@ -92,7 +178,5 @@ if (isArray(value)) { | ||
} | ||
if (isObject(value)) { | ||
return mapValues(cloneDeep, value); | ||
} | ||
return value; | ||
@@ -103,3 +187,4 @@ } | ||
// TODO: possibly could be rewritten using short-ish regexp | ||
var preserveCamelCase = function preserveCamelCase(input) { | ||
function preserveCamelCase(input) { | ||
var text = input; | ||
@@ -109,6 +194,4 @@ var isLastCharLower = false; | ||
var isLastLastCharUpper = false; | ||
for (var index = 0; index < text.length; index++) { | ||
var _char = text[index]; | ||
if (isLastCharLower && /[a-zA-Z]/.test(_char) && _char.toUpperCase() === _char) { | ||
@@ -131,27 +214,26 @@ text = text.slice(0, index) + '-' + text.slice(index); | ||
} | ||
return text; | ||
}; | ||
} | ||
/** | ||
* converts provided string to camel case | ||
* @example | ||
* camelCase('hello_world') | ||
* // returns 'helloWorld' | ||
*/ | ||
function camelCase(input) { | ||
var text = input.trim(); | ||
if (text.length === 0) { | ||
return ''; | ||
} | ||
if (text.length === 1) { | ||
return text.toLowerCase(); | ||
} | ||
if (/^[a-z\d]+$/.test(text)) { | ||
return text; | ||
} | ||
var hasUpperCase = text !== text.toLowerCase(); | ||
if (hasUpperCase) { | ||
text = preserveCamelCase(text); | ||
} | ||
text = text.replace(/^[_.\- ]+/, '').toLowerCase().replace(/[_.\- ]+(\w|$)/g, function (match, p1) { | ||
@@ -168,7 +250,5 @@ return p1.toUpperCase(); | ||
var value = collection[key]; | ||
if (value !== null && value !== undefined && !Number.isNaN(value)) { | ||
result[key] = value; | ||
} | ||
return result; | ||
@@ -178,2 +258,12 @@ }, {}); | ||
/** | ||
* returns a composed value from the provided arguments | ||
* @example | ||
* const f3 = (a: string) => `f3(${a})` | ||
* const f2 = (a: string) => `f2(${a})` | ||
* const f1 = (a: string) => `f1(${a})` | ||
* compose(f3, f2, f1)('arg') | ||
* // returns 'f3(f2(f1(arg)))' | ||
*/ | ||
function compose() { | ||
@@ -183,3 +273,2 @@ for (var _len = arguments.length, funcs = new Array(_len), _key = 0; _key < _len; _key++) { | ||
} | ||
return funcs.reduce(function (composed, next) { | ||
@@ -192,2 +281,7 @@ return function () { | ||
/** | ||
* delays the execution of a function until a certain period of time has passed without the function being called again | ||
* @example | ||
* debounce(1000, someFunction) | ||
*/ | ||
function debounce(ms, fn) { | ||
@@ -198,7 +292,5 @@ // actual return type of setTimeout differs per platform (browser vs node) | ||
clearTimeout(timeoutId); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
timeoutId = setTimeout.apply(void 0, [fn, ms].concat(args)); | ||
@@ -208,3 +300,9 @@ }; | ||
function mapKeys(mapper, obj) { | ||
/** | ||
* recurses through an object and transforms its keys – and the keys of any nested objects – based on the provided key mapping function | ||
* @example | ||
* deepMapKeys(key => `test_${key}`, { a: 1, b: { c: 2 } }) | ||
* // returns { test_a: 1, test_b: { test_c: 2 } } | ||
*/ | ||
function deepMapKeys(mapper, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
@@ -214,11 +312,9 @@ if (obj[key] && typeof obj[key] === 'object') { | ||
acc[mapper(key)] = obj[key].map(function (el) { | ||
return mapKeys(mapper, el); | ||
return deepMapKeys(mapper, el); | ||
}); | ||
return acc; | ||
} | ||
acc[mapper(key)] = mapKeys(mapper, obj[key]); | ||
acc[mapper(key)] = deepMapKeys(mapper, obj[key]); | ||
return acc; | ||
} | ||
acc[mapper(key)] = obj[key]; | ||
@@ -229,2 +325,8 @@ return acc; | ||
/** | ||
* deep merges provided objects by assigning the sources onto the target object passed as the first argument | ||
* @example | ||
* deepMerge({ a: 1, b: { c: 2 } }, { b: { d: 3 } }) | ||
* // returns { a: 1, b: { c: 2, d: 3 } } | ||
*/ | ||
function deepMerge(target) { | ||
@@ -234,6 +336,4 @@ for (var _len = arguments.length, sources = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
} | ||
if (!sources.length) return target; | ||
var source = sources.shift(); | ||
if (isObject(target) && isObject(source)) { | ||
@@ -243,3 +343,2 @@ for (var key in source) { | ||
var _Object$assign; | ||
if (!target[key]) Object.assign(target, (_Object$assign = {}, _Object$assign[key] = {}, _Object$assign)); | ||
@@ -249,3 +348,2 @@ deepMerge(target[key], source[key]); | ||
var _Object$assign2; | ||
Object.assign(target, (_Object$assign2 = {}, _Object$assign2[key] = source[key], _Object$assign2)); | ||
@@ -255,6 +353,11 @@ } | ||
} | ||
return deepMerge.apply(void 0, [target].concat(sources)); | ||
} | ||
/** | ||
* returns true or false depending if the provided value is null or undefined | ||
* @example | ||
* isNil(null) | ||
* // returns true | ||
*/ | ||
function isNil(value) { | ||
@@ -264,2 +367,11 @@ return value === null || value === undefined; | ||
/** | ||
* returns a function that invoked with a null or undefined values returns the provided default | ||
* @example | ||
* const defaulter = defaultTo('default') | ||
* defaulter('hello') | ||
* // returns 'hello' | ||
* defaulter(null) | ||
* // returns 'default' | ||
*/ | ||
function defaultTo(defaultValue) { | ||
@@ -271,2 +383,23 @@ return function (value) { | ||
/** | ||
* returns an array of keys of the object passed as the first argument that are not present, or that are present, but have different values, in object passed as the second argument | ||
* @example | ||
* diffKeys({ a: 1, b: 2, c: 3 }, { c: 3, d: 4 }) | ||
* // returns ['a', 'b'] | ||
*/ | ||
function diffKeys(obj, comparable) { | ||
return keys(obj).reduce(function (diffKeys, key) { | ||
if (obj[key] !== comparable[key]) { | ||
diffKeys.push(key); | ||
} | ||
return diffKeys; | ||
}, []); | ||
} | ||
/** | ||
* drops specific amount of elements of the provided array starting at the beginning | ||
* @example | ||
* drop(2, [1, 2, 3, 4]) | ||
* // returns [3, 4] | ||
*/ | ||
function drop(count, arr) { | ||
@@ -276,2 +409,8 @@ return arr.slice(count); | ||
/** | ||
* drops specific amount of elements of the provided array starting at the end | ||
* @example | ||
* dropRight(2, [1, 2, 3, 4]) | ||
* // returns [1, 2] | ||
*/ | ||
function dropRight(count, arr) { | ||
@@ -281,2 +420,10 @@ return arr.slice(0, -count); | ||
/** | ||
* returns an array if an array was provided, or a new array if provided value was not an array | ||
* @example | ||
* ensureArray([1, 2]) | ||
* // returns [1, 2] | ||
* ensureArray('test') | ||
* // returns ['test'] | ||
*/ | ||
function ensureArray(maybeArr) { | ||
@@ -286,4 +433,10 @@ return isArray(maybeArr) ? maybeArr : [maybeArr]; | ||
/** | ||
* returns an array of entries of the provided object | ||
* @example | ||
* entries({ a: 1, b: 2 }) | ||
* // returns [['a', 1], ['b', 2]] | ||
*/ | ||
function entries(obj) { | ||
return Object.keys(obj).map(function (key) { | ||
return keys(obj).map(function (key) { | ||
return [key, obj[key]]; | ||
@@ -293,6 +446,11 @@ }); | ||
/** | ||
* returns the first element of the provided array that returns true for the predicate function, or undefined if the element was not found | ||
* @example | ||
* find(el => el === 2, [1, 2, 3]) | ||
* // returns 2 | ||
*/ | ||
function find(predicate, arr) { | ||
for (var index = 0; index < arr.length; index++) { | ||
var element = arr[index]; | ||
if (predicate(element)) { | ||
@@ -304,2 +462,10 @@ return element; | ||
/** | ||
* returns the index of the element of the provided array that returns true for the predicate function, or -1 if the element was not found | ||
* @example | ||
* findIndex(el => el === 2, [1, 2, 3]) | ||
* // returns 1 | ||
* findIndex(el => el === 5, [1, 2, 3]) | ||
* // returns -1 | ||
*/ | ||
function findIndex(predicate, arr) { | ||
@@ -311,12 +477,25 @@ for (var index = 0; index < arr.length; index++) { | ||
} | ||
return -1; | ||
} | ||
/** | ||
* returns the key of the provided object that returns true for the predicate function, or undefined if the key was not found | ||
* @example | ||
* findKey(el => el === 1, { a: 1, b: { c: 2 } }) | ||
* // returns 'a' | ||
* findKey(el => el === 2, { a: 1, b: { c: 2 } }) | ||
* // returns undefined | ||
*/ | ||
function findKey(predicate, obj) { | ||
return find(function (key) { | ||
return predicate(obj[key]); | ||
}, Object.keys(obj)); | ||
}, keys(obj)); | ||
} | ||
/** | ||
* returns the last element of the provided array that returns true for the predicate function, or undefined if the element was not found | ||
* @example | ||
* findLast(el => el % 2 === 0, [1, 2, 3, 4]) | ||
* // returns 4 | ||
*/ | ||
function findLast(predicate, arr) { | ||
@@ -330,2 +509,8 @@ for (var index = arr.length - 1; index >= 0; index--) { | ||
/** | ||
* returns the index of the last element of the provided array that returns true for the predicate function, starting from the specified index element to the begining of the array, or -1 if the element was not found | ||
* @example | ||
* findLastIndexFrom(el => el % 2 === 0, 3, [1, 2, 3, 4, 5, 6]) | ||
* // returns 3 | ||
*/ | ||
function findLastIndexFrom(predicate, startIndex, arr) { | ||
@@ -337,6 +522,11 @@ for (var index = startIndex; index >= 0; index--) { | ||
} | ||
return -1; | ||
} | ||
/** | ||
* returns the index of the last element of the provided array that returns true for the predicate function, or -1 if the element was not found | ||
* @example | ||
* findLastIndex(el => el % 2 === 0, [1, 2, 3, 4]) | ||
* // returns 3 | ||
*/ | ||
function findLastIndex(predicate, arr) { | ||
@@ -346,12 +536,25 @@ return findLastIndexFrom(predicate, arr.length - 1, arr); | ||
/** | ||
* returns an array of requested length filled with provided value | ||
* @example | ||
* filledArray(3, 1) | ||
* // returns [1, 1, 1] | ||
*/ | ||
function filledArray(length, value) { | ||
if (length <= 0) { | ||
return []; | ||
} | ||
var arr = []; | ||
while (length--) { | ||
arr.push(value); | ||
} | ||
return arr; | ||
} | ||
/** | ||
* returns the provided value | ||
* @example | ||
* identity('hello') | ||
* // returns 'hello' | ||
*/ | ||
function identity(value) { | ||
@@ -361,2 +564,10 @@ return value; | ||
/** | ||
* flattens the provided array by one level | ||
* @example | ||
* flatten([1, 2, [3, 4]]) | ||
* // returns [1, 2, 3, 4] | ||
* flatten([1, 2, [3, [4, 5]], 6]) | ||
* // returns [1, 2, 3, [4, 5], 6] | ||
*/ | ||
function flatten(arr) { | ||
@@ -366,4 +577,12 @@ return flatMap(identity, arr); | ||
/** | ||
* invokes the provided callback for each of the provided object fields | ||
* @example | ||
* const obj = { a: 1, b: 2, c: 3 } | ||
* const arr: string[] = [] | ||
* forOwn(el => arr.push(el.toString()), obj) | ||
* // arr equals ['1', '2', '3'] | ||
*/ | ||
function forOwn(callback, obj) { | ||
return Object.keys(obj).forEach(function (key) { | ||
return keys(obj).forEach(function (key) { | ||
callback(obj[key], key); | ||
@@ -373,6 +592,13 @@ }); | ||
/** | ||
* returns an object constructed from the provided array of key, value pairs' | ||
* @example | ||
* const arr = [['a', 1], ['b', 2], ['c', 3]] | ||
* fromPairs(arr) | ||
* // returns { a: 1, b: 2, c: 3 } | ||
*/ | ||
function fromPairs(pairs) { | ||
return pairs.reduce(function (obj, _ref) { | ||
var key = _ref[0], | ||
value = _ref[1]; | ||
value = _ref[1]; | ||
obj[key] = value; | ||
@@ -383,2 +609,8 @@ return obj; | ||
/** | ||
* generates a random id | ||
* @example | ||
* generateRandomId() | ||
* // returns 'd1rjknhhch8' | ||
*/ | ||
function generateRandomId() { | ||
@@ -388,2 +620,8 @@ return Math.random().toString(36).substring(2); | ||
/** | ||
* generates an unique id based on the provided object keys | ||
* @example | ||
* generateUniqueId({ xuvarw8cao: 1, b837g2nba1d: 2 }) | ||
* // returns 'd1rjknhhch8' | ||
*/ | ||
function generateUniqueId(map) { | ||
@@ -394,3 +632,11 @@ var id = generateRandomId(); | ||
// based on https://github.com/developit/dlv/blob/d7ec976d12665f1c25dec2acf955dfc2e8757a9c/index.js | ||
/** | ||
* returns the value from the specififed path from the provided object | ||
* @example | ||
* const obj = { a: { b: [1, 2, 3] } } | ||
* get('a.b.1', obj) | ||
* // returns 2 | ||
* get(['a', 'b', '1'], obj) | ||
* // returns 2 | ||
*/ | ||
function get(propPath, obj) { | ||
@@ -400,10 +646,17 @@ var arrPath = typeof propPath === 'string' ? propPath.split('.') : propPath; | ||
var result = obj; | ||
while (result && pathPartIndex < arrPath.length) { | ||
result = result[arrPath[pathPartIndex++]]; | ||
} | ||
return result; | ||
} | ||
/** | ||
* returns the value from the specififed path from the provided object or the default value if the path element was not found | ||
* @example | ||
* const obj = { a: { b: [1, 2, 3] } } | ||
* getOr(null, 'a.b.1', obj) | ||
* // returns 2 | ||
* getOr(null, 'a.c', obj) | ||
* // returns null | ||
*/ | ||
function getOr(defaultValue, prop, obj) { | ||
@@ -414,2 +667,19 @@ var propValue = get(prop, obj); | ||
/** | ||
* groups values from the provided array or object based on the result of the provided mapper function | ||
* @example | ||
* const arr = [ | ||
* { a: 1, b: 2 }, | ||
* { a: 2, b: 4 }, | ||
* { a: 3, b: 6 }, | ||
* ] | ||
* groupBy(el => (el.a % 2 === 0 ? 'even' : 'odd'), arr) | ||
* // returns { | ||
* // odd: [ | ||
* // { a: 1, b: 2 }, | ||
* // { a: 3, b: 6 }, | ||
* // ], | ||
* // even: [{ a: 2, b: 4 }], | ||
* // } | ||
*/ | ||
function groupBy(mapper, collection) { | ||
@@ -425,4 +695,14 @@ return Object.keys(collection).reduce(function (grouped, key) { | ||
/** | ||
* groups values from the provided object based on the result of the provided key mapping function | ||
* @example | ||
* const obj = { a: 1, b: 2, c: 3 } | ||
* groupKeys(el => (el === 'a' ? 'aaa' : 'rest'), obj) | ||
* // returns { | ||
* // aaa: { a: 1 }, | ||
* // rest: { b: 2, c: 3 }, | ||
* // } | ||
*/ | ||
function groupKeys(mapper, obj) { | ||
return Object.keys(obj).reduce(function (grouped, key) { | ||
return keys(obj).reduce(function (grouped, key) { | ||
var groupKey = mapper(key); | ||
@@ -435,2 +715,11 @@ grouped[groupKey] = grouped[groupKey] || {}; | ||
/** | ||
* returns true or false depending if the provided value is present inside the provided array or string | ||
* @example | ||
* includes('a', ['a', 'b', 'c']) | ||
* // returns true | ||
* includes('d', 'abc') | ||
* // returns false | ||
*/ | ||
function includes(value, arrOrStr) { | ||
@@ -440,2 +729,8 @@ return arrOrStr.indexOf(value) !== -1; | ||
/** | ||
* returns true or false depending if the provided value is an empty object or an empty array | ||
* @example | ||
* isEmpty({}) | ||
* // returns true | ||
*/ | ||
function isEmpty(collection) { | ||
@@ -445,14 +740,43 @@ return (isArray(collection) ? collection : Object.keys(collection)).length === 0; | ||
function isFalsy (value) { | ||
/** | ||
* returns true or false depending if the provided value is falsy | ||
* @example | ||
* isFalsy(0) | ||
* // returns true | ||
*/ | ||
function isFalsy(value) { | ||
return !value; | ||
} | ||
function isTruthy (value) { | ||
/** | ||
* returns true or false depending if the provided value is truthy | ||
* @example | ||
* isTruthy(1) | ||
* // returns true | ||
*/ | ||
function isTruthy(value) { | ||
return !!value; | ||
} | ||
var isPromise = function isPromise(promise) { | ||
/** | ||
* returns true or false depending if the provided value is a Promise | ||
* @example | ||
* isPromise(new Promise(res => res(null))) | ||
* // returns true | ||
*/ | ||
function isPromise(promise) { | ||
return !!promise && typeof promise.then === 'function'; | ||
}; | ||
} | ||
/** | ||
* constructs an object from the provided array of objects, grouped by the value of the specified key | ||
* @example | ||
* const arr = [ | ||
* { a: 'foo', b: 'bar' }, | ||
* { a: 'foo', b: 'baz' }, | ||
* { a: 'test', b: 'bab' }, | ||
* ] | ||
* keyBy('a', arr) | ||
* // returns { foo: { a: 'foo', b: 'baz' }, test: { a: 'test', b: 'bab' } } | ||
*/ | ||
function keyBy(prop, arr) { | ||
@@ -465,22 +789,24 @@ return arr.reduce(function (acc, el) { | ||
function keys(obj) { | ||
var keysArray = []; | ||
for (var property in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, property)) { | ||
keysArray.push(property); | ||
} | ||
} | ||
return keysArray; | ||
} | ||
// TODO: this should return `T | undefined` to match native behavior | ||
/** | ||
* returns the last element of the provided array or undefined when array is empty | ||
* @example | ||
* last([1, 2, 3]) | ||
* // returns 3 | ||
*/ | ||
function last(arr) { | ||
return arr.length > 0 ? arr[arr.length - 1] : null; | ||
return arr.length > 0 ? arr[arr.length - 1] : undefined; | ||
} | ||
function mapKeys$1(mapper, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
acc[mapper(key)] = obj[key]; | ||
/** | ||
* maps keys of the provided object | ||
* @example | ||
* mapKeys(key => key.toUpperCase(), { a: 1, b: 2 }) | ||
* // returns { A: 1, B: 2 } | ||
*/ | ||
function mapKeys(mapper, obj) { | ||
return keys(obj).reduce(function (acc, key) { | ||
Object.defineProperty(acc, mapper(key), { | ||
value: obj[key], | ||
enumerable: true | ||
}); | ||
return acc; | ||
@@ -490,4 +816,10 @@ }, {}); | ||
/** | ||
* maps values and keys of the provided object | ||
* @example | ||
* mapValuesIndexed((val, key) => `${key}-${val}`, { a: 1, b: 2 }) | ||
* // returns { a: 'a-1', b: 'b-2' } | ||
*/ | ||
function mapValuesIndexed(iteratee, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
acc[key] = iteratee(obj[key], key); | ||
@@ -498,2 +830,8 @@ return acc; | ||
/** | ||
* deep merges the two provided objects | ||
* @example | ||
* merge({ a: 1 }, { b: 2 }) | ||
* // returns { a: 1, b: 2 } | ||
*/ | ||
function merge(objA, objB) { | ||
@@ -503,34 +841,11 @@ if (isEmpty(objB)) { | ||
} | ||
var result = {}; | ||
forOwn(function (value, key) { | ||
if (hasOwn(key, objB)) { | ||
if (isObject(objA[key]) && isObject(objB[key])) { | ||
result[key] = merge(objA[key], objB[key]); | ||
} else if (isArray(objA[key]) && isArray(objB[key])) { | ||
var length = Math.max(objA[key].length, objB[key].length); | ||
result[key] = new Array(length); | ||
for (var i = 0; i < length; i++) { | ||
if (i in objB[key]) { | ||
result[key][i] = objB[key][i]; | ||
} else if (i in objA[key]) { | ||
result[key][i] = objA[key][i]; | ||
} | ||
} | ||
} else { | ||
result[key] = objB[key]; | ||
} | ||
} else { | ||
result[key] = objA[key]; | ||
} | ||
}, objA); | ||
forOwn(function (value, key) { | ||
if (!hasOwn(key, result)) { | ||
result[key] = objB[key]; | ||
} | ||
}, objB); | ||
return result; | ||
return deepMerge({}, objA, objB); | ||
} | ||
/** | ||
* deep merges all provided objects | ||
* @example | ||
* mergeAll({ a: 1 }, { b: 2 }, { c: 3 }) | ||
* // returns { a: 1, b: 2, c: 3 } | ||
*/ | ||
function mergeAll(objs) { | ||
@@ -540,5 +855,4 @@ if (objs.length === 0) { | ||
} | ||
var first = objs[0], | ||
rest = objs.slice(1); | ||
rest = objs.slice(1); | ||
return rest.reduce(function (merged, obj) { | ||
@@ -549,2 +863,9 @@ return merge(merged, obj); | ||
/** | ||
* memoizes the provided function so it returns cached results but based on the provided key resolver | ||
* @example | ||
* const memoizedFunc = memoizeWith(key => (key === 'a' ? key : 'bar'), (val: string) => ({ current: val })) | ||
* memoizedFunc('a') === memoizedFunc('a') // true | ||
* memoizedFunc('b') === memoizedFunc('c') // true | ||
*/ | ||
function memoizeWith(keyResolver, func) { | ||
@@ -554,7 +875,5 @@ var cache = {}; | ||
var key = keyResolver.apply(void 0, arguments); | ||
if (hasOwn(key, cache)) { | ||
return cache[key]; | ||
} | ||
var value = func.apply(void 0, arguments); | ||
@@ -567,2 +886,10 @@ cache[key] = value; | ||
// TODO: technically this should accept AnyFunction but it doesn't type check currently with that for some reason | ||
/** | ||
* memoizes the provided function so it returns cached results when invoked with the same arguments | ||
* @example | ||
* const memoizedFunc = memoize((val: number) => ({ current: val }) | ||
* memoizedFunc(3) === memoizedFunc(3) | ||
* // returns true | ||
*/ | ||
function memoize(func) { | ||
@@ -572,2 +899,15 @@ return memoizeWith(identity, func); | ||
/** | ||
* memoizes the provided function so it returns cached results but only with one cache key | ||
* @example | ||
* const memoizedFunc = memoizeOne((val: number) => ({ current: val }) | ||
* | ||
* const resultFor1 = memoizedFunc(1) // { current: 1 } | ||
* resultFor1 === memoizedFunc(1) // true | ||
* resultFor1 === memoizedFunc(1) // true | ||
* | ||
* const resultFor2 = memoizedFunc(2) // { current: 2 } | ||
* resultFor2 === memoizedFunc(2) // true | ||
* resultFor1 === memoizedFunc(1) // false | ||
*/ | ||
function memoizeOne(fn) { | ||
@@ -581,3 +921,2 @@ var called = false; | ||
} | ||
called = true; | ||
@@ -590,7 +929,18 @@ cacheKey = arguments.length <= 0 ? undefined : arguments[0]; | ||
/** | ||
* does literally nothing | ||
* @example | ||
* somethingAsyncAndDangerous().catch(noop) | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
function noop() {} | ||
/** | ||
* returns an array of values of the provided object | ||
* @example | ||
* values({ a: 1, b: 2, c: 3 }) | ||
* // returns [1, 2, 3] | ||
*/ | ||
function values(obj) { | ||
return Object.keys(obj).map(function (key) { | ||
return keys(obj).map(function (key) { | ||
return obj[key]; | ||
@@ -600,2 +950,13 @@ }); | ||
/** | ||
* sorts values of the provided object or array based on the provided mapping function or the provided prop string | ||
* @example | ||
* const obj = { | ||
* a: { chats: 1 }, | ||
* b: { chats: 3 }, | ||
* c: { chats: 2 }, | ||
* } | ||
* numericSortBy(el => el.chats * -1, obj) | ||
* // returns [{ chats: 3 }, { chats: 2 }, { chats: 1 }] | ||
*/ | ||
function numericSortBy(propOrMapper, collection) { | ||
@@ -610,8 +971,13 @@ var mapper = typeof propOrMapper === 'function' ? propOrMapper : function (element) { | ||
/** | ||
* returns an object the same as the provided one but without the fields omitted by the predicate function | ||
* @example | ||
* omitByIndexed((val, key) => key === 'b', { a: 1, b: 2, c: 3 }) | ||
* // returns { a: 1, c: 3 } | ||
*/ | ||
function omitByIndexed(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
if (!predicate(obj[key], key)) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
@@ -621,2 +987,8 @@ }, {}); | ||
/** | ||
* returns the provided object but with specified keys omitted | ||
* @example | ||
* omit(['a'], { a: 1, b: 2 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function omit(keys, obj) { | ||
@@ -628,8 +1000,13 @@ return omitByIndexed(function (_, key) { | ||
/** | ||
* returns an object same as the provided one but without the fields omitted by the predicate function | ||
* @example | ||
* omitBy(el => el % 2 === 0, { a: 1, b: 2, c: 3 }) | ||
* // returns { a: 1, c: 3 } | ||
*/ | ||
function omitBy(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
if (!predicate(obj[key])) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
@@ -639,2 +1016,12 @@ }, {}); | ||
/** | ||
* returns a particular function to be executed only a single time | ||
* @example | ||
* const results = [1, 2, 3, 4, 5] | ||
* const getResult = once(() => results.pop()) | ||
* getResult() // 5 | ||
* getResult() // 5 | ||
* getResult() // 5 | ||
* | ||
*/ | ||
function once(fn) { | ||
@@ -647,3 +1034,2 @@ var called = false; | ||
} | ||
called = true; | ||
@@ -654,2 +1040,9 @@ return result = fn.apply(void 0, arguments); | ||
/** | ||
* returns a function that called invokes the provided array of functions with the call arguments | ||
* @example | ||
* const func = over([val => val + 1, val => val * 10, Boolean]) | ||
* func(1) | ||
* // returns [2, 10, true] | ||
*/ | ||
function over(fns) { | ||
@@ -660,3 +1053,2 @@ return function () { | ||
} | ||
return fns.map(function (fn) { | ||
@@ -668,2 +1060,8 @@ return fn.apply(void 0, args); | ||
/** | ||
* returns the specified amount of the provided array elements starting from the end | ||
* @example | ||
* takeLast(2, ['a', 'b', 'c', 'd']) | ||
* // returns ['c', 'd'] | ||
*/ | ||
function takeLast(count, arr) { | ||
@@ -673,2 +1071,9 @@ return arr.slice(-count); | ||
/** | ||
* returns a function that called invokes the provided arguments transformers | ||
* @example | ||
* const func = overArgs((a, b, c) => [a, b, c], [n => n * n, Boolean]) | ||
* func(2, 0, 1) | ||
* // returns [4, false, 1] | ||
*/ | ||
function overArgs(fn, transformers) { | ||
@@ -679,3 +1084,2 @@ return function () { | ||
} | ||
var transformed = transformers.map(function (transform, index) { | ||
@@ -688,2 +1092,8 @@ return transform(args[index]); | ||
/** | ||
* returns a tuple from the two provided values | ||
* @example | ||
* pair('a', 'b') | ||
* // returns ['a', 'b'] | ||
*/ | ||
function pair(a, b) { | ||
@@ -693,4 +1103,10 @@ return [a, b]; | ||
/** | ||
* returns a tuple of valid and invalid parts of the object as defined in validation function passed in the first argument | ||
* @example | ||
* partitionObject(el => el % 2 === 0, { a: 1, b: 2, c: 3 }) | ||
* // returns [{ b: 2 }, { a: 1, c: 3 }] | ||
*/ | ||
function partitionObject(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
acc[predicate(obj[key]) ? 0 : 1][key] = obj[key]; | ||
@@ -701,2 +1117,8 @@ return acc; | ||
/** | ||
* picks specified properties from the object | ||
* @example | ||
* pick(['b'], { a: 1, b: 2 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function pick(props, obj) { | ||
@@ -709,8 +1131,13 @@ return props.reduce(function (acc, prop) { | ||
/** | ||
* returns an object same as the provided one but with the fields picked by the predicate function | ||
* @example | ||
* pickBy(el => el % 2 === 0, { a: 1, b: 2, c: 3 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function pickBy(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
if (predicate(obj[key])) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
@@ -720,8 +1147,13 @@ }, {}); | ||
/** | ||
* returns an object same as the provided one but with the fields picked by the predicate function | ||
* @example | ||
* pickByIndexed((val, key) => key === 'b', { a: 1, b: 2, c: 3 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function pickByIndexed(predicate, obj) { | ||
return Object.keys(obj).reduce(function (acc, key) { | ||
return keys(obj).reduce(function (acc, key) { | ||
if (predicate(obj[key], key)) { | ||
acc[key] = obj[key]; | ||
} | ||
return acc; | ||
@@ -731,2 +1163,8 @@ }, {}); | ||
/** | ||
* picks specified props only if they exist on the provided object | ||
* @example | ||
* pickOwn(['b'], { a: 1, b: 2 }) | ||
* // returns { b: 2 } | ||
*/ | ||
function pickOwn(props, obj) { | ||
@@ -737,3 +1175,2 @@ return props.reduce(function (acc, prop) { | ||
} | ||
return acc; | ||
@@ -743,2 +1180,8 @@ }, {}); | ||
/** | ||
* returns a random integer number between the specified min and max values | ||
* @example | ||
* randomInt(0, 10) | ||
* // returns 7 | ||
*/ | ||
function randomInt(min, max) { | ||
@@ -748,13 +1191,23 @@ return min + Math.floor(Math.random() * (max - min + 1)); | ||
/** | ||
* returns an array filled with numbers in an ascending order to the provided max value | ||
* @example | ||
* range(5) | ||
* // returns [0, 1, 2, 3, 4, 5] | ||
*/ | ||
function range(max) { | ||
var arr = []; | ||
var counter = 0; | ||
while (counter <= max) { | ||
arr.push(counter++); | ||
} | ||
return arr; | ||
} | ||
/** | ||
* returns an array with elements that return false for the provided predicate function | ||
* @example | ||
* reject(el => el % 2 === 0, [1, 2, 3, 4, 5]) | ||
* // returns [1, 3, 5] | ||
*/ | ||
function reject(predicate, arr) { | ||
@@ -766,2 +1219,8 @@ return arr.filter(function (element) { | ||
/** | ||
* returns the provided array without the specified index element | ||
* @example | ||
* removeAt(2, [1, 2, 3, 4, 5]) | ||
* // returns [1, 2, 4, 5] | ||
*/ | ||
function removeAt(index, arr) { | ||
@@ -774,6 +1233,5 @@ var copy = [].concat(arr); | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
_extends = Object.assign ? Object.assign.bind() : function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
@@ -785,9 +1243,6 @@ if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
@@ -801,43 +1256,40 @@ if (!o) return; | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _createForOfIteratorHelperLoose(o, allowArrayLike) { | ||
var it; | ||
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
return function () { | ||
if (i >= o.length) return { | ||
done: true | ||
}; | ||
return { | ||
done: false, | ||
value: o[i++] | ||
}; | ||
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; | ||
if (it) return (it = it.call(o)).next.bind(it); | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
return function () { | ||
if (i >= o.length) return { | ||
done: true | ||
}; | ||
} | ||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
return { | ||
done: false, | ||
value: o[i++] | ||
}; | ||
}; | ||
} | ||
it = o[Symbol.iterator](); | ||
return it.next.bind(it); | ||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
/** | ||
* sets the property of the provided object specified by the provided path in form of a string or an array of keys | ||
* @example | ||
* const obj = { a: 1, b: { c: 2 } } | ||
* set('b.d', 3, obj) | ||
* // returns { a: 1, b: { c: 2, d: 3 } } | ||
* set(['b', 'd'], 3, obj) | ||
* // returns { a: 1, b: { c: 2, d: 3 } } | ||
*/ | ||
function set(_keys, val, obj) { | ||
var _extends2; | ||
var keys = _keys.split ? _keys.split('.') : _keys; | ||
var keys = isArray(_keys) ? _keys : _keys.split('.'); | ||
var index = keys[0]; | ||
var finalVal = val; | ||
if (keys.length > 1) { | ||
@@ -848,3 +1300,2 @@ // eslint-disable-next-line eqeqeq | ||
} | ||
return _extends({}, obj, (_extends2 = {}, _extends2[index] = finalVal, _extends2)); | ||
@@ -854,23 +1305,26 @@ } | ||
// https://github.com/reactjs/react-redux/blob/5d792a283554cff3d2f54fad1be1f79cbcab33fe/src/utils/shallowEqual.js | ||
function is(first, second) { | ||
if (first === second) { | ||
return first !== 0 || second !== 0 || 1 / first === 1 / second; | ||
} // eslint-disable-next-line no-self-compare | ||
} | ||
// eslint-disable-next-line no-self-compare | ||
return first !== first && second !== second; | ||
} | ||
/** | ||
* returns true if the provided values are shallow equal | ||
* @example | ||
* shallowEqual({ a: 1 }, { a: 1 }) | ||
* // returns true | ||
* shallowEqual({ a: { b: 1 } }, { a: { b: 1 } }) | ||
* // returns false | ||
*/ | ||
function shallowEqual(objA, objB) { | ||
if (is(objA, objB)) return true; | ||
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) { | ||
return false; | ||
} | ||
var keysA = Object.keys(objA); | ||
var keysB = Object.keys(objB); | ||
if (keysA.length !== keysB.length) return false; | ||
for (var index = 0; index < keysA.length; index++) { | ||
@@ -881,29 +1335,37 @@ if (!hasOwn(keysA[index], objB) || !is(objA[keysA[index]], objB[keysA[index]])) { | ||
} | ||
return true; | ||
} | ||
var shortenLongText = function shortenLongText(limit, text) { | ||
/** | ||
* shortens the provided string by the specified amount | ||
* @example | ||
* shortenLongText(5, 'Lorem ipsum dolor') | ||
* // returns 'Lorem...' | ||
*/ | ||
function shortenLongText(limit, text) { | ||
if (text.length <= limit) { | ||
return text; | ||
} | ||
var words = text.split(' '); | ||
var sentence = []; | ||
var totalLength = 0; | ||
for (var _iterator = _createForOfIteratorHelperLoose(words), _step; !(_step = _iterator()).done;) { | ||
var word = _step.value; | ||
if (totalLength + word.length > limit) { | ||
break; | ||
} | ||
totalLength += word.length + 1; | ||
sentence.push(word); | ||
} | ||
return sentence.join(' ') + "..."; | ||
}; | ||
} | ||
/** | ||
* returns a sign of the provided number or NaN if value different than number was provided | ||
* @example | ||
* sign(10) | ||
* // returns 1 | ||
* sign(-8) | ||
* // returns -1 | ||
*/ | ||
function sign(value) { | ||
@@ -913,19 +1375,20 @@ if (typeof value !== 'number' || isNaN(value)) { | ||
} | ||
if (value === 0) { | ||
if (Object.is(value, 0)) { | ||
return 0; | ||
} // eslint-disable-next-line no-compare-neg-zero | ||
if (value === -0) { | ||
} | ||
if (Object.is(value, -0)) { | ||
return -0; | ||
} | ||
return value > 0 ? 1 : -1; | ||
} | ||
/** | ||
* returns an array with the provided array values shuffled | ||
* @example | ||
* shuffle([1, 2, 3, 4]) | ||
* // returns [4, 1, 3, 2] | ||
*/ | ||
function shuffle(arr) { | ||
var copy = arr.slice(); | ||
var lastIndex = arr.length - 1; | ||
for (var i = 0; i < arr.length; i++) { | ||
@@ -937,6 +1400,11 @@ var randomIndex = randomInt(i, lastIndex); | ||
} | ||
return copy; | ||
} | ||
/** | ||
* returns the diff object from the provided object and slice comparison | ||
* @example | ||
* sliceDiff({ a: 1, b: 10, g: 3 }, { a: 1, b: 2, c: 3 }) | ||
* // returns { b: 10, g: 3 } | ||
*/ | ||
function sliceDiff(slice, obj) { | ||
@@ -950,2 +1418,9 @@ var picked = pickByIndexed(function (value, key) { | ||
// TODO: this could be written a lot better | ||
/** | ||
* snake cases the provided string | ||
* @example | ||
* snakeCase('helloWorld') | ||
* // returns 'hello_world' | ||
*/ | ||
function snakeCase(str) { | ||
@@ -960,2 +1435,8 @@ var snakeCased = str.replace(/[A-Z]|([-_ ]+)/g, function (match) { | ||
/** | ||
* returns true if some of the array values is truthy | ||
* @example | ||
* someAreTruthy([0, 1, 2]) | ||
* // returns true | ||
*/ | ||
function someAreTruthy(arr) { | ||
@@ -965,2 +1446,11 @@ return arr.some(identity); | ||
/** | ||
* splits an array or a string at the given index | ||
* @example | ||
* splitAt(2, [1, 2, 3, 4, 5]) | ||
* // returns [[1, 2], [3, 4, 5]] | ||
* splitAt(2, 'foobar') | ||
* // returns ['fo', 'obar'] | ||
*/ | ||
function splitAt(splitPoint, arrOrStr) { | ||
@@ -970,12 +1460,14 @@ return [arrOrStr.slice(0, splitPoint), arrOrStr.slice(splitPoint, arrOrStr.length)]; | ||
/** | ||
* returns a tuple with the provided array splited on an element that returned true for the provided function, iterating from the right side | ||
* @example | ||
* splitRightWhenAccum((el, acc) => [el % 2 === 0, acc], [], [1, 2, 3, 4]) | ||
* // returns [[1, 2, 3], [4]] | ||
*/ | ||
function splitRightWhenAccum(fn, acc, arr) { | ||
var result = false; | ||
for (var index = arr.length; index > 0; index--) { | ||
var _fn = fn(arr[index - 1], acc); | ||
result = _fn[0]; | ||
acc = _fn[1]; | ||
if (result) { | ||
@@ -985,6 +1477,12 @@ return splitAt(index - 1, arr); | ||
} | ||
return [[], arr]; | ||
} | ||
/** | ||
* returns a function that called with an array of values calls the provided function with the spreaded arguments from the array arguments | ||
* @example | ||
* const func = spread((a, b, c) => a + b + c) | ||
* func([1, 2, 3]) | ||
* // returns 6 | ||
*/ | ||
function spread(fn) { | ||
@@ -996,2 +1494,8 @@ return function (args) { | ||
/** | ||
* returns a sum of all the provided array number values | ||
* @example | ||
* sum([1, 2, 3]) | ||
* // returns 6 | ||
*/ | ||
function sum(numbers) { | ||
@@ -1001,2 +1505,8 @@ return numbers.reduce(add, 0); | ||
/** | ||
* returns the specified amount of the provided array elements starting from the beginning | ||
* @example | ||
* take(2, ['a', 'b', 'c', 'd']) | ||
* // returns ['a', 'b'] | ||
*/ | ||
function take(count, arr) { | ||
@@ -1006,2 +1516,8 @@ return arr.slice(0, count); | ||
/** | ||
* takes elements while `predicate` returns true starting from the right from the specified index | ||
* @example | ||
* takeRightWhileFrom(el => el > 2, 3, [1, 2, 3, 4, 5]) | ||
* // returns [3, 4] | ||
*/ | ||
function takeRightWhileFrom(predicate, startIndex, arr) { | ||
@@ -1014,2 +1530,8 @@ var endIndex = findLastIndexFrom(function (element) { | ||
/** | ||
* takes elements while `predicate` returns true | ||
* @example | ||
* takeRightWhile(el => el > 2, [1, 2, 3, 4, 5]) | ||
* // returns [3, 4] | ||
*/ | ||
function takeRightWhile(predicate, arr) { | ||
@@ -1019,6 +1541,12 @@ return takeRightWhileFrom(predicate, arr.length - 1, arr); | ||
/** | ||
* ensures that a function is executed at a fixed interval, so that it is not called too frequently | ||
* @example | ||
* const updatePreview = () => { ... } | ||
* const throttledUpdatePreview = throttle(updatePreview, 500); | ||
* inputField.addEventListener('input', throttledUpdatePreview); | ||
*/ | ||
function throttle(ms, fn) { | ||
var lastCall = Date.now() - 2 * ms; | ||
var timerId; | ||
var invoke = function invoke() { | ||
@@ -1028,14 +1556,10 @@ lastCall = Date.now(); | ||
}; | ||
var cancel = function cancel() { | ||
return clearTimeout(timerId); | ||
}; | ||
var throttled = function throttled() { | ||
var now = Date.now(); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
if (now - lastCall >= ms) { | ||
@@ -1045,7 +1569,5 @@ invoke.apply(void 0, args); | ||
} | ||
cancel(); | ||
timerId = setTimeout.apply(void 0, [invoke, lastCall - now + ms].concat(args)); | ||
}; | ||
throttled.cancel = cancel; | ||
@@ -1055,2 +1577,8 @@ return throttled; | ||
/** | ||
* converts values that are iterable to an array | ||
* @example | ||
* toArray('hello') | ||
* // returns ['h', 'e', 'l', 'l', 'o'] | ||
*/ | ||
function toArray(arrayLike) { | ||
@@ -1060,16 +1588,34 @@ return Array.prototype.slice.call(arrayLike); | ||
var toFixedNumber = function toFixedNumber(num, digits) { | ||
/** | ||
* returns the provided number with specified amount of decimal digits | ||
* @example | ||
* toFixedNumber(1.2345, 2) | ||
* // returns 1.23 | ||
*/ | ||
function toFixedNumber(num, digits) { | ||
return Number(num.toFixed(digits)); | ||
}; | ||
} | ||
var toPairs = function toPairs(obj) { | ||
return Object.keys(obj).map(function (key) { | ||
/** | ||
* returns an array of derived from the provided object key-value tuples | ||
* @example | ||
* toPairs({ a: 1, b: 2 }) | ||
* // returns [['a', 1], ['b', 2]] | ||
*/ | ||
function toPairs(obj) { | ||
return keys(obj).map(function (key) { | ||
return [key, obj[key]]; | ||
}); | ||
}; | ||
} | ||
/** | ||
* ensures that a function is executed at a fixed interval, so that it is not called too frequently | ||
* @example | ||
* const updatePreview = () => { ... } | ||
* const throttledUpdatePreview = trailingThrottle(updatePreview, 500); | ||
* inputField.addEventListener('input', throttledUpdatePreview); | ||
*/ | ||
function trailingThrottle(ms, fn) { | ||
var lastCall = Date.now() - 2 * ms; | ||
var trailingId; | ||
var invoke = function invoke() { | ||
@@ -1079,23 +1625,16 @@ lastCall = Date.now(); | ||
}; | ||
var cancel = function cancel() { | ||
return clearTimeout(trailingId); | ||
}; | ||
var throttled = function throttled() { | ||
var now = Date.now(); | ||
if (now - lastCall >= ms) { | ||
lastCall = Date.now(); | ||
} | ||
cancel(); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
trailingId = setTimeout.apply(void 0, [invoke, lastCall - now + ms].concat(args)); | ||
}; | ||
throttled.cancel = cancel; | ||
@@ -1105,2 +1644,9 @@ return throttled; | ||
/** | ||
* ensures that a function is executed at a fixed interval, so that it is not called too frequently | ||
* @example | ||
* const updatePreview = () => { ... } | ||
* const throttledUpdatePreview = leadingThrottle(updatePreview, 500); | ||
* inputField.addEventListener('input', throttledUpdatePreview); | ||
*/ | ||
function leadingThrottle(ms, fn) { | ||
@@ -1110,3 +1656,2 @@ var lastCall = 0; | ||
var now = Date.now(); | ||
if (now - lastCall >= ms) { | ||
@@ -1119,11 +1664,27 @@ lastCall = Date.now(); | ||
var stringCompare = function stringCompare(strA, strB) { | ||
/** | ||
* returns 0 for matching strings | ||
* return -1 for string with lower char code at some position | ||
* return 1 for string with greater char code at some position | ||
* @example | ||
* stringCompare('abc', 'abc') | ||
* // returns 0 | ||
* stringCompare('abc', 'abd') | ||
* // returns -1 | ||
*/ | ||
function stringCompare(strA, strB) { | ||
if (strA === strB) { | ||
return 0; | ||
} | ||
return strA < strB ? -1 : 1; | ||
}; | ||
} | ||
var leadingWhiteSpace = /^\s+/; | ||
/** | ||
* trims the beginning whitespaces from the provided string | ||
* @example | ||
* trimStart(' hello') | ||
* // returns 'hello' | ||
*/ | ||
function trimStart(str) { | ||
@@ -1134,2 +1695,9 @@ return str.replace(leadingWhiteSpace, ''); | ||
var trailingWhiteSpace = /\s+$/; | ||
/** | ||
* trims the end whitespaces from the provided string | ||
* @example | ||
* trimEnd('hello ') | ||
* // returns 'hello' | ||
*/ | ||
function trimEnd(str) { | ||
@@ -1139,2 +1707,8 @@ return str.replace(trailingWhiteSpace, ''); | ||
/** | ||
* returns the provided string repeated the specified amount of times | ||
* @example | ||
* repeat(3, 'test') | ||
* // returns 'testtesttest' | ||
*/ | ||
function repeat(count, text) { | ||
@@ -1144,2 +1718,8 @@ return Array(count + 1).join(text); | ||
/** | ||
* returns an array with all the duplicates from the provided array removed based on the iteratee function | ||
* @example | ||
* uniqBy(el => el.toString(), [1, '1', 2, '3', 3]) | ||
* // returns [1, 2, '3'] | ||
*/ | ||
function uniqBy(iteratee, arr) { | ||
@@ -1150,3 +1730,2 @@ // with polyfills this could be just: return Array.from(new Set(arr.map(iteratee))) | ||
var key = iteratee(element); | ||
if (seen.indexOf(key) === -1) { | ||
@@ -1156,3 +1735,2 @@ seen.push(key); | ||
} | ||
return false; | ||
@@ -1162,2 +1740,8 @@ }); | ||
/** | ||
* returns an array with all the duplicates from the provided array removed | ||
* @example | ||
* uniq([1, 1, 2, 3, 3]) | ||
* // returns [1, 2, 3] | ||
*/ | ||
function uniq(arr) { | ||
@@ -1167,2 +1751,8 @@ return uniqBy(identity, arr); | ||
/** | ||
* updates the provided array with the provided value on the specified index | ||
* @example | ||
* update(2, 3, [1, 2, 5]) | ||
* // returns [1, 2, 3] | ||
*/ | ||
function update(index, newElement, arr) { | ||
@@ -1172,2 +1762,8 @@ return [].concat(arr.slice(0, index), [newElement], arr.slice(index + 1, arr.length)); | ||
/** | ||
* returns an array without the specified elements from the provided array | ||
* @example | ||
* without([2, 4], [1, 2, 3, 4, 5]) | ||
* // returns [1, 3, 5] | ||
*/ | ||
function without(removed, arr) { | ||
@@ -1179,2 +1775,8 @@ return arr.filter(function (element) { | ||
/** | ||
* returns an array of tuples of certain index elements from two provided arrays based on the zipper function | ||
* @example | ||
* zipWith((a, b) => [a * 2, b.toString()], [1, 2, 3], [10, 20, 30]) | ||
* // returns [[2, '10'], [4, '20'], [6, '30']] | ||
*/ | ||
function zipWith(zipper, arrayA, arrayB) { | ||
@@ -1186,2 +1788,8 @@ return arrayA.map(function (elementA, index) { | ||
/** | ||
* returns an array of tuples of certain index elements from two provided arrays | ||
* @example | ||
* zip([1, 2, 3], [10, 20, 30]) | ||
* // returns [[1, 10], [2, 20], [3, 30]] | ||
*/ | ||
function zip(arrayA, arrayB) { | ||
@@ -1201,5 +1809,6 @@ return zipWith(pair, arrayA, arrayB); | ||
exports.debounce = debounce; | ||
exports.deepMapKeys = mapKeys; | ||
exports.deepMapKeys = deepMapKeys; | ||
exports.deepMerge = deepMerge; | ||
exports.defaultTo = defaultTo; | ||
exports.diffKeys = diffKeys; | ||
exports.drop = drop; | ||
@@ -1240,3 +1849,3 @@ exports.dropRight = dropRight; | ||
exports.leadingThrottle = leadingThrottle; | ||
exports.mapKeys = mapKeys$1; | ||
exports.mapKeys = mapKeys; | ||
exports.mapValues = mapValues; | ||
@@ -1243,0 +1852,0 @@ exports.mapValuesIndexed = mapValuesIndexed; |
@@ -1,1 +0,1 @@ | ||
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((n="undefined"!=typeof globalThis?globalThis:n||self).DataUtils={})}(this,(function(n){"use strict";function r(n,r){return n+r}var t={}.hasOwnProperty;function e(n,r){return t.call(r,n)}function u(){return(u=Object.assign||function(n){for(var r=arguments.length,t=Array(r>1?r-1:0),u=1;r>u;u++)t[u-1]=arguments[u];return t.forEach((function(r){for(var t in r)e(t,r)&&(n[t]=r[t])})),n}).apply(void 0,arguments)}function o(n,r){var t;return(t=[]).concat.apply(t,r.map(n))}var i=Array.isArray;function c(n){return"object"==typeof n&&null!==n&&!i(n)}function f(n,r){return Object.keys(r).reduce((function(t,e){return t[e]=n(r[e]),t}),{})}function a(n){return null==n}function l(n,r){for(var t=0;r.length>t;t++){var e=r[t];if(n(e))return e}}function s(n,r,t){for(var e=r;e>=0;e--)if(n(t[e]))return e;return-1}function p(n){return n}function y(n,r){return Object.keys(r).forEach((function(t){n(r[t],t)}))}function v(){return Math.random().toString(36).substring(2)}function d(n,r){for(var t="string"==typeof n?n.split("."):n,e=0,u=r;u&&t.length>e;)u=u[t[e++]];return u}function h(n){return 0===(i(n)?n:Object.keys(n)).length}function g(n,r){if(h(r))return n;var t={};return y((function(u,o){if(e(o,r))if(c(n[o])&&c(r[o]))t[o]=g(n[o],r[o]);else if(i(n[o])&&i(r[o])){var f=Math.max(n[o].length,r[o].length);t[o]=Array(f);for(var a=0;f>a;a++)a in r[o]?t[o][a]=r[o][a]:a in n[o]&&(t[o][a]=n[o][a])}else t[o]=r[o];else t[o]=n[o]}),n),y((function(n,u){e(u,t)||(t[u]=r[u])}),r),t}function m(n,r){var t={};return function(){var u=n.apply(void 0,arguments);if(e(u,t))return t[u];var o=r.apply(void 0,arguments);return t[u]=o,o}}function b(n){return Object.keys(n).map((function(r){return n[r]}))}function O(n,r){return Object.keys(r).reduce((function(t,e){return n(r[e],e)||(t[e]=r[e]),t}),{})}function j(n,r){return r.slice(-n)}function A(n,r){return[n,r]}function k(n,r){return Object.keys(r).reduce((function(t,e){return n(r[e],e)&&(t[e]=r[e]),t}),{})}function w(n,r){return n+Math.floor(Math.random()*(r-n+1))}function x(){return(x=Object.assign||function(n){for(var r=1;arguments.length>r;r++){var t=arguments[r];for(var e in t)Object.prototype.hasOwnProperty.call(t,e)&&(n[e]=t[e])}return n}).apply(this,arguments)}function C(n,r){(null==r||r>n.length)&&(r=n.length);for(var t=0,e=Array(r);r>t;t++)e[t]=n[t];return e}function T(n,r){var t;if("undefined"==typeof Symbol||null==n[Symbol.iterator]){if(Array.isArray(n)||(t=function(n,r){if(n){if("string"==typeof n)return C(n,r);var t=Object.prototype.toString.call(n).slice(8,-1);return"Object"===t&&n.constructor&&(t=n.constructor.name),"Map"===t||"Set"===t?Array.from(n):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?C(n,r):void 0}}(n))||r&&n&&"number"==typeof n.length){t&&(n=t);var e=0;return function(){return n.length>e?{done:!1,value:n[e++]}:{done:!0}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(t=n[Symbol.iterator]()).next.bind(t)}function N(n,r){return n===r?0!==n||0!==r||1/n==1/r:n!=n&&r!=r}function D(n,r){return[r.slice(0,n),r.slice(n,r.length)]}function I(n,r,t){var e=s((function(r){return!n(r)}),r,t);return e===r?[]:t.slice(e+1,r+1)}var L=/^\s+/;var z=/\s+$/;function M(n,r){var t=[];return r.filter((function(r){var e=n(r);return-1===t.indexOf(e)&&(t.push(e),!0)}))}function S(n,r,t){return r.map((function(r,e){return n(r,t[e])}))}n.add=r,n.assign=u,n.camelCase=function(n){var r=n.trim();return 0===r.length?"":1===r.length?r.toLowerCase():/^[a-z\d]+$/.test(r)?r:(r!==r.toLowerCase()&&(r=function(n){for(var r=n,t=!1,e=!1,u=!1,o=0;r.length>o;o++){var i=r[o];t&&/[a-zA-Z]/.test(i)&&i.toUpperCase()===i?(r=r.slice(0,o)+"-"+r.slice(o),t=!1,u=e,e=!0,o++):e&&u&&/[a-zA-Z]/.test(i)&&i.toLowerCase()===i?(r=r.slice(0,o-1)+"-"+r.slice(o-1),u=e,e=!1,t=!0):(t=i.toLowerCase()===i,u=e,e=i.toUpperCase()===i)}return r}(r)),r=r.replace(/^[_.\- ]+/,"").toLowerCase().replace(/[_.\- ]+(\w|$)/g,(function(n,r){return r.toUpperCase()})))},n.capitalizeFirstLetter=function(n){return n.charAt(0).toUpperCase()+n.slice(1)},n.chain=function(n,r){return"function"==typeof r?function(t){return n(r(t))(t)}:o(n,r)},n.chunk=function(n,r){for(var t=[],e=0;n.length>e;e+=r)t.push(n.slice(e,e+r));return t},n.cloneDeep=function n(r){return i(r)?r.map(n):c(r)?f(n,r):r},n.compact=function(n){return i(n)?n.filter((function(n){return null!=n&&!Number.isNaN(n)})):Object.keys(n).reduce((function(r,t){var e=n[t];return null==e||Number.isNaN(e)||(r[t]=e),r}),{})},n.compose=function(){for(var n=arguments.length,r=Array(n),t=0;n>t;t++)r[t]=arguments[t];return r.reduce((function(n,r){return function(){return n(r.apply(void 0,arguments))}}))},n.debounce=function(n,r){var t;return function(){clearTimeout(t);for(var e=arguments.length,u=Array(e),o=0;e>o;o++)u[o]=arguments[o];t=setTimeout.apply(void 0,[r,n].concat(u))}},n.deepMapKeys=function n(r,t){return Object.keys(t).reduce((function(e,u){return t[u]&&"object"==typeof t[u]?Array.isArray(t[u])?(e[r(u)]=t[u].map((function(t){return n(r,t)})),e):(e[r(u)]=n(r,t[u]),e):(e[r(u)]=t[u],e)}),{})},n.deepMerge=function n(r){for(var t=arguments.length,e=Array(t>1?t-1:0),u=1;t>u;u++)e[u-1]=arguments[u];if(!e.length)return r;var o=e.shift();if(c(r)&&c(o))for(var i in o){var f,a;if(c(o[i]))r[i]||Object.assign(r,((f={})[i]={},f)),n(r[i],o[i]);else Object.assign(r,((a={})[i]=o[i],a))}return n.apply(void 0,[r].concat(e))},n.defaultTo=function(n){return function(r){return a(r)?n:r}},n.drop=function(n,r){return r.slice(n)},n.dropRight=function(n,r){return r.slice(0,-n)},n.ensureArray=function(n){return i(n)?n:[n]},n.entries=function(n){return Object.keys(n).map((function(r){return[r,n[r]]}))},n.filledArray=function(n,r){for(var t=[];n--;)t.push(r);return t},n.find=l,n.findIndex=function(n,r){for(var t=0;r.length>t;t++)if(n(r[t]))return t;return-1},n.findKey=function(n,r){return l((function(t){return n(r[t])}),Object.keys(r))},n.findLast=function(n,r){for(var t=r.length-1;t>=0;t--)if(n(r[t]))return r[t]},n.findLastIndex=function(n,r){return s(n,r.length-1,r)},n.findLastIndexFrom=s,n.flatMap=o,n.flatten=function(n){return o(p,n)},n.forOwn=y,n.fromPairs=function(n){return n.reduce((function(n,r){return n[r[0]]=r[1],n}),{})},n.generateRandomId=v,n.generateUniqueId=function n(r){var t=v();return e(t,r)?n(r):t},n.get=d,n.getOr=function(n,r,t){var e=d(r,t);return null!=e?e:n},n.groupBy=function(n,r){return Object.keys(r).reduce((function(t,e){var u=r[e],o=n(u);return t[o]=t[o]||[],t[o].push(u),t}),{})},n.groupKeys=function(n,r){return Object.keys(r).reduce((function(t,e){var u=n(e);return t[u]=t[u]||{},t[u][e]=r[e],t}),{})},n.hasOwn=e,n.identity=p,n.includes=function(n,r){return-1!==r.indexOf(n)},n.isArray=i,n.isEmpty=h,n.isFalsy=function(n){return!n},n.isNil=a,n.isObject=c,n.isPromise=function(n){return!!n&&"function"==typeof n.then},n.isTruthy=function(n){return!!n},n.keyBy=function(n,r){return r.reduce((function(r,t){return r[t[n]]=t,r}),{})},n.keys=function(n){var r=[];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&r.push(t);return r},n.last=function(n){return n.length>0?n[n.length-1]:null},n.leadingThrottle=function(n,r){var t=0;return function(){var e=Date.now();n>e-t||(t=Date.now(),r.apply(void 0,arguments))}},n.mapKeys=function(n,r){return Object.keys(r).reduce((function(t,e){return t[n(e)]=r[e],t}),{})},n.mapValues=f,n.mapValuesIndexed=function(n,r){return Object.keys(r).reduce((function(t,e){return t[e]=n(r[e],e),t}),{})},n.memoize=function(n){return m(p,n)},n.memoizeOne=function(n){var r,t,e=!1;return function(){return e&&(arguments.length>0?arguments[0]:void 0)===t?r:(e=!0,t=arguments.length>0?arguments[0]:void 0,r=n.apply(void 0,arguments))}},n.memoizeWith=m,n.merge=g,n.mergeAll=function(n){if(0===n.length)return{};var r=n[0];return n.slice(1).reduce((function(n,r){return g(n,r)}),r)},n.noop=function(){},n.numericSortBy=function(n,r){var t="function"==typeof n?n:function(r){return d(n,r)};return(i(r)?[].concat(r):b(r)).sort((function(n,r){return t(n)-t(r)}))},n.omit=function(n,r){return O((function(r,t){return-1!==n.indexOf(t)}),r)},n.omitBy=function(n,r){return Object.keys(r).reduce((function(t,e){return n(r[e])||(t[e]=r[e]),t}),{})},n.omitByIndexed=O,n.once=function(n){var r,t=!1;return function(){return t?r:(t=!0,r=n.apply(void 0,arguments))}},n.over=function(n){return function(){for(var r=arguments.length,t=Array(r),e=0;r>e;e++)t[e]=arguments[e];return n.map((function(n){return n.apply(void 0,t)}))}},n.overArgs=function(n,r){return function(){for(var t=arguments.length,e=Array(t),u=0;t>u;u++)e[u]=arguments[u];var o=r.map((function(n,r){return n(e[r])}));return n.apply(void 0,e.length>o.length?o.concat(j(e.length-o.length,e)):o)}},n.pair=A,n.partitionObject=function(n,r){return Object.keys(r).reduce((function(t,e){return t[n(r[e])?0:1][e]=r[e],t}),[{},{}])},n.pick=function(n,r){return n.reduce((function(n,t){return n[t]=r[t],n}),{})},n.pickBy=function(n,r){return Object.keys(r).reduce((function(t,e){return n(r[e])&&(t[e]=r[e]),t}),{})},n.pickByIndexed=k,n.pickOwn=function(n,r){return n.reduce((function(n,t){return e(t,r)&&(n[t]=r[t]),n}),{})},n.randomInt=w,n.range=function(n){for(var r=[],t=0;n>=t;)r.push(t++);return r},n.reject=function(n,r){return r.filter((function(r){return!n(r)}))},n.removeAt=function(n,r){var t=[].concat(r);return t.splice(n,1),t},n.repeat=function(n,r){return Array(n+1).join(r)},n.set=function n(r,t,u){var o,i=r.split?r.split("."):r,c=i[0],f=t;if(i.length>1){var a=null!=u&&e(c,u)?u[c]:{};f=n(i.slice(1),t,a)}return x({},u,((o={})[c]=f,o))},n.shallowEqual=function(n,r){if(N(n,r))return!0;if("object"!=typeof n||null===n||"object"!=typeof r||null===r)return!1;var t=Object.keys(n);if(t.length!==Object.keys(r).length)return!1;for(var u=0;t.length>u;u++)if(!e(t[u],r)||!N(n[t[u]],r[t[u]]))return!1;return!0},n.shortenLongText=function(n,r){if(n>=r.length)return r;for(var t,e=[],u=0,o=T(r.split(" "));!(t=o()).done;){var i=t.value;if(u+i.length>n)break;u+=i.length+1,e.push(i)}return e.join(" ")+"..."},n.shuffle=function(n){for(var r=n.slice(),t=n.length-1,e=0;n.length>e;e++){var u=w(e,t),o=r[u];r[u]=r[e],r[e]=o}return r},n.sign=function(n){return"number"!=typeof n||isNaN(n)?NaN:0===n?0:-0===n?-0:n>0?1:-1},n.sliceDiff=function(n,r){var t=k((function(n,t){return n!==r[t]}),n);return h(t)?null:t},n.snakeCase=function(n){var r=n.replace(/[A-Z]|([-_ ]+)/g,(function(n){var r=n.charCodeAt(0);return r>64&&91>r?"_"+n.toLowerCase():"_"}));return"_"===r[0]?r.substr(1):r},n.someAreTruthy=function(n){return n.some(p)},n.splitAt=D,n.splitRightWhenAccum=function(n,r,t){for(var e=t.length;e>0;e--){var u=n(t[e-1],r);if(r=u[1],u[0])return D(e-1,t)}return[[],t]},n.spread=function(n){return function(r){return n.apply(void 0,r)}},n.stringCompare=function(n,r){return n===r?0:r>n?-1:1},n.sum=function(n){return n.reduce(r,0)},n.take=function(n,r){return r.slice(0,n)},n.takeLast=j,n.takeRightWhile=function(n,r){return I(n,r.length-1,r)},n.takeRightWhileFrom=I,n.throttle=function(n,r){var t,e=Date.now()-2*n,u=function(){e=Date.now(),r.apply(void 0,arguments)},o=function(){return clearTimeout(t)},i=function(){for(var r=Date.now(),i=arguments.length,c=Array(i),f=0;i>f;f++)c[f]=arguments[f];n>r-e?(o(),t=setTimeout.apply(void 0,[u,e-r+n].concat(c))):u.apply(void 0,c)};return i.cancel=o,i},n.toArray=function(n){return Array.prototype.slice.call(n)},n.toFixedNumber=function(n,r){return Number(n.toFixed(r))},n.toPairs=function(n){return Object.keys(n).map((function(r){return[r,n[r]]}))},n.trailingThrottle=function(n,r){var t,e=Date.now()-2*n,u=function(){return e=Date.now(),r.apply(void 0,arguments)},o=function(){return clearTimeout(t)},i=function(){var r=Date.now();n>r-e||(e=Date.now()),o();for(var i=arguments.length,c=Array(i),f=0;i>f;f++)c[f]=arguments[f];t=setTimeout.apply(void 0,[u,e-r+n].concat(c))};return i.cancel=o,i},n.trimEnd=function(n){return n.replace(z,"")},n.trimStart=function(n){return n.replace(L,"")},n.uniq=function(n){return M(p,n)},n.uniqBy=M,n.update=function(n,r,t){return[].concat(t.slice(0,n),[r],t.slice(n+1,t.length))},n.values=b,n.without=function(n,r){return r.filter((function(r){return-1===n.indexOf(r)}))},n.zip=function(n,r){return S(A,n,r)},n.zipWith=S,Object.defineProperty(n,"__esModule",{value:!0})})); | ||
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((n="undefined"!=typeof globalThis?globalThis:n||self).DataUtils={})}(this,(function(n){"use strict";function r(n,r){return n+r}var t={}.hasOwnProperty;function e(n,r){return t.call(r,n)}function u(){return(u=Object.assign||function(n){for(var r=arguments.length,t=Array(r>1?r-1:0),u=1;r>u;u++)t[u-1]=arguments[u];return t.forEach((function(r){for(var t in r)e(t,r)&&(n[t]=r[t])})),n}).apply(void 0,arguments)}function o(n,r){var t;return(t=[]).concat.apply(t,r.map(n))}function i(n){return Array.isArray(n)}function c(n){return"object"==typeof n&&null!==n&&!i(n)}function f(n){if("keys"in Object&&"function"==typeof Object.keys)return Object.keys(n);var r=[];for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&r.push(t);return r}function a(n,r){return f(r).reduce((function(t,e){return t[e]=n(r[e]),t}),{})}function l(n){for(var r=arguments.length,t=Array(r>1?r-1:0),e=1;r>e;e++)t[e-1]=arguments[e];if(!t.length)return n;var u=t.shift();if(c(n)&&c(u))for(var o in u){var i,f;if(c(u[o]))n[o]||Object.assign(n,((i={})[o]={},i)),l(n[o],u[o]);else Object.assign(n,((f={})[o]=u[o],f))}return l.apply(void 0,[n].concat(t))}function s(n){return null==n}function p(n,r){for(var t=0;r.length>t;t++){var e=r[t];if(n(e))return e}}function d(n,r,t){for(var e=r;e>=0;e--)if(n(t[e]))return e;return-1}function v(n){return n}function y(){return Math.random().toString(36).substring(2)}function h(n,r){for(var t="string"==typeof n?n.split("."):n,e=0,u=r;u&&t.length>e;)u=u[t[e++]];return u}function g(n){return 0===(i(n)?n:Object.keys(n)).length}function m(n,r){return g(r)?n:l({},n,r)}function b(n,r){var t={};return function(){var u=n.apply(void 0,arguments);if(e(u,t))return t[u];var o=r.apply(void 0,arguments);return t[u]=o,o}}function O(n){return f(n).map((function(r){return n[r]}))}function A(n,r){return f(r).reduce((function(t,e){return n(r[e],e)||(t[e]=r[e]),t}),{})}function j(n,r){return r.slice(-n)}function w(n,r){return[n,r]}function k(n,r){return f(r).reduce((function(t,e){return n(r[e],e)&&(t[e]=r[e]),t}),{})}function x(n,r){return n+Math.floor(Math.random()*(r-n+1))}function C(){return(C=Object.assign?Object.assign.bind():function(n){for(var r=1;arguments.length>r;r++){var t=arguments[r];for(var e in t)Object.prototype.hasOwnProperty.call(t,e)&&(n[e]=t[e])}return n}).apply(this,arguments)}function T(n,r){(null==r||r>n.length)&&(r=n.length);for(var t=0,e=Array(r);r>t;t++)e[t]=n[t];return e}function N(n,r){var t="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(t)return(t=t.call(n)).next.bind(t);if(Array.isArray(n)||(t=function(n,r){if(n){if("string"==typeof n)return T(n,r);var t=Object.prototype.toString.call(n).slice(8,-1);return"Object"===t&&n.constructor&&(t=n.constructor.name),"Map"===t||"Set"===t?Array.from(n):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?T(n,r):void 0}}(n))||r&&n&&"number"==typeof n.length){t&&(n=t);var e=0;return function(){return n.length>e?{done:!1,value:n[e++]}:{done:!0}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function D(n,r){return n===r?0!==n||0!==r||1/n==1/r:n!=n&&r!=r}function I(n,r){return[r.slice(0,n),r.slice(n,r.length)]}function L(n,r,t){var e=d((function(r){return!n(r)}),r,t);return e===r?[]:t.slice(e+1,r+1)}var z=/^\s+/;var B=/\s+$/;function M(n,r){var t=[];return r.filter((function(r){var e=n(r);return-1===t.indexOf(e)&&(t.push(e),!0)}))}function P(n,r,t){return r.map((function(r,e){return n(r,t[e])}))}n.add=r,n.assign=u,n.camelCase=function(n){var r=n.trim();return 0===r.length?"":1===r.length?r.toLowerCase():/^[a-z\d]+$/.test(r)?r:(r!==r.toLowerCase()&&(r=function(n){for(var r=n,t=!1,e=!1,u=!1,o=0;r.length>o;o++){var i=r[o];t&&/[a-zA-Z]/.test(i)&&i.toUpperCase()===i?(r=r.slice(0,o)+"-"+r.slice(o),t=!1,u=e,e=!0,o++):e&&u&&/[a-zA-Z]/.test(i)&&i.toLowerCase()===i?(r=r.slice(0,o-1)+"-"+r.slice(o-1),u=e,e=!1,t=!0):(t=i.toLowerCase()===i,u=e,e=i.toUpperCase()===i)}return r}(r)),r=r.replace(/^[_.\- ]+/,"").toLowerCase().replace(/[_.\- ]+(\w|$)/g,(function(n,r){return r.toUpperCase()})))},n.capitalizeFirstLetter=function(n){return n.charAt(0).toUpperCase()+n.slice(1)},n.chain=function(n,r){return"function"==typeof r?function(t){return n(r(t))(t)}:o(n,r)},n.chunk=function(n,r){if(0>=r)return[];for(var t=[],e=0;n.length>e;e+=r)t.push(n.slice(e,e+r));return t},n.cloneDeep=function n(r){return i(r)?r.map(n):c(r)?a(n,r):r},n.compact=function(n){return i(n)?n.filter((function(n){return null!=n&&!Number.isNaN(n)})):Object.keys(n).reduce((function(r,t){var e=n[t];return null==e||Number.isNaN(e)||(r[t]=e),r}),{})},n.compose=function(){for(var n=arguments.length,r=Array(n),t=0;n>t;t++)r[t]=arguments[t];return r.reduce((function(n,r){return function(){return n(r.apply(void 0,arguments))}}))},n.debounce=function(n,r){var t;return function(){clearTimeout(t);for(var e=arguments.length,u=Array(e),o=0;e>o;o++)u[o]=arguments[o];t=setTimeout.apply(void 0,[r,n].concat(u))}},n.deepMapKeys=function n(r,t){return Object.keys(t).reduce((function(e,u){return t[u]&&"object"==typeof t[u]?Array.isArray(t[u])?(e[r(u)]=t[u].map((function(t){return n(r,t)})),e):(e[r(u)]=n(r,t[u]),e):(e[r(u)]=t[u],e)}),{})},n.deepMerge=l,n.defaultTo=function(n){return function(r){return s(r)?n:r}},n.diffKeys=function(n,r){return f(n).reduce((function(t,e){return n[e]!==r[e]&&t.push(e),t}),[])},n.drop=function(n,r){return r.slice(n)},n.dropRight=function(n,r){return r.slice(0,-n)},n.ensureArray=function(n){return i(n)?n:[n]},n.entries=function(n){return f(n).map((function(r){return[r,n[r]]}))},n.filledArray=function(n,r){if(0>=n)return[];for(var t=[];n--;)t.push(r);return t},n.find=p,n.findIndex=function(n,r){for(var t=0;r.length>t;t++)if(n(r[t]))return t;return-1},n.findKey=function(n,r){return p((function(t){return n(r[t])}),f(r))},n.findLast=function(n,r){for(var t=r.length-1;t>=0;t--)if(n(r[t]))return r[t]},n.findLastIndex=function(n,r){return d(n,r.length-1,r)},n.findLastIndexFrom=d,n.flatMap=o,n.flatten=function(n){return o(v,n)},n.forOwn=function(n,r){return f(r).forEach((function(t){n(r[t],t)}))},n.fromPairs=function(n){return n.reduce((function(n,r){return n[r[0]]=r[1],n}),{})},n.generateRandomId=y,n.generateUniqueId=function n(r){var t=y();return e(t,r)?n(r):t},n.get=h,n.getOr=function(n,r,t){var e=h(r,t);return null!=e?e:n},n.groupBy=function(n,r){return Object.keys(r).reduce((function(t,e){var u=r[e],o=n(u);return t[o]=t[o]||[],t[o].push(u),t}),{})},n.groupKeys=function(n,r){return f(r).reduce((function(t,e){var u=n(e);return t[u]=t[u]||{},t[u][e]=r[e],t}),{})},n.hasOwn=e,n.identity=v,n.includes=function(n,r){return-1!==r.indexOf(n)},n.isArray=i,n.isEmpty=g,n.isFalsy=function(n){return!n},n.isNil=s,n.isObject=c,n.isPromise=function(n){return!!n&&"function"==typeof n.then},n.isTruthy=function(n){return!!n},n.keyBy=function(n,r){return r.reduce((function(r,t){return r[t[n]]=t,r}),{})},n.keys=f,n.last=function(n){return n.length>0?n[n.length-1]:void 0},n.leadingThrottle=function(n,r){var t=0;return function(){var e=Date.now();n>e-t||(t=Date.now(),r.apply(void 0,arguments))}},n.mapKeys=function(n,r){return f(r).reduce((function(t,e){return Object.defineProperty(t,n(e),{value:r[e],enumerable:!0}),t}),{})},n.mapValues=a,n.mapValuesIndexed=function(n,r){return f(r).reduce((function(t,e){return t[e]=n(r[e],e),t}),{})},n.memoize=function(n){return b(v,n)},n.memoizeOne=function(n){var r,t,e=!1;return function(){return e&&(arguments.length>0?arguments[0]:void 0)===t?r:(e=!0,t=arguments.length>0?arguments[0]:void 0,r=n.apply(void 0,arguments))}},n.memoizeWith=b,n.merge=m,n.mergeAll=function(n){if(0===n.length)return{};var r=n[0];return n.slice(1).reduce((function(n,r){return m(n,r)}),r)},n.noop=function(){},n.numericSortBy=function(n,r){var t="function"==typeof n?n:function(r){return h(n,r)};return(i(r)?[].concat(r):O(r)).sort((function(n,r){return t(n)-t(r)}))},n.omit=function(n,r){return A((function(r,t){return-1!==n.indexOf(t)}),r)},n.omitBy=function(n,r){return f(r).reduce((function(t,e){return n(r[e])||(t[e]=r[e]),t}),{})},n.omitByIndexed=A,n.once=function(n){var r,t=!1;return function(){return t?r:(t=!0,r=n.apply(void 0,arguments))}},n.over=function(n){return function(){for(var r=arguments.length,t=Array(r),e=0;r>e;e++)t[e]=arguments[e];return n.map((function(n){return n.apply(void 0,t)}))}},n.overArgs=function(n,r){return function(){for(var t=arguments.length,e=Array(t),u=0;t>u;u++)e[u]=arguments[u];var o=r.map((function(n,r){return n(e[r])}));return n.apply(void 0,e.length>o.length?o.concat(j(e.length-o.length,e)):o)}},n.pair=w,n.partitionObject=function(n,r){return f(r).reduce((function(t,e){return t[n(r[e])?0:1][e]=r[e],t}),[{},{}])},n.pick=function(n,r){return n.reduce((function(n,t){return n[t]=r[t],n}),{})},n.pickBy=function(n,r){return f(r).reduce((function(t,e){return n(r[e])&&(t[e]=r[e]),t}),{})},n.pickByIndexed=k,n.pickOwn=function(n,r){return n.reduce((function(n,t){return e(t,r)&&(n[t]=r[t]),n}),{})},n.randomInt=x,n.range=function(n){for(var r=[],t=0;n>=t;)r.push(t++);return r},n.reject=function(n,r){return r.filter((function(r){return!n(r)}))},n.removeAt=function(n,r){var t=[].concat(r);return t.splice(n,1),t},n.repeat=function(n,r){return Array(n+1).join(r)},n.set=function n(r,t,u){var o,c=i(r)?r:r.split("."),f=c[0],a=t;if(c.length>1){var l=null!=u&&e(f,u)?u[f]:{};a=n(c.slice(1),t,l)}return C({},u,((o={})[f]=a,o))},n.shallowEqual=function(n,r){if(D(n,r))return!0;if("object"!=typeof n||null===n||"object"!=typeof r||null===r)return!1;var t=Object.keys(n);if(t.length!==Object.keys(r).length)return!1;for(var u=0;t.length>u;u++)if(!e(t[u],r)||!D(n[t[u]],r[t[u]]))return!1;return!0},n.shortenLongText=function(n,r){if(n>=r.length)return r;for(var t,e=[],u=0,o=N(r.split(" "));!(t=o()).done;){var i=t.value;if(u+i.length>n)break;u+=i.length+1,e.push(i)}return e.join(" ")+"..."},n.shuffle=function(n){for(var r=n.slice(),t=n.length-1,e=0;n.length>e;e++){var u=x(e,t),o=r[u];r[u]=r[e],r[e]=o}return r},n.sign=function(n){return"number"!=typeof n||isNaN(n)?NaN:Object.is(n,0)?0:Object.is(n,-0)?-0:n>0?1:-1},n.sliceDiff=function(n,r){var t=k((function(n,t){return n!==r[t]}),n);return g(t)?null:t},n.snakeCase=function(n){var r=n.replace(/[A-Z]|([-_ ]+)/g,(function(n){var r=n.charCodeAt(0);return r>64&&91>r?"_"+n.toLowerCase():"_"}));return"_"===r[0]?r.substr(1):r},n.someAreTruthy=function(n){return n.some(v)},n.splitAt=I,n.splitRightWhenAccum=function(n,r,t){for(var e=t.length;e>0;e--){var u=n(t[e-1],r);if(r=u[1],u[0])return I(e-1,t)}return[[],t]},n.spread=function(n){return function(r){return n.apply(void 0,r)}},n.stringCompare=function(n,r){return n===r?0:r>n?-1:1},n.sum=function(n){return n.reduce(r,0)},n.take=function(n,r){return r.slice(0,n)},n.takeLast=j,n.takeRightWhile=function(n,r){return L(n,r.length-1,r)},n.takeRightWhileFrom=L,n.throttle=function(n,r){var t,e=Date.now()-2*n,u=function(){e=Date.now(),r.apply(void 0,arguments)},o=function(){return clearTimeout(t)},i=function(){for(var r=Date.now(),i=arguments.length,c=Array(i),f=0;i>f;f++)c[f]=arguments[f];n>r-e?(o(),t=setTimeout.apply(void 0,[u,e-r+n].concat(c))):u.apply(void 0,c)};return i.cancel=o,i},n.toArray=function(n){return Array.prototype.slice.call(n)},n.toFixedNumber=function(n,r){return Number(n.toFixed(r))},n.toPairs=function(n){return f(n).map((function(r){return[r,n[r]]}))},n.trailingThrottle=function(n,r){var t,e=Date.now()-2*n,u=function(){return e=Date.now(),r.apply(void 0,arguments)},o=function(){return clearTimeout(t)},i=function(){var r=Date.now();n>r-e||(e=Date.now()),o();for(var i=arguments.length,c=Array(i),f=0;i>f;f++)c[f]=arguments[f];t=setTimeout.apply(void 0,[u,e-r+n].concat(c))};return i.cancel=o,i},n.trimEnd=function(n){return n.replace(B,"")},n.trimStart=function(n){return n.replace(z,"")},n.uniq=function(n){return M(v,n)},n.uniqBy=M,n.update=function(n,r,t){return[].concat(t.slice(0,n),[r],t.slice(n+1,t.length))},n.values=O,n.without=function(n,r){return r.filter((function(r){return-1===n.indexOf(r)}))},n.zip=function(n,r){return P(w,n,r)},n.zipWith=P,Object.defineProperty(n,"__esModule",{value:!0})})); |
{ | ||
"name": "@livechat/data-utils", | ||
"version": "0.2.17", | ||
"description": "Collections utility functions", | ||
"version": "1.0.0", | ||
"description": "Collection of utility functions", | ||
"contributors": [ | ||
@@ -22,7 +22,7 @@ "Konrad Kruk <k.kruk@livechatinc.com>", | ||
"devDependencies": { | ||
"@babel/core": "^7.10.5", | ||
"@babel/core": "7.20.7", | ||
"@babel/plugin-proposal-class-properties": "^7.10.4", | ||
"@babel/plugin-syntax-dynamic-import": "^7.8.3", | ||
"@babel/preset-env": "^7.10.4", | ||
"@babel/preset-typescript": "^7.10.4", | ||
"@babel/preset-typescript": "^7.18.6", | ||
"@rollup/plugin-babel": "^5.2.1", | ||
@@ -34,2 +34,3 @@ "@rollup/plugin-commonjs": "^15.0.0", | ||
"jest": "26.6.0", | ||
"jsdoc-to-markdown": "^8.0.0", | ||
"lerna-alias": "3.0.3-0", | ||
@@ -39,3 +40,3 @@ "rimraf": "^2.6.1", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"typescript": "^3.9.7" | ||
"typescript": "4.9.4" | ||
}, | ||
@@ -46,5 +47,7 @@ "scripts": { | ||
"test": "jest", | ||
"test:watch": "jest --watch", | ||
"prepare": "npm run build", | ||
"prepublish": "test $(npm -v | tr . '\\n' | head -n 1) -ge '4' || exit 1" | ||
"prepublish": "test $(npm -v | tr . '\\n' | head -n 1) -ge '4' || exit 1", | ||
"docs": "npm run build && jsdoc2md dist/data-utils.js > README.md" | ||
} | ||
} |
@@ -0,2 +1,8 @@ | ||
/** | ||
* adds two numbers | ||
* @example | ||
* add(1, 2) | ||
* // returns 3 | ||
*/ | ||
export default function add(first: number, second: number): number; | ||
//# sourceMappingURL=add.d.ts.map |
@@ -1,3 +0,8 @@ | ||
declare const capitalizeFirstLetter: (text: string) => string; | ||
export default capitalizeFirstLetter; | ||
/** | ||
* capitalizes first letter of string | ||
* @example | ||
* capitalizeFirstLetter('hello') | ||
* // returns 'Hello' | ||
*/ | ||
export default function capitalizeFirstLetter<T extends string>(text: T): Capitalize<T>; | ||
//# sourceMappingURL=capitalizeFirstLetter.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* chunks provided array to specified size chunks | ||
* @example | ||
* chunk([1, 2, 3, 4], 2) | ||
* // returns [[1, 2], [3, 4]] | ||
*/ | ||
export default function chunk<T>(arr: T[], number: number): Array<T[]>; | ||
//# sourceMappingURL=chunk.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns an exact clone of the provided value with a new reference | ||
* @example | ||
* cloneDeep({ a: 1, b: 2 }) | ||
* // returns { a: 1, b: 2 } | ||
*/ | ||
export default function cloneDeep<T>(value: T): T; | ||
//# sourceMappingURL=cloneDeep.d.ts.map |
@@ -0,1 +1,10 @@ | ||
/** | ||
* returns a composed value from the provided arguments | ||
* @example | ||
* const f3 = (a: string) => `f3(${a})` | ||
* const f2 = (a: string) => `f2(${a})` | ||
* const f1 = (a: string) => `f1(${a})` | ||
* compose(f3, f2, f1)('arg') | ||
* // returns 'f3(f2(f1(arg)))' | ||
*/ | ||
export default function compose<Input, Return, T1>(second: (a: T1) => Return, first: (a: Input) => T1): (a: Input) => Return; | ||
@@ -2,0 +11,0 @@ export default function compose<Input, Return, T1, T2>(third: (a: T2) => Return, second: (a: T1) => T2, first: (a: Input) => T1): (a: Input) => Return; |
@@ -1,3 +0,8 @@ | ||
import { AnyFunction } from './types'; | ||
export default function debounce<Fn extends AnyFunction>(ms: number, fn: Fn): (...args: Parameters<Fn>) => void; | ||
import type { AnyFunction } from './types'; | ||
/** | ||
* delays the execution of a function until a certain period of time has passed without the function being called again | ||
* @example | ||
* debounce(1000, someFunction) | ||
*/ | ||
export default function debounce<T extends AnyFunction>(ms: number, fn: T): (...args: Parameters<T>) => void; | ||
//# sourceMappingURL=debounce.d.ts.map |
@@ -1,2 +0,8 @@ | ||
export default function mapKeys<T>(mapper: (key: string) => string, obj: Record<string, any>): T; | ||
/** | ||
* recurses through an object and transforms its keys – and the keys of any nested objects – based on the provided key mapping function | ||
* @example | ||
* deepMapKeys(key => `test_${key}`, { a: 1, b: { c: 2 } }) | ||
* // returns { test_a: 1, test_b: { test_c: 2 } } | ||
*/ | ||
export default function deepMapKeys<T>(mapper: (key: string) => string, obj: Record<string, any>): T; | ||
//# sourceMappingURL=deepMapKeys.d.ts.map |
@@ -0,2 +1,10 @@ | ||
/** | ||
* returns an array if an array was provided, or a new array if provided value was not an array | ||
* @example | ||
* ensureArray([1, 2]) | ||
* // returns [1, 2] | ||
* ensureArray('test') | ||
* // returns ['test'] | ||
*/ | ||
export default function ensureArray<T>(maybeArr: T | T[]): T[]; | ||
//# sourceMappingURL=ensureArray.d.ts.map |
@@ -1,2 +0,11 @@ | ||
export default function entries<T extends Record<string, unknown>>(obj: T): [string, T[keyof T]][]; | ||
import type { AnyObject } from './types'; | ||
/** | ||
* returns an array of entries of the provided object | ||
* @example | ||
* entries({ a: 1, b: 2 }) | ||
* // returns [['a', 1], ['b', 2]] | ||
*/ | ||
export default function entries<T extends AnyObject>(obj: T): Array<{ | ||
[K in keyof T]: [K, T[K]]; | ||
}[keyof T]>; | ||
//# sourceMappingURL=entries.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns an array of requested length filled with provided value | ||
* @example | ||
* filledArray(3, 1) | ||
* // returns [1, 1, 1] | ||
*/ | ||
export default function filledArray<T>(length: number, value: T): T[]; | ||
//# sourceMappingURL=filledArray.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns the first element of the provided array that returns true for the predicate function, or undefined if the element was not found | ||
* @example | ||
* find(el => el === 2, [1, 2, 3]) | ||
* // returns 2 | ||
*/ | ||
export default function find<T>(predicate: (el: T) => boolean, arr: T[]): T | undefined; | ||
//# sourceMappingURL=find.d.ts.map |
@@ -0,2 +1,10 @@ | ||
/** | ||
* returns the index of the element of the provided array that returns true for the predicate function, or -1 if the element was not found | ||
* @example | ||
* findIndex(el => el === 2, [1, 2, 3]) | ||
* // returns 1 | ||
* findIndex(el => el === 5, [1, 2, 3]) | ||
* // returns -1 | ||
*/ | ||
export default function findIndex<T>(predicate: (el: T) => boolean, arr: T[]): number; | ||
//# sourceMappingURL=findIndex.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns the last element of the provided array that returns true for the predicate function, or undefined if the element was not found | ||
* @example | ||
* findLast(el => el % 2 === 0, [1, 2, 3, 4]) | ||
* // returns 4 | ||
*/ | ||
export default function findLast<T>(predicate: (el: T) => boolean, arr: T[]): T | undefined; | ||
//# sourceMappingURL=findLast.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns the index of the last element of the provided array that returns true for the predicate function, starting from the specified index element to the begining of the array, or -1 if the element was not found | ||
* @example | ||
* findLastIndexFrom(el => el % 2 === 0, 3, [1, 2, 3, 4, 5, 6]) | ||
* // returns 3 | ||
*/ | ||
export default function findLastIndexFrom<T>(predicate: (el: T) => boolean, startIndex: number, arr: T[]): number; | ||
//# sourceMappingURL=findLastIndexFrom.d.ts.map |
@@ -0,2 +1,9 @@ | ||
/** | ||
* returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level | ||
* @example | ||
* const arr = [{ a: [1, 2] }, { a: [3, 4] }, { a: [5, 6] }] | ||
* flatMap(el => el.a, arr) | ||
* // returns [1, 2, 3, 4, 5, 6] | ||
*/ | ||
export default function flatMap<T, R>(iteratee: (el: T) => R[], arr: T[]): R[]; | ||
//# sourceMappingURL=flatMap.d.ts.map |
@@ -1,8 +0,15 @@ | ||
declare type IndexerCallback<T extends { | ||
import type { AnyObject } from './types'; | ||
type IndexerCallback<T extends { | ||
[key: string]: any; | ||
}> = (val: T[keyof T], key: keyof T) => any; | ||
export default function forOwn<Obj extends { | ||
[key: string]: any; | ||
}>(callback: IndexerCallback<Obj>, obj: Obj): void; | ||
/** | ||
* invokes the provided callback for each of the provided object fields | ||
* @example | ||
* const obj = { a: 1, b: 2, c: 3 } | ||
* const arr: string[] = [] | ||
* forOwn(el => arr.push(el.toString()), obj) | ||
* // arr equals ['1', '2', '3'] | ||
*/ | ||
export default function forOwn<T extends AnyObject>(callback: IndexerCallback<T>, obj: T): void; | ||
export {}; | ||
//# sourceMappingURL=forOwn.d.ts.map |
@@ -1,4 +0,21 @@ | ||
import { StringKeyObject } from './types'; | ||
declare function fromPairs<T>(pairs: Array<[string, T]>): StringKeyObject<T>; | ||
export default fromPairs; | ||
type ArrayElement<A> = A extends readonly (infer T)[] ? T : never; | ||
type DeepWriteable<T> = { | ||
-readonly [P in keyof T]: DeepWriteable<T[P]>; | ||
}; | ||
type Cast<X, Y> = X extends Y ? X : Y; | ||
type FromPairs<T> = T extends [infer Key, any][] ? { | ||
[K in Cast<Key, PropertyKey>]: Extract<ArrayElement<T>, [K, any]>[1]; | ||
} : { | ||
[key in string]: any; | ||
}; | ||
type FromPairsWithReadOnly<T> = FromPairs<DeepWriteable<T>>; | ||
/** | ||
* returns an object constructed from the provided array of key, value pairs' | ||
* @example | ||
* const arr = [['a', 1], ['b', 2], ['c', 3]] | ||
* fromPairs(arr) | ||
* // returns { a: 1, b: 2, c: 3 } | ||
*/ | ||
export default function fromPairs<T extends ReadonlyArray<readonly [PropertyKey, any]>>(pairs: T): FromPairsWithReadOnly<T>; | ||
export {}; | ||
//# sourceMappingURL=fromPairs.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* generates a random id | ||
* @example | ||
* generateRandomId() | ||
* // returns 'd1rjknhhch8' | ||
*/ | ||
export default function generateRandomId(): string; | ||
//# sourceMappingURL=generateRandomId.d.ts.map |
@@ -1,2 +0,8 @@ | ||
export default function generateUniqueId(map: any): string; | ||
/** | ||
* generates an unique id based on the provided object keys | ||
* @example | ||
* generateUniqueId({ xuvarw8cao: 1, b837g2nba1d: 2 }) | ||
* // returns 'd1rjknhhch8' | ||
*/ | ||
export default function generateUniqueId(map: Record<string, unknown>): string; | ||
//# sourceMappingURL=generateUniqueId.d.ts.map |
@@ -1,2 +0,20 @@ | ||
export default function groupBy<Obj extends Record<string, unknown>>(mapper: (element: Obj) => string, collection: Obj[] | Record<string, Obj>): Record<string, Obj[]>; | ||
import type { AnyObject } from './types'; | ||
/** | ||
* groups values from the provided array or object based on the result of the provided mapper function | ||
* @example | ||
* const arr = [ | ||
* { a: 1, b: 2 }, | ||
* { a: 2, b: 4 }, | ||
* { a: 3, b: 6 }, | ||
* ] | ||
* groupBy(el => (el.a % 2 === 0 ? 'even' : 'odd'), arr) | ||
* // returns { | ||
* // odd: [ | ||
* // { a: 1, b: 2 }, | ||
* // { a: 3, b: 6 }, | ||
* // ], | ||
* // even: [{ a: 2, b: 4 }], | ||
* // } | ||
*/ | ||
export default function groupBy<T extends AnyObject>(mapper: (element: T) => PropertyKey, collection: T[] | Record<PropertyKey, T>): Record<PropertyKey, T[]>; | ||
//# sourceMappingURL=groupBy.d.ts.map |
@@ -1,2 +0,9 @@ | ||
export default function hasOwn<Obj extends Record<string, unknown>, Own extends keyof Obj, Prop extends PropertyKey>(prop: Prop, obj: Obj): Prop extends Own ? true : false; | ||
import type { AnyObject } from './types'; | ||
/** | ||
* returns true or false depending if the provided property is present inside the provided object | ||
* @example | ||
* hasOwn('a', { a: 1, b: 2 }) | ||
* // returns true | ||
*/ | ||
export default function hasOwn<Obj extends AnyObject, Own extends keyof Obj, Prop extends PropertyKey>(prop: Prop, obj: Obj): Prop extends Own ? true : false; | ||
//# sourceMappingURL=hasOwn.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns the provided value | ||
* @example | ||
* identity('hello') | ||
* // returns 'hello' | ||
*/ | ||
export default function identity<T>(value: T): T; | ||
//# sourceMappingURL=identity.d.ts.map |
@@ -0,3 +1,11 @@ | ||
/** | ||
* returns true or false depending if the provided value is present inside the provided array or string | ||
* @example | ||
* includes('a', ['a', 'b', 'c']) | ||
* // returns true | ||
* includes('d', 'abc') | ||
* // returns false | ||
*/ | ||
export default function includes<T>(value: T, arr: ReadonlyArray<T>): boolean; | ||
export default function includes(value: string, str: string): boolean; | ||
//# sourceMappingURL=includes.d.ts.map |
@@ -14,2 +14,3 @@ export { default as add } from './add'; | ||
export { default as defaultTo } from './defaultTo'; | ||
export { default as diffKeys } from './diffKeys'; | ||
export { default as drop } from './drop'; | ||
@@ -16,0 +17,0 @@ export { default as dropRight } from './dropRight'; |
@@ -1,3 +0,10 @@ | ||
declare const isArray: (arr: any) => arr is Array<any>; | ||
export default isArray; | ||
/** | ||
* determines whether provided value is an array | ||
* @example | ||
* isArray([1, 2]) | ||
* // returns true | ||
* isArray('hello') | ||
* // returns false | ||
*/ | ||
export default function isArray(arr: unknown): arr is Array<unknown>; | ||
//# sourceMappingURL=isArray.d.ts.map |
@@ -1,2 +0,9 @@ | ||
export default function isEmpty(collection: Array<any> | Record<string, unknown>): boolean; | ||
import type { AnyObject } from './types'; | ||
/** | ||
* returns true or false depending if the provided value is an empty object or an empty array | ||
* @example | ||
* isEmpty({}) | ||
* // returns true | ||
*/ | ||
export default function isEmpty(collection: unknown[] | AnyObject): boolean; | ||
//# sourceMappingURL=isEmpty.d.ts.map |
@@ -0,2 +1,10 @@ | ||
/** | ||
* returns true or false depending if the provided value is an object | ||
* @example | ||
* isObject({ a: 1 }) | ||
* // returns true | ||
* isObject([1, 2]) | ||
* // returns false | ||
*/ | ||
export default function isObject(obj: any): obj is object; | ||
//# sourceMappingURL=isObject.d.ts.map |
@@ -1,3 +0,8 @@ | ||
declare const isPromise: (promise: any) => promise is Promise<any>; | ||
export default isPromise; | ||
/** | ||
* returns true or false depending if the provided value is a Promise | ||
* @example | ||
* isPromise(new Promise(res => res(null))) | ||
* // returns true | ||
*/ | ||
export default function isPromise(promise: any): promise is Promise<any>; | ||
//# sourceMappingURL=isPromise.d.ts.map |
@@ -1,2 +0,16 @@ | ||
export default function keyBy<Obj extends Record<string, any>, Prop extends keyof Obj>(prop: Prop, arr: Obj[]): Record<string, Obj>; | ||
import type { AnyObject } from './types'; | ||
/** | ||
* constructs an object from the provided array of objects, grouped by the value of the specified key | ||
* @example | ||
* const arr = [ | ||
* { a: 'foo', b: 'bar' }, | ||
* { a: 'foo', b: 'baz' }, | ||
* { a: 'test', b: 'bab' }, | ||
* ] | ||
* keyBy('a', arr) | ||
* // returns { foo: { a: 'foo', b: 'baz' }, test: { a: 'test', b: 'bab' } } | ||
*/ | ||
export default function keyBy<Obj extends AnyObject, Prop extends keyof Obj>(prop: Prop, arr: Obj[]): { | ||
[P in Obj[Prop]]: Obj; | ||
}; | ||
//# sourceMappingURL=keyBy.d.ts.map |
@@ -1,2 +0,9 @@ | ||
export default function keys(obj: Record<string, unknown>): Array<string>; | ||
import type { AnyObject } from './types'; | ||
/** | ||
* returns an array of the provided object keys | ||
* @example | ||
* keys({ a: 1, b: 2, c: 3 }) | ||
* // returns ['a', 'b', 'c'] | ||
*/ | ||
export default function keys<T extends AnyObject>(obj: T): Array<keyof T>; | ||
//# sourceMappingURL=keys.d.ts.map |
@@ -1,2 +0,10 @@ | ||
export default function last<T>(arr: T[]): T | null; | ||
type LastArrayElement<ValueType extends readonly unknown[]> = ValueType extends readonly [] ? undefined : ValueType extends readonly [infer ElementType] ? ElementType : ValueType extends readonly [infer _, ...infer Tail] ? LastArrayElement<Tail> : ValueType extends ReadonlyArray<infer ElementType> ? ElementType : never; | ||
/** | ||
* returns the last element of the provided array or undefined when array is empty | ||
* @example | ||
* last([1, 2, 3]) | ||
* // returns 3 | ||
*/ | ||
export default function last<T extends readonly any[]>(arr: T): LastArrayElement<T>; | ||
export {}; | ||
//# sourceMappingURL=last.d.ts.map |
@@ -1,2 +0,9 @@ | ||
export default function leadingThrottle(ms: number, fn: CallableFunction): (...args: any[]) => void; | ||
/** | ||
* ensures that a function is executed at a fixed interval, so that it is not called too frequently | ||
* @example | ||
* const updatePreview = () => { ... } | ||
* const throttledUpdatePreview = leadingThrottle(updatePreview, 500); | ||
* inputField.addEventListener('input', throttledUpdatePreview); | ||
*/ | ||
export default function leadingThrottle<T extends (...args: any[]) => any>(ms: number, fn: T): (...args: Parameters<T>) => void; | ||
//# sourceMappingURL=leadingThrottle.d.ts.map |
@@ -1,2 +0,15 @@ | ||
export default function mapKeys<T>(mapper: (key: string) => string, obj: Record<string, T>): Record<string, T>; | ||
import type { AnyObject } from './types'; | ||
type IncludesSubstring<T, S> = T extends `${string}${string & S}` ? T : T extends `${string & S}${string}` ? T : T extends `${string}${string & S}${string}` ? T : never; | ||
/** | ||
* maps keys of the provided object | ||
* @example | ||
* mapKeys(key => key.toUpperCase(), { a: 1, b: 2 }) | ||
* // returns { A: 1, B: 2 } | ||
*/ | ||
export default function mapKeys<T extends AnyObject, K extends string, StaticKeyResult = { | ||
[Key in keyof T as IncludesSubstring<K, Key>]: T[Key]; | ||
}, Result = keyof StaticKeyResult extends never ? { | ||
[key in keyof T as K]: T[key]; | ||
} : StaticKeyResult>(mapper: (key: keyof T) => K, obj: T): Result; | ||
export {}; | ||
//# sourceMappingURL=mapKeys.d.ts.map |
@@ -1,2 +0,11 @@ | ||
export default function mapValues<T, R>(mapper: (value: T) => R, obj: Record<string, T>): Record<string, R>; | ||
import type { AnyObject } from './types'; | ||
/** | ||
* maps values of the provided object | ||
* @example | ||
* mapValues(val => val.toUpperCase(), { a: 'foo', b: 'bar' }) | ||
* // returns { a: 'FOO', b: 'BAR' } | ||
*/ | ||
export default function mapValues<T extends AnyObject, R>(mapper: (value: T[keyof T]) => R, obj: T): { | ||
[key in keyof T]: R; | ||
}; | ||
//# sourceMappingURL=mapValues.d.ts.map |
@@ -1,4 +0,11 @@ | ||
declare type UnaryFunction = (args: any) => any; | ||
type UnaryFunction = (args: any) => any; | ||
/** | ||
* memoizes the provided function so it returns cached results when invoked with the same arguments | ||
* @example | ||
* const memoizedFunc = memoize((val: number) => ({ current: val }) | ||
* memoizedFunc(3) === memoizedFunc(3) | ||
* // returns true | ||
*/ | ||
export default function memoize<T extends UnaryFunction>(func: T): T; | ||
export {}; | ||
//# sourceMappingURL=memoize.d.ts.map |
@@ -1,5 +0,12 @@ | ||
import { AnyFunction } from './types'; | ||
declare type KeyResolver<T extends AnyFunction> = (...args: Parameters<T>) => string; | ||
import type { AnyFunction } from './types'; | ||
type KeyResolver<T extends AnyFunction> = (...args: Parameters<T>) => PropertyKey; | ||
/** | ||
* memoizes the provided function so it returns cached results but based on the provided key resolver | ||
* @example | ||
* const memoizedFunc = memoizeWith(key => (key === 'a' ? key : 'bar'), (val: string) => ({ current: val })) | ||
* memoizedFunc('a') === memoizedFunc('a') // true | ||
* memoizedFunc('b') === memoizedFunc('c') // true | ||
*/ | ||
export default function memoizeWith<T extends AnyFunction, K extends KeyResolver<T>>(keyResolver: K, func: T): T; | ||
export {}; | ||
//# sourceMappingURL=memoizeWith.d.ts.map |
@@ -0,2 +1,7 @@ | ||
/** | ||
* does literally nothing | ||
* @example | ||
* somethingAsyncAndDangerous().catch(noop) | ||
*/ | ||
export default function noop(): void; | ||
//# sourceMappingURL=noop.d.ts.map |
@@ -1,2 +0,9 @@ | ||
export default function omitBy<T>(predicate: (el: T) => boolean, obj: Record<string, T>): Record<string, T>; | ||
import type { AnyObject } from './types'; | ||
/** | ||
* returns an object same as the provided one but without the fields omitted by the predicate function | ||
* @example | ||
* omitBy(el => el % 2 === 0, { a: 1, b: 2, c: 3 }) | ||
* // returns { a: 1, c: 3 } | ||
*/ | ||
export default function omitBy<O, T extends AnyObject>(predicate: (el: T[keyof T]) => boolean, obj: T): O; | ||
//# sourceMappingURL=omitBy.d.ts.map |
@@ -1,3 +0,13 @@ | ||
import { AnyFunction } from './types'; | ||
export default function once<Fn extends AnyFunction>(fn: Fn): (...args: Parameters<Fn>) => ReturnType<Fn>; | ||
import type { AnyFunction } from './types'; | ||
/** | ||
* returns a particular function to be executed only a single time | ||
* @example | ||
* const results = [1, 2, 3, 4, 5] | ||
* const getResult = once(() => results.pop()) | ||
* getResult() // 5 | ||
* getResult() // 5 | ||
* getResult() // 5 | ||
* | ||
*/ | ||
export default function once<T extends AnyFunction>(fn: T): (...args: Parameters<T>) => ReturnType<T>; | ||
//# sourceMappingURL=once.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns a tuple from the two provided values | ||
* @example | ||
* pair('a', 'b') | ||
* // returns ['a', 'b'] | ||
*/ | ||
export default function pair<A, B>(a: A, b: B): [A, B]; | ||
//# sourceMappingURL=pair.d.ts.map |
@@ -1,4 +0,9 @@ | ||
export default function pick<Obj extends { | ||
[key: string]: any; | ||
}, Key extends keyof Obj>(props: Key[], obj: Obj): Pick<Obj, Key>; | ||
import type { AnyObject } from './types'; | ||
/** | ||
* picks specified properties from the object | ||
* @example | ||
* pick(['b'], { a: 1, b: 2 }) | ||
* // returns { b: 2 } | ||
*/ | ||
export default function pick<T extends AnyObject, K extends keyof T>(props: K[], obj: T): Pick<T, K>; | ||
//# sourceMappingURL=pick.d.ts.map |
@@ -1,2 +0,9 @@ | ||
export default function pickOwn<Obj extends Record<string, unknown>, Prop extends keyof Obj>(props: Prop[], obj: Obj): Pick<Obj, Prop>; | ||
import type { AnyObject } from './types'; | ||
/** | ||
* picks specified props only if they exist on the provided object | ||
* @example | ||
* pickOwn(['b'], { a: 1, b: 2 }) | ||
* // returns { b: 2 } | ||
*/ | ||
export default function pickOwn<Obj extends AnyObject, Prop extends keyof Obj>(props: Prop[], obj: Obj): Pick<Obj, Prop>; | ||
//# sourceMappingURL=pickOwn.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns a random integer number between the specified min and max values | ||
* @example | ||
* randomInt(0, 10) | ||
* // returns 7 | ||
*/ | ||
export default function randomInt(min: number, max: number): number; | ||
//# sourceMappingURL=randomInt.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns the provided string repeated the specified amount of times | ||
* @example | ||
* repeat(3, 'test') | ||
* // returns 'testtesttest' | ||
*/ | ||
export default function repeat(count: number, text: string): string; | ||
//# sourceMappingURL=repeat.d.ts.map |
@@ -1,3 +0,8 @@ | ||
declare const shortenLongText: (limit: number, text: string) => string; | ||
export default shortenLongText; | ||
/** | ||
* shortens the provided string by the specified amount | ||
* @example | ||
* shortenLongText(5, 'Lorem ipsum dolor') | ||
* // returns 'Lorem...' | ||
*/ | ||
export default function shortenLongText(limit: number, text: string): string; | ||
//# sourceMappingURL=shortenLongText.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns an array with the provided array values shuffled | ||
* @example | ||
* shuffle([1, 2, 3, 4]) | ||
* // returns [4, 1, 3, 2] | ||
*/ | ||
export default function shuffle<T>(arr: T[]): T[]; | ||
//# sourceMappingURL=shuffle.d.ts.map |
@@ -0,2 +1,10 @@ | ||
/** | ||
* returns a sign of the provided number or NaN if value different than number was provided | ||
* @example | ||
* sign(10) | ||
* // returns 1 | ||
* sign(-8) | ||
* // returns -1 | ||
*/ | ||
export default function sign(value: unknown): number; | ||
//# sourceMappingURL=sign.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns true if some of the array values is truthy | ||
* @example | ||
* someAreTruthy([0, 1, 2]) | ||
* // returns true | ||
*/ | ||
export default function someAreTruthy(arr: any[]): boolean; | ||
//# sourceMappingURL=someAreTruthy.d.ts.map |
@@ -0,3 +1,11 @@ | ||
/** | ||
* splits an array or a string at the given index | ||
* @example | ||
* splitAt(2, [1, 2, 3, 4, 5]) | ||
* // returns [[1, 2], [3, 4, 5]] | ||
* splitAt(2, 'foobar') | ||
* // returns ['fo', 'obar'] | ||
*/ | ||
export default function splitAt<T>(splitPoint: number, arr: T[]): [T[], T[]]; | ||
export default function splitAt(splitPoint: number, str: string): [string, string]; | ||
//# sourceMappingURL=splitAt.d.ts.map |
@@ -1,4 +0,14 @@ | ||
declare type CompareResult = 1 | -1 | 0; | ||
declare const stringCompare: (strA: string, strB: string) => CompareResult; | ||
export default stringCompare; | ||
type CompareResult = 1 | -1 | 0; | ||
/** | ||
* returns 0 for matching strings | ||
* return -1 for string with lower char code at some position | ||
* return 1 for string with greater char code at some position | ||
* @example | ||
* stringCompare('abc', 'abc') | ||
* // returns 0 | ||
* stringCompare('abc', 'abd') | ||
* // returns -1 | ||
*/ | ||
export default function stringCompare(strA: string, strB: string): CompareResult; | ||
export {}; | ||
//# sourceMappingURL=stringCompare.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns a sum of all the provided array number values | ||
* @example | ||
* sum([1, 2, 3]) | ||
* // returns 6 | ||
*/ | ||
export default function sum(numbers: number[]): number; | ||
//# sourceMappingURL=sum.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* takes elements while `predicate` returns true | ||
* @example | ||
* takeRightWhile(el => el > 2, [1, 2, 3, 4, 5]) | ||
* // returns [3, 4] | ||
*/ | ||
export default function takeRightWhile<T>(predicate: (el: T) => boolean, arr: T[]): T[]; | ||
//# sourceMappingURL=takeRightWhile.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* takes elements while `predicate` returns true starting from the right from the specified index | ||
* @example | ||
* takeRightWhileFrom(el => el > 2, 3, [1, 2, 3, 4, 5]) | ||
* // returns [3, 4] | ||
*/ | ||
export default function takeRightWhileFrom<T>(predicate: (el: T) => boolean, startIndex: number, arr: T[]): T[]; | ||
//# sourceMappingURL=takeRightWhileFrom.d.ts.map |
@@ -1,6 +0,12 @@ | ||
declare type ArrayLike = { | ||
type ArrayLike = { | ||
length: number; | ||
}; | ||
/** | ||
* converts values that are iterable to an array | ||
* @example | ||
* toArray('hello') | ||
* // returns ['h', 'e', 'l', 'l', 'o'] | ||
*/ | ||
export default function toArray<T>(arrayLike: ArrayLike): T[]; | ||
export {}; | ||
//# sourceMappingURL=toArray.d.ts.map |
@@ -1,3 +0,8 @@ | ||
declare const toFixedNumber: (num: number, digits: number) => number; | ||
export default toFixedNumber; | ||
/** | ||
* returns the provided number with specified amount of decimal digits | ||
* @example | ||
* toFixedNumber(1.2345, 2) | ||
* // returns 1.23 | ||
*/ | ||
export default function toFixedNumber(num: number, digits: number): number; | ||
//# sourceMappingURL=toFixedNumber.d.ts.map |
@@ -1,4 +0,11 @@ | ||
import { StringKeyObject } from './types'; | ||
declare const toPairs: <T>(obj: StringKeyObject<T>) => [string, T][]; | ||
export default toPairs; | ||
import type { AnyObject } from './types'; | ||
/** | ||
* returns an array of derived from the provided object key-value tuples | ||
* @example | ||
* toPairs({ a: 1, b: 2 }) | ||
* // returns [['a', 1], ['b', 2]] | ||
*/ | ||
export default function toPairs<T extends AnyObject>(obj: T): { | ||
[K in keyof T]: [K, T[K]]; | ||
}[keyof T][]; | ||
//# sourceMappingURL=toPairs.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* trims the end whitespaces from the provided string | ||
* @example | ||
* trimEnd('hello ') | ||
* // returns 'hello' | ||
*/ | ||
export default function trimEnd(str: string): string; | ||
//# sourceMappingURL=trimEnd.d.ts.map |
@@ -1,5 +0,14 @@ | ||
export declare type AnyFunction = (...args: any[]) => any; | ||
export declare type StringKeyObject<T> = { | ||
[key: string]: T; | ||
}; | ||
export type AnyFunction = (...args: any[]) => any; | ||
export type Primitives = string | number | boolean | bigint | symbol | Date | AnyFunction; | ||
export type AnyObject = Record<PropertyKey, any>; | ||
export type StringDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'; | ||
export type UpperCaseCharacters = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z'; | ||
export type Whitespace = '\u{9}' | '\u{A}' | '\u{B}' | '\u{C}' | '\u{D}' | '\u{20}' | '\u{85}' | '\u{A0}' | '\u{1680}' | '\u{2000}' | '\u{2001}' | '\u{2002}' | '\u{2003}' | '\u{2004}' | '\u{2005}' | '\u{2006}' | '\u{2007}' | '\u{2008}' | '\u{2009}' | '\u{200A}' | '\u{2028}' | '\u{2029}' | '\u{202F}' | '\u{205F}' | '\u{3000}' | '\u{FEFF}'; | ||
export type WordSeparators = '-' | '_' | Whitespace; | ||
export type Prettify<T> = { | ||
[K in keyof T]: T[K] extends AnyObject ? Prettify<T[K]> : T[K]; | ||
} & {}; | ||
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; | ||
export type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true; | ||
export {}; | ||
//# sourceMappingURL=types.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns an array with all the duplicates from the provided array removed | ||
* @example | ||
* uniq([1, 1, 2, 3, 3]) | ||
* // returns [1, 2, 3] | ||
*/ | ||
export default function uniq<T>(arr: T[]): T[]; | ||
//# sourceMappingURL=uniq.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns an array with all the duplicates from the provided array removed based on the iteratee function | ||
* @example | ||
* uniqBy(el => el.toString(), [1, '1', 2, '3', 3]) | ||
* // returns [1, 2, '3'] | ||
*/ | ||
export default function uniqBy<T>(iteratee: (el: T) => any, arr: T[]): T[]; | ||
//# sourceMappingURL=uniqBy.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns an array of tuples of certain index elements from two provided arrays | ||
* @example | ||
* zip([1, 2, 3], [10, 20, 30]) | ||
* // returns [[1, 10], [2, 20], [3, 30]] | ||
*/ | ||
export default function zip<A, B>(arrayA: A[], arrayB: B[]): Array<[A, B]>; | ||
//# sourceMappingURL=zip.d.ts.map |
@@ -0,2 +1,8 @@ | ||
/** | ||
* returns an array of tuples of certain index elements from two provided arrays based on the zipper function | ||
* @example | ||
* zipWith((a, b) => [a * 2, b.toString()], [1, 2, 3], [10, 20, 30]) | ||
* // returns [[2, '10'], [4, '20'], [6, '30']] | ||
*/ | ||
export default function zipWith<A, B, R>(zipper: (a: A, b: B) => R, arrayA: A[], arrayB: B[]): R[]; | ||
//# sourceMappingURL=zipWith.d.ts.map |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
253789
209
6201
1
0
1883
17
1