diff --git a/index.js b/index.js index 9481391..f7d58cd 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,8 @@ function rfdc (opts) { [Set, (o, fn) => new Set(cloneArray(Array.from(o), fn))] ].concat(opts.constructorHandlers || [])) + let handler = null + return opts.proto ? cloneProto : clone function cloneArray (a, fn) { @@ -29,8 +31,8 @@ function rfdc (opts) { const cur = a[k] if (typeof cur !== 'object' || cur === null) { a2[k] = cur - } else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) { - a2[k] = constructorHandlers.get(cur.constructor)(cur, fn) + } else if (cur.constructor !== Object && (handler = constructorHandlers.get(cur.constructor))) { + a2[k] = handler(cur, fn) } else if (ArrayBuffer.isView(cur)) { a2[k] = copyBuffer(cur) } else { @@ -43,8 +45,8 @@ function rfdc (opts) { function clone (o) { if (typeof o !== 'object' || o === null) return o if (Array.isArray(o)) return cloneArray(o, clone) - if (o.constructor !== Object && constructorHandlers.has(o.constructor)) { - return constructorHandlers.get(o.constructor)(o, clone) + if (o.constructor !== Object && (handler = constructorHandlers.get(o.constructor))) { + return handler(o, clone) } const o2 = {} for (const k in o) { @@ -52,8 +54,8 @@ function rfdc (opts) { const cur = o[k] if (typeof cur !== 'object' || cur === null) { o2[k] = cur - } else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) { - o2[k] = constructorHandlers.get(cur.constructor)(cur, clone) + } else if (cur.constructor !== Object && (handler = constructorHandlers.get(cur.constructor))) { + o2[k] = handler(cur, clone) } else if (ArrayBuffer.isView(cur)) { o2[k] = copyBuffer(cur) } else { @@ -66,16 +68,16 @@ function rfdc (opts) { function cloneProto (o) { if (typeof o !== 'object' || o === null) return o if (Array.isArray(o)) return cloneArray(o, cloneProto) - if (o.constructor !== Object && constructorHandlers.has(o.constructor)) { - return constructorHandlers.get(o.constructor)(o, cloneProto) + if (o.constructor !== Object && (handler = constructorHandlers.get(o.constructor))) { + return handler(o, cloneProto) } const o2 = {} for (const k in o) { const cur = o[k] if (typeof cur !== 'object' || cur === null) { o2[k] = cur - } else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) { - o2[k] = constructorHandlers.get(cur.constructor)(cur, cloneProto) + } else if (cur.constructor !== Object && (handler = constructorHandlers.get(cur.constructor))) { + o2[k] = handler(cur, cloneProto) } else if (ArrayBuffer.isView(cur)) { o2[k] = copyBuffer(cur) } else { @@ -96,6 +98,7 @@ function rfdcCircles (opts) { [Set, (o, fn) => new Set(cloneArray(Array.from(o), fn))] ].concat(opts.constructorHandlers || [])) + let handler = null return opts.proto ? cloneProto : clone function cloneArray (a, fn) { @@ -106,8 +109,8 @@ function rfdcCircles (opts) { const cur = a[k] if (typeof cur !== 'object' || cur === null) { a2[k] = cur - } else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) { - a2[k] = constructorHandlers.get(cur.constructor)(cur, fn) + } else if (cur.constructor !== Object && (handler = constructorHandlers.get(cur.constructor))) { + a2[k] = handler(cur, fn) } else if (ArrayBuffer.isView(cur)) { a2[k] = copyBuffer(cur) } else { @@ -125,8 +128,8 @@ function rfdcCircles (opts) { function clone (o) { if (typeof o !== 'object' || o === null) return o if (Array.isArray(o)) return cloneArray(o, clone) - if (o.constructor !== Object && constructorHandlers.has(o.constructor)) { - return constructorHandlers.get(o.constructor)(o, clone) + if (o.constructor !== Object && (handler = constructorHandlers.get(o.constructor))) { + return handler(o, clone) } const o2 = {} refs.push(o) @@ -136,8 +139,8 @@ function rfdcCircles (opts) { const cur = o[k] if (typeof cur !== 'object' || cur === null) { o2[k] = cur - } else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) { - o2[k] = constructorHandlers.get(cur.constructor)(cur, clone) + } else if (cur.constructor !== Object && (handler = constructorHandlers.get(cur.constructor))) { + o2[k] = handler(cur, clone) } else if (ArrayBuffer.isView(cur)) { o2[k] = copyBuffer(cur) } else { @@ -157,8 +160,8 @@ function rfdcCircles (opts) { function cloneProto (o) { if (typeof o !== 'object' || o === null) return o if (Array.isArray(o)) return cloneArray(o, cloneProto) - if (o.constructor !== Object && constructorHandlers.has(o.constructor)) { - return constructorHandlers.get(o.constructor)(o, cloneProto) + if (o.constructor !== Object && (handler = constructorHandlers.get(o.constructor))) { + return handler(o, cloneProto) } const o2 = {} refs.push(o) @@ -167,8 +170,8 @@ function rfdcCircles (opts) { const cur = o[k] if (typeof cur !== 'object' || cur === null) { o2[k] = cur - } else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) { - o2[k] = constructorHandlers.get(cur.constructor)(cur, cloneProto) + } else if (cur.constructor !== Object && (handler = constructorHandlers.get(cur.constructor))) { + o2[k] = handler(cur, cloneProto) } else if (ArrayBuffer.isView(cur)) { o2[k] = copyBuffer(cur) } else {