This is a collection of various useful Javascript utility functions that I've written over the years.
Many of them are wrappers for simple code snippets that exist only to make my code more self-documenting and/or easier to read. Some are more complicated and are the kind of things that I prefer not to copy-and-paste into different projects.
You'll find similar functions in other libraries like underscore and Lodash. These just happen to be the ones I tend to use all the time.
Install badger-utils
using your favourite package manager.
npm add @abw/badger-utils
pnpm add @abw/badger-utils
yarn add @abw/badger-utils
Visit the website for detailed documentation.
- isBoolean(value)
- isString(value)
- isNumber(value)
- isInteger(value)
- isFloat(value)
- isArray(value)
- isFunction(value)
- isRegExp(value)
- isObject(value)
- isUndefined(value)
- isNull(value)
- hasValue(value)
- noValue(value)
- haveValue(...values)
- firstValue(...values)
- range(from, to, step=1)
- formatNumber(number, options)
- currency(number, options)
- commas(n)
- setNumberDefaults(defaults)
- splitLines(text)
- splitList(value)
- splitHash(value, set=true, hash={})
- joinList(array, joint=' ', lastJoint=joint)
- joinListAnd(array, joint=', ', lastJoint=' and ')
- joinListOr(array, joint=', ', lastJoint=' or ')
- capitalise(word) / capitalize(word)
- capitaliseWords(string) / capitalizeWords(string)
- snakeToStudly(snake)
- snakeToCamel(snake)
- plural(singular)
- singular(plural)
- inflect(n, singular, plural, no='no')
- Inflect(n, singular, plural, no='No')
- format(message, data)
- hash(source, options)
- objMap(obj, fn)
- remove(object, key)
- extract(object, keys, options)
- keys(object)
- values(object)
- entries(object)
- stringSort(field)
- numberSort(field)
- integerSort(field)
- multiSort(fields)
- stringField(obj,field)
- numberField(obj,field)
- integerField(obj,field)
- descendingOrder(sortFn)
- ascendingOrder(sortFn)
Andy Wardley abw@wardley.org