Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions lib/node_modules/@stdlib/object/move-property/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<!--

@license Apache-2.0

Copyright (c) 2018 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# Move Property

> Move a property from one object to another object.

<section class="usage">

## Usage

```javascript
var moveProperty = require( '@stdlib/object/move-property' );
```

#### moveProperty( source, prop, target )

Moves a property from one `object` to another `object`.

```javascript
var obj1 = {
'a': 'b'
};
var obj2 = {};

var bool = moveProperty( obj1, 'a', obj2 );
// returns true
```

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

```javascript
var obj1 = {
'a': 'b'
};
var obj2 = {};

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

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- A transfer is **shallow**.

```javascript
var arr = [ 1, 2, 3 ];

var obj1 = {
'a': arr
};
var obj2 = {};

var bool = moveProperty( obj1, 'a', obj2 );
console.log( obj2.a === arr );
// => true
```

- The property is **deleted** from the _source_ `object`.

- The property's descriptor **is** preserved during transfer.

- If a _source_ property is **not** `configurable`, the function throws an `Error`, as the property **cannot** be deleted from the _source_ `object`.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var moveProperty = require( '@stdlib/object/move-property' );

var obj1 = {
'beep': 'boop'
};

var obj2 = {
'foo': 'bar'
};

var bool = moveProperty( obj1, 'beep', obj2 );
if ( bool === false ) {
console.log( 'failed to move property' );
}
console.dir( obj1 );
/* =>
{}
*/
console.dir( obj2 );
/* =>
{
'foo': 'bar',
'beep': 'boop'
}
*/
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var pkg = require( './../package.json' ).name;
var moveProperty = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var obj1;
var obj2;
var out;
var i;

obj1 = {
'A': 'beep',
'B': 'boop'
};
obj2 = {
'foo': 'bar',
'c': randu()
};
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
obj2.c = randu();
out = moveProperty( obj2, 'foo', obj1 );
if ( typeof out !== 'boolean' ) {
b.fail( 'should return an boolean' );
}
out = moveProperty( obj1, 'foo', obj2 );
}
b.toc();
if ( typeof out !== 'boolean' ) {
b.fail( 'should return an boolean' );
}
if ( typeof obj2.foo !== 'string' ) {
b.fail( 'should move property back to source object' );
}
if ( obj1.foo ) {
b.fail( 'should delete property' );
}
b.pass( 'benchmark finished' );
b.end();
});
38 changes: 38 additions & 0 deletions lib/node_modules/@stdlib/object/move-property/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

{{alias}}( source, prop, target )
Moves a property from one object to another object.

The property is deleted from the source object and the property's descriptor
is preserved during transfer.

If a source property is not configurable, the function throws an error, as
the property cannot be deleted from the source object.

Parameters
----------
source: Object
Source object.

prop: string
Property to move.

target: Object
Target object.

Returns
-------
bool: boolean
Boolean indicating whether operation was successful.

Examples
--------
> var obj1 = { 'a': 'b' };
> var obj2 = {};
> var bool = {{alias}}( obj1, 'a', obj2 )
true
> bool = {{alias}}( obj1, 'c', obj2 )
false

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/**
* Moves a property from one object to another object.
*
* ## Notes
*
* - The property is deleted from the source object and the property's descriptor is preserved during transfer.
* - If a source property is not configurable, the function throws an error, as the property cannot be deleted from the source object.
*
* @param source - source object
* @param prop - property to move
* @param target - target object
* @returns boolean indicating whether operation was successful
*
* @example
* var obj1 = { 'a': 'b' };
* var obj2 = {};
*
* var bool = moveProperty( obj1, 'a', obj2 );
* // returns true
*
* @example
* var obj1 = { 'a': 'b' };
* var obj2 = {};
*
* var bool = moveProperty( obj1, 'c', obj2 );
* // returns false
*/
declare function moveProperty( source: any, prop: string, target: any ): boolean;


// EXPORTS //

export = moveProperty;
46 changes: 46 additions & 0 deletions lib/node_modules/@stdlib/object/move-property/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import moveProperty = require( './index' );


// TESTS //

// The function returns a boolean...
{
moveProperty( { 'a': 'b' }, 'a', {} ); // $ExpectType boolean
moveProperty( { 'a': 'b' }, 'a', {} ); // $ExpectType boolean
}

// The compiler throws an error if the function is not provided a string as its second argument...
{
moveProperty( { 'a': 'b' }, true, {} ); // $ExpectError
moveProperty( { 'a': 'b' }, 3.12, {} ); // $ExpectError
moveProperty( { 'a': 'b' }, false, {} ); // $ExpectError
moveProperty( { 'a': 'b' }, ( x: number ): number => x, {} ); // $ExpectError
moveProperty( { 'a': 'b' }, [], {} ); // $ExpectError
moveProperty( { 'a': 'b' }, {}, {} ); // $ExpectError
}

// The compiler throws an error if the function is provided an incorrect number of arguments...
{
moveProperty(); // $ExpectError
moveProperty( { 'a': 'b' } ); // $ExpectError
moveProperty( { 'a': 'b' }, 'a' ); // $ExpectError
moveProperty( { 'a': 'b' }, 'a', {}, 3.12 ); // $ExpectError
}
45 changes: 45 additions & 0 deletions lib/node_modules/@stdlib/object/move-property/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var moveProperty = require( './../lib' );

var obj1 = {
'beep': 'boop'
};

var obj2 = {
'foo': 'bar'
};

var bool = moveProperty( obj1, 'beep', obj2 );
if ( bool === false ) {
console.log( 'failed to move property' );
}
console.dir( obj1 );
/* =>
{}
*/
console.dir( obj2 );
/* =>
{
'foo': 'bar',
'beep': 'boop'
}
*/
Loading