time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

move_property.js (2006B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var defineProperty = require( './../../define-property' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Moves a property from one object to another object.
     30 *
     31 * @param {Object} source - source object
     32 * @param {string} prop - property to move
     33 * @param {Object} target - target object
     34 * @throws {TypeError} first argument must be an object
     35 * @throws {TypeError} third argument must be an object
     36 * @returns {boolean} boolean indicating whether operation was successful
     37 *
     38 * @example
     39 * var obj1 = { 'a': 'b' };
     40 * var obj2 = {};
     41 *
     42 * var bool = moveProperty( obj1, 'a', obj2 );
     43 * // returns true
     44 *
     45 * @example
     46 * var obj1 = { 'a': 'b' };
     47 * var obj2 = {};
     48 *
     49 * var bool = moveProperty( obj1, 'c', obj2 );
     50 * // returns false
     51 */
     52 function moveProperty( source, prop, target ) {
     53 	var desc;
     54 	if ( typeof source !== 'object' || source === null ) {
     55 		throw new TypeError( 'invalid argument. Source argument must be an object. Value: `' + source + '`.' );
     56 	}
     57 	if ( typeof target !== 'object' || target === null ) {
     58 		throw new TypeError( 'invalid argument. Target argument must be an object. Value: `' + target + '`.' );
     59 	}
     60 	// TODO: handle case where gOPD is not supported
     61 	desc = Object.getOwnPropertyDescriptor( source, prop );
     62 	if ( desc === void 0 ) {
     63 		return false;
     64 	}
     65 	delete source[ prop ];
     66 	defineProperty( target, prop, desc );
     67 	return true;
     68 }
     69 
     70 
     71 // EXPORTS //
     72 
     73 module.exports = moveProperty;