Utilities Plugin for the Sandbox API
Eric Bréchemier © 2011-2013, Some Rights Reserved Legal-Box SAS © 2010-2011, All Rights Reserved
BSD License http://creativecommons.org/licenses/BSD/
2013-09-10
lb. | Utilities Plugin for the Sandbox API |
Functions | |
utils(sandbox) | Define methods in the ‘utils’ property of given sandbox. |
sandbox. | Check whether given value is null or undefined |
sandbox. | Get a default value when given value is null or undefined |
sandbox. | Check whether an object property is present and not null nor undefined. |
sandbox. | Check the type of a value, possibly nested in sub-properties. |
sandbox. | Get current timestamp, in milliseconds. |
sandbox. | Plan the delayed execution of a callback function. |
sandbox. | Cancels the planned execution of a callback function. |
sandbox. | Remove leading and trailing whitespace from a string. |
sandbox. | Log a message. |
sandbox. | Open a confirmation (OK/Cancel) dialog. |
Check whether given value is null or undefined
The intent of this method is to replace unsafe tests relying on type coercion for optional arguments or object properties:
function onEvent(name,data){ if (!name || !data || !data.value){ // unsafe due to type coercion: all falsy values '', false, 0 // are discarded, not just null and undefined return; } // ... }
with a safer test without type coercion:
function onEvent(event,options){ if ( no(name) || no(data) || no(data.value) ){ // safe check: only null/undefined values are rejected; return; } // ... }
value | any, the value to check |
boolean, false when the value is null or undefined, true otherwise
Get a default value when given value is null or undefined
The intent of this method is to replace unsafe initialization of optional parameters to a default value relying on type coercion:
function on(event,options){ options = options || {}; // type coercion var isUser = options.isUser || false; // type coercion // ... }
with a safer initialization without type coercion:
function on(event,options){ options = or(options, {}); // no type coercion var isUser = or(options.isUser, false); // no type coercion // ... }
a | any, the value to check |
b | any, the default value |
any, the default value when the value is null or undefined, the value itself otherwise.
Check whether an object property is present and not null nor undefined.
A chain of nested properties may be checked by providing more than two arguments.
The intent of this method is to replace unsafe tests relying on type coercion for optional arguments or object properties:
function on(event,options){ options = options || {}; // type coercion if (!event || !event.data || !event.data.value){ // unsafe due to type coercion: all falsy values '', false, 0 // are discarded, not just null and undefined return; } // ... }
with a safer test without type coercion:
function on(event,options){ options = or(options, {}); // no type coercion if ( !has(event,'data','value') ){ // safe check: only null/undefined values are rejected; return; } // ... }
object | any, an object or any other value |
property | string, the name of the property to look up |
... | string, additional property names to check in turn |
Check the type of a value, possibly nested in sub-properties.
The method may be called with a single argument to check that the value is neither null nor undefined.
If more than two arguments are provided, the value is considered to be nested within a chain of properties starting with the first argument:
is(object,'parent','child','leaf','boolean')
will check whether the property object.parent.child.leaf exists and is a boolean.
The intent of this method is to replace unsafe guard conditions that rely on type coercion:
if (object && object.parent && object.parent.child) { // Issue: all falsy values are treated like null and undefined: // '', 0, false... }
with a safer check in a single call:
if ( is(object,'parent','child','number') ) { // only null and undefined values are rejected // and the type expected (here 'number') is explicit }
... | any, optional, a chain of parent properties for a nested value |
value | any, the value to check, which may be nested in a chain made of previous arguments (see above) |
type | string, optional, the type expected for the value. Alternatively, a constructor function may be provided to check whether the value is an instance of given constructor. |
’undefined’ | undefined |
’null’ | null |
’boolean’ | false, true |
’number’ | -1, 0, 1, 2, 3, Math.sqrt(2), Math.E, Math.PI... |
’string’ | ’’, ‘abc’, “Text!?”... |
’array’ | [], [1,2,3], [‘a’,{},3]... |
’object’ | {}, {question:’?’,answer:42}, {a:{b:{c:3}}}... |
’regexp’ | /abc/g, /[0-9a-z]+/i... |
’function’ | function(){}, Date, setTimeout... |
This method retrieves the internal class of the provided value using
Object.prototype.toString.call(value).slice(8, -1)
The class is then converted to lower case.
See “The Class of an Object” section in the JavaScript Garden for more details on the internal class: http://bonsaiden.github.com
The internal class is only guaranteed to be the same in all browsers for Core JavaScript classes defined in ECMAScript. It differs for classes part of the Browser Object Model (BOM) and Document Object Model (DOM): window, document, DOM nodes:
window | ’Object’ (IE), ‘Window’ (Firefox,Opera), ‘global’ (Chrome), ‘DOMWindow’ (Safari) |
document | ’Object’ (IE), ‘HTMLDocument’ (Firefox,Chrome,Safari,Opera) |
document.body | ’Object’ (IE), ‘HTMLBodyElement’ (Firefox,Chrome,Safari,Opera) |
document.createElement(‘div’) | ’Object’ (IE) ‘HTMLDivElement’ (Firefox,Chrome,Safari,Opera) |
document.createComment(‘’) | ’Object’ (IE), ‘Comment’ (Firefox,Chrome,Safari,Opera) |
Plan the delayed execution of a callback function.
callback | function, the function to run after a delay |
delay | integer, the delay in milliseconds |
number, the timeout identifier to be passed to utils.clearTimeout() to cancel the planned execution.