提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.5 KiB

  1. # Copy anything 🎭
  2. <a href="https://www.npmjs.com/package/copy-anything"><img src="https://img.shields.io/npm/v/copy-anything.svg" alt="Total Downloads"></a>
  3. <a href="https://www.npmjs.com/package/copy-anything"><img src="https://img.shields.io/npm/dw/copy-anything.svg" alt="Latest Stable Version"></a>
  4. ```
  5. npm i copy-anything
  6. ```
  7. An optimised way to copy'ing (cloning) an object or array. A small and simple integration.
  8. ## Motivation
  9. I created this package because I tried a lot of similar packages that do copy'ing/cloning. But all had its quirks, and _all of them break things they are not supposed to break_... 😞
  10. I was looking for:
  11. - a simple copy/clone function
  12. - has to be fast!
  13. - props must lose any reference to original object
  14. - works with arrays and objects in arrays!
  15. - supports symbols
  16. - can copy non-enumerable props as well
  17. - **does not break special class instances** ‼️
  18. This last one is crucial! So many libraries use custom classes that create objects with special prototypes, and such objects all break when trying to copy them improperly. So we gotta be careful!
  19. copy-anything will copy objects and nested properties, but only as long as they're "plain objects". As soon as a sub-prop is not a "plain object" and has a special prototype, it will copy that instance over "as is". ♻️
  20. ## Meet the family (more tiny utils with TS support)
  21. - [is-what 🙉](https://github.com/mesqueeb/is-what)
  22. - [is-where 🙈](https://github.com/mesqueeb/is-where)
  23. - [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)
  24. - [check-anything 👁](https://github.com/mesqueeb/check-anything)
  25. - [remove-anything ✂️](https://github.com/mesqueeb/remove-anything)
  26. - [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything)
  27. - [map-anything 🗺](https://github.com/mesqueeb/map-anything)
  28. - [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)
  29. - [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
  30. - [case-anything 🐫](https://github.com/mesqueeb/case-anything)
  31. - [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
  32. - [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything)
  33. ## Usage
  34. <!-- prettier-ignore-start -->
  35. ```js
  36. import { copy } from 'copy-anything'
  37. const original = { name: 'Ditto', type: { water: true } }
  38. const copy = copy(original)
  39. // now if we change a nested prop like the type
  40. copy.type.water = false
  41. // or add a new nested prop
  42. copy.type.fire = true
  43. // then the original object will still be the same:
  44. (original.type.water === true) // true
  45. (original.type.fire === undefined) // true
  46. ```
  47. > Please note, by default copy-anything does not copy non-enumerable props. If you need to copy those, see the instructions further down below.
  48. ## Works with arrays
  49. It will also clone arrays, **as well as objects inside arrays!** 😉
  50. ```js
  51. const original = [{ name: 'Squirtle' }]
  52. const copy = copy(original)
  53. // now if we change a prop in the array
  54. copy[0].name = 'Wartortle'
  55. // or add a new item to the array
  56. copy.push({ name: 'Charmander' })
  57. // then the original array will still be the same:
  58. (original[0].name === 'Squirtle') // true
  59. (original[1] === undefined) // true
  60. ```
  61. ## Non-enumerable
  62. By default, copy-anything only copies enumerable properties. If you also want to copy non-enumerable properties you can do so by passing that as an option.
  63. ```js
  64. const original = { name: 'Bulbasaur' }
  65. // bulbasaur's ID is non-enumerable
  66. Object.defineProperty(original, 'id', {
  67. value: '001',
  68. writable: true,
  69. enumerable: false,
  70. configurable: true,
  71. })
  72. const copy1 = copy(original)
  73. (copy1.id === undefined) // true
  74. const copy2 = copy(original, { nonenumerable: true })
  75. (copy2.id === '001') // true
  76. ```
  77. ## Limit to specific props
  78. You can limit to specific props.
  79. ```js
  80. const original = { name: 'Flareon', type: ['fire'], id: '136' }
  81. const copy = copy(original, { props: ['name'] })
  82. (copy) // will look like: `{ name: 'Flareon' }`
  83. ```
  84. > Please note, if the props you have specified are non-enumerable, you will also need to pass `{nonenumerable: true}`.
  85. <!-- prettier-ignore-end -->
  86. ## Source code
  87. The source code is literally just these lines. Most of the magic comes from the isPlainObject function from the [is-what library](https://github.com/mesqueeb/is-what).
  88. ```JavaScript
  89. import { isPlainObject } from 'is-what'
  90. export function copy (target) {
  91. if (isArray(target)) return target.map(i => copy(i))
  92. if (!isPlainObject(target)) return target
  93. return Object.keys(target)
  94. .reduce((carry, key) => {
  95. const val = target[key]
  96. carry[key] = copy(val)
  97. return carry
  98. }, {})
  99. }
  100. ```