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

Version Path
immosquare-cleaner-0.1.62 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.61 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.60 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.59 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.58 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.57 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.56 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.55 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.54 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.53 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.52 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.51 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.50 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.49 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.48 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.47 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.46 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.45 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.44 node_modules/clone-deep/index.js
immosquare-cleaner-0.1.43 node_modules/clone-deep/index.js