aboutsummaryrefslogtreecommitdiff
path: root/node_modules/es5-ext/object/copy-deep.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-12-10 21:51:33 +0100
committerFlorian Dold <florian.dold@gmail.com>2017-12-10 21:51:33 +0100
commit0469abd4a9c9270a1fdc962969e36e63699af8b4 (patch)
treef9864d4a4148621378958794cbbfdc2393733283 /node_modules/es5-ext/object/copy-deep.js
parent6947e79bbc258f7bc96af424ddb71a511f0c15a3 (diff)
upgrade dependencies
Diffstat (limited to 'node_modules/es5-ext/object/copy-deep.js')
-rw-r--r--node_modules/es5-ext/object/copy-deep.js53
1 files changed, 24 insertions, 29 deletions
diff --git a/node_modules/es5-ext/object/copy-deep.js b/node_modules/es5-ext/object/copy-deep.js
index d1d39c982..39f9a1d69 100644
--- a/node_modules/es5-ext/object/copy-deep.js
+++ b/node_modules/es5-ext/object/copy-deep.js
@@ -3,40 +3,35 @@
var forEach = require("./for-each")
, isPlainObject = require("./is-plain-object")
, ensureValue = require("./valid-value")
- , isArray = Array.isArray
- , copy
- , copyItem;
+ , isArray = Array.isArray;
-copyItem = function (value) {
- var index;
- if (!isPlainObject(value) && !isArray(value)) return value;
- index = this[0].indexOf(value);
- if (index === -1) return copy.call(this, value);
- return this[1][index];
-};
+var copyValue = function (value, ancestors, ancestorsCopy) {
+ var mode;
+ if (isPlainObject(value)) mode = "object";
+ else if (isArray(value)) mode = "array";
+ if (!mode) return value;
+
+ var copy = ancestorsCopy[ancestors.indexOf(value)];
+ if (copy) return copy;
+ copy = mode === "object" ? {} : [];
-copy = function (source) {
- var target = isArray(source) ? [] : {};
- this[0].push(source);
- this[1].push(target);
- if (isArray(source)) {
- source.forEach(function (value, key) {
- target[key] = copyItem.call(this, value, key);
- }, this);
+ ancestors.push(value);
+ ancestorsCopy.push(copy);
+ if (mode === "object") {
+ forEach(value, function (item, key) {
+ copy[key] = copyValue(item, ancestors, ancestorsCopy);
+ });
} else {
- forEach(
- source,
- function (value, key) {
- target[key] = copyItem.call(this, value, key);
- },
- this
- );
+ value.forEach(function (item, index) {
+ copy[index] = copyValue(item, ancestors, ancestorsCopy);
+ });
}
- return target;
+ ancestors.pop();
+ ancestorsCopy.pop();
+
+ return copy;
};
module.exports = function (source) {
- var obj = Object(ensureValue(source));
- if (obj !== source) return obj;
- return copy.call([[], []], obj);
+ return copyValue(ensureValue(source), [], []);
};