node-clone-object
Clone object with:
- Circular references
- Property descriptors
- Symbol properties
- Non-enumerable properties
Install
npm i clone-obj
// or
yarn add clone-obj
Requires node >= 6
Usage
const clone = const newObj =
undefined
or null
clone-obj
will throw TypeError: Cannot clone undefined or null
when cloning undefined
or null
.
Primitive values
clone-obj
will return primitive values directly.
=== 1 === true === 'foo'
Objects
new
operator
Object created by clone-obj
will use obj.constructor
to create new object.
const arr = !== arr const foo = const bar = bar !== foobar === foo const foo = 'foo'const bar = Bufferbar !== foobar === foo
Plain object
const foo = a: 1 const bar = bar !== foobara === fooa
Circular references
clone-obj
will auto link circular references with cloned objects.
const foo = {}fooself = fooconst bar = foo !== barfooself !== barselfbar === barselfconsole // { self: [Circular] }
Property descriptors
clone-obj
respects property descriptors.
const foo = { return this_baz * 2 } { this_baz = val }const bar = typeof bar_baz === 'undefined'barbaz = 2bar_baz === 2barbaz === 4
Symbols properties
const sym = Symbol'foo'const foo = sym: 1 const bar = barbarsym === foosym
Non-enumerable properties
const foo = {}Objectconst bar = const descriptor = Objectdescriptorenumrable === falseObjectlength === 0foobaz === barbaz === descriptorvalue
Arrays
const foo = 1 {}const bar = Arrayfoo !== barfoolength === barlengthfoo0 === bar0foo1 !== bar1 const foo = a: 1 const bar = Arrayfooa !== barafooalength === baralength