String

RightJS extends the native String class with some additional powerful methods to make your life easier.

DOM Shortcuts

Most of the Element and Input methods are available as shortcuts from strings, for example

"div.something".addClass('marked');
"div#something".highlight();

In both cases it is the same as those ones

$$("div.something").each(function(element) {
element.addClass('marked');
});

$$("div#something").each('highlight');

BUT: data retrieval methods, like Element#get or Element#children will return the result of call on the first matching element

"div#some-id".get('id'); // -> 'some-id'
"div.some-class".getClass(); // -> 'some-class'

Which is the same as calling

$$("div#some-id").first().get('id');
$$("div.some-class").first().getClass();

If you need results of all the method calls from all the elements, please use the standard {$$} and Array#map methods combination, for example

$$("div.some-class").map("getClass");

UJS Shortcuts

Methods like Element#on, Element#stopObserving, etc when called from strings will not attach the event handlers directly to the elements, but use will UJS listeners instead.

"div.something".onClick('toggleClass', 'clicked');

It is same as calling

$(document).delegate('click', 'div.something', 'toggleClass', 'clicked');

If you really need to attach event listeners directly to every matching element, please use {$$} and Array#each calls as it shown above.

Methods

include, blank, camelize, capitalize, dasherize, empty, endsWith, evalScripts, extractScripts, includes, on, startsWith, stripScripts, stripTags, toFloat, toInt, trim, underscored

top ↑include

String.include(Object new_methods[, Boolean dont_overwrite) -> String

Description

Registers new functionality for String instances

If the second argument is true, then the method will skip already existing methods

Example

String.include({
double: function() {
return this + ' ' + this;
}
});

'boo'.double(); // -> 'boo boo'

top ↑blank

blank() -> boolean

Description

Checks if the string is empty or contains whitespaces only.

Example

''.blank(); // -> true
' '.blank(); // -> true
"\n".blank(); // -> true
' a'.blank(); // -> false

top ↑camelize

camelize() -> String new

Description

Converts the string from underscored or dashed to camel case.

Example

'foo_bar'.camelize(); // -> 'fooBar'
'foo-bar'.camelize(); // -> 'fooBar'

top ↑capitalize

capitalize() -> String new

Description

Creates a capitalized version of the string.

NOTE: This method supports most of the UTF-8 symbols.

Example

'boo boo boo'.capitalize(); // -> 'Boo boo boo'
'йокарный бабай'.capitalize(); // -> 'Йокарный бабай'

top ↑dasherize

dasherize() -> String new

Description

Creates a dashed version of the string

Example

'fooBar'.dasherize(); // -> 'foo-bar'
'foo_bar'.dasherize(); // -> 'foo-bar'

top ↑empty

empty() -> boolean

Description

Checks if the string is empty.

Example

''.empty(); // -> true
' '.empty(); // -> false
'a'.empty(); // -> false

top ↑endsWith

endsWith(String token[, boolean ignorecase]) -> boolean

Description

Checks if the string ends with the given substring.

Example

'image.gif'.endsWith('.gif'); // -> true

'image.gif'.endsWith('.GIF'); // -> false
'image.gif'.endsWith('.GIF', true); // -> true

top ↑evalScripts

evalScripts() -> String self

Description

Evals scripts in the string.

NOTE: This method will not remove the scripts from the string.

Example

var string = 'asdf <script>alert(1);</script>';

string.evalScripts(); // 'asdf <script>alert(1);</script>' and you'll see the alert

top ↑extractScripts

extractScripts() -> String scripts

Description

Extracts the bodies of every JavaScript tag in the string and returns them as a single string.

Example

var string = 'asdf <script>alert(1);</script>asdf<script>alert(2);</script>';

string.extractScripts(); // -> 'alert(1);alert(2);'

top ↑includes

includes(String token) -> boolean

Description

Checks if the string contains the given substring.

Example

'foo'.includes('bar'); // -> false
'foo'.includes('oo'); // -> true

top ↑on

on(String event, Function callback) -> void
on(String event, String callback[, arg, arg...]) -> void
on(Object events_hash) -> void

Description

Attaches a document level events delegation listener.

Example

// simple case
"div.red".on('click', function() {
alert("That's red")
});

// by name reference
"div.red".on('click', 'addClass', 'that-was-red');

// several event callbacks attachment
"div.red".on({
mouseover: function() { this.addClas('that-was-red')},
mouseout: ['removeClass', 'that-was-red'],
click: 'hide'
});

top ↑startsWith

startsWith(String token[, boolean ignorecase]) -> boolean

Description

Checks if the string starts with the given substring.

Example

'onSomething'.startsWith('on'); // -> true

'onSomething'.startsWith('onsome'); // -> false
'onSomething'.startsWith('onsome', true); // -> true

top ↑stripScripts

stripScripts(); -> String new
stripScripts(true); -> String new
stripScripts(Function receiver); -> String new

Description

Strips all JavaScript tags from the string.

This method takes several possible arguments. If a boolean true is passed then the scripts will be evaluated after extraction. If a function is passed every script body will be tossed into the function.

Example

var string = 'asdf<script>alert('bla');</script>';

string.stripScripts(); // -> 'asdf'
string.stripScripts(true); // -> 'asdf' and you'll see the alert

string.stripScripts(function(script) {
// do something with the script in here
});

// will return 'asdf'

top ↑stripTags

stripTags(); -> String new

Description

Removes all tags from the string.

Example

'a <b>c</b> d'.stripTags(); // -> 'a c d'

top ↑toFloat

toFloat() -> Float

Description

Converts the string to a float number.

NOTE: Processes dashes and commas as decimal markers unless you pass the argument true.

Example

'123.45'.toFloat(); // -> 123.45
'123,45'.toFloat(); // -> 123.45
'123-45'.toFloat(); // -> 123.45

top ↑toInt

toInt([Integer base]) -> Integer

Description

Converts the string to an Integer number.

Example

'123'.toInt(); // -> 123

top ↑trim

trim() -> String new

Description

Removes whitespaces from the beginning and the end of the string.

Example

' asdf '.trim(); // -> 'asdf'

top ↑underscored

underscored() -> String new

Description

Converts the string from camel or dashed to underscored case.

Example

'fooBar'.underscored(); // -> 'foo_bar'
'foo-bar'.underscored(); // -> 'foo_bar'