Class: Util

Util


new Util()

Methods


.addClass(element, name)

Add class to the element

Parameters:
Name Type Description
element Element

the element

name string

the class name to add


.allString(list)

Return true if all items in list are strings

Parameters:
Name Type Description
list Array

an array of items


.assign(destination)

Assign source properties to destination. If the property is an object it is assigned as a whole, overriding the destination object.

Parameters:
Name Type Description
destination Object

the object to assign to


.base64EncodeURL(url)

Returns the Base64-decoded version of url.
This method delegates to btoa if present. Otherwise it tries Buffer.

Parameters:
Name Type Description
url string

the url to encode. the value is URIdecoded and then re-encoded before converting to base64 representation

Returns: string

the base64 representation of the URL

Type
string

.camelCase(string)

Convert string to camelCase

Parameters:
Name Type Description
string string

the string to convert

Returns: string

in camelCase format

Type
string

.cloneDeep(value)

Create a new copy of the given object, including all internal objects.

Parameters:
Name Type Description
value Object

the object to clone

Returns: Object

a new deep copy of the object

Type
Object

.compact(array)

Creates a new array from the parameter with "falsey" values removed

Parameters:
Name Type Description
array Array

the array to remove values from

Returns: Array

a new array without falsey values

Type
Array

.contains(array, item)

Check if a given item is included in the given array

Parameters:
Name Type Description
array Array

the array to search in

item *

the item to search for

Returns: boolean

true if the item is included in the array

Type
boolean

.defaults(destination, source)

Assign values from sources if they are not defined in the destination. Once a value is set it does not change

Parameters:
Name Type Argument Description
destination Object

the object to assign defaults to

source Object <repeatable>

the source object(s) to assign defaults from

Returns: Object

destination after it was modified

Type
Object

.difference(arr, values)

Returns values in the given array that are not included in the other array

Parameters:
Name Type Description
arr Array

the array to select from

values Array

values to filter from arr

Returns: Array

the filtered values

Type
Array

.functions(object)

Returns a list of all the function names in obj

Parameters:
Name Type Description
object Object

the object to inspect

Returns: Array

a list of functions of object

Type
Array

.getAttribute(element, name)

Get attribute from the DOM element.

Parameters:
Name Type Description
element Element

the element to set the attribute for

name string

the name of the attribute

Returns: *

the value of the attribute

Type
*

.getData(element, name)

Get data from the DOM element.

This method will use jQuery's data() method if it is available, otherwise it will get the data- attribute

Parameters:
Name Type Description
element Element

the element to get the data from

name string

the name of the data item

Returns:

the value associated with the name


.hasClass(element, name)

Checks if element has a css class

Parameters:
Name Type Description
element Element

the element to check

name string

the class name

Returns: boolean

true if the element has the class

Type
boolean

.identity(value)

Returns the provided value. This functions is used as a default predicate function.

Parameters:
Name Type Description
value *
Returns: *

the provided value

Type
*

.isEmpty(item)

Returns true if item is empty:

  • item is null or undefined
  • item is an array or string of length 0
  • item is an object with no keys
Parameters:
Name Type Description
item
Returns: boolean

true if item is empty

Type
boolean

.isFunction(value)

Checks if value is classified as a Function object.

Parameters:
Name Type Description
value *

The value to check.

Returns: boolean

Returns true if value is correctly classified, else false.

Type
boolean
Example
function Foo(){};  
isFunction(Foo);
// => true

isFunction(/abc/);
// => false

.isNumberLike(value)

Return true is value is a number or a string representation of a number.

Parameters:
Name Type Description
value *
Returns: boolean

true if value is a number

Type
boolean
Example
Util.isNumber(0) // true
   Util.isNumber("1.3") // true
   Util.isNumber("") // false
   Util.isNumber(undefined) // false

.isString(item)

Returns true if item is a string

Parameters:
Name Type Description
item
Returns: boolean

true if item is a string

Type
boolean

.merge(destination, sources)

Recursively assign source properties to destination

Parameters:
Name Type Argument Description
destination Object

the object to assign to

sources Object <optional>
<repeatable>

The source objects.


.removeAttribute(element, name)

Remove an attribute in the DOM element.

Parameters:
Name Type Description
element Element

the element to set the attribute for

name string

the name of the attribute


.setAttribute(element, name, value)

Set attribute in the DOM element.

Parameters:
Name Type Description
element Element

the element to set the attribute for

name string

the name of the attribute

value *

the value to be set


.setAttributes(element, attributes)

Set a group of attributes to the element

Parameters:
Name Type Description
element Element

the element to set the attributes for

attributes Object

a hash of attribute names and values


.setData(element, name, value)

Set data in the DOM element.

This method will use jQuery's data() method if it is available, otherwise it will set the data- attribute

Parameters:
Name Type Description
element Element

the element to set the data in

name string

the name of the data item

value *

the value to be set


.smartEscape(string, unsafe)

Escape all characters matching unsafe in the given string

Parameters:
Name Type Description
string string

source string to escape

unsafe RegExp

characters that must be escaped

Returns: string

escaped string

Type
string

.snakeCase(string)

Convert string to snake_case

Parameters:
Name Type Description
string string

the string to convert

Returns: string

in snake_case format

Type
string

.trim(text)

Remove leading or trailing spaces from text

Parameters:
Name Type Description
text string
Returns: string

the text without leading or trailing spaces

Type
string

.withCamelCaseKeys(value)

Create a copy of the source object with all keys in camelCase

Parameters:
Name Type Description
value Object

the object to copy

Returns: Object

a new object

Type
Object

.without(array, item)

Creates a new array without the given item.

Parameters:
Name Type Description
array Array

original array

item *

the item to exclude from the new array

Returns: Array

a new array made of the original array's items except for item

Type
Array

.withSnakeCaseKeys(value)

Create a copy of the source object with all keys in snake_case

Parameters:
Name Type Description
value Object

the object to copy

Returns: Object

a new object

Type
Object