core.js

Collection of Array and String prototypes, type and equality checking methods, Promises and a powerful Class framework.

Explore Source on GitHub Download boost.min.js

Utilities

noop

void noop() Empty function.

uid

String uid([Integer n]) Creates an random n-digit string.

Arguments

run

Any run(fn, [args, _this]) If fn is a function, it will evaluate it with the provided arguments and this-value. Otherwise, run just returns fn. This is particularly useful for parsing parameters or configurations that could be both a dynamic function or a static value.

Arguments

Returns

Example

run('xy', 'a', 'b')             // -> 'xy'
run((a, b) => a + b, 'a', 'b')  // -> 'ab'

isOneOf

Boolean isOneOf(x, ...values) Checks if x is strictly equals to any one of values.

Arguments

Returns

Example

isOneOf(10, 20, 30)     // -> false
isOneOf('a', 'b', 'a')  // -> true

extend

extend(first: object, ...rest: object) => object

Adds all properties from the rest objects to the first objects. Later objects in rest will overwrite properties set in previous objects in rest, or the first one.

Arguments

Returns

*

Example

let x = { a: 1, b: 2 };
let y = { b: 3, c: 4 };
let z = { c: 5, d: 6 };
extend(x, y, z);
// x -> { a: 1, b: 3, c: 5, d: 6 }

clamp

Number clamp(Number x, Number min, Number max) Returns the number x bounded between min and max.

Arguments

Returns

Example

clamp(12, 10, 5)   // → 10

isBetween

Boolean isBetween(Number x, Number min, Number max)

performance

Number performance(Function fn, [Integer repetitions])

square

Number square(Number x)

cube

Number square(Number x)

defer

{promise, resolve, reject} = defer() Returns a deferred object that contains promise, resolve and reject properties.

Returns

Example

let deferred = defer()
deferred.promise.then(x => { console.log(x); });
deferred.resolve('foo')
  // -> logs 'foo'

has

Boolean has(Object obj, String key) Checks if obj has an own property key. This is simply a wrapper for JavaScript's native hasOwnProperty method.

each

(Object|Array) each((Object|Array) obj, Function fn) Runs fn(value, key) for all enumerable properties key in a collection obj and returns a new collection with the corresponding values returned by fn (like .map). Collections can be arrays or objects, but not strings or maps.

Examples

each([4, 5, 6], (v, k) => v + k);  // -> [4, 6, 8]
each({ a: 1, b: 2 }, (k, v) => v + k);  // -> { a: '1a', b: '2b' }

some

Any some((Object|Arrya) obj, Function fn)

cache

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

Arguments

Returns

Example

let slow = cache(function(x) {
    console.log('executing...');
    return Math.pow(x, x)
});

slow(100);   // logs 'executing...' and returns 1e+200
slow(100);   // returns 1e+200

throttle

Function throttle(Function fn, Number t) Function wrapper that prevents fn from being executed more than once every tms. This is particularly useful for optimising callbacks for continues events like scroll, resize or slider move.

Arguments

Returns

Example

let slow = cache(function(x) {
    el.height = innerHeight;
});

window.onresize = slow;

shallowCopy

Any shallowCopy(Any obj)

deepCopy

Any deepCopy(Any obj)

Types

typeof

String typeOf(Any obj) Returns the type of obj. Possible return values are number, string, boolean, array, object, date, function, NaN, null, undefined, as well as class names like htmldocument. Note that typeof caches all type checks for better performance.

isType

Boolean isType(Any obj, String type) Shortcut for typeOf(x) === type.

isString

isNumber

isInteger

isDate

isBoolean

isObject

Arrays

tabulate

Array tabulate(Any fn, Integer ...dimensions) If n integers x1 are specified, tabulate will create a multi-dimensional matrix that contains fn(i1, i2, ..., in) in cell [i1][i2]...[in]. If fn is not a function, every item in the resulting array will have value fn.

Examples

tabulate(2, 5)                        // -> [2, 2, 2, 2, 2]
tabulate(i => i * i, 5)               // -> [0, 1, 4, 9, 16]
tabulate((i, j) => '' + i + j, 2, 2)  // -> [['00', '01'], ['10', '11]]
tabulate(1, 3, 3);                    // -> [[1, 1, 1], [1, 1, 1], [1, 1, 1]]

list

Number[] list(Number a, [Number b, Number step])

Examples

list(5);  // -> [0, 1, 2, 3, 4]
list(-2, 2);  // -> [-2, -1, 0, 1, 2]
list(5, 15, 5);  // -> [5, 10, 15]

map

Any[] map(Function fn, Any ...args) Applies fn to multiple arrays of arguments and returns an array with the results of every operation. This function is particularly useful for vector operations.

Example

map(Math.min, [5, 4], [7, 3]);  // -> [min(5, 7), min(4, 3)] = [5, 3]

total

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

average

Number average(Number[] a)

extract

Array extract(Object[] a, String id)

zip

Object zip(Array keys, Array values)

sortBy

Array sortBy(Array a, String key, [Boolean reverse])

unique

Array unique(Array a)

clean

Array clean(Array a)

without

Array without(Array a)

flatten

Array flatten(Array a)

cumulative

Number[] cumulative(Number[] a)

chunk

Array[] chunk(Array a)

rotate

Array rotate(Array a)

intersect

Array intersect(Array a, Array b)

difference

Array difference(Array a, Array b)

Strings

words

String[] words(String str) Splits a string str into its space separated words.

Example

words(' Hello here   is text!');  // -> ['Hello', 'here', 'is', 'text!']

toTitleCase

String toTitleCase(String str) Converts a string str to title Case.

toCamelCase

String toCamelCase(String str) Converts a string str from-snake-case toCamelCase.

repeat

String repeat(String str, [Number n])

padStart

String padStart(String str, Number maxLength, [Str fill])

Evented

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

Constructor

Evented

new Evented({ [split], [lowercase] })

Arguments

Methods

.on

.on(String events, Function fn) Adds an event listener fn to one or more events.

.one

.one(String events, Function fn) Adds a one-time event listener fn to one or more events.

.off

.off(String events, Function fn) Removes an event listener fn from one or more events.

.trigger

.trigger(String events, Any ...args Triggers one or more events, and executes all bound event listeners with ...args as arguments.

Observable (beta)

Until all platforms support ES6 proxies, we will have to simulate their functionality using getters and setters, and a slightly more verbose syntax.

Constructor

observable

Object observable(Object model)

Arguments

Methods

.watch

.watch(String key, Function callback)

.unwatch

.unwatch(String key)