Util

Introduced in 3.0.0

Stability: 2 - Stable

const Util = require('kado/lib/Util')

The Util library implements several convenience methods.

Class: Util

Util is a completely static class of loosely related methods

NOTE In 4.1.0 the capitalize, printDate, and escapeAndTruncate methods were moved to the Parser library. See their documentation and usage from there. Update any usages accordingly.

static Util.is()

static Util.compare()

static Util.find()

Iterates over elements of collection, returning the first element predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

Example:

const users = [
  { 'user': 'barney',  'age': 36, 'active': true },
  { 'user': 'fred',    'age': 40, 'active': false },
  { 'user': 'pebbles', 'age':  1, 'active': true }
]

find(users, (o) => o.age < 40)
// => object for 'barney'

// The `matches` iteratee shorthand.
find(users, { 'age': 1, 'active': true })
// => object for 'pebbles'

// The `matchesProperty` iteratee shorthand.
find(users, ['active', false])
// => object for 'fred'

// The `property` iteratee shorthand.
find(users, 'active')
// => object for 'barney'

static Util.max()

Computes the maximum value of array. If array is empty or falsey, undefined is returned.

Example:

max([4, 2, 8, 6])
// => 8

max([])
 // => undefined

static Util.padStart()

Pads string on the left side if it's shorter than length. Padding characters are truncated if they exceed length.

Example:

padStart('abc', 6)
// => '   abc'

padStart('abc', 6, '_-')
// => '_-_abc'

padStart('abc', 3)
// => 'abc'

static Util.repeat()

Repeats the given string n times.

Example:

repeat('*', 3)
// => '***'

repeat('abc', 2)
// => 'abcabc'

repeat('abc', 0)
// => ''