lb.base.object

Object Adapter Module for Base Library

Authors

Copyright

Eric Bréchemier © 2011-2013, Some Rights Reserved Legal-Box SAS © 2010-2011, All Rights Reserved

License

BSD License http://creativecommons.org/licenses/BSD/

Version

2013-09-10

Summary
lb.base.objectObject Adapter Module for Base Library
Functions
has(object,property[,...]): booleanCheck whether an object property is present and not null nor undefined.
clone(object[,deep]): objectGet a shallow or a deep copy of an object.

Functions

has(object,property[,...]): boolean

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;
  }
  // ...
}

Parameters

objectany, an object or any other value
propertystring, the name of the property to look up
...string, additional property names to check in turn

Returns

  • false if no argument is provided or if the object is null or undefined, whatever the number of arguments
  • true if the full chain of nested properties is found in the object and the corresponding value is neither null nor undefined
  • false otherwise

clone(object[,deep]): object

Get a shallow or a deep copy of an object.

Parameter

objectobject, an object or array
deepboolean, optional, defaults to false, whether to make a deep copy (true) or a shallow copy (false)

Returns

  • a deep copy of given object, when deep is true,
  • a shallow copy of given object, wheen deep is false.

Notes

In the case of a deep copy, there must be no cyclic references in the given object.

Close