Sha256: 9ad91634b3920d12619e43cc3a878b778583fd8b418c0f7168c1b9e85c8d5c74
Contents?: true
Size: 1023 Bytes
Versions: 44
Compression:
Stored size: 1023 Bytes
Contents
'use strict'; /** * Module dependenices */ const clone = require('shallow-clone'); const typeOf = require('kind-of'); const isPlainObject = require('is-plain-object'); 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(val, instanceClone) { if (typeof instanceClone === 'function') { return instanceClone(val); } if (instanceClone || isPlainObject(val)) { const res = new val.constructor(); for (let key in val) { res[key] = cloneDeep(val[key], instanceClone); } return res; } return val; } function cloneArrayDeep(val, instanceClone) { const res = new val.constructor(val.length); for (let i = 0; i < val.length; i++) { res[i] = cloneDeep(val[i], instanceClone); } return res; } /** * Expose `cloneDeep` */ module.exports = cloneDeep;
Version data entries
44 entries across 43 versions & 9 rubygems