String

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

Methods

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

top ↑behave

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

Description

Attaches a document level events delegation listener

Example

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

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

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

top ↑blank

blank() -> boolean

Description

Checks if the string is empty or contains only spaces

Example

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

top ↑camelize

camelize() -> String new

Description

Converts a string from an underscored/dashed version into a camel cased one

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 ↑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 up 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 out of 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 bodies of every script 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 ↑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 the javascript tags out of 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 the tags out of 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 comas as decimal pinter unless you pass true as the parameter

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 trailing spaces at the beginning and the end of the string

Example

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

top ↑underscored

underscored() -> String new

Description

Converts a camel cased or dashed string into an underscored one

Example

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