@ezez/utils
Formerly: bottom-line-utils, but I never liked the name
A tiny and fast utils library.
Documentation
Documentation can be found here: documentation.
Installing
npm install @ezez/utils
Motivation
Lodash complexity that causes big bundle sizes just for using single function (even with tree shaking!).
Lodash is trying to cover each possible use case, even very rare while typical observed use case of lodash is basic
methods used on basic data. As an example, let's take a _.pick method that simply returns an object, based on source
object, with just some properties copied. Easy, right? Few lines of code? Lodash makes that 114 imports!
@ezez/utils aims to:
- cover only most typical use cases of given functions,
- add useful methods that are missing in lodash,
- NOT being 100% compatible replacement for lodash/underscore, while keeping MOST behavior identical,
- not being a "polyfill" (no
map/forEachfunctions etc.), - perform fast and have code size small.
Supported methods
cap- cap a value between lower and upper boundcapitalize- capitalize a string, optionally lower casing the restcoalesce- return first non-nullish valueensureArray- ensure that given value is an arrayensureError- ensure that given value is an errorget- extract a value from a deep object using specified path, optionally with a default valuegetMultiple- same as above, but test multiple pathsinsertSeparator- insert a separator between every character in an arrayisEmpty- check if given value is emptyisPlainObject- check if given value is a plain objectlast- get last element of an arraymapAsync- map an array asynchronously if sync version blocks your event loop for too longmapValues- map values of an objectmatch- split your array into two groups - one that matches given predicate, and one that doesn'tmerge- merge two objects shallowly, allowing to remove properties while doing somostFrequent- find most frequent value in an arraynoop- do nothingomit- omit properties from an objectpack- group array items into arrays of given sizepages- create a pages list to display on front-endpick- pick some properties from an objectpull- remove values from an array (by mutating)remove- remove values from an array (by mutating) using predicate function and return removed valuesrethrow- throw given valuescale- scale given value from old range to new rangeseq- sequentially execute Promise-returning functions until one returns a valueset- set a value in a deep object using specified pathsetImmutable- set a value in a deep object using specified path, but return a new objectsortBy- sort an array by given property (create callback function forArray.prototype.sort)throttle- throttle a function, very configurabletruthy- is value truthy? (useful for filtering with more correct TypeScript support thanfilter(Boolean))wait- wait given amount of time (async)waitSync- wait given amount of time (sync)waitFor- wait for a condition to be true, checking every given amount of time
Methods will be added as I need them. I am open for suggestions and PRs. See next heading for list of methods that I want to add.
Methods to be supported
(TODO)
- debounce
Tested
This library is fully unit tested and can be used on production. See License.
License
MIT