Skip to content

Commit

Permalink
Merge pull request #23 from JammSpread/master
Browse files Browse the repository at this point in the history
feat: support cloning sets and maps
  • Loading branch information
davidmarkclements authored Mar 14, 2021
2 parents a6cb5f0 + 4b284f2 commit e0601b7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function rfdc (opts) {
if (typeof o !== 'object' || o === null) return o
if (o instanceof Date) return new Date(o)
if (Array.isArray(o)) return cloneArray(o, clone)
if (o instanceof Map) return new Map(cloneArray(Array.from(o), clone))
if (o instanceof Set) return new Set(cloneArray(Array.from(o), clone))
var o2 = {}
for (var k in o) {
if (Object.hasOwnProperty.call(o, k) === false) continue
Expand All @@ -46,6 +48,10 @@ function rfdc (opts) {
o2[k] = cur
} else if (cur instanceof Date) {
o2[k] = new Date(cur)
} else if (cur instanceof Map) {
o2[k] = new Map(cloneArray(Array.from(cur), clone))
} else if (cur instanceof Set) {
o2[k] = new Set(cloneArray(Array.from(cur), clone))
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
} else {
Expand All @@ -59,13 +65,19 @@ function rfdc (opts) {
if (typeof o !== 'object' || o === null) return o
if (o instanceof Date) return new Date(o)
if (Array.isArray(o)) return cloneArray(o, cloneProto)
if (o instanceof Map) return new Map(cloneArray(Array.from(o), cloneProto))
if (o instanceof Set) return new Set(cloneArray(Array.from(o), cloneProto))
var o2 = {}
for (var k in o) {
var cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (cur instanceof Date) {
o2[k] = new Date(cur)
} else if (cur instanceof Map) {
o2[k] = new Map(cloneArray(Array.from(cur), cloneProto))
} else if (cur instanceof Set) {
o2[k] = new Set(cloneArray(Array.from(cur), cloneProto))
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
} else {
Expand Down Expand Up @@ -110,6 +122,8 @@ function rfdcCircles (opts) {
if (typeof o !== 'object' || o === null) return o
if (o instanceof Date) return new Date(o)
if (Array.isArray(o)) return cloneArray(o, clone)
if (o instanceof Map) return new Map(cloneArray(Array.from(o), clone))
if (o instanceof Set) return new Set(cloneArray(Array.from(o), clone))
var o2 = {}
refs.push(o)
refsNew.push(o2)
Expand All @@ -120,6 +134,10 @@ function rfdcCircles (opts) {
o2[k] = cur
} else if (cur instanceof Date) {
o2[k] = new Date(cur)
} else if (cur instanceof Map) {
o2[k] = new Map(cloneArray(Array.from(cur), clone))
} else if (cur instanceof Set) {
o2[k] = new Set(cloneArray(Array.from(cur), clone))
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
} else {
Expand All @@ -140,6 +158,8 @@ function rfdcCircles (opts) {
if (typeof o !== 'object' || o === null) return o
if (o instanceof Date) return new Date(o)
if (Array.isArray(o)) return cloneArray(o, cloneProto)
if (o instanceof Map) return new Map(cloneArray(Array.from(o), cloneProto))
if (o instanceof Set) return new Set(cloneArray(Array.from(o), cloneProto))
var o2 = {}
refs.push(o)
refsNew.push(o2)
Expand All @@ -149,6 +169,10 @@ function rfdcCircles (opts) {
o2[k] = cur
} else if (cur instanceof Date) {
o2[k] = new Date(cur)
} else if (cur instanceof Map) {
o2[k] = new Map(cloneArray(Array.from(cur), cloneProto))
} else if (cur instanceof Set) {
o2[k] = new Set(cloneArray(Array.from(cur), cloneProto))
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
} else {
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,24 @@ function types (clone, label) {
deepEqual(Array.from(cloned.view2), [input2, input3], 'cloned value content is correct')
deepEqual(Array.from(cloned.view3), [input1, 0, input2, input3], 'cloned value content is correct')
})
test(`${label} - maps`, async ({ same, isNot }) => {
const map = new Map([['a', 1]])
same(Array.from(clone(map)), [['a', 1]], 'same value')
isNot(clone(map), map, 'different object')
})
test(`${label} - sets`, async ({ same, isNot }) => {
const set = new Set([1])
same(Array.from(clone(set)), [1])
isNot(clone(set), set, 'different object')
})
test(`${label} - nested maps`, async ({ same, isNot }) => {
const data = { m: new Map([['a', 1]]) }
same(Array.from(clone(data).m), [['a', 1]], 'same value')
isNot(clone(data).m, data.m, 'different object')
})
test(`${label} - nested sets`, async ({ same, isNot }) => {
const data = { s: new Set([1]) }
same(Array.from(clone(data).s), [1], 'same value')
isNot(clone(data).s, data.s, 'different object')
})
}

0 comments on commit e0601b7

Please # to comment.