core.js

JavaScript utility function and classes, including array and string helper functions, type checking and event classes.

Arrays

tabulate(fn, ...dimensions) : Array

Creates a multi-dimensional array contains fn(i1, i2, ..., in) in cell [i1][i2]...[in]. If the first argument is not a function, every item in the resulting array will have that static value.

Kind: global function

Param Type
fn function | *
...dimensions number

list(a, b, step) : Array

Kind: global function

Param Type
a number
b number
step number

last(array, i) : *

Returns the last item in an array, or the ith item from the end.

Kind: global function

Param Type
array Array
i Number

map(fn, ...args) : Array

Maps a function over multiple arrays, the entries of which are passed as multiple arguments.

Kind: global function

Param Type
fn function
...args Array

total(array) : number

Finds the sum of all elements in an numeric array. This operation is safe, i.e. any values that can't be cast to a number are counted as 0.

Kind: global function

Param Type
array Array

average(array) : number

Evaluates the average of all the elements in an array.

Kind: global function

Param Type
array Array

contains(array, value) : boolean

Checks if an array contains a specific value.

Kind: global function

Param Type
array Array
value *

extract(array, id)

Kind: global function

Param
array
id

zip(keys, values) : Object

Kind: global function

Param
keys
values

sortBy(array, id, reverse) : Array

Kind: global function

Param Type
array Array
id string
reverse boolean

sortByFn(array, fn, reverse) : Array

Kind: global function

Param Type
array Array
fn function
reverse boolean

loop(array) : function

Returns a function that can be called repeatedly, and returns items of the array, continuously looping

Kind: global function

Param Type
array Array

unique(array) : function

Kind: global function

Param
array

clean(array) : Array

Removes any null or undefined values in array a.

Kind: global function

Param Type
array Array

without(array, x) : Array

Removes all occurrences of x from an array.

Kind: global function

Param Type
array Array
x *

flatten(array)

Kind: global function

Param
array

cumulative(array)

Kind: global function

Param
array

chunk(array, n) : Array

Breaks an array into chunks of size at most n.

Kind: global function

Param
array
n

rotate(array, offset)

Rotates the elements of an array by offset.

Kind: global function

Param
array
offset

intersect(a1, a2) : Array

Returns all elements that are in both a1 and a2.

Kind: global function

Param
a1
a2

difference(a1, a2) : Array

Returns all elements that are only in one of a1 and a2.

Kind: global function

Param
a1
a2

toLinkedList(array) : Array

Converts an array into a one-way linked list, with .val and .next properties.

Kind: global function

Param
array

Evented

Evented

Base class for event management. It is rarely used on its own, but many other classes inherit from Evented.

Kind: global class


evented.on(events, fn)

Adds an event listener for one or more events.

Kind: instance method of Evented

Param Type
events string
fn function

evented.one(events, fn)

Adds a one-time event listener to one or more events.

Kind: instance method of Evented

Param Type
events string
fn function

evented.off(events, fn)

Removes an event listener from one or more events.

Kind: instance method of Evented

Param Type
events string
fn function

evented.trigger(events, ...args)

Triggers one or more events, and executes all bound event listeners with the given arguments.

Kind: instance method of Evented

Param Type
events string
...args *

Strings

words(str) : Array.<string>

Splits a string into space separated words.

Kind: global function

Param Type
str string

toTitleCase(str) : string

Converts a string to title case.

Kind: global function

Param Type
str string

toCamelCase(str) : string

Converts a string to camel case.

Kind: global function

Param Type
str string

repeat(str, n) : string

Repeats a string n times.

Kind: global function

Param Type
str string
n number

isPalindrome(str) : boolean

Checks if a string is a palindrome.

Kind: global function

Param Type
str string

stringDistance(s1, s2) : number

Determines the Levenshtein distance between two strings.

Kind: global function

Param Type
s1 string
s2 string

autocorrect(word, dict) : string | null

Tries to autocorrect a word from a dictionary.

Kind: global function

Param Type
word string
dict Array.<string>

Types

typeOf(obj) : string

Returns the type of an object. Possible values are 'number', 'string', 'boolean', 'array', 'object', 'date', 'function', 'NaN', 'null', 'undefined', as well as class names like 'htmldocument'.

Kind: global function

Param Type
obj *

isType(x, type) : boolean

Checks if an object has a specific type.

Kind: global function

Param Type
x *
type string

Utilities

noop()

Empty Function.

Kind: global function


uid(n) : string

Creates a random UID string of a given length.

Kind: global function

Param Type
n number

run(obj, ...args) : *

If obj is a function, it evaluates it with a given set of attributes. Otherwise it just returns obj directly.

Kind: global function

Param Type
obj function | *
...args *

isOneOf(x, ...values) : boolean

Checks if x is strictly equal to any one of the following arguments

Kind: global function

Param Type
x *
...values *

extend(first, ...others)

Merges multiple objects into the first one.

Kind: global function

Param Type
first Object
...others Object

deepExtend(obj1, obj2)

Deep extends obj1 using th

Kind: global function

Param
obj1
obj2

clamp(x, min, max) : number

Bounds a number between a lower and an upper limit.

Kind: global function

Param Type
x number
min number
max number

performance(fn, n) : number

Checks the average speed of a function by running it n times.

Kind: global function

Param Type
fn function
n number

square(x) : number

Squares a number.

Kind: global function

Param Type
x number

cube(x) : number

Cubes a number.

Kind: global function

Param Type
x number

delay(fn, t) : number

Replacement for setTimeout() that is synchronous for time 0.

Kind: global function

Param Type
fn function
t number

wait(t) : Promise

Returns a promise that resolves after a fixed time.

Kind: global function

Param Type
t number

defer() : Object

Creates a new promise together with functions to resolve or reject.

Kind: global function


has(obj, key) : boolean

Checks if an object contains a property.

Kind: global function

Param Type
obj Object
key number

each(collection, fn) : Object

Kind: global function

Param Type
collection Object
fn function

some(collection, fn) : *

Kind: global function

Param Type
collection Object
fn function

cache(fn) : function

Function wrapper that modifies a function to cache its return values. This is useful for performance intensive functions which are called repeatedly with the same arguments. However it can reduce performance for functions which are always called with different arguments. Note that argument comparison doesn't not work with Objects or nested arrays.

Kind: global function

Param Type
fn function

throttle(fn, t) : function

Function wrapper that prevents a function from being executed more than once every t ms. This is particularly useful for optimising callbacks for continues events like scroll, resize or slider move.

Kind: global function

Param Type
fn function
t number

shallowCopy(obj) : *

Shallow copies any JavaScript object.

Kind: global function

Param Type
obj *

deepCopy(obj) : *

Deep copies any JavaScript object.

Kind: global function

Param Type
obj *