Copy
Copy or deep clone a value to an arbitrary depth.
Installation
$ npm install utils-copy
Usage
var cp = ;
cp( value[, level] )
Copy or deep clone a value
to an arbitrary depth. The function
accepts both objects
and primitives
.
var value copy;// Primitives...value = 'beep';copy = ;// returns 'beep'// Objects...value = 'a':1'b':true'c':123;copy = ;// returns [{'a':1,'b':true,'c':[1,2,3]}]console;// returns false
The default behavior returns a full deep copy of any object
. To limit the copy depth, set the level
option.
var value copy;value = 'a':1'b':true'c':123;// Trivial case => return the same referencecopy = ;// returns [{'a':1,'b':true,'c':[1,2,3]}]console;// returns true// Shallow copy:copy = ;console;// returns falseconsole;// returns true// Deep copy:copy = ;console;// returns false
Notes
-
List of supported values/types:
undefined
null
boolean
/Boolean
string
/String
number
/Number
function
Object
Date
RegExp
Set
Map
Error
URIError
ReferenceError
SyntaxError
RangeError
EvalError
TypeError
Array
Int8Array
Uint8Array
Uint8ClampedArray
Init16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
Buffer
(Node.js)
-
List of unsupported values/types:
DOMElement
: to copy DOM elements, useelement.cloneNode()
.Symbol
WeakMap
WeakSet
Blob
File
FileList
ImageData
ImageBitmap
ArrayBuffer
-
The implementation can handle circular references.
-
If a
Number
,String
, orBoolean
object is encountered, the value is cloned as a primitive. This behavior is intentional. The implementation is opinionated in wanting to avoid creatingnumbers
,strings
, andbooleans
via thenew
operator and a constructor. -
For
objects
, the implementation only copiesenumerable
keys and their associated property descriptors. -
The implementation only checks whether basic
Objects
,Arrays
, and class instances areextensible
,sealed
, and/orfrozen
. -
functions
are not cloned; their reference is copied. -
Support for copying class instances is inherently fragile. Any instances with privileged access to variables (e.g., within closures) cannot be cloned. This stated, basic copying of class instances is supported. Provided an environment which supports ES5, the implementation is greedy and performs a deep clone of any arbitrary class instance and its properties. The implementation assumes that the concept of
level
applies only to the class instance reference, but not to its internal state.{this_data = 1 2 3 4 ;this_name = 'bar';return this;}var foo = ;var fooey = ;console;// returns trueconsole;// returns falseconsole;// returns true
Examples
var cp = ;var arr ='x':'y': MathMath'z': 1234'label': 'Beep''x':'y': MathMath'z': 3124'label': 'Boop';var copy = ;console;// returns falseconsole;// returns falsecopy = ;console;// returns trueconsole;// returns true
To run the example code from the top-level application directory,
$ node ./examples/index.js
Tests
Unit
This repository uses tape for unit tests. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
Browser Support
This repository uses Testling for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:
$ make test-browsers
To view the tests in a local web browser,
$ make view-browser-tests
License
Copyright
Copyright © 2015-2016. Athan Reines.