@livechat/data-utils
v1.0.4
Published
Collection of utility functions
Downloads
23,654
Keywords
Readme
Functions
add()
adds two numbers
Kind: global function
Example
add(1, 2)
// returns 3
hasOwn()
returns true or false depending if the provided property is present inside the provided object
Kind: global function
Example
hasOwn('a', { a: 1, b: 2 })
// returns true
assign()
assigns properties of one or more source objects onto target object
Kind: global function
Example
assign({ a: 1, b: 2 }, { b: 4, c: 5 })
// returns { a: 1, b: 4, c: 5 }
capitalizeFirstLetter()
capitalizes first letter of string
Kind: global function
Example
capitalizeFirstLetter('hello')
// returns 'Hello'
flatMap()
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
Kind: global function
Example
const arr = [{ a: [1, 2] }, { a: [3, 4] }, { a: [5, 6] }]
flatMap(el => el.a, arr)
// returns [1, 2, 3, 4, 5, 6]
chain()
takes two arguments: a function and either an array or another function. The function applies the input function to every element of the input array or to the result of calling the input function on the input array. Then, the results are concatenated into a new array and returned.
Kind: global function
Example
chain(n => [n, n], [1, 2, 3])
// returns [1, 1, 2, 2, 3, 3]
const head = list => list[0]
const append = el => list => list.concat(el)
chain(append, head)([1, 2, 3])
// returns [1, 2, 3, 1]
chunk()
chunks provided array to specified size chunks
Kind: global function
Example
chunk([1, 2, 3, 4], 2)
// returns [[1, 2], [3, 4]]
isArray()
determines whether provided value is an array
Kind: global function
Example
isArray([1, 2])
// returns true
isArray('hello')
// returns false
isObject()
returns true or false depending if the provided value is an object
Kind: global function
Example
isObject({ a: 1 })
// returns true
isObject([1, 2])
// returns false
keys()
returns an array of the provided object keys
Kind: global function
Example
keys({ a: 1, b: 2, c: 3 })
// returns ['a', 'b', 'c']
mapValues()
maps values of the provided object
Kind: global function
Example
mapValues(val => val.toUpperCase(), { a: 'foo', b: 'bar' })
// returns { a: 'FOO', b: 'BAR' }
cloneDeep()
returns an exact clone of the provided value with a new reference
Kind: global function
Example
cloneDeep({ a: 1, b: 2 })
// returns { a: 1, b: 2 }
camelCase()
converts provided string to camel case
Kind: global function
Example
camelCase('hello_world')
// returns 'helloWorld'
compact()
removes all nullish and NaN values from the provided object or array
Kind: global function
Example
compact({ a: 1, b: null })
// returns { a: 1 }
compact(['hello', undefined])
// returns ['hello']
compose()
returns a composed value from the provided arguments
Kind: global function
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)))'
debounce()
delays the execution of a function until a certain period of time has passed without the function being called again
Kind: global function
Example
debounce(1000, someFunction)
deepMapKeys()
recurses through an object and transforms its keys – and the keys of any nested objects – based on the provided key mapping function
Kind: global function
Example
deepMapKeys(key => `test_${key}`, { a: 1, b: { c: 2 } })
// returns { test_a: 1, test_b: { test_c: 2 } }
deepMerge()
deep merges provided objects by assigning the sources onto the target object passed as the first argument
Kind: global function
Example
deepMerge({ a: 1, b: { c: 2 } }, { b: { d: 3 } })
// returns { a: 1, b: { c: 2, d: 3 } }
isNil()
returns true or false depending if the provided value is null or undefined
Kind: global function
Example
isNil(null)
// returns true
defaultTo()
returns a function that invoked with a null or undefined values returns the provided default
Kind: global function
Example
const defaulter = defaultTo('default')
defaulter('hello')
// returns 'hello'
defaulter(null)
// returns 'default'
diffKeys()
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
Kind: global function
Example
diffKeys({ a: 1, b: 2, c: 3 }, { c: 3, d: 4 })
// returns ['a', 'b']
drop()
drops specific amount of elements of the provided array starting at the beginning
Kind: global function
Example
drop(2, [1, 2, 3, 4])
// returns [3, 4]
dropRight()
drops specific amount of elements of the provided array starting at the end
Kind: global function
Example
dropRight(2, [1, 2, 3, 4])
// returns [1, 2]
ensureArray()
returns an array if an array was provided, or a new array if provided value was not an array
Kind: global function
Example
ensureArray([1, 2])
// returns [1, 2]
ensureArray('test')
// returns ['test']
entries()
returns an array of entries of the provided object
Kind: global function
Example
entries({ a: 1, b: 2 })
// returns [['a', 1], ['b', 2]]
find()
returns the first element of the provided array that returns true for the predicate function, or undefined if the element was not found
Kind: global function
Example
find(el => el === 2, [1, 2, 3])
// returns 2
findIndex()
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
Kind: global function
Example
findIndex(el => el === 2, [1, 2, 3])
// returns 1
findIndex(el => el === 5, [1, 2, 3])
// returns -1
findKey()
returns the key of the provided object that returns true for the predicate function, or undefined if the key was not found
Kind: global function
Example
findKey(el => el === 1, { a: 1, b: { c: 2 } })
// returns 'a'
findKey(el => el === 2, { a: 1, b: { c: 2 } })
// returns undefined
findLast()
returns the last element of the provided array that returns true for the predicate function, or undefined if the element was not found
Kind: global function
Example
findLast(el => el % 2 === 0, [1, 2, 3, 4])
// returns 4
findLastIndexFrom()
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
Kind: global function
Example
findLastIndexFrom(el => el % 2 === 0, 3, [1, 2, 3, 4, 5, 6])
// returns 3
findLastIndex()
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
Kind: global function
Example
findLastIndex(el => el % 2 === 0, [1, 2, 3, 4])
// returns 3
filledArray()
returns an array of requested length filled with provided value
Kind: global function
Example
filledArray(3, 1)
// returns [1, 1, 1]
identity()
returns the provided value
Kind: global function
Example
identity('hello')
// returns 'hello'
flatten()
flattens the provided array by one level
Kind: global function
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]
forOwn()
invokes the provided callback for each of the provided object fields
Kind: global function
Example
const obj = { a: 1, b: 2, c: 3 }
const arr: string[] = []
forOwn(el => arr.push(el.toString()), obj)
// arr equals ['1', '2', '3']
fromPairs()
returns an object constructed from the provided array of key, value pairs'
Kind: global function
Example
const arr = [
['a', 1],
['b', 2],
['c', 3],
]
fromPairs(arr)
// returns { a: 1, b: 2, c: 3 }
generateRandomId()
generates a random id
Kind: global function
Example
generateRandomId()
// returns 'd1rjknhhch8'
generateUniqueId()
generates an unique id based on the provided object keys
Kind: global function
Example
generateUniqueId({ xuvarw8cao: 1, b837g2nba1d: 2 })
// returns 'd1rjknhhch8'
get()
returns the value from the specififed path from the provided object
Kind: global function
Example
const obj = { a: { b: [1, 2, 3] } }
get('a.b.1', obj)
// returns 2
get(['a', 'b', '1'], obj)
// returns 2
getOr()
returns the value from the specififed path from the provided object or the default value if the path element was not found
Kind: global function
Example
const obj = { a: { b: [1, 2, 3] } }
getOr(null, 'a.b.1', obj)
// returns 2
getOr(null, 'a.c', obj)
// returns null
groupBy()
groups values from the provided array or object based on the result of the provided mapper function
Kind: global 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 }],
// }
groupKeys()
groups values from the provided object based on the result of the provided key mapping function
Kind: global 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 },
// }
includes()
returns true or false depending if the provided value is present inside the provided array or string
Kind: global function
Example
includes('a', ['a', 'b', 'c'])
// returns true
includes('d', 'abc')
// returns false
isEmpty()
returns true or false depending if the provided value is an empty object or an empty array
Kind: global function
Example
isEmpty({})
// returns true
isFalsy()
returns true or false depending if the provided value is falsy
Kind: global function
Example
isFalsy(0)
// returns true
isTruthy()
returns true or false depending if the provided value is truthy
Kind: global function
Example
isTruthy(1)
// returns true
isPromise()
returns true or false depending if the provided value is a Promise
Kind: global function
Example
isPromise(new Promise(res => res(null)))
// returns true
keyBy()
constructs an object from the provided array of objects, grouped by the value of the specified key
Kind: global function
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' } }
last()
returns the last element of the provided array or undefined when array is empty
Kind: global function
Example
last([1, 2, 3])
// returns 3
mapKeys()
maps keys of the provided object
Kind: global function
Example
mapKeys(key => key.toUpperCase(), { a: 1, b: 2 })
// returns { A: 1, B: 2 }
mapValuesIndexed()
maps values and keys of the provided object
Kind: global function
Example
mapValuesIndexed((val, key) => `${key}-${val}`, { a: 1, b: 2 })
// returns { a: 'a-1', b: 'b-2' }
merge()
deep merges the two provided objects
Kind: global function
Example
merge({ a: 1 }, { b: 2 })
// returns { a: 1, b: 2 }
mergeAll()
deep merges all provided objects
Kind: global function
Example
mergeAll({ a: 1 }, { b: 2 }, { c: 3 })
// returns { a: 1, b: 2, c: 3 }
memoizeWith()
memoizes the provided function so it returns cached results but based on the provided key resolver
Kind: global function
Example
const memoizedFunc = memoizeWith(
key => (key === 'a' ? key : 'bar'),
(val: string) => ({ current: val }),
)
memoizedFunc('a') === memoizedFunc('a') // true
memoizedFunc('b') === memoizedFunc('c') // true
memoize()
memoizes the provided function so it returns cached results when invoked with the same arguments
Kind: global function
Example
const memoizedFunc = memoize((val: number) => ({ current: val })
memoizedFunc(3) === memoizedFunc(3)
// returns true
memoizeOne()
memoizes the provided function so it returns cached results but only with one cache key
Kind: global function
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
noop()
does literally nothing
Kind: global function
Example
somethingAsyncAndDangerous().catch(noop)
values()
returns an array of values of the provided object
Kind: global function
Example
values({ a: 1, b: 2, c: 3 })
// returns [1, 2, 3]
numericSortBy()
sorts values of the provided object or array based on the provided mapping function or the provided prop string
Kind: global function
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 }]
omitByIndexed()
returns an object the same as the provided one but without the fields omitted by the predicate function
Kind: global function
Example
omitByIndexed((val, key) => key === 'b', { a: 1, b: 2, c: 3 })
// returns { a: 1, c: 3 }
omit()
returns the provided object but with specified keys omitted
Kind: global function
Example
omit(['a'], { a: 1, b: 2 })
// returns { b: 2 }
omitBy()
returns an object same as the provided one but without the fields omitted by the predicate function
Kind: global function
Example
omitBy(el => el % 2 === 0, { a: 1, b: 2, c: 3 })
// returns { a: 1, c: 3 }
once()
returns a particular function to be executed only a single time
Kind: global function
Example
const results = [1, 2, 3, 4, 5]
const getResult = once(() => results.pop())
getResult() // 5
getResult() // 5
getResult() // 5
over()
returns a function that called invokes the provided array of functions with the call arguments
Kind: global function
Example
const func = over([val => val + 1, val => val * 10, Boolean])
func(1)
// returns [2, 10, true]
takeLast()
returns the specified amount of the provided array elements starting from the end
Kind: global function
Example
takeLast(2, ['a', 'b', 'c', 'd'])
// returns ['c', 'd']
overArgs()
returns a function that called invokes the provided arguments transformers
Kind: global function
Example
const func = overArgs((a, b, c) => [a, b, c], [n => n * n, Boolean])
func(2, 0, 1)
// returns [4, false, 1]
pair()
returns a tuple from the two provided values
Kind: global function
Example
pair('a', 'b')
// returns ['a', 'b']
partitionObject()
returns a tuple of valid and invalid parts of the object as defined in validation function passed in the first argument
Kind: global function
Example
partitionObject(el => el % 2 === 0, { a: 1, b: 2, c: 3 })
// returns [{ b: 2 }, { a: 1, c: 3 }]
pick()
picks specified properties from the object
Kind: global function
Example
pick(['b'], { a: 1, b: 2 })
// returns { b: 2 }
pickBy()
returns an object same as the provided one but with the fields picked by the predicate function
Kind: global function
Example
pickBy(el => el % 2 === 0, { a: 1, b: 2, c: 3 })
// returns { b: 2 }
pickByIndexed()
returns an object same as the provided one but with the fields picked by the predicate function
Kind: global function
Example
pickByIndexed((val, key) => key === 'b', { a: 1, b: 2, c: 3 })
// returns { b: 2 }
pickOwn()
picks specified props only if they exist on the provided object
Kind: global function
Example
pickOwn(['b'], { a: 1, b: 2 })
// returns { b: 2 }
randomInt()
returns a random integer number between the specified min and max values
Kind: global function
Example
randomInt(0, 10)
// returns 7
range()
returns an array filled with numbers in an ascending order to the provided max value
Kind: global function
Example
range(5)
// returns [0, 1, 2, 3, 4, 5]
reject()
returns an array with elements that return false for the provided predicate function
Kind: global function
Example
reject(el => el % 2 === 0, [1, 2, 3, 4, 5])
// returns [1, 3, 5]
removeAt()
returns the provided array without the specified index element
Kind: global function
Example
removeAt(2, [1, 2, 3, 4, 5])
// returns [1, 2, 4, 5]
set()
sets the property of the provided object specified by the provided path in form of a string or an array of keys
Kind: global function
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 } }
shallowEqual()
returns true if the provided values are shallow equal
Kind: global function
Example
shallowEqual({ a: 1 }, { a: 1 })
// returns true
shallowEqual({ a: { b: 1 } }, { a: { b: 1 } })
// returns false
shortenLongText()
shortens the provided string by the specified amount
Kind: global function
Example
shortenLongText(5, 'Lorem ipsum dolor')
// returns 'Lorem...'
sign()
returns a sign of the provided number or NaN if value different than number was provided
Kind: global function
Example
sign(10)
// returns 1
sign(-8)
// returns -1
shuffle()
returns an array with the provided array values shuffled
Kind: global function
Example
shuffle([1, 2, 3, 4])
// returns [4, 1, 3, 2]
sliceDiff()
returns the diff object from the provided object and slice comparison
Kind: global function
Example
sliceDiff({ a: 1, b: 10, g: 3 }, { a: 1, b: 2, c: 3 })
// returns { b: 10, g: 3 }
snakeCase()
snake cases the provided string
Kind: global function
Example
snakeCase('helloWorld')
// returns 'hello_world'
someAreTruthy()
returns true if some of the array values is truthy
Kind: global function
Example
someAreTruthy([0, 1, 2])
// returns true
splitAt()
splits an array or a string at the given index
Kind: global function
Example
splitAt(2, [1, 2, 3, 4, 5])
// returns [[1, 2], [3, 4, 5]]
splitAt(2, 'foobar')
// returns ['fo', 'obar']
splitRightWhenAccum()
returns a tuple with the provided array splited on an element that returned true for the provided function, iterating from the right side
Kind: global function
Example
splitRightWhenAccum((el, acc) => [el % 2 === 0, acc], [], [1, 2, 3, 4])
// returns [[1, 2, 3], [4]]
spread()
returns a function that called with an array of values calls the provided function with the spreaded arguments from the array arguments
Kind: global function
Example
const func = spread((a, b, c) => a + b + c)
func([1, 2, 3])
// returns 6
sum()
returns a sum of all the provided array number values
Kind: global function
Example
sum([1, 2, 3])
// returns 6
take()
returns the specified amount of the provided array elements starting from the beginning
Kind: global function
Example
take(2, ['a', 'b', 'c', 'd'])
// returns ['a', 'b']
takeRightWhileFrom()
takes elements while predicate
returns true starting from the right from the specified index
Kind: global function
Example
takeRightWhileFrom(el => el > 2, 3, [1, 2, 3, 4, 5])
// returns [3, 4]
takeRightWhile()
takes elements while predicate
returns true
Kind: global function
Example
takeRightWhile(el => el > 2, [1, 2, 3, 4, 5])
// returns [3, 4]
throttle()
ensures that a function is executed at a fixed interval, so that it is not called too frequently
Kind: global function
Example
const updatePreview = () => { ... }
const throttledUpdatePreview = throttle(updatePreview, 500);
inputField.addEventListener('input', throttledUpdatePreview);
toArray()
converts values that are iterable to an array
Kind: global function
Example
toArray('hello')
// returns ['h', 'e', 'l', 'l', 'o']
toFixedNumber()
returns the provided number with specified amount of decimal digits
Kind: global function
Example
toFixedNumber(1.2345, 2)
// returns 1.23
toPairs()
returns an array of derived from the provided object key-value tuples
Kind: global function
Example
toPairs({ a: 1, b: 2 })
// returns [['a', 1], ['b', 2]]
trailingThrottle()
ensures that a function is executed at a fixed interval, so that it is not called too frequently
Kind: global function
Example
const updatePreview = () => { ... }
const throttledUpdatePreview = trailingThrottle(updatePreview, 500);
inputField.addEventListener('input', throttledUpdatePreview);
leadingThrottle()
ensures that a function is executed at a fixed interval, so that it is not called too frequently
Kind: global function
Example
const updatePreview = () => { ... }
const throttledUpdatePreview = leadingThrottle(updatePreview, 500);
inputField.addEventListener('input', throttledUpdatePreview);
stringCompare()
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
Kind: global function
Example
stringCompare('abc', 'abc')
// returns 0
stringCompare('abc', 'abd')
// returns -1
trimStart()
trims the beginning whitespaces from the provided string
Kind: global function
Example
trimStart(' hello')
// returns 'hello'
trimEnd()
trims the end whitespaces from the provided string
Kind: global function
Example
trimEnd('hello ')
// returns 'hello'
repeat()
returns the provided string repeated the specified amount of times
Kind: global function
Example
repeat(3, 'test')
// returns 'testtesttest'
uniqBy()
returns an array with all the duplicates from the provided array removed based on the iteratee function
Kind: global function
Example
uniqBy(el => el.toString(), [1, '1', 2, '3', 3])
// returns [1, 2, '3']
uniq()
returns an array with all the duplicates from the provided array removed
Kind: global function
Example
uniq([1, 1, 2, 3, 3])
// returns [1, 2, 3]
update()
updates the provided array with the provided value on the specified index
Kind: global function
Example
update(2, 3, [1, 2, 5])
// returns [1, 2, 3]
without()
returns an array without the specified elements from the provided array
Kind: global function
Example
without([2, 4], [1, 2, 3, 4, 5])
// returns [1, 3, 5]
zipWith()
returns an array of tuples of certain index elements from two provided arrays based on the zipper function
Kind: global function
Example
zipWith((a, b) => [a * 2, b.toString()], [1, 2, 3], [10, 20, 30])
// returns [[2, '10'], [4, '20'], [6, '30']]
zip()
returns an array of tuples of certain index elements from two provided arrays
Kind: global function
Example
zip([1, 2, 3], [10, 20, 30])
// returns [[1, 10], [2, 20], [3, 30]]