Utility Functions

RightJS comes with a collection of utility functions for the most common JavaScript development operations. Most of them might be seen in other frameworks like Prototype or Mootools, in RightJS they have the same meaning and interfaces.

Methods

$, $$, $A, $E, $alias, $break, $eval, $ext, $w, defined, isArray, isElement, isFunction, isHash, isNode, isNumber, isString

top ↑$

$(String element_id) -> Element
$(Element element) -> Element

Description

Finds an element by its ID or wraps a raw dom element with a dom-wrapper

Example

$('some-id');
$(element);
$(window);
$(document);

top ↑$$

$$(String css_rule[, boolean raw]) -> Array of elements

Description

Finds all the elements on the page that match the given CSS rule. It supports all valid CSS3 selectors.

NOTE: if the second attribute is true then it will return a list of raw dom elements.

Example

$$('div > span.foo'); // -> [Element, ....]
$$('div > span.foo', true); // -> [HTMLElement, ...]

top ↑$A

$A(Iterable it) -> Array

Description

Converts any iterable object into an Array instance.

Example

function() {
var args = $A(arguments);
var first = args.shift();
};

top ↑$E

$E(String tag_name[, Object options]) -> Element new

Description

A shortcut for new Element

Example

var div = $E('div', {id: 'some-id'});

top ↑$alias

$alias(Object object, Object aliases) -> Object the first

Description

Creates the first object property aliases.

Example

var object = {
foo: function() {};
};

$alias(object, {
bar: 'foo'
});

// now 'foo' and 'bar' both refer to the same function

top ↑$break

$break() -> void

Description

Initiates a manual break in case of callback based iterations.

Example

[1,2,3,4].walk(function(value, i) {
if (i > 1) $break();
return i * 2;
});

// [2,4,3,4]

top ↑$eval

$eval(String code) -> void

Description

Evaluates the given piece of JavaScript code in the context of the current window.

Example

$eval('var foo = "bar";');

window['foo']; // -> 'bar'

top ↑$ext

$ext(Object first, Object second[, boolean dont_overwrite]) -> Object first

Description

Extends the first object with data from the second one.

If the third argument is true, it skips all the overlapping keys.

Example

var o = {1:1};
$ext(o, {2:2}); // -> {1:1, 2:2}

$ext(o, {2:4, 3:3}, true); // -> {1:1, 2:2, 3:3}

top ↑$w

$w(String string) -> Array of strings

Description

Splits the given string into a list of words (separated by spaces).

Example

$w('one two three'); // -> ['one', 'two', 'three']

top ↑defined

defined(mixed value) -> boolean check result

Description

Checks if the given value is not equal to undefined.

Example

var object = {
foo: 'foo'
};

defined(object.foo); // -> true
defined(object.bar); // -> false

top ↑isArray

isArray(mixed value) -> boolean check result

Description

Checks if the given value is an instance of Array.

Example

isArray({}); // -> false
isArray(''); // -> false
isArray([]); // -> true

top ↑isElement

isElement(mixed value) -> boolean check result

Description

Checks if the given value is an HTMLElement instance.

Example

isElement(document.createTextNode('boo')); // -> false
isElement(document.createElement('div')); // -> true

top ↑isFunction

isFunction(mixed value) -> boolean check result

Description

Checks if the given value is a Function.

Example

isFunction('boo'); // -> false
isFunction(function() {}); // -> true

top ↑isHash

isHash(mixed value) -> boolean check result

Description

Checks if the given value is a pure JavaScript object (a hash).

Example

isHash([1,2,3]); // -> false
isHash('foo bar'); // -> false
isHash({foo: 'bar'}); // -> true

top ↑isNode

isNode(mixed value) -> boolean check result

Description

Checks if the given value is a DOM node (both TextNode or HTMLElement).

Example

isNode(document.createTextNode('boo')); // -> true
isNode(document.createElement('div')); // -> true

isNode('foo'); // -> false

top ↑isNumber

isNumber(mixed value) -> boolean check result

Description

Checks if the given value is a Number.

Example

isNumber('1'); // -> false
isNumber(111); // -> true
isNumber(1.1); // -> true

top ↑isString

isString(mixed value) -> boolean check result

Description

Checks if the given value is a String.

Example

isString([]); // -> false
isString({}); // -> false
isString(''); // -> true