Skip to content

Commit 6f538a3

Browse files
Neerajpathak07kgryte
authored andcommitted
feat: add object/move-property
Ref: #8755
1 parent c8d88a1 commit 6f538a3

File tree

10 files changed

+765
-0
lines changed

10 files changed

+765
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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/object/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/object/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 for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
131+
132+
<section class="related">
133+
134+
</section>
135+
136+
<!-- /.related -->
137+
138+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
139+
140+
<section class="links">
141+
142+
</section>
143+
144+
<!-- /.links -->
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var moveProperty = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var obj1;
33+
var obj2;
34+
var out;
35+
var i;
36+
37+
obj1 = {
38+
'A': 'beep',
39+
'B': 'boop'
40+
};
41+
obj2 = {
42+
'foo': 'bar',
43+
'c': randu()
44+
};
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
obj2.c = randu();
48+
out = moveProperty( obj2, 'foo', obj1 );
49+
if ( typeof out !== 'boolean' ) {
50+
b.fail( 'should return an boolean' );
51+
}
52+
out = moveProperty( obj1, 'foo', obj2 );
53+
}
54+
b.toc();
55+
if ( typeof out !== 'boolean' ) {
56+
b.fail( 'should return an boolean' );
57+
}
58+
if ( typeof obj2.foo !== 'string' ) {
59+
b.fail( 'should move property back to source object' );
60+
}
61+
if ( obj1.foo ) {
62+
b.fail( 'should delete property' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( source, prop, target )
3+
Moves a property from one object to another object.
4+
5+
The property is deleted from the source object and the property's descriptor
6+
is preserved during transfer.
7+
8+
If a source property is not configurable, the function throws an error, as
9+
the property cannot be deleted from the source object.
10+
11+
Parameters
12+
----------
13+
source: Object
14+
Source object.
15+
16+
prop: string
17+
Property to move.
18+
19+
target: Object
20+
Target object.
21+
22+
Returns
23+
-------
24+
bool: boolean
25+
Boolean indicating whether operation was successful.
26+
27+
Examples
28+
--------
29+
> var obj1 = { 'a': 'b' };
30+
> var obj2 = {};
31+
> var bool = {{alias}}( obj1, 'a', obj2 )
32+
true
33+
> bool = {{alias}}( obj1, 'c', obj2 )
34+
false
35+
36+
See Also
37+
--------
38+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Moves a property from one object to another object.
23+
*
24+
* ## Notes
25+
*
26+
* - The property is deleted from the source object and the property's descriptor is preserved during transfer.
27+
* - If a source property is not configurable, the function throws an error, as the property cannot be deleted from the source object.
28+
*
29+
* @param source - source object
30+
* @param prop - property to move
31+
* @param target - target object
32+
* @returns boolean indicating whether operation was successful
33+
*
34+
* @example
35+
* var obj1 = { 'a': 'b' };
36+
* var obj2 = {};
37+
*
38+
* var bool = moveProperty( obj1, 'a', obj2 );
39+
* // returns true
40+
*
41+
* @example
42+
* var obj1 = { 'a': 'b' };
43+
* var obj2 = {};
44+
*
45+
* var bool = moveProperty( obj1, 'c', obj2 );
46+
* // returns false
47+
*/
48+
declare function moveProperty( source: any, prop: string, target: any ): boolean;
49+
50+
51+
// EXPORTS //
52+
53+
export = moveProperty;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 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+
import moveProperty = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
moveProperty( { 'a': 'b' }, 'a', {} ); // $ExpectType boolean
27+
moveProperty( { 'a': 'b' }, 'a', {} ); // $ExpectType boolean
28+
}
29+
30+
// The compiler throws an error if the function is not provided a string as its second argument...
31+
{
32+
moveProperty( { 'a': 'b' }, true, {} ); // $ExpectError
33+
moveProperty( { 'a': 'b' }, 3.12, {} ); // $ExpectError
34+
moveProperty( { 'a': 'b' }, false, {} ); // $ExpectError
35+
moveProperty( { 'a': 'b' }, ( x: number ): number => x, {} ); // $ExpectError
36+
moveProperty( { 'a': 'b' }, [], {} ); // $ExpectError
37+
moveProperty( { 'a': 'b' }, {}, {} ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the function is provided an incorrect number of arguments...
41+
{
42+
moveProperty(); // $ExpectError
43+
moveProperty( { 'a': 'b' } ); // $ExpectError
44+
moveProperty( { 'a': 'b' }, 'a' ); // $ExpectError
45+
moveProperty( { 'a': 'b' }, 'a', {}, 3.12 ); // $ExpectError
46+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
var moveProperty = require( './../lib' );
22+
23+
var obj1 = {
24+
'beep': 'boop'
25+
};
26+
27+
var obj2 = {
28+
'foo': 'bar'
29+
};
30+
31+
var bool = moveProperty( obj1, 'beep', obj2 );
32+
if ( bool === false ) {
33+
console.log( 'failed to move property' );
34+
}
35+
console.dir( obj1 );
36+
/* =>
37+
{}
38+
*/
39+
console.dir( obj2 );
40+
/* =>
41+
{
42+
'foo': 'bar',
43+
'beep': 'boop'
44+
}
45+
*/

0 commit comments

Comments
 (0)