This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccountGUI/node_modulesOLD/clone-deep/index.js

55 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-08-11 18:48:02 +00:00
'use strict';
/**
* Module dependenices
*/
var isObject = require('is-plain-object');
var clone = require('shallow-clone');
var typeOf = require('kind-of');
var forOwn = require('for-own');
/**
* Recursively clone native types.
*/
function cloneDeep(val, instanceClone) {
switch (typeOf(val)) {
case 'object':
return cloneObjectDeep(val, instanceClone);
case 'array':
return cloneArrayDeep(val, instanceClone);
default: {
return clone(val);
}
}
}
function cloneObjectDeep(obj, instanceClone) {
if (isObject(obj) || (instanceClone === true && typeOf(obj) === 'object')) {
var res = {};
forOwn(obj, function(val, key) {
this[key] = cloneDeep(val, instanceClone);
}, res);
return res;
}
if (typeof instanceClone === 'function') {
return instanceClone(obj);
}
return obj;
}
function cloneArrayDeep(arr, instanceClone) {
var res = [];
for (var i = 0; i < arr.length; i++) {
res[i] = cloneDeep(arr[i], instanceClone);
}
return res;
}
/**
* Expose `cloneDeep`
*/
module.exports = cloneDeep;