utils-move-property v1.0.1
Move Property
Move a property from one object to another object.
Installation
$ npm install utils-move-propertyUsage
var mv = require( 'utils-move-property' );mv( source, prop, target )
Moves a property from one object to another object.
var obj1 = { 'a': 'b' };
var obj2 = {};
var bool = mv( obj1, 'a', obj2 );
// returns trueIf the operation is successful, the function returns true; otherwise, false.
var bool = mv( obj1, 'c', obj2 );
// returns falseNotes
- The property is deleted from the source
object. - The property's descriptor is preserved during transfer.
A transfer is __shallow__. ``` javascript var arr = [ 1, 2, 3 ]; var obj1 = { 'a': arr }; var obj2 = {}; var bool = mv( obj1, 'a', obj2 ); console.log( obj2.a === arr ); // returns true ```If a source property is not
configurable, thefunctionthrows anError, as the property cannot be deleted from the sourceobject.
Examples
var mv = require( 'utils-move-property' );
var obj1 = {
'beep': 'boop'
};
var obj2 = {
'foo': 'bar'
};
var bool = mv( obj1, 'beep', obj2 );
if ( bool === false ) {
console.log( 'failed to move property' );
}
console.dir( obj1 );
/*
{}
*/
console.dir( obj2 );
/*
{
'foo': 'bar',
'beep': 'boop'
}
*/To run the example code from the top-level application directory,
$ node ./examples/index.jsTests
Unit
This repository uses tape for unit tests. To run the tests, execute the following command in the top-level application directory:
$ make testAll new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-covIstanbul creates a ./reports/coverage directory. To access an HTML version of the report,
$ make view-covBrowser Support
This repository uses Testling for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:
$ make test-browsersTo view the tests in a local web browser,
$ make view-browser-testsLicense
Copyright
Copyright © 2015. Athan Reines.