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.

index.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. test('custom constructor handler', async ({ same, ok, isNot }) => {
  108. class Foo {
  109. constructor (s) {
  110. this.s = s
  111. }
  112. }
  113. const data = { foo: new Foo('foo') }
  114. const cloned = rfdc({ constructorHandlers: [[Foo, (o) => new Foo(o.s)]] })(data)
  115. ok(cloned.foo instanceof Foo)
  116. same(cloned.foo.s, data.foo.s, 'same values')
  117. isNot(cloned.foo, data.foo, 'different objects')
  118. })
  119. test('custom RegExp handler', async ({ same, ok, isNot }) => {
  120. const data = { regex: /foo/ }
  121. const cloned = rfdc({ constructorHandlers: [[RegExp, (o) => new RegExp(o)]] })(data)
  122. isNot(cloned.regex, data.regex, 'different objects')
  123. ok(cloned.regex.test('foo'))
  124. })
  125. function types (clone, label) {
  126. test(label + ' – number', async ({ is }) => {
  127. is(clone(42), 42, 'same value')
  128. })
  129. test(label + ' – string', async ({ is }) => {
  130. is(clone('str'), 'str', 'same value')
  131. })
  132. test(label + ' – boolean', async ({ is }) => {
  133. is(clone(true), true, 'same value')
  134. })
  135. test(label + ' – function', async ({ is }) => {
  136. const fn = () => {}
  137. is(clone(fn), fn, 'same function')
  138. })
  139. test(label + ' – async function', async ({ is }) => {
  140. const fn = async () => {}
  141. is(clone(fn), fn, 'same function')
  142. })
  143. test(label + ' – generator function', async ({ is }) => {
  144. const fn = function * () {}
  145. is(clone(fn), fn, 'same function')
  146. })
  147. test(label + ' – date', async ({ is, isNot }) => {
  148. const date = new Date()
  149. is(+clone(date), +date, 'same value')
  150. isNot(clone(date), date, 'different object')
  151. })
  152. test(label + ' – null', async ({ is }) => {
  153. is(clone(null), null, 'same value')
  154. })
  155. test(label + ' – shallow object', async ({ same, isNot }) => {
  156. const o = { a: 1, b: 2 }
  157. same(clone(o), o, 'same values')
  158. isNot(clone(o), o, 'different object')
  159. })
  160. test(label + ' – shallow array', async ({ same, isNot }) => {
  161. const o = [1, 2]
  162. same(clone(o), o, 'same values')
  163. isNot(clone(o), o, 'different arrays')
  164. })
  165. test(label + ' – deep object', async ({ same, isNot }) => {
  166. const o = { nest: { a: 1, b: 2 } }
  167. same(clone(o), o, 'same values')
  168. isNot(clone(o), o, 'different objects')
  169. isNot(clone(o).nest, o.nest, 'different nested objects')
  170. })
  171. test(label + ' – deep array', async ({ same, isNot }) => {
  172. const o = [{ a: 1, b: 2 }, [3]]
  173. same(clone(o), o, 'same values')
  174. isNot(clone(o), o, 'different arrays')
  175. isNot(clone(o)[0], o[0], 'different array elements')
  176. isNot(clone(o)[1], o[1], 'different array elements')
  177. })
  178. test(label + ' – nested number', async ({ is }) => {
  179. is(clone({ a: 1 }).a, 1, 'same value')
  180. })
  181. test(label + ' – nested string', async ({ is }) => {
  182. is(clone({ s: 'str' }).s, 'str', 'same value')
  183. })
  184. test(label + ' – nested boolean', async ({ is }) => {
  185. is(clone({ b: true }).b, true, 'same value')
  186. })
  187. test(label + ' – nested function', async ({ is }) => {
  188. const fn = () => {}
  189. is(clone({ fn }).fn, fn, 'same function')
  190. })
  191. test(label + ' – nested async function', async ({ is }) => {
  192. const fn = async () => {}
  193. is(clone({ fn }).fn, fn, 'same function')
  194. })
  195. test(label + ' – nested generator function', async ({ is }) => {
  196. const fn = function * () {}
  197. is(clone({ fn }).fn, fn, 'same function')
  198. })
  199. test(label + ' – nested date', async ({ is, isNot }) => {
  200. const date = new Date()
  201. is(+clone({ d: date }).d, +date, 'same value')
  202. isNot(clone({ d: date }).d, date, 'different object')
  203. })
  204. test(label + ' – nested date in array', async ({ is, isNot }) => {
  205. const date = new Date()
  206. is(+clone({ d: [date] }).d[0], +date, 'same value')
  207. isNot(clone({ d: [date] }).d[0], date, 'different object')
  208. is(+cloneCircles({ d: [date] }).d[0], +date, 'same value')
  209. isNot(cloneCircles({ d: [date] }).d, date, 'different object')
  210. })
  211. test(label + ' – nested null', async ({ is }) => {
  212. is(clone({ n: null }).n, null, 'same value')
  213. })
  214. test(label + ' – arguments', async ({ isNot, same }) => {
  215. function fn (...args) {
  216. same(clone(arguments), args, 'same values')
  217. isNot(clone(arguments), arguments, 'different object')
  218. }
  219. fn(1, 2, 3)
  220. })
  221. test(`${label} copies buffers from object correctly`, async ({ ok, is, isNot }) => {
  222. const input = Date.now().toString(36)
  223. const inputBuffer = Buffer.from(input)
  224. const clonedBuffer = clone({ a: inputBuffer }).a
  225. ok(Buffer.isBuffer(clonedBuffer), 'cloned value is buffer')
  226. isNot(clonedBuffer, inputBuffer, 'cloned buffer is not same as input buffer')
  227. is(clonedBuffer.toString(), input, 'cloned buffer content is correct')
  228. })
  229. test(`${label} copies buffers from arrays correctly`, async ({ ok, is, isNot }) => {
  230. const input = Date.now().toString(36)
  231. const inputBuffer = Buffer.from(input)
  232. const [clonedBuffer] = clone([inputBuffer])
  233. ok(Buffer.isBuffer(clonedBuffer), 'cloned value is buffer')
  234. isNot(clonedBuffer, inputBuffer, 'cloned buffer is not same as input buffer')
  235. is(clonedBuffer.toString(), input, 'cloned buffer content is correct')
  236. })
  237. test(`${label} copies TypedArrays from object correctly`, async ({ ok, is, isNot }) => {
  238. const [input1, input2] = [rnd(10), rnd(10)]
  239. const buffer = new ArrayBuffer(8)
  240. const int32View = new Int32Array(buffer)
  241. int32View[0] = input1
  242. int32View[1] = input2
  243. const cloned = clone({ a: int32View }).a
  244. ok(cloned instanceof Int32Array, 'cloned value is instance of class')
  245. isNot(cloned, int32View, 'cloned value is not same as input value')
  246. is(cloned[0], input1, 'cloned value content is correct')
  247. is(cloned[1], input2, 'cloned value content is correct')
  248. })
  249. test(`${label} copies TypedArrays from array correctly`, async ({ ok, is, isNot }) => {
  250. const [input1, input2] = [rnd(10), rnd(10)]
  251. const buffer = new ArrayBuffer(16)
  252. const int32View = new Int32Array(buffer)
  253. int32View[0] = input1
  254. int32View[1] = input2
  255. const [cloned] = clone([int32View])
  256. ok(cloned instanceof Int32Array, 'cloned value is instance of class')
  257. isNot(cloned, int32View, 'cloned value is not same as input value')
  258. is(cloned[0], input1, 'cloned value content is correct')
  259. is(cloned[1], input2, 'cloned value content is correct')
  260. })
  261. test(`${label} copies complex TypedArrays`, async ({ ok, deepEqual, is, isNot }) => {
  262. const [input1, input2, input3] = [rnd(10), rnd(10), rnd(10)]
  263. const buffer = new ArrayBuffer(4)
  264. const view1 = new Int8Array(buffer, 0, 2)
  265. const view2 = new Int8Array(buffer, 2, 2)
  266. const view3 = new Int8Array(buffer)
  267. view1[0] = input1
  268. view2[0] = input2
  269. view3[3] = input3
  270. const cloned = clone({ view1, view2, view3 })
  271. ok(cloned.view1 instanceof Int8Array, 'cloned value is instance of class')
  272. ok(cloned.view2 instanceof Int8Array, 'cloned value is instance of class')
  273. ok(cloned.view3 instanceof Int8Array, 'cloned value is instance of class')
  274. isNot(cloned.view1, view1, 'cloned value is not same as input value')
  275. isNot(cloned.view2, view2, 'cloned value is not same as input value')
  276. isNot(cloned.view3, view3, 'cloned value is not same as input value')
  277. deepEqual(Array.from(cloned.view1), [input1, 0], 'cloned value content is correct')
  278. deepEqual(Array.from(cloned.view2), [input2, input3], 'cloned value content is correct')
  279. deepEqual(Array.from(cloned.view3), [input1, 0, input2, input3], 'cloned value content is correct')
  280. })
  281. test(`${label} - maps`, async ({ same, isNot }) => {
  282. const map = new Map([['a', 1]])
  283. same(Array.from(clone(map)), [['a', 1]], 'same value')
  284. isNot(clone(map), map, 'different object')
  285. })
  286. test(`${label} - sets`, async ({ same, isNot }) => {
  287. const set = new Set([1])
  288. same(Array.from(clone(set)), [1])
  289. isNot(clone(set), set, 'different object')
  290. })
  291. test(`${label} - nested maps`, async ({ same, isNot }) => {
  292. const data = { m: new Map([['a', 1]]) }
  293. same(Array.from(clone(data).m), [['a', 1]], 'same value')
  294. isNot(clone(data).m, data.m, 'different object')
  295. })
  296. test(`${label} - nested sets`, async ({ same, isNot }) => {
  297. const data = { s: new Set([1]) }
  298. same(Array.from(clone(data).s), [1], 'same value')
  299. isNot(clone(data).s, data.s, 'different object')
  300. })
  301. }