Gathering detailed insights and metrics for copy-props
Gathering detailed insights and metrics for copy-props
Gathering detailed insights and metrics for copy-props
Gathering detailed insights and metrics for copy-props
npm install copy-props
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.3
Supply Chain
99.5
Quality
77.3
Maintenance
100
Vulnerability
100
License
Cumulative downloads
Total Downloads
Last Day
-30.4%
110,528
Compared to previous day
Last Week
0.9%
1,875,549
Compared to previous week
Last Month
10.6%
7,830,171
Compared to previous month
Last Year
16.6%
72,167,506
Compared to previous year
2
6
Copy properties between two objects deeply.
To install from npm:
1$ npm i copy-props --save
Copy src to dst simply (and return dst) :
1const copyProps = require('copy-props'); 2 3var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc' }; 4var dst = { a: 2, b: { b1: 'xxx', b2: 'yyy' } }; 5 6copyProps(src, dst); 7// => { a: 1, b: { b1: 'bbb', b2: 'yyy' }, c: 'ccc' }
Copy src to dst with property mapping (and return dst) :
1const copyProps = require('copy-props'); 2 3var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc', d: 'ddd' }; 4var dst = { f: { a: 2, b1: 'xxx', b2: 'yyy' }, e: 'zzz' }; 5 6copyProps(src, dst, { 7 a: 'f.a', 8 'b.b1': 'f.b1', 9 'b.b2': 'f.b2', 10 c: 'f.c', 11}); 12// => { f: { a: 1, b1: 'bbb', b2: 'yyy', c: 'ccc' }, e: 'zzz' }
Copy src to dst with convert function (and return dst) :
1const copyProps = require('copy-props'); 2 3var src = { a: 1, b: { b1: 'bbb' } }; 4var dst = { a: 0 }; 5 6copyProps(src, dst, function (srcInfo) { 7 if (srcInfo.keyChain === 'a') { 8 return srcInfo.value * 2; 9 } 10 if (srcInfo.keyChain === 'b.b1') { 11 return srcInfo.value.toUpperCase(); 12 } 13}); 14// => { a: 2, b: { b1: 'BBB' } }
Can use an array instead of a map as property mapping :
1const copyProps = require('copy-props'); 2 3var src = { a: 1, b: { c: 'CCC' }, d: { e: 'EEE' } }; 4var dst = { a: 9, b: { c: 'xxx' }, d: { e: 'yyy' } }; 5var fromto = ['b.c', 'd.e']; 6copyProps(src, dst, fromto); 7// => { a: 9, b: { c: 'CCC' }, d: { e: 'EEE' } }
Can copy reversively (from dst to src) by reverse flag (and return src):
1const copyProps = require('copy-props'); 2 3var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc' }; 4var dst = { a: 2, b: { b1: 'xxx', b2: 'yyy' } }; 5 6copyProps(src, dst, true); 7// => { a: 2, b: { b1: 'xxx', b2: 'yyy' }, c: 'ccc' }
1const copyProps = require('copy-props'); 2 3var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc', d: 'ddd' }; 4var dst = { f: { a: 2, b1: 'xxx', b2: 'yyy' }, e: 'zzz' }; 5 6copyProps( 7 src, 8 dst, 9 { 10 a: 'f.a', 11 'b.b2': 'f.b2', 12 c: 'f.c', 13 }, 14 true 15); 16// => { a: 2, b: { b1: 'bbb', b2: 'yyy' }, c: 'ccc', d: 'ddd' }
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.
1const copyProps = require('copy-props'); 2 3var src = { a: 'A', b: undefined, c: null, d: 1 }; 4var dst = { a: 'a', b: 'b', c: 'c' }; 5 6copyProps(src, dst, function (srcInfo) { 7 if (srcInfo.keyChain === 'd') { 8 return undefined; 9 } else { 10 return srcInfo.value; 11 } 12}); 13// => { a: 'A', b: 'b', c: null }
You can operate the parent node object directly in converter.
1const copyProps = require('copy-props');
2
3var src = { a: 1, b: 2 };
4var dst = {};
5
6copyProps(src, dst, function (srcInfo, dstInfo) {
7 Object.defineProperty(dstInfo.parent, dstInfo.key, {
8 writable: false,
9 enumerable: true,
10 configurable: false,
11 value: srcInfo.value * 2,
12 });
13}); // => { a: 2, b: 4 }
14
15dst; // => { a: 2, b: 4 }
16dst.a = 9;
17dst; // -> { a: 2, b: 4 }
Copy properties of src to dst deeply. If fromto is given, it is able to copy between different properties. If converter is given, it is able to convert the terminal values.
Parameter | Type | Description |
---|---|---|
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 | 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) |
dst object after copying.
Type: object
Format of fromto
fromto is a non-nested key-value object. And the keys are property key chains of src and the values 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'
.
The following example copys the value of src.aaa.bbb.ccc
to dst.xxx.yyy
.
1copyProps(src, dst, {
2 'aaa.bbb.ccc': 'xxx.yyy',
3});
fromto can be an array. In that case, the array works as a map which has pairs of same key and value.
API of converter
converter(srcInfo, dstInfo) : Any
converter is a function to convert terminal values of propeerties of src.
Parameters:
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 srcInfo and dstInfo
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. |
MIT
No vulnerabilities found.