index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. 'use strict'
  2. const { test } = require('tap')
  3. const rfdc = require('..')
  4. const cloneDefault = require('../default')
  5. const clone = rfdc()
  6. const cloneProto = rfdc({ proto: true })
  7. const cloneCircles = rfdc({ circles: true })
  8. const cloneCirclesProto = rfdc({ circles: true, proto: true })
  9. const rnd = (max) => Math.round(Math.random() * max)
  10. types(clone, 'default')
  11. types(cloneProto, 'proto option')
  12. types(cloneCircles, 'circles option')
  13. types(cloneCirclesProto, 'circles and proto option')
  14. test('default – does not copy proto properties', async ({ is }) => {
  15. is(clone(Object.create({ a: 1 })).a, undefined, 'value not copied')
  16. })
  17. test('default – shorthand import', async ({ same }) => {
  18. same(
  19. clone(Object.create({ a: 1 })),
  20. cloneDefault(Object.create({ a: 1 })),
  21. 'import equals clone with default options'
  22. )
  23. })
  24. test('proto option – copies enumerable proto properties', async ({ is }) => {
  25. is(cloneProto(Object.create({ a: 1 })).a, 1, 'value copied')
  26. })
  27. test('circles option - circular object', async ({ same, is, isNot }) => {
  28. const o = { nest: { a: 1, b: 2 } }
  29. o.circular = o
  30. same(cloneCircles(o), o, 'same values')
  31. isNot(cloneCircles(o), o, 'different objects')
  32. isNot(cloneCircles(o).nest, o.nest, 'different nested objects')
  33. const c = cloneCircles(o)
  34. is(c.circular, c, 'circular references point to copied parent')
  35. isNot(c.circular, o, 'circular references do not point to original parent')
  36. })
  37. test('circles option – deep circular object', async ({ same, is, isNot }) => {
  38. const o = { nest: { a: 1, b: 2 } }
  39. o.nest.circular = o
  40. same(cloneCircles(o), o, 'same values')
  41. isNot(cloneCircles(o), o, 'different objects')
  42. isNot(cloneCircles(o).nest, o.nest, 'different nested objects')
  43. const c = cloneCircles(o)
  44. is(c.nest.circular, c, 'circular references point to copied parent')
  45. isNot(
  46. c.nest.circular,
  47. o,
  48. 'circular references do not point to original parent'
  49. )
  50. })
  51. test('circles option alone – does not copy proto properties', async ({
  52. is
  53. }) => {
  54. is(cloneCircles(Object.create({ a: 1 })).a, undefined, 'value not copied')
  55. })
  56. test('circles and proto option – copies enumerable proto properties', async ({
  57. is
  58. }) => {
  59. is(cloneCirclesProto(Object.create({ a: 1 })).a, 1, 'value copied')
  60. })
  61. test('circles and proto option - circular object', async ({
  62. same,
  63. is,
  64. isNot
  65. }) => {
  66. const o = { nest: { a: 1, b: 2 } }
  67. o.circular = o
  68. same(cloneCirclesProto(o), o, 'same values')
  69. isNot(cloneCirclesProto(o), o, 'different objects')
  70. isNot(cloneCirclesProto(o).nest, o.nest, 'different nested objects')
  71. const c = cloneCirclesProto(o)
  72. is(c.circular, c, 'circular references point to copied parent')
  73. isNot(c.circular, o, 'circular references do not point to original parent')
  74. })
  75. test('circles and proto option – deep circular object', async ({
  76. same,
  77. is,
  78. isNot
  79. }) => {
  80. const o = { nest: { a: 1, b: 2 } }
  81. o.nest.circular = o
  82. same(cloneCirclesProto(o), o, 'same values')
  83. isNot(cloneCirclesProto(o), o, 'different objects')
  84. isNot(cloneCirclesProto(o).nest, o.nest, 'different nested objects')
  85. const c = cloneCirclesProto(o)
  86. is(c.nest.circular, c, 'circular references point to copied parent')
  87. isNot(
  88. c.nest.circular,
  89. o,
  90. 'circular references do not point to original parent'
  91. )
  92. })
  93. test('circles and proto option – deep circular array', async ({
  94. same,
  95. is,
  96. isNot
  97. }) => {
  98. const o = { nest: [1, 2] }
  99. o.nest.push(o)
  100. same(cloneCirclesProto(o), o, 'same values')
  101. isNot(cloneCirclesProto(o), o, 'different objects')
  102. isNot(cloneCirclesProto(o).nest, o.nest, 'different nested objects')
  103. const c = cloneCirclesProto(o)
  104. is(c.nest[2], c, 'circular references point to copied parent')
  105. isNot(c.nest[2], o, 'circular references do not point to original parent')
  106. })
  107. function types (clone, label) {
  108. test(label + ' – number', async ({ is }) => {
  109. is(clone(42), 42, 'same value')
  110. })
  111. test(label + ' – string', async ({ is }) => {
  112. is(clone('str'), 'str', 'same value')
  113. })
  114. test(label + ' – boolean', async ({ is }) => {
  115. is(clone(true), true, 'same value')
  116. })
  117. test(label + ' – function', async ({ is }) => {
  118. const fn = () => {}
  119. is(clone(fn), fn, 'same function')
  120. })
  121. test(label + ' – async function', async ({ is }) => {
  122. const fn = async () => {}
  123. is(clone(fn), fn, 'same function')
  124. })
  125. test(label + ' – generator function', async ({ is }) => {
  126. const fn = function * () {}
  127. is(clone(fn), fn, 'same function')
  128. })
  129. test(label + ' – date', async ({ is, isNot }) => {
  130. const date = new Date()
  131. is(+clone(date), +date, 'same value')
  132. isNot(clone(date), date, 'different object')
  133. })
  134. test(label + ' – null', async ({ is }) => {
  135. is(clone(null), null, 'same value')
  136. })
  137. test(label + ' – shallow object', async ({ same, isNot }) => {
  138. const o = { a: 1, b: 2 }
  139. same(clone(o), o, 'same values')
  140. isNot(clone(o), o, 'different object')
  141. })
  142. test(label + ' – shallow array', async ({ same, isNot }) => {
  143. const o = [1, 2]
  144. same(clone(o), o, 'same values')
  145. isNot(clone(o), o, 'different arrays')
  146. })
  147. test(label + ' – deep object', async ({ same, isNot }) => {
  148. const o = { nest: { a: 1, b: 2 } }
  149. same(clone(o), o, 'same values')
  150. isNot(clone(o), o, 'different objects')
  151. isNot(clone(o).nest, o.nest, 'different nested objects')
  152. })
  153. test(label + ' – deep array', async ({ same, isNot }) => {
  154. const o = [{ a: 1, b: 2 }, [3]]
  155. same(clone(o), o, 'same values')
  156. isNot(clone(o), o, 'different arrays')
  157. isNot(clone(o)[0], o[0], 'different array elements')
  158. isNot(clone(o)[1], o[1], 'different array elements')
  159. })
  160. test(label + ' – nested number', async ({ is }) => {
  161. is(clone({ a: 1 }).a, 1, 'same value')
  162. })
  163. test(label + ' – nested string', async ({ is }) => {
  164. is(clone({ s: 'str' }).s, 'str', 'same value')
  165. })
  166. test(label + ' – nested boolean', async ({ is }) => {
  167. is(clone({ b: true }).b, true, 'same value')
  168. })
  169. test(label + ' – nested function', async ({ is }) => {
  170. const fn = () => {}
  171. is(clone({ fn }).fn, fn, 'same function')
  172. })
  173. test(label + ' – nested async function', async ({ is }) => {
  174. const fn = async () => {}
  175. is(clone({ fn }).fn, fn, 'same function')
  176. })
  177. test(label + ' – nested generator function', async ({ is }) => {
  178. const fn = function * () {}
  179. is(clone({ fn }).fn, fn, 'same function')
  180. })
  181. test(label + ' – nested date', async ({ is, isNot }) => {
  182. const date = new Date()
  183. is(+clone({ d: date }).d, +date, 'same value')
  184. isNot(clone({ d: date }).d, date, 'different object')
  185. })
  186. test(label + ' – nested date in array', async ({ is, isNot }) => {
  187. const date = new Date()
  188. is(+clone({ d: [date] }).d[0], +date, 'same value')
  189. isNot(clone({ d: [date] }).d[0], date, 'different object')
  190. is(+cloneCircles({ d: [date] }).d[0], +date, 'same value')
  191. isNot(cloneCircles({ d: [date] }).d, date, 'different object')
  192. })
  193. test(label + ' – nested null', async ({ is }) => {
  194. is(clone({ n: null }).n, null, 'same value')
  195. })
  196. test(label + ' – arguments', async ({ isNot, same }) => {
  197. function fn (...args) {
  198. same(clone(arguments), args, 'same values')
  199. isNot(clone(arguments), arguments, 'different object')
  200. }
  201. fn(1, 2, 3)
  202. })
  203. test(`${label} copies buffers from object correctly`, async ({ ok, is, isNot }) => {
  204. const input = Date.now().toString(36)
  205. const inputBuffer = Buffer.from(input)
  206. const clonedBuffer = clone({ a: inputBuffer }).a
  207. ok(Buffer.isBuffer(clonedBuffer), 'cloned value is buffer')
  208. isNot(clonedBuffer, inputBuffer, 'cloned buffer is not same as input buffer')
  209. is(clonedBuffer.toString(), input, 'cloned buffer content is correct')
  210. })
  211. test(`${label} copies buffers from arrays correctly`, async ({ ok, is, isNot }) => {
  212. const input = Date.now().toString(36)
  213. const inputBuffer = Buffer.from(input)
  214. const [clonedBuffer] = clone([inputBuffer])
  215. ok(Buffer.isBuffer(clonedBuffer), 'cloned value is buffer')
  216. isNot(clonedBuffer, inputBuffer, 'cloned buffer is not same as input buffer')
  217. is(clonedBuffer.toString(), input, 'cloned buffer content is correct')
  218. })
  219. test(`${label} copies TypedArrays from object correctly`, async ({ ok, is, isNot }) => {
  220. const [input1, input2] = [rnd(10), rnd(10)]
  221. var buffer = new ArrayBuffer(8)
  222. const int32View = new Int32Array(buffer)
  223. int32View[0] = input1
  224. int32View[1] = input2
  225. const cloned = clone({ a: int32View }).a
  226. ok(cloned instanceof Int32Array, 'cloned value is instance of class')
  227. isNot(cloned, int32View, 'cloned value is not same as input value')
  228. is(cloned[0], input1, 'cloned value content is correct')
  229. is(cloned[1], input2, 'cloned value content is correct')
  230. })
  231. test(`${label} copies TypedArrays from array correctly`, async ({ ok, is, isNot }) => {
  232. const [input1, input2] = [rnd(10), rnd(10)]
  233. var buffer = new ArrayBuffer(16)
  234. const int32View = new Int32Array(buffer)
  235. int32View[0] = input1
  236. int32View[1] = input2
  237. const [cloned] = clone([int32View])
  238. ok(cloned instanceof Int32Array, 'cloned value is instance of class')
  239. isNot(cloned, int32View, 'cloned value is not same as input value')
  240. is(cloned[0], input1, 'cloned value content is correct')
  241. is(cloned[1], input2, 'cloned value content is correct')
  242. })
  243. test(`${label} copies complex TypedArrays`, async ({ ok, deepEqual, is, isNot }) => {
  244. const [input1, input2, input3] = [rnd(10), rnd(10), rnd(10)]
  245. var buffer = new ArrayBuffer(4)
  246. const view1 = new Int8Array(buffer, 0, 2)
  247. const view2 = new Int8Array(buffer, 2, 2)
  248. const view3 = new Int8Array(buffer)
  249. view1[0] = input1
  250. view2[0] = input2
  251. view3[3] = input3
  252. const cloned = clone({ view1, view2, view3 })
  253. ok(cloned.view1 instanceof Int8Array, 'cloned value is instance of class')
  254. ok(cloned.view2 instanceof Int8Array, 'cloned value is instance of class')
  255. ok(cloned.view3 instanceof Int8Array, 'cloned value is instance of class')
  256. isNot(cloned.view1, view1, 'cloned value is not same as input value')
  257. isNot(cloned.view2, view2, 'cloned value is not same as input value')
  258. isNot(cloned.view3, view3, 'cloned value is not same as input value')
  259. deepEqual(Array.from(cloned.view1), [input1, 0], 'cloned value content is correct')
  260. deepEqual(Array.from(cloned.view2), [input2, input3], 'cloned value content is correct')
  261. deepEqual(Array.from(cloned.view3), [input1, 0, input2, input3], 'cloned value content is correct')
  262. })
  263. test(`${label} - maps`, async ({ same, isNot }) => {
  264. const map = new Map([['a', 1]])
  265. same(Array.from(clone(map)), [['a', 1]], 'same value')
  266. isNot(clone(map), map, 'different object')
  267. })
  268. test(`${label} - sets`, async ({ same, isNot }) => {
  269. const set = new Set([1])
  270. same(Array.from(clone(set)), [1])
  271. isNot(clone(set), set, 'different object')
  272. })
  273. test(`${label} - nested maps`, async ({ same, isNot }) => {
  274. const data = { m: new Map([['a', 1]]) }
  275. same(Array.from(clone(data).m), [['a', 1]], 'same value')
  276. isNot(clone(data).m, data.m, 'different object')
  277. })
  278. test(`${label} - nested sets`, async ({ same, isNot }) => {
  279. const data = { s: new Set([1]) }
  280. same(Array.from(clone(data).s), [1], 'same value')
  281. isNot(clone(data).s, data.s, 'different object')
  282. })
  283. }