Socket
Socket
Sign inDemoInstall

copy-props

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

copy-props - npm Package Compare versions

Comparing version 2.0.3 to 2.0.4

23

package.json
{
"name": "copy-props",
"version": "2.0.3",
"version": "2.0.4",
"description": "Copy properties deeply between two objects.",

@@ -12,7 +12,8 @@ "main": "index.js",

"test": "mocha",
"coverage": "istanbul cover _mocha",
"coveralls": "istanbul cover _mocha && istanbul-coveralls",
"web:install": "npm install --no-save phantomjs-prebuilt mocha-phantomjs",
"web:build": "browserify index.js --standalone copyProps | uglifyjs --compress --mangle -o web/copy-props.js && node test/web/make.js",
"web:test": "mocha-phantomjs -p node_modules/.bin/phantomjs test/web/copy-props.test.html"
"coverage": "nyc --reporter=lcov --reporter=text-summary npm test",
"coveralls": "nyc --reporter=text-lcov npm test | coveralls",
"web:build": "browserify index.js --standalone copyProps -o web/copy-props.js && cd web && uglifyjs copy-props.js --compress --mangle -o copy-props.min.js --source-map url=copy-props.min.js.map",
"chrome:install": "npm i --no-save mocha-chrome",
"chrome:test": "mocha-chrome test/web/browser-test.html",
"build": "npm run lint && npm run coverage && npm run web:build && node test/web/make.js"
},

@@ -42,10 +43,10 @@ "repository": {

"devDependencies": {
"browserify": "^14.1.0",
"browserify": "^16.2.2",
"chai": "^3.5.0",
"eslint": "^3.16.1",
"istanbul": "^0.4.5",
"istanbul-coveralls": "^1.0.3",
"coveralls": "^3.0.1",
"eslint": "^4.19.1",
"mocha": "^3.2.0",
"uglify-js": "^2.8.1"
"nyc": "^11.7.2",
"uglify-js": "^3.3.24"
}
}

@@ -1,135 +0,141 @@

[copy-props][repo-url] [![NPM][npm-img]][npm-url] [![MIT License][mit-img]][mit-url] [![Build Status][travis-img]][travis-url] [![Build Status][appveyor-img]][appveyor-url] [![Coverage Status][coverage-img]][coverage-url]
============
# [copy-props][repo-url] [![NPM][npm-img]][npm-url] [![MIT License][mit-img]][mit-url] [![Build Status][travis-img]][travis-url] [![Build Status][appveyor-img]][appveyor-url] [![Coverage Status][coverage-img]][coverage-url]
Copy properties deeply between two objects.
Copy properties between two objects deeply.
Install
-------
## Install
```
To install from npm:
```sh
$ npm i copy-props --save
```
Usage
-----
## Load this module
* Load this module :
For Node.js:
```js
const copyProps = require('copy-props');
```
```js
const copyProps = require('copy-props');
```
* Copy *src* to *dst* simply (and return *dst*) :
For Web browser:
```js
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc' };
var dst = { a: 2, b: { b1: 'xxx', b2: 'yyy' } };
```html
<script src="copy-props.min.js"></script>
```
copyProps(src, dst);
// => { a: 1, b: { b1: 'bbb', b2: 'yyy' }, c: 'ccc' }
```
## Usage
* Copy *src* to *dst* with property mapping (and return *dst*) :
Copy *src* to *dst* simply (and return *dst*) :
```js
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc', d: 'ddd' };
var dst = { f: { a: 2, b1: 'xxx', b2: 'yyy' }, e: 'zzz' };
```js
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc' };
var dst = { a: 2, b: { b1: 'xxx', b2: 'yyy' } };
copyProps(src, dst, {
a: 'f.a',
'b.b1': 'f.b1',
'b.b2': 'f.b2',
'c': 'f.c',
});
// => { f: { a: 1, b1: 'bbb', b2: 'yyy', c: 'ccc' }, e: 'zzz' }
```
copyProps(src, dst);
// => { a: 1, b: { b1: 'bbb', b2: 'yyy' }, c: 'ccc' }
```
* Copy *src* to *dst* with convert function (and return *dst*) :
Copy *src* to *dst* with property mapping (and return *dst*) :
```js
var src = { a: 1, b: { b1: 'bbb' } };
var dst = { a: 0 };
```js
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc', d: 'ddd' };
var dst = { f: { a: 2, b1: 'xxx', b2: 'yyy' }, e: 'zzz' };
copyProps(src, dst, function(srcInfo) {
if (srcInfo.keyChain === 'a') {
return srcInfo.value * 2;
}
if (srcInfo.keyChain === 'b.b1') {
return srcInfo.value.toUpperCase();
}
});
// => { a: 2, b: { b1: 'BBB' } }
```
copyProps(src, dst, {
a: 'f.a',
'b.b1': 'f.b1',
'b.b2': 'f.b2',
'c': 'f.c',
});
// => { f: { a: 1, b1: 'bbb', b2: 'yyy', c: 'ccc' }, e: 'zzz' }
```
* Can use an array instead of a map as property mapping :
Copy *src* to *dst* with convert function (and return *dst*) :
```js
var src = { a: 1, b: { c: 'CCC' }, d: { e: 'EEE' } };
var dst = { a: 9, b: { c: 'xxx' }, d: { e: 'yyy' } };
var fromto = [ 'b.c', 'd.e' ];
copyProps(src, dst, fromto);
// => { a: 9, b: { c: 'CCC' }, d: { e: 'EEE' } }
```
```js
var src = { a: 1, b: { b1: 'bbb' } };
var dst = { a: 0 };
* Can copy reversively (from *dst* to *src*) by reverse flag (and return *src*):
copyProps(src, dst, function(srcInfo) {
if (srcInfo.keyChain === 'a') {
return srcInfo.value * 2;
}
if (srcInfo.keyChain === 'b.b1') {
return srcInfo.value.toUpperCase();
}
});
// => { a: 2, b: { b1: 'BBB' } }
```
```js
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc' };
var dst = { a: 2, b: { b1: 'xxx', b2: 'yyy' } };
Can use an array instead of a map as property mapping :
copyProps(src, dst, true);
// => { a: 2, b: { b1: 'xxx', b2: 'yyy' }, c: 'ccc' }
```
```js
var src = { a: 1, b: { c: 'CCC' }, d: { e: 'EEE' } };
var dst = { a: 9, b: { c: 'xxx' }, d: { e: 'yyy' } };
var fromto = [ 'b.c', 'd.e' ];
copyProps(src, dst, fromto);
// => { a: 9, b: { c: 'CCC' }, d: { e: 'EEE' } }
```
```js
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc', d: 'ddd' };
var dst = { f: { a: 2, b1: 'xxx', b2: 'yyy' }, e: 'zzz' };
Can copy reversively (from *dst* to *src*) by reverse flag (and return *src*):
copyProps(src, dst, {
a: 'f.a',
'b.b2': 'f.b2',
'c': 'f.c',
}, true);
// => { a: 2, b: { b1: 'bbb', b2: 'yyy' }, c: 'ccc', d: 'ddd' }
```
```js
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc' };
var dst = { a: 2, b: { b1: 'xxx', b2: 'yyy' } };
* If a value of source property is undefined (when not using converter), or a result of converter is undefined (when using converter), the value is not copied.
copyProps(src, dst, true);
// => { a: 2, b: { b1: 'xxx', b2: 'yyy' }, c: 'ccc' }
```
```js
var src = { a: 'A', b: undefined, c: null, d: 1 };
var dst = { a: 'a', b: 'b', c: 'c' };
```js
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc', d: 'ddd' };
var dst = { f: { a: 2, b1: 'xxx', b2: 'yyy' }, e: 'zzz' };
copyProps(src, dst, function(srcInfo) {
if (srcInfo.keyChain === 'd') {
return undefined;
} else {
return srcInfo.value;
}
});
// => { a: 'A', b: 'b', c: null }
```
copyProps(src, dst, {
a: 'f.a',
'b.b2': 'f.b2',
'c': 'f.c',
}, true);
// => { a: 2, b: { b1: 'bbb', b2: 'yyy' }, c: 'ccc', d: 'ddd' }
```
* You can operate the parent node object directly in converter.
If a value of source property is undefined (when not using converter), or a result of converter is undefined (when using converter), the value is not copied.
```js
var src = { a: 1, b: 2 };
var dst = {};
```js
var src = { a: 'A', b: undefined, c: null, d: 1 };
var dst = { a: 'a', b: 'b', c: 'c' };
copyProps(src, dst, function(srcInfo, dstInfo) {
Object.defineProperty(dstInfo.parent, dstInfo.key, {
writable: false,
enumerable: true,
configurable: false,
value: srcInfo.value * 2
})
}); // => { a: 2, b: 4 }
copyProps(src, dst, function(srcInfo) {
if (srcInfo.keyChain === 'd') {
return undefined;
} else {
return srcInfo.value;
}
});
// => { a: 'A', b: 'b', c: null }
```
dst // => { a: 2, b: 4 }
dst.a = 9
dst // -> { a: 2, b: 4 }
```
You can operate the parent node object directly in converter.
API
---
```js
var src = { a: 1, b: 2 };
var dst = {};
copyProps(src, dst, function(srcInfo, dstInfo) {
Object.defineProperty(dstInfo.parent, dstInfo.key, {
writable: false,
enumerable: true,
configurable: false,
value: srcInfo.value * 2
})
}); // => { a: 2, b: 4 }
dst // => { a: 2, b: 4 }
dst.a = 9
dst // -> { a: 2, b: 4 }
```
## API
### <u>copyProps(src, dst [, fromto] [, converter] [, reverse]) => object</u>

@@ -141,57 +147,67 @@

**Arguments:**
#### Parameters:
* **src** [object] : a source object of copy.
* **dst** [object] : a destinate object of copy.
* **fromto** [object | array] : an object mapping properties between *src* and *dst*. (optional)
* **converter** [function] : a function to convert terminal values in *src*. (optional)
* **reverse** [boolean] : copys reversively from dst to src and returns src object. `fromto` is also reversively used from value to key. This default value is `false`. (optional)
| Parameter | Type | Description |
|:------------|:------:|:-------------------------------------------------|
| *src* | object | A source object of copy. |
| *dst* | object | A destinate object of copy. |
| *fromto* | object &#124; array | An object mapping properties between *src* and *dst*. (Optional) |
| *converter* |function| A function to convert terminal values in *src*. (Optional) |
| *reverse* |boolean | True, if copying reversively from dst to src and returns src object. `fromto` is also reversively used from value to key. This default value is `false`. (Optional) |
**Return** [object] : *dst* object after copying.
#### Returns:
#### *Format of fromto*
*dst* object after copying.
*fromto* is a non-nested key-value object. And the *key*s are property key chains of *src* and the *value*s are property key chains of *dst*.
The key chain is a string which is concatenated property keys on each level with dots, like `'aaa.bbb.ccc'`.
**Type:** object
The following example copys the value of `src.aaa.bbb.ccc` to `dst.xxx.yyy`.
* **Format of <i>fromto</i>**
```js
copyProps(src, dst, {
'aaa.bbb.ccc' : 'xxx.yyy'
})
```
*fromto* is a non-nested key-value object. And the *key*s are property key chains of *src* and the *value*s are property key chains of *dst*.
The key chain is a string which is concatenated property keys on each level with dots, like `'aaa.bbb.ccc'`.
*fromto* can be an array. In that case, the array works as a map which has pairs of same key and value.
The following example copys the value of `src.aaa.bbb.ccc` to `dst.xxx.yyy`.
#### *API of converter*
```js
copyProps(src, dst, {
'aaa.bbb.ccc' : 'xxx.yyy'
})
```
**<u>converter(srcInfo, dstInfo) => any</u>**
*fromto* can be an array. In that case, the array works as a map which has pairs of same key and value.
* **API of <i>converter</i>**
*converter* is a function to convert terminal values of propeerties of *src*.
**<u>converter(srcInfo, dstInfo) : Any</u>**
**Arguments:**
*converter* is a function to convert terminal values of propeerties of *src*.
* **srcInfo** [object] : an object which has informations about the current node of *src*. This object has following properties:
**Parameters:**
* **value** : The value of the current node.
* **key** : The key name of the current node.
* **keyChain** : The full key of the current node concatenated with dot.
* **depth** : The depth of the current node.
* **parent** : The parent node of the current node.
| Parameter | Type | Description |
|:------------|:------:|:---------------------------------------------|
| *srcInfo* | object | An object which has informations about the current node of *src*. |
| *dstInfo* | object | An object which has informations about the current node of *dst*. |
**Return:**
The converted value to be set as a destination property value. If this value is undefined, the destination property is not set to the destination node object.
**Type:** *Any*
* **Properties of <i>srcInfo</i> and <i>dstInfo</i>**
* **dstInfo** [object] : an object which has informations about the current node of *dst*. This object has following properties:
*srcInfo* and *dstInfo* has same properties, as follows:
| Property | Type | Description |
|:-----------|:------:|:------------------------------------------|
| *value* | *Any* | The value of the current node. |
| *key* | string | The key name of the current node. |
| *keyChain* | string | The full key of the current node concatenated with dot. |
| *depth* | number | The depth of the current node. |
| *parent* | object | The parent node of the current node. |
* **value** : The value of the current node.
* **key** : The key name of the current node.
* **keyChain** : The full key of the current node concatenated with dot.
* **depth** : The depth of the current node.
* **parent** : The parent node of the current node.
## License
**Return:** [any] : converted value to be set as a destination property value. If this value is undefined, the destination property is not set to the destination node object.
License
-------
Copyright (C) 2016-2018 Takayuki Sato

@@ -203,3 +219,3 @@

[repo-url]: https://github.com/sttk/copy-props/
[npm-img]: https://img.shields.io/badge/npm-v2.0.3-blue.svg
[npm-img]: https://img.shields.io/badge/npm-v2.0.4-blue.svg
[npm-url]: https://www.npmjs.org/package/copy-props/

@@ -206,0 +222,0 @@ [mit-img]: https://img.shields.io/badge/license-MIT-green.svg

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc