Formerly: bottom-line-utils, but I never liked the name
A tiny and fast utils library.
Documentation can be found here: documentation.
npm install @ezez/utils
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:
map
/forEach
functions etc.),addPrefix
- add prefix to a string if not already presentaddSuffix
- add suffix to a string if not already presentcapitalize
- capitalize a string, optionally lower casing the restisNumericString
- check if given value is a numeric string, features configurableoccurrences
- count occurrences of a substring in a string, optionally allow overlappingreplace
- replace all occurrences of a substring using a map of replacementsstripPrefix
- strip prefix from a stringstripSuffix
- strip suffix from a stringcap
- cap a value between lower and upper boundround
- round a value to given precisionscale
- scale given value from old range to new rangecompareArrays
- compare two arraysensureArray
- ensure that given value is an arrayinsertSeparator
- insert a separator between every character in an arraylast
- get last element of an arraymatch
- split your array into two groups - one that matches given predicate, and one that doesn'tmostFrequent
- find most frequent value in an arraypull
- remove values from an array (by mutating)remove
- remove values from an array (by mutating) using predicate function and return removed valuessortBy
- sort an array by given property (create callback function for Array.prototype.sort
)unique
- get unique values from an arraycompareProps
- compare two objects and return list of different propertiesdeserialize
- deserialize values serialized with serialize
isPlainObject
- check if given value is a plain objectmapValues
- map values of an objectmerge
- merge two objects shallowly, allowing to remove properties while doing soomit
- omit properties from an objectpick
- pick some properties from an objectreplaceDeep
- recursively replace all occurrences of a value in an object/arraysortProps
- sort object properties by keyserialize
- like JSON.stringify but any data type is allowedensureDate
- ensure that given value is a Date instanceensureTimestamp
- ensure that given value is a numeric timestampformatDate
- format a date using a format string, syntax from PHPcoalesce
- return first non-nullish valueensureError
- ensure that given value is an errorescapeRegExp
- escape a string to be used in a regular expressionsafe
- runs function and returns its result or default value if function throws an errorlater
- create a promise you can resolve later, outside of Promise constructormapAsync
- map an array asynchronously if sync version blocks your event loop for too longnoop
- do nothingrethrow
- throw given valueseq
- sequentially execute Promise-returning functions until one returns a valueretry
- execute Promise-returning in a loop until it resolveswait
- 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 timeget
- extract a value from a deep object using specified path, optionally with a default valuegetMultiple
- same as above, but test multiple pathsset
- set a value in a deep object using specified pathsetImmutable
- set a value in a deep object using specified path, but return a new objectisEmpty
- check if given value is emptythrottle
- throttle a function, very configurabletruthy
- is value truthy? (useful for filtering with more correct TypeScript support than filter(Boolean)
)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.
(TODO)
This library is fully unit tested and can be used on production. See License.
MIT