cd2oolz
v1.0.13
Published
Package of utility functions (mostly to avoid including lodash)
Downloads
6
Readme
Cd2oolz (see-dee-too-ls)
Package of utility functions (mostly to avoid including lodash)
Debounce
restricts how ofter a function can run
debounce(timeInMillis)(func)
@debounce(timeInMillis)(func)
Example:
Class:
class X {
@debounce(1000) // time in millis
myFunc() {
// this will run at most once a second
}
}
Function:
const x = debounce(5000)(function() {
console.log("hello")
})
Memoize
caches the result of a function so it doesnt have to rerun
/*
* func: function to memoize
* argSerializer: function which transforms arguments into a key for caching,
* defaults to json stringifing the arguments
*/
memoize(func, argSerializer)
Example:
const x = memoize(function(arg1) {
console.log("run")
return arg1 * 2
})
x(1) // will log
x(1) // wont log as result has been cached
Shuffle
this function will randomize the order of an array in place.
shuffle(arr)
Example:
shuffle([1, 2, 3, 4]) // => [3,2,4,1]
Round
rounds a number.
This will throw an error if decimalPlaces
is < 0
/*
* number: number to round
* decimalPlaces: places to round to. Defaults to 0
*/
round(number, decimalPlaces)
Example:
round(5.1) // => 5
round(5.1, 1) // => 5.1
round(5.11111, 2) // => 5.11