README.md (2390B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Move Property 22 23 > Move a property from one object to another object. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var moveProperty = require( '@stdlib/utils/move-property' ); 31 ``` 32 33 #### moveProperty( source, prop, target ) 34 35 Moves a property from one `object` to another `object`. 36 37 ```javascript 38 var obj1 = { 39 'a': 'b' 40 }; 41 var obj2 = {}; 42 43 var bool = moveProperty( obj1, 'a', obj2 ); 44 // returns true 45 ``` 46 47 If the operation is successful, the function returns `true`; otherwise, `false`. 48 49 ```javascript 50 var obj1 = { 51 'a': 'b' 52 }; 53 var obj2 = {}; 54 55 var bool = moveProperty( obj1, 'c', obj2 ); 56 // returns false 57 ``` 58 59 </section> 60 61 <!-- /.usage --> 62 63 <section class="notes"> 64 65 ## Notes 66 67 - A transfer is **shallow**. 68 69 ```javascript 70 var arr = [ 1, 2, 3 ]; 71 72 var obj1 = { 73 'a': arr 74 }; 75 var obj2 = {}; 76 77 var bool = moveProperty( obj1, 'a', obj2 ); 78 console.log( obj2.a === arr ); 79 // => true 80 ``` 81 82 - The property is **deleted** from the _source_ `object`. 83 84 - The property's descriptor **is** preserved during transfer. 85 86 - If a _source_ property is **not** `configurable`, the function throws an `Error`, as the property **cannot** be deleted from the _source_ `object`. 87 88 </section> 89 90 <!-- /.notes --> 91 92 <section class="examples"> 93 94 ## Examples 95 96 <!-- eslint no-undef: "error" --> 97 98 ```javascript 99 var moveProperty = require( '@stdlib/utils/move-property' ); 100 101 var obj1 = { 102 'beep': 'boop' 103 }; 104 105 var obj2 = { 106 'foo': 'bar' 107 }; 108 109 var bool = moveProperty( obj1, 'beep', obj2 ); 110 if ( bool === false ) { 111 console.log( 'failed to move property' ); 112 } 113 console.dir( obj1 ); 114 /* => 115 {} 116 */ 117 console.dir( obj2 ); 118 /* => 119 { 120 'foo': 'bar', 121 'beep': 'boop' 122 } 123 */ 124 ``` 125 126 </section> 127 128 <!-- /.examples --> 129 130 <section class="links"> 131 132 </section> 133 134 <!-- /.links -->