Global

Methods

basicUID(safe)

Basic timestamp first UID generator that's good enough for most use cases but not for security purposes. There's an extremely small chance of collision, so create a map object to check for collisions if you're worried about that.

  • Date.now().toString(16) is used for the timestamp, which is a base16 representation of the current timestamp in milliseconds.
  • random().toString(16).substring(2) is used for the random number, which is a base16 representation of a random number between 0 and 1, with the first two characters removed.
Source:
Parameters:
Name Type Description
safe boolean

Defaults to false, if true will use a cryptographically secure random number generator for the random number improving security but reducing performance. If crypto is not available, will use Math.random() instead.

Returns:

string

Example
basicUID() // => '18d4613e4d2-750bf066ac6158'

clone(o)

Deep clone function that's mindful of nested arrays and objects

Source:
To Do:
  • Check if faster than assign. This function is pretty old...
Parameters:
Name Type Description
o object

The object to clone

Returns:

object The cloned object

Example
const obj = { foo: 'bar' }
const clone = clone(obj)
clone.foo = 'baz'
console.log(obj.foo) // 'bar'
console.log(clone.foo) // 'baz'
console.log(obj === clone) // false
console.log(JSON.stringify(obj) === JSON.stringify(clone)) // true

closestNumber(goal, arr)

Finds the closest number to the set goal in an array to a given number

Source:
Parameters:
Name Type Description
goal number

Number to search for

arr array

Array of numbers to search in

Returns:

number

Example
closestNumber(10, [1, 2, 3, 4, 5, 6, 7, 8, 9]) // => 9
closestNumber(10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 11]) // => 9
closestNumber(10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 9.5]) // => 9.5
closestNumber(10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) // => 10

deepMerge(target, source)

Deep merge function that's mindful of arrays and objects

Source:
Parameters:
Name Type Description
target object

The target object to merge into

source object

The source object to merge from

Returns:

object The merged object, in this case the target object with the source object's properties merged into it

Example
const target = { foo: 'bar' }
const source = { bar: 'baz' }
deepMerge(target, source) // { foo: 'bar', bar: 'baz' }

fixed(number, digits)

Gets fixed number of digits after the decimal point

Source:
Parameters:
Name Type Description
number number

Number to fix

digits number

Number of digits to fix to

Returns:

number

Example
fixed(1.234, 2) // => 1.23
fixed(1.235, 2) // => 1.24
fixed(1.234) // => 1
fixed(1.234, 0) // => 1
fixed(1.234, 5) // => 1.234

generateUUID(safe)

Generates a UUID v4

  • Uses crypto.randomUUID if available
  • Uses crypto.getRandomValues if available
  • Uses a fallback if neither is available, which is not safe because it uses Math.random() instead of a cryptographically secure random number generator

I'm bad at crypto and bitwise operations, not my cup of tea, so I had to rely on StackOverflow for the fallback: https://stackoverflow.com/a/2117523/5437943

Source:
Parameters:
Name Type Description
safe boolean

Defaults to true, if false will use a fallback that's not cryptographically secure but significantly faster

Returns:

string

Example
generateUUID() // UUID v4, example 09ed0fe4-8eb6-4c2a-a8d3-a862b7513294

getObjectValueByPath(obj, path) → {*}

Access nested object properties using a path

Source:
Parameters:
Name Type Description
obj object

The object to access

path array | string

The path to access

Returns:
Type:
*

The value of the accessed property

Example
const obj = { foo: { bar: 'baz' } }
getObjectValueByPath(obj, 'foo.bar') // => 'baz'

hasOwnProperties(obj, properties)

Check if object has multiple properties

Source:
Parameters:
Name Type Description
obj object
properties string | array
Returns:

boolean

Example
const obj = { foo: 'bar', baz: 'qux' }
hasOwnProperties(obj, ['foo', 'baz']) // => true
hasOwnProperties(obj, ['foo', 'baz', 'qux']) // => false

isArray(o)

If provided variable is an array. Just a wrapper for Array.isArray

Source:
Parameters:
Name Type Description
o any
Returns:

boolean

Example
isArray([]) // => true
isArray({}) // => false

isEmpty(o)

Check if a variable is empty

Source:
Parameters:
Name Type Description
o any

The variable to check

Returns:

boolean True if the variable is empty, false otherwise

Example
isEmpty({}) // => true
isEmpty([]) // => true
isEmpty('') // => true
isEmpty(null) // => false
isEmpty(undefined) // => false
isEmpty(0) // => false

isEmptyArray(o)

Check if an array is empty, substitute for Array.length === 0

Source:
Parameters:
Name Type Description
o array

The array to check

Returns:

boolean True if the array is empty, false otherwise

Example
isEmptyArray([]) // => true
isEmptyArray([1, 2, 3]) // => false

isEmptyObject(o)

Check if an object is empty

Source:
Parameters:
Name Type Description
o object

The object to check

Returns:

boolean True if the object is empty, false otherwise

Example
isEmptyObject({}) // => true
isEmptyObject({ foo: 'bar' }) // => false

isFunction(o)

If provided variable is a function, substitute for typeof === 'function'

Source:
Parameters:
Name Type Description
o any
Returns:

boolean

Example
isFunction(function() {}) // => true
isFunction({}) // => false

isObject(o)

If provided variable is an object

Source:
Parameters:
Name Type Description
o any
Returns:

boolean

Example
isObject({}) // => true
isObject([]) // => false
isObject(null) // => false

isString(o)

If provided variable is a string. Just a wrapper for typeof === 'string'

Source:
Parameters:
Name Type Description
o any
Returns:

boolean

Example
isString('foo') // => true
isString({}) // => false

mapByProperty(arr, propertyName)

Maps an array of objects by a property name

Source:
Parameters:
Name Type Description
arr array
propertyName string
Returns:

object

Example
const arr = [{ foo: 'bar' }, { foo: 'baz' }]
mapByProperty(arr, 'foo') // => { bar: { foo: 'bar' }, baz: { foo: 'baz' } }

mapPropertyToProperty(arr, keyPropertyName, valuePropertyName)

Maps an array of objects by a property name to another property name

Source:
Parameters:
Name Type Description
arr array
keyPropertyName string
valuePropertyName string
Returns:

object

Example
const arr = [{ foo: 'bar', baz: 'qux' }, { foo: 'quux', baz: 'corge' }]
mapPropertyToProperty(arr, 'foo', 'baz') // => { bar: 'qux', quux: 'corge' }

percentage(num, total)

Calculates the percentage of a number in relation to another number

Source:
Parameters:
Name Type Description
num number

Number to calculate percentage of

total number

Total number

Returns:

number

Example
percentage(1, 10) // => 10
percentage(5, 10) // => 50
percentage(10, 10) // => 100
percentage(0, 10) // => 0
percentage(10, 2) // => 500

pick(obj, props)

Pick properties from an object or elements from an array

Source:
Parameters:
Name Type Description
obj array

Object or array to pick properties or elements from

props array | string | number

Properties to remove, can be an array of strings or a single string or number

Returns:

object | array | undefined

Example
pick({ foo: 'bar', bar: 'baz', baz: 'qux' }) // => {}
pick({}, []) // => {}
pick(null, 'foo') // => undefined
pick({ foo: 'bar', bar: 'baz', baz: 'qux' }, undefined) // => {}
pick({ foo: 'bar', bar: 'baz', baz: 'qux' }, 'foo') // => { foo: 'bar'}
pick({ foo: 'bar', bar: 'baz', baz: 'qux' }, ['foo', 'baz']) // => { foo: 'bar', baz: 'qux' }

pick(['foo', 'bar', 'baz'], []) // => []
pick([], []) // => []
pick(null, 0) // => undefined
pick(['foo', 'bar', 'baz'], undefined) // => []
pick(['foo', 'bar', 'baz'], 0) // => ['foo']
pick(['foo', 'bar', 'baz'], [0, 2]) // => ['foo', 'baz']
pick(['foo', 'bar', 'baz'], [0, 2, 3]) // => ['foo', 'baz']

propertyIsFunction(obj, propertyName)

If object property is a function

Source:
Parameters:
Name Type Description
obj object
propertyName string
Returns:

boolean

Example
const obj = { foo: 'bar', baz: function() {} }
propertyIsFunction(obj, 'foo') // => false
propertyIsFunction(obj, 'baz') // => true

propertyIsString(obj, propertyName)

If object property is a string

Source:
Parameters:
Name Type Description
obj object
propertyName string
Returns:

boolean

Example
const obj = { foo: 'bar', baz: function() {} }
propertyIsString(obj, 'foo') // => true
propertyIsString(obj, 'baz') // => false

random()

Generates a random number between 0 and 1, inclusive of 0 but not inclusive of 1.

  • Uses crypto.getRandomValues if available
  • Uses Math.random() if crypto.getRandomValues is not available
Source:
Returns:

number

Example
random() // => 0.123456789

randomIntInclusive(min, max, safe)

Generates a random integer between two values, inclusive of both

Source:
Parameters:
Name Type Description
min number

Minimum value

max number

Maximum value

safe boolean

Defaults to false, if true will use a cryptographically secure random number generator

Returns:

number

Example
randomIntInclusive(1, 10) // => 1
randomIntInclusive(1, 10) // => 10
randomIntInclusive(1, 10) // => 5

reject(obj, props, clone)

Remove properties from an object or elements from an array

Source:
Parameters:
Name Type Description
obj array

Object or array to remove properties or elements from

props array | string | number

Properties to remove, can be an array of strings or a single string or number

clone boolean

Defaults to true, will clone the object or array before removing properties or elements.

Returns:

object | array | undefined

Example
reject({ foo: 'bar', bar: 'baz', baz: 'qux' }) // => {}
reject({}, []) // => {}
reject(null, 'foo') // => undefined
reject({ foo: 'bar', bar: 'baz', baz: 'qux' }, undefined) // => {}
reject({ foo: 'bar', bar: 'baz', baz: 'qux' }, 'foo') // => { bar: 'baz', baz: 'qux' }
reject({ foo: 'bar', bar: 'baz', baz: 'qux' }, ['foo', 'baz']) // => { bar: 'baz' }

reject(['foo', 'bar', 'baz'], []) // => []
reject([], []) // => []
reject(null, 0) // => undefined
reject(['foo', 'bar', 'baz'], undefined) // => []
reject(['foo', 'bar', 'baz'], 0) // => ['bar', 'baz']
reject(['foo', 'bar', 'baz'], [0, 2]) // => ['bar']
reject(['foo', 'bar', 'baz'], [0, 2, 3]) // => ['bar']

removeAccents(inputString)

Remove accents from a string

Source:
Parameters:
Name Type Description
inputString string
Returns:

string

Example
removeAccents('áéíóú') // => 'aeiou'
removeAccents('ÁÉÍÓÚ') // => 'AEIOU'
removeAccents('señor') // => 'senor'
removeAccents('Œ') // => 'OE'

shallowMerge(target, source)

Shallow merges two objects together. Used to pass simple options to functions.

Source:
Parameters:
Name Type Description
target object

The target object to merge into

source object

The source object to merge from

Returns:

object The merged object, in this case the target object with the source object's properties merged into it

Example
const target = { foo: 'bar' }
const source = { bar: 'baz' }
shallowMerge(target, source) // { foo: 'bar', bar: 'baz' }

slugify(str)

Slugify a string, e.g. 'Foo Bar' => 'foo-bar'. Similar to WordPress' sanitize_title(). Will remove accents and HTML tags.

Source:
Parameters:
Name Type Description
str string
Returns:

string

Example
slugify('Foo Bar') // => 'foo-bar'
slugify('Foo Bar <span>baz</span>') // => 'foo-bar-baz'

stringToArray(str)

Try to convert a string to an array

Source:
Parameters:
Name Type Description
str string

The string to convert

Returns:

array The converted array or undefined if conversion failed

Example
stringToArray('[1, 2, 3]') // => [1, 2, 3]
stringToArray('foo') // => null
stringToArray('1') // => null
stringToArray('{"foo": "bar"}') // => null

stringToBoolean(str)

Try to convert a string to a boolean

Source:
Parameters:
Name Type Description
str string

The string to convert

Returns:

boolean The converted boolean or undefined if conversion failed

Example
stringToBoolean('true') // => true
stringToBoolean('false') // => false
stringToBoolean('foo') // => null

stringToNumber(str)

Try to convert a string to a number

Source:
Parameters:
Name Type Description
str string

The string to convert

Returns:

number The converted number or undefined if conversion failed

Example
stringToNumber('1') // => 1
stringToNumber('1.5') // => 1.5
stringToNumber('foo') // => null
stringToNumber('1foo') // => null

stringToObject(str)

Try to convert a string to an object

Source:
Parameters:
Name Type Description
str string

The string to convert

Returns:

object The converted object or undefined if conversion failed

Example
stringToObject('{ "foo": "bar" }') // => { foo: 'bar' }
stringToObject('foo') // => null
stringToObject('1') // => null
stringToObject('[1, 2, 3]') // => null

stringToPrimitive(str) → {null|boolean|int|float|string}

Try to convert a string to a primitive

Source:
Parameters:
Name Type Description
str string

The string to convert

Returns:
Type:
null | boolean | int | float | string

The converted primitive or input string if conversion failed

Example
stringToPrimitive('null') // => null
stringToPrimitive('true') // => true
stringToPrimitive('false') // => false
stringToPrimitive('1') // => 1
stringToPrimitive('1.5') // => 1.5
stringToPrimitive('foo') // => 'foo'
stringToPrimitive('1foo') // => '1foo'

stringToRegex(str)

Try to convert a string to a regex

Source:
Parameters:
Name Type Description
str string

The string to convert

Returns:

regex The converted regex or undefined if conversion failed

Example
stringToRegex('/foo/i') // => /foo/i
stringToRegex('foo') // => null
stringToRegex('1') // => null

stringToType(str)

Try to convert a string to a data type

Source:
Parameters:
Name Type Description
str string

The string to convert

Returns:

any The converted data type or input string if conversion failed

Example
stringToData('null') // => null
stringToData('true') // => true
stringToData('false') // => false
stringToData('1') // => 1
stringToData('1.5') // => 1.5
stringToData('foo') // => 'foo'
stringToData('1foo') // => '1foo'
stringToData('[1, 2, 3]') // => [1, 2, 3]
stringToData('{ "foo": "bar" }') // => { foo: 'bar' }
stringToData('/foo/i') // => /foo/i

stripHTMLTags(inputString)

Strip HTML tags from a string

Source:
Parameters:
Name Type Description
inputString string
Returns:

string

Example
stripHTMLTags('<span>foo</span>') // => 'foo'
stripHTMLTags('<span>foo</span> <span>bar</span>') // => 'foo bar'

transformCamelCaseToDash(str)

Transforms a camelCase string to dash separated string

Source:
Parameters:
Name Type Description
str string
Returns:

boolean

Example
transformCamelCaseToDash('fooBar') // => 'foo-bar'
transformCamelCaseToDash('fooBarBaz') // => 'foo-bar-baz'
transformCamelCaseToDash('foo') // => 'foo'
transformDashToCamelCase('fooBarBaz-qux') // => 'foo-bar-baz-qux'

transformDashToCamelCase(str)

Transforms a dash separated string to camelCase

Source:
Parameters:
Name Type Description
str string
Returns:

boolean

Example
transformDashToCamelCase('foo-bar') // => 'fooBar'
transformDashToCamelCase('foo-bar-baz') // => 'fooBarBaz'
transformDashToCamelCase('foo') // => 'foo'
transformDashToCamelCase('fooBarBaz-qux') // => 'fooBarBazQux'

truncateString(str, numWords, ellipsis)

Truncate a string to a given number of words

Source:
Parameters:
Name Type Description
str string

String to truncate

numWords number

Number of words to truncate to

ellipsis string

Ellipsis to append to the end of the string

Returns:

string

Example
truncateString('foo bar baz', 2) // => 'foo bar…'
truncateString('foo bar baz', 2, '...') // => 'foo bar...'
truncateString('foo bar. baz', 2, '...') // => 'foo bar. ...'