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.),capitalize
- capitalize a string, optionally lower casing the restensurePrefix
- add prefix to a string if not already presentensureSuffix
- add suffix to a string if not already presentisNumericString
- 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 stringtrim
- remove given characters from both sides of a stringtrimEnd
- remove given characters from the end of a stringtrimStart
- remove given characters from the start of 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 valuessample
- pick a random element from an arraysamples
- pick multiple random elements from an array without duplicatesshuffle
- shuffle an array, returning a new arraysortBy
- sort an array by given property (create callback function for Array.prototype.sort
)sortByMultiple
- sort an array by multiple properties with individual sort directionstoggle
- toggle an item in an array (add if missing, remove if present)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 objectremoveCommonProperties
- remove properties from targets that have the same value as in sourcereplaceDeep
- recursively replace all occurrences of a value in an object/arrayreplaceDeepByFn
- recursively replace values in an object/array using a callback functionserialize
- like JSON.stringify but any data type is allowedsortProps
- sort object properties by keyensureDate
- 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 errorignore
- execute a callback and ignore its result, errors, or promise rejectionslater
- 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 nothingrace
- timeout your promises while keeping TypeScript types correctrethrow
- throw given valueretry
- execute Promise-returning in a loop until it resolvesseq
- sequentially execute Promise-returning functions until one returns a valuewait
- wait given amount of time (async)waitFor
- wait for a condition to be true, checking every given amount of timewaitSync
- wait given amount of time (sync)get
- extract a value from a deep object using specified path, optionally with a default valuegetMultiple
- same as above, but test multiple pathsisEmpty
- check if given value is emptymemoize
- memoize a function, caching the result of the last callset
- set a value in a deep object using specified pathsetImmutable
- set a value in a deep object using specified path, but return a new objectthrottle
- 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