index.es.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { isArray, isPlainObject } from 'is-what';
  2. function assignProp(carry, key, newVal, originalObject, includeNonenumerable) {
  3. const propType = {}.propertyIsEnumerable.call(originalObject, key)
  4. ? 'enumerable'
  5. : 'nonenumerable';
  6. if (propType === 'enumerable')
  7. carry[key] = newVal;
  8. if (includeNonenumerable && propType === 'nonenumerable') {
  9. Object.defineProperty(carry, key, {
  10. value: newVal,
  11. enumerable: false,
  12. writable: true,
  13. configurable: true,
  14. });
  15. }
  16. }
  17. /**
  18. * Copy (clone) an object and all its props recursively to get rid of any prop referenced of the original object. Arrays are also cloned, however objects inside arrays are still linked.
  19. *
  20. * @export
  21. * @template T
  22. * @param {T} target Target can be anything
  23. * @param {Options} [options = {}] Options can be `props` or `nonenumerable`
  24. * @returns {T} the target with replaced values
  25. * @export
  26. */
  27. function copy(target, options = {}) {
  28. if (isArray(target)) {
  29. return target.map((item) => copy(item, options));
  30. }
  31. if (!isPlainObject(target)) {
  32. return target;
  33. }
  34. const props = Object.getOwnPropertyNames(target);
  35. const symbols = Object.getOwnPropertySymbols(target);
  36. return [...props, ...symbols].reduce((carry, key) => {
  37. if (isArray(options.props) && !options.props.includes(key)) {
  38. return carry;
  39. }
  40. const val = target[key];
  41. const newVal = copy(val, options);
  42. assignProp(carry, key, newVal, target, options.nonenumerable);
  43. return carry;
  44. }, {});
  45. }
  46. export { copy };