Helpers

Object

Object.extend, Object.toQueryString

String

capitalize, include, camelize, classize, ends_with, strip

Array

from, include

Function

bind, params

The Helpers plugin provides helper functions for JavaScript's core objects. Some noteworthy items about this plugin:

  1. With the exception of Object, these functions are added to their class' prototype.
  2. When MVC.no_conflict() is called, the functions are not added to their class' prototype to avoid conflicts, but are available through as class methods.
  3. When Prototype or jQuery are included before this plugin, their object, string, array, and function helpers take precendence over this plugin's helpers. The API is the same, but the underlying functions are from the external library, if a corresponding match exists.

Object

Object.extend

MVC.Object.extend(dest, src) -> Object

Copies all properties from the source to the destination object.

- The destination object to which the properties of the source will be copied.
- The source object from which the properties of the source will be copied.

Object.to_query_string

MVC.Object.to_query_string(obj) -> String

Turns an object into its URL-encoded query string representation.

- An object (can include nested objects) of key-value pairs to be converted to a URL string.
MVC.Object.to_query_string({one: 'two', two: 'three', object: {hello: 'world'}});
// 'one=two&two=three&object%5Bhello%5D=world'

String

capitalize

String.capitalize() -> String

Capitalizes the first letter of a string and downcases all the others.

String.capitalize('yes');
// 'Yes'

include

String.include(substring) -> Boolean

Check if the string contains a substring.

String.include('JavaScript','Script');
// true

camelize

String.camelize() -> String

Converts a string separated by underscores into a camelCase equivalent.

String.camelize('one_two');
// 'oneTwo'

classize

String.classize() -> String

Checks if the string ends with substring.

String.classize('one_two');
// 'oneTwo'

ends_with

String.ends_with(substring) -> Boolean

Converts a string separated by underscores into a camelCase equivalent.

String.ends_with('JavaScript','ipt');
// true

strip

String.strip() -> String

Strips all leading and trailing whitespace from a string.

String.camelize(' word  ');
// 'word'

Array

from

Array.from(iterable) -> Array

Clones an existing array or creates a new one from an array-like collection.

Array.from([1,2]);
// [1,2]

include

Array.include(iterable, checked_item) -> Boolean

Check if the array contains an item.

Array.include([1,2,3], 2);
// true

Function

bind

Function.bind(some_function,thisObj[, arg...]) -> Function

Wraps the function in another, locking its execution scope to an object specified by thisObj. Works the same as the PrototypeJS method, which is described in more detail in their documentation.

function(){
   this.assert(true);
}.bind(this);

params

Function.params(some_function) -> Boolean

Returns the parameter names for the given function in an array.

Function.params(function( one, two ,three){ return 'yes'});
// ['one','two','three']