Stapes.util
Introduction
Until Stapes 0.5.1 the
library included a utility module under Stapes.util. This was removed
in version 0.6. For applications that depend on this utility module this
plugin can be used. It's pretty simple, just include the file after the script tag for Stapes.
Downloads
Use with Node and Require.js
Here are some instructions if you want to use this plugin with
Node or Require.js
With Node.js
var Stapes = require('stapes');
Stapes.util = require('stapes-util');
With Require.js
define(['stapes', 'stapes-util'], function(Stapes, util) {
Stapes.util = util;
});
Another option would be to make a 'dummy' module for Require and
use that:
// filename: stapes.js
define(['lib/stapes', 'lib/stapes-util'], function(Stapes, util) {
Stapes.util = util;
return Stapes;
});
// Some other file
define(['stapes'], function(Stapes) {
// proceed as usual...
});
bind
Stapes.util.bind( function, context )
Stapes.util.bind( object[, context] )
Binds the this variable inside function
to context.
Uses the native Ecmascript 5
bind if available.
When giving an object as the first argument all
methods (functions in the object) are binded to that object, akin
to the _.bindAll method of Underscore.js.
It's also possible to bind multiple methods to another context, simply
specify the context parameter, just as you would with
normal use of bind.
clone
Stapes.util.clone( object )
Returns a shallow copy of an object or array with copies
of the items instead of references.
create
Stapes.util.create( context )
Creates a new Object using the 'real prototypal inheritance pattern' made famous by Douglas Crockford. Uses Object.create if available.
each
Stapes.util.each( list, function [,context] )
Runs function on all items in list (which can be an array or an object). By using the optional third context parameter you can set the value of this inside of the function.
each doesn't return any values.
var singers = ['Johnny', 'June', 'Emmylou'];
Stapes.util.each(singers, function(singer) {
console.log(singer);
});
filter
Stapes.util.filter( list, function [, context]
Comparable to Stapes.util.each. Runs over all items in the list and returns an array of all the items that pass the test defined in function.
var singers = ['Johnny', 'June', 'Emmylou'];
var girls = Stapes.util.filter(singers, function(singer) {
return singer !== "Johnny";
});
console.log(girls); // ['June', 'Emmylou']
isArray
Stapes.util.isArray( value )
Unfortunately Javascript's native typeof doesn't return the correct type of an Array (it returns 'object' instead). This util fixes that.
var ar = [],
obj = {};
console.log( Stapes.util.isArray( ar ) ); // 'true'
console.log( Stapes.util.isArray( obj ) ); // 'false'
isObject
Stapes.util.isObject( value )
Comparable to Stapes.util.inArray, but returns true if a value is an object.
keys
Stapes.util.keys( object )
Returns all keys of an object as an array.
var obj = {"one" : 1, "two" : 2, "three" : 3};
console.log( Stapes.util.keys( obj ) ); // ["one", "two", "three"]
makeUuid
Stapes.util.makeUuid()
Returns a random uniqe 128-bit id of the version 4 (random) type.
map
Stapes.util.map( list, function [, context] )
Comparable to Stapes.util.each but returns an array of transformed items.
var values = ["Johnny", "Emmylou", "June"];
var newValues = util.map(values, function(value) {
return "Hi " + value + "!";
});
console.log(newValues); ["Hi Johnny!", "Hi Emmylou!", "Hi June!"];
size
Stapes.util.size( list )
Returns the number of items in an object or array.
var values = { "one" : 1, "two" : 2, "three" : 3"};
console.log( Stapes.util.size( values ) ); // '3'
toArray
Stapes.util.toArray( value )
Converts Array-like values (like the arguments variable in a function or a DOM Node collection) to real arrays. Calling this function with an object as the argument is the same as calling Stapes.util.values.
typeOf
Stapes.util.typeOf( value )
A better typeof operator that returns the correct type of a value.
console.log( Stapes.util.typeOf( {} ) ); // 'object'
console.log( Stapes.util.typeOf( [] ) ); // 'array'
console.log( Stapes.util.typeOf( 1 ) ); // 'number'
console.log( Stapes.util.typeOf( '' ) ); // 'string'
console.log( Stapes.util.typeOf( null ) ); // 'null'
console.log( Stapes.util.typeOf( undefined ) ); // 'undefined'
values
Stapes.util.values( object )
Returns all values of an object as an array.
var obj = {"one" : 1, "two" : 2, "three" : 3};
console.log( Stapes.util.values( obj ) ); // [1, 2, 3]