Skip to content

Commit

Permalink
chore: Run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored and actions-user committed Sep 25, 2021
1 parent 85b1165 commit 404ff55
Show file tree
Hide file tree
Showing 10 changed files with 1,600 additions and 1,068 deletions.
137 changes: 70 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ For Web browser:

## Usage

Copy *src* to *dst* simply (and return *dst*) :
Copy _src_ to _dst_ simply (and return _dst_) :

```js
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc' };
Expand All @@ -44,7 +44,7 @@ copyProps(src, dst);
// => { a: 1, b: { b1: 'bbb', b2: 'yyy' }, c: 'ccc' }
```

Copy *src* to *dst* with property mapping (and return *dst*) :
Copy _src_ to _dst_ with property mapping (and return _dst_) :

```js
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc', d: 'ddd' };
Expand All @@ -54,18 +54,18 @@ copyProps(src, dst, {
a: 'f.a',
'b.b1': 'f.b1',
'b.b2': 'f.b2',
'c': 'f.c',
c: 'f.c',
});
// => { f: { a: 1, b1: 'bbb', b2: 'yyy', c: 'ccc' }, e: 'zzz' }
```

Copy *src* to *dst* with convert function (and return *dst*) :
Copy _src_ to _dst_ with convert function (and return _dst_) :

```js
var src = { a: 1, b: { b1: 'bbb' } };
var dst = { a: 0 };

copyProps(src, dst, function(srcInfo) {
copyProps(src, dst, function (srcInfo) {
if (srcInfo.keyChain === 'a') {
return srcInfo.value * 2;
}
Expand All @@ -81,12 +81,12 @@ Can use an array instead of a map as property mapping :
```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' ];
var fromto = ['b.c', 'd.e'];
copyProps(src, dst, fromto);
// => { a: 9, b: { c: 'CCC' }, d: { e: 'EEE' } }
```

Can copy reversively (from *dst* to *src*) by reverse flag (and return *src*):
Can copy reversively (from _dst_ to _src_) by reverse flag (and return _src_):

```js
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc' };
Expand All @@ -100,11 +100,16 @@ copyProps(src, dst, true);
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, {
a: 'f.a',
'b.b2': 'f.b2',
'c': 'f.c',
}, true);
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' }
```

Expand All @@ -114,7 +119,7 @@ If a value of source property is undefined (when not using converter), or a resu
var src = { a: 'A', b: undefined, c: null, d: 1 };
var dst = { a: 'a', b: 'b', c: 'c' };

copyProps(src, dst, function(srcInfo) {
copyProps(src, dst, function (srcInfo) {
if (srcInfo.keyChain === 'd') {
return undefined;
} else {
Expand All @@ -130,90 +135,89 @@ You can operate the parent node object directly in converter.
var src = { a: 1, b: 2 };
var dst = {};

copyProps(src, dst, function(srcInfo, dstInfo) {
copyProps(src, dst, function (srcInfo, dstInfo) {
Object.defineProperty(dstInfo.parent, dstInfo.key, {
writable: false,
enumerable: true,
configurable: false,
value: srcInfo.value * 2
})
value: srcInfo.value * 2,
});
}); // => { a: 2, b: 4 }

dst // => { a: 2, b: 4 }
dst.a = 9
dst // -> { 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>

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.
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.

#### Parameters:

| 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) |
| 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) |

#### Returns:

*dst* object after copying.
_dst_ object after copying.

**Type:** object

* **Format of <i>fromto</i>**
- **Format of <i>fromto</i>**

_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'`.

The following example copys the value of `src.aaa.bbb.ccc` to `dst.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'`.
```js
copyProps(src, dst, {
'aaa.bbb.ccc': 'xxx.yyy',
});
```

The following example copys the value of `src.aaa.bbb.ccc` to `dst.xxx.yyy`.
_fromto_ can be an array. In that case, the array works as a map which has pairs of same key and value.

```js
copyProps(src, dst, {
'aaa.bbb.ccc' : 'xxx.yyy'
})
```
- **API of <i>converter</i>**

*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>**
**<u>converter(srcInfo, dstInfo) : Any</u>**

**<u>converter(srcInfo, dstInfo) : Any</u>**
_converter_ is a function to convert terminal values of propeerties of _src_.

*converter* is a function to convert terminal values of propeerties of *src*.
**Parameters:**

**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_. |

| 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>**
**Return:**

*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. |
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>**

_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. |

## License

Expand All @@ -222,7 +226,6 @@ Copyright (C) 2016-2021 Gulp Team.
This program is free software under [MIT][mit-url] License.
See the file LICENSE in this distribution for more details.


<!-- prettier-ignore-start -->
[downloads-image]: https://img.shields.io/npm/dm/copy-props.svg?style=flat-square
[npm-url]: https://www.npmjs.org/package/copy-props
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
var eachProps = require('each-props');
var isPlainObject = require('is-plain-object').isPlainObject;

module.exports = function(src, dst, fromto, converter, reverse) {

module.exports = function (src, dst, fromto, converter, reverse) {
if (!isObject(src)) {
src = {};
}
Expand Down Expand Up @@ -92,7 +91,7 @@ function copyWithFromto(value, keyChain, nodeInfo) {
};

for (var i = 0, n = dstKeyChains.length; i < n; i++) {
setDeep(nodeInfo.dest, dstKeyChains[i], function(parent, key, depth) {
setDeep(nodeInfo.dest, dstKeyChains[i], function (parent, key, depth) {
var dstInfo = {
keyChain: dstKeyChains[i],
value: parent[key],
Expand Down Expand Up @@ -123,7 +122,7 @@ function copyWithoutFromto(value, keyChain, nodeInfo) {
parent: nodeInfo.parent,
};

setDeep(nodeInfo.dest, keyChain, function(parent, key, depth) {
setDeep(nodeInfo.dest, keyChain, function (parent, key, depth) {
var dstInfo = {
keyChain: keyChain,
value: parent[key],
Expand Down Expand Up @@ -193,7 +192,8 @@ function _setDeep(obj, keyElems, depth, valueCreator) {
if (value === undefined) {
return;
}
if (isPlainObject(value)) { // value is always an empty object.
if (isPlainObject(value)) {
// value is always an empty object.
if (isPlainObject(obj[key])) {
return;
}
Expand Down Expand Up @@ -230,5 +230,5 @@ function isObject(v) {
}

function isPossibilityOfPrototypePollution(key) {
return (key === '__proto__' || key === 'constructor');
return key === '__proto__' || key === 'constructor';
}

0 comments on commit 404ff55

Please sign in to comment.