utils-move-property

1.0.1 • Public • Published

Move Property

NPM version Build Status Coverage Status Dependencies

Move a property from one object to another object.

Installation

$ npm install utils-move-property

Usage

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 true

If the operation is successful, the function returns true; otherwise, false.

var bool = mv( obj1, 'c', obj2 );
// returns false

Notes

  • The property is deleted from the source object.

  • The property's descriptor is preserved during transfer.

  • A transfer is shallow.

    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, the function throws an Error, as the property cannot be deleted from the source object.

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.js

Tests

Unit

This repository uses tape for unit tests. To run the tests, execute the following command in the top-level application directory:

$ make test

All 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-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

Browser 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-browsers

To view the tests in a local web browser,

$ make view-browser-tests

License

MIT license.

Copyright

Copyright © 2015. Athan Reines.

Package Sidebar

Install

npm i utils-move-property

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • kgryte