index.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { isPlainObject, isArray } from 'is-what'
  2. type PlainObject = { [key in string | symbol]: any }
  3. function assignProp (
  4. carry: PlainObject,
  5. key: string | symbol,
  6. newVal: any,
  7. originalObject: PlainObject,
  8. includeNonenumerable: boolean
  9. ): void {
  10. const propType = {}.propertyIsEnumerable.call(originalObject, key)
  11. ? 'enumerable'
  12. : 'nonenumerable'
  13. if (propType === 'enumerable') carry[key as any] = newVal
  14. if (includeNonenumerable && propType === 'nonenumerable') {
  15. Object.defineProperty(carry, key, {
  16. value: newVal,
  17. enumerable: false,
  18. writable: true,
  19. configurable: true,
  20. })
  21. }
  22. }
  23. export type Options = { props?: (string | symbol)[]; nonenumerable?: boolean }
  24. /**
  25. * 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.
  26. *
  27. * @export
  28. * @template T
  29. * @param {T} target Target can be anything
  30. * @param {Options} [options = {}] Options can be `props` or `nonenumerable`
  31. * @returns {T} the target with replaced values
  32. * @export
  33. */
  34. export function copy<T extends any> (target: T, options: Options = {}): T {
  35. if (isArray(target)) return target.map((item) => copy(item, options)) as T
  36. if (!isPlainObject(target)) return target
  37. const props = Object.getOwnPropertyNames(target)
  38. const symbols = Object.getOwnPropertySymbols(target)
  39. return [...props, ...symbols].reduce((carry, key) => {
  40. if (isArray(options.props) && !options.props.includes(key)) {
  41. return carry
  42. }
  43. const val = target[key]
  44. const newVal = copy(val, options)
  45. assignProp(carry, key, newVal, target, options.nonenumerable)
  46. return carry
  47. }, {} as T)
  48. }