{"version":3,"file":"deep.cjs","names":["isPrimitive","isRecord","isArray","isObject"],"sources":["../../../src/common/data/deep.ts"],"sourcesContent":["/* eslint-disable no-prototype-builtins */\n\nimport { isArray, isObject, isPrimitive, isRecord } from './is'\n\nexport function deepEqual(a: any, b: any, hash = new WeakSet()) {\n  // if both x and y are null or undefined and exactly the same\n  if (a === b)\n    return true\n\n  // Cyclic\n  if (hash.has(b)) {\n    // console.log('cyclic')\n    return false\n  }\n\n  if (!isPrimitive(b))\n    hash.add(b)\n\n  // if they are not strictly equal, they both need to be Objects\n  if (!(a instanceof Object) || !(b instanceof Object))\n    return false\n\n  // they must have the exact same prototype chain, the closest we can do is\n  // test there constructor.\n  if (a.constructor !== b.constructor)\n    return false\n\n  // Shortcut to avoid to many loops\n  if (a.length !== b.length)\n    return false\n\n  for (const p in a) {\n    // other properties were tested using x.constructor === y.constructor\n    if (!a.hasOwnProperty(p))\n      continue\n\n    // allows to compare x[ p ] and y[ p ] when set to undefined\n    if (!b.hasOwnProperty(p))\n      return false\n\n    const aa = a[p]\n    const bb = b[p]\n\n    // if they have the same strict value or identity then they are equal\n    // if (aa === bb) {\n    //   console.log('eq', typeof bb)\n    //   if (bb != null) hash.set(bb, true)\n    //   continue\n    // }\n    //\n    // // Numbers, Strings, Functions, Booleans must be strictly equal\n    // if (typeof (aa) !== 'object') {\n    //   return false\n    // }\n\n    // Objects and Arrays must be tested recursively\n    if (!deepEqual(aa, bb, hash))\n      return false\n  }\n\n  // allows x[ p ] to be set to undefined\n  for (const p in b) {\n    if (b.hasOwnProperty(p) && !a.hasOwnProperty(p))\n      return false\n  }\n\n  return true\n}\n\n/** Strip properties with value `undefined` in place */\nexport function deepStripUndefinedInPlace(a: any, hash:WeakSet<object> = new WeakSet()) {\n  // Cyclic\n  if (hash.has(a))\n    return '[Circular ~]'\n  if (!isPrimitive(a))\n    hash.add(a)\n\n  if (isRecord(a)) {\n    for (const p in a) {\n      if (!a.hasOwnProperty(p))\n        continue\n      if (a[p] === undefined) {\n        delete a[p]\n        continue\n      }\n      deepStripUndefinedInPlace(a[p], hash)\n    }\n  }\n  else if (isArray(a)) {\n    for (let i = a.length - 1; i >= 0; i--) {\n      if (a[i] === undefined)\n        a.splice(i, 1)\n    }\n  }\n  // else ignore\n\n  return a\n}\n\nexport function deepMerge(target: any, ...sources: any[]) {\n  // todo cyclic protection\n  for (const source of sources) {\n    if (!isObject(target))\n      target = {}\n\n    if (source == null || !isObject(source))\n      continue\n\n    Object.keys(source).forEach((key) => {\n      const targetValue = target[key]\n      const sourceValue = (source as any)[key]\n\n      if (Array.isArray(targetValue) && Array.isArray(sourceValue))\n        target[key] = targetValue.concat(sourceValue)\n      else if (isObject(targetValue) && isObject(sourceValue))\n        target[key] = deepMerge(Object.assign({}, targetValue), sourceValue)\n      else\n        target[key] = sourceValue\n    })\n  }\n\n  return target\n}\n\n// // https://stackoverflow.com/a/40294058/140927\n// // Handles cyclic references\n// export function deepClonePrimitives(obj, hash = new WeakMap()) {\n//\n//   // primitives\n//   if (Object(obj) !== obj) {\n//     // log('Primitive', obj)\n//     return obj\n//   }\n//\n//   // cyclic reference\n//   if (hash.has(obj)) {\n//     return hash.get(obj)\n//   }\n//\n//   let result\n//\n//   // primitives as objects like new String(), new Number()\n//   if (\n//     obj instanceof String ||\n//     obj instanceof Number ||\n//     obj instanceof BigInt ||\n//     obj instanceof Boolean ||\n//     obj instanceof Symbol\n//   ) {\n//     result = new obj.constructor(obj.valueOf())\n//     // hash.set(obj, result)\n//     return result\n//   }\n//\n//   // log('Obj', obj)\n//\n//   if (obj instanceof Set) {\n//     result = new Set(obj)\n//   } else if (obj instanceof Map) {\n//     result = new Map(Array.from(obj, ([key, val]) => [key, deepClonePrimitives(val, hash)]))\n//   } else if (obj instanceof Date) {\n//     result = new Date(obj)\n//   } else if (obj instanceof RegExp) {\n//     result = new RegExp(obj.source, obj.flags)\n//   } else if (Array.isArray(obj)) {\n//     result = Array.from(obj, val => deepClonePrimitives(val, hash))\n//   } else if (obj.constructor) {\n//     result = new obj.constructor()\n//     log('bj object', result)\n//   } else if (obj instanceof Function || typeof obj === 'function') {\n//     log('Keep object', obj, Object.getPrototypeOf(obj))\n//     result = obj\n//   } else {\n//     result = Object.create(null)\n//   }\n//\n//   hash.set(obj, result)\n//\n//   return Object.assign(result, ...Object.keys(obj).map(key => ({\n//     [key]: deepClonePrimitives(obj[key], hash),\n//   })))\n// }\n"],"mappings":";;;;AAIA,SAAgB,UAAU,GAAQ,GAAQ,uBAAO,IAAI,SAAS,EAAE;AAE9D,KAAI,MAAM,EACR,QAAO;AAGT,KAAI,KAAK,IAAI,EAAE,CAEb,QAAO;AAGT,KAAI,CAACA,mCAAY,EAAE,CACjB,MAAK,IAAI,EAAE;AAGb,KAAI,EAAE,aAAa,WAAW,EAAE,aAAa,QAC3C,QAAO;AAIT,KAAI,EAAE,gBAAgB,EAAE,YACtB,QAAO;AAGT,KAAI,EAAE,WAAW,EAAE,OACjB,QAAO;AAET,MAAK,MAAM,KAAK,GAAG;AAEjB,MAAI,CAAC,EAAE,eAAe,EAAE,CACtB;AAGF,MAAI,CAAC,EAAE,eAAe,EAAE,CACtB,QAAO;EAET,MAAM,KAAK,EAAE;EACb,MAAM,KAAK,EAAE;AAeb,MAAI,CAAC,UAAU,IAAI,IAAI,KAAK,CAC1B,QAAO;;AAIX,MAAK,MAAM,KAAK,EACd,KAAI,EAAE,eAAe,EAAE,IAAI,CAAC,EAAE,eAAe,EAAE,CAC7C,QAAO;AAGX,QAAO;;;AAIT,SAAgB,0BAA0B,GAAQ,uBAAuB,IAAI,SAAS,EAAE;AAEtF,KAAI,KAAK,IAAI,EAAE,CACb,QAAO;AACT,KAAI,CAACA,mCAAY,EAAE,CACjB,MAAK,IAAI,EAAE;AAEb,KAAIC,gCAAS,EAAE,CACb,MAAK,MAAM,KAAK,GAAG;AACjB,MAAI,CAAC,EAAE,eAAe,EAAE,CACtB;AACF,MAAI,EAAE,OAAO,QAAW;AACtB,UAAO,EAAE;AACT;;A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