Socket
Socket
Sign inDemoInstall

utils-copy

Package Overview
Dependencies
17
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.1.0

lib/deepcopy.js

255

lib/index.js

@@ -1,29 +0,1 @@

/**
*
* UTILS: copy
*
*
* DESCRIPTION:
* - Copy or deep clone a value to an arbitrary depth.
*
*
* NOTES:
* [1]
*
*
* TODO:
* [1]
*
*
* LICENSE:
* MIT
*
* Copyright (c) 2015. Athan Reines.
*
*
* AUTHOR:
* Athan Reines. kgryte@gmail.com. 2015.
*
*/
'use strict';

@@ -33,227 +5,10 @@

var isArray = require( 'validate.io-array' ),
isBuffer = require( 'validate.io-buffer' ),
isNonNegativeInteger = require( 'validate.io-nonnegative-integer' ),
typeName = require( 'type-name' );
var isArray = require( 'validate.io-array' );
var isNonNegativeInteger = require( 'validate.io-nonnegative-integer' );
var deepCopy = require( './deepcopy.js' );
// FUNCTIONS //
// COPY //
/**
* FUNCTION: indexOf( arr, val )
* Returns the array index of a value. If the array does not contain the value, the function returns `-1`.
*
* @private
* @param {Array} arr - array
* @param {*} val - value for which to search
* @returns {Number} array index
*/
function indexOf( arr, val ) {
var len = arr.length,
i;
for ( i = 0; i < len; i++ ) {
if ( arr[ i ] === val ) {
return i;
}
}
return -1;
} // end FUNCTION indexOf()
/**
* FUNCTION: objectKeys( obj )
* Returns an object's keys.
*
* @private
* @param {Array|Object} obj - object
* @returns {Array} array of keys
*/
function objectKeys( obj ) {
var keys = [],
key;
for ( key in obj ) {
if ( obj.hasOwnProperty( key ) ) {
keys.push( key );
}
}
return keys;
} // end METHOD objectKeys()
/**
* Create functions for copying typed arrays.
*/
var typedArrays = {
'Int8Array': null,
'Uint8Array': null,
'Uint8ClampedArray': null,
'Int16Array': null,
'Uint16Array': null,
'Int32Array': null,
'Uint32Array': null,
'Float32Array': null,
'Float64Array': null
};
(function createTypedArrayFcns() {
/* jshint evil:true */
var keys = objectKeys( typedArrays ),
len = keys.length,
key,
i;
for ( i = 0; i < len; i++ ) {
key = keys[ i ];
typedArrays[ key ] = new Function( 'arr', 'return new '+key+'( arr );' );
}
})();
/**
* FUNCTION: cloneInstance( val )
* Clones a class instance.
*
* WARNING: this should only be used for simple cases. Any instances with privileged access to variables (e.g., within closures) cannot be cloned. This approach should be considered fragile.
*
* NOTE: the function is greedy, disregarding the notion of a 'level'. Instead, the function deep copies all properties, as we assume the concept of 'level' applies only to the class instance reference but not to its internal state. This prevents, in theory, two instances from sharing state.
*
* @private
* @param {Object} val - class instance
* @returns {Object} new instance
*/
function cloneInstance( val ) {
var cache = [],
refs = [],
names,
name,
desc,
tmp,
ref,
i;
ref = Object.create( Object.getPrototypeOf( val ) );
cache.push( val );
refs.push( ref );
names = Object.getOwnPropertyNames( val );
for ( i = 0; i < names.length; i++ ) {
name = names[ i ];
desc = Object.getOwnPropertyDescriptor( val, name );
if ( desc.hasOwnProperty( 'value' ) ) {
tmp = ( isArray( val[name] ) ) ? [] : {};
desc.value = deepCopy( val[name], tmp, cache, refs, -1 );
}
Object.defineProperty( ref, name, desc );
}
if ( !Object.isExtensible( val ) ) {
Object.preventExtensions( ref );
}
if ( Object.isSealed( val ) ) {
Object.seal( ref );
}
if ( Object.isFrozen( val ) ) {
Object.freeze( ref );
}
return ref;
} // end FUNCTION cloneInstance()
// DEEP COPY //
/**
* FUNCTION: deepCopy( val, copy, cache, refs, level )
* Recursively performs a deep copy of an input object.
*
* @private
* @param {Array|Object} val - value to copy
* @param {Array|Object} copy - copy
* @param {Array} cache - an array of visited objects
* @param {Array} refs - an array of object references
* @param {Number} level - copy depth
* @returns {*} deep copy
*/
function deepCopy( val, copy, cache, refs, level ) {
var keys,
name,
key,
ref,
x,
i, j;
// [-] Decrement the level...
level = level - 1;
// [0] Primitives and functions...
if ( typeof val !== 'object' || val === null ) {
return val;
}
// [1] Node.js Buffer objects...
if ( isBuffer( val ) ) {
return new val.constructor( val );
}
name = typeName( val );
// [2] Sets and Maps...
if ( name === 'Set' || name === 'Map' ) {
console.warn( 'copy()::not supported. Copying Sets and Maps is not currently supported. File an issue, submit a pull request, or contact the author directly at kgryte@gmail.com.' );
return {};
}
// [3] Number, String, and Boolean objects...
if ( name === 'String' || name === 'Boolean' || name === 'Number' ) {
// Return an equivalent primitive!
return val.valueOf();
}
// [4] Date objects...
if ( name === 'Date' ) {
return new Date( +val );
}
// [5] Regular expressions...
if ( name === 'RegExp' ) {
val = val.toString();
i = val.lastIndexOf( '/' );
return new RegExp( val.slice( 1, i ), val.slice( i+1 ) );
}
// [6] Typed arrays...
if ( typedArrays.hasOwnProperty( name ) ) {
return typedArrays[ name ]( val );
}
// [7] Class instances...
if ( name !== 'Array' && name !== 'Object' ) {
// Require ES5 or higher...
if ( typeof Object.freeze === 'function' ) {
return cloneInstance( val );
}
return {};
}
// [8] Arrays and objects...
keys = objectKeys( val );
if ( level > 0 ) {
for ( j = 0; j < keys.length; j++ ) {
key = keys[ j ];
x = val[ key ];
// [8.1] Primitive, Buffer, special class instance...
name = typeName( x );
if ( typeof x !== 'object' || x === null || (name !== 'Array' && name !== 'Object') || isBuffer( x ) ) {
copy[ key ] = deepCopy( x );
continue;
}
// [8.2] Circular reference...
i = indexOf( cache, x );
if ( i !== -1 ) {
copy[ key ] = refs[ i ];
continue;
}
// [8.3] Plain array or object...
ref = ( isArray(x) ) ? [] : {};
cache.push( x );
refs.push( ref );
copy[ key ] = deepCopy( x, ref, cache, refs, level );
}
} else {
for ( j = 0; j < keys.length; j++ ) {
key = keys[ j ];
copy[ key ] = val[ key ];
}
}
return copy;
} // end FUNCTION deepCopy()
/**
* FUNCTION: createCopy( value[, level] )

@@ -270,3 +25,3 @@ * Copy or deep clone a value to an arbitrary depth.

if ( !isNonNegativeInteger( level ) ) {
throw new TypeError( 'copy()::invalid input argument. Level must be a nonnegative integer. Value: `' + level + '`.' );
throw new TypeError( 'invalid input argument. Level must be a nonnegative integer. Value: `' + level + '`.' );
}

@@ -273,0 +28,0 @@ if ( level === 0 ) {

{
"name": "utils-copy",
"version": "1.0.0",
"version": "1.1.0",
"description": "Copy or deep clone a value to an arbitrary depth.",

@@ -16,5 +16,8 @@ "author": {

"scripts": {
"test": "./node_modules/.bin/mocha",
"test-cov": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --dir ./reports/coverage -- -R spec",
"coveralls": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --dir ./reports/coveralls/coverage --report lcovonly -- -R spec && cat ./reports/coveralls/coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./reports/coveralls"
"test": "if [ \"${TRAVIS}\" ]; then npm run test-ci; else npm run test-local; fi",
"test-local": "tape \"./test/*.js\" | tap-spec",
"test-ci": "npm run test-local && xvfb-run npm run test-browsers",
"test-cov": "istanbul cover --dir ./reports/coverage --report lcov tape -- \"./test/*.js\"",
"test-browsers": "browserify ./test/*.js | testling | tap-spec",
"coverage": "istanbul cover --dir ./reports/codecov/coverage --report lcovonly tape -- \"./test/*.js\" && cat ./reports/codecov/coverage/lcov.info | codecov && rm -rf ./reports/codecov"
},

@@ -40,3 +43,8 @@ "main": "./lib",

"date",
"regexp"
"regexp",
"typed array",
"error",
"set",
"map",
"cp"
],

@@ -47,21 +55,40 @@ "bugs": {

"dependencies": {
"type-name": "^1.0.1",
"object-keys": "^1.0.9",
"type-name": "^1.1.0",
"utils-copy-error": "^1.0.0",
"utils-regex-from-string": "^1.0.0",
"validate.io-array": "^1.0.3",
"validate.io-buffer": "^1.0.0",
"validate.io-buffer": "^1.0.1",
"validate.io-nonnegative-integer": "^1.0.0"
},
"devDependencies": {
"chai": "2.x.x",
"mocha": "2.x.x",
"coveralls": "^2.11.1",
"istanbul": "^0.3.0",
"browserify": "12.x.x",
"chai": "^3.4.1",
"codecov.io": "^0.1.5",
"istanbul": "^0.4.1",
"jshint": "2.x.x",
"jshint-stylish": "^1.0.0"
"jshint-stylish": "2.x.x",
"tap-spec": "4.x.x",
"tape": "4.x.x",
"testling": "1.x.x"
},
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/MIT"
}
]
"testling": {
"files": [
"test/*.js"
],
"browsers": [
"iexplore/6.0..latest",
"firefox/3.0..latest",
"firefox/nightly",
"chrome/4.0..latest",
"chrome/canary",
"opera/10.0..latest",
"opera/next",
"safari/4.0..latest",
"ipad/6.0..latest",
"iphone/6.0..latest",
"android-browser/4.2..latest"
]
},
"license": "MIT"
}
Copy
===
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependencies][dependencies-image]][dependencies-url]
[![NPM version][npm-image]][npm-url] [![Build Status][build-image]][build-url] [![Coverage Status][coverage-image]][coverage-url] [![Dependencies][dependencies-image]][dependencies-url]

@@ -14,12 +14,10 @@ > Copy or deep clone a value to an arbitrary depth.

For use in the browser, use [browserify](https://github.com/substack/node-browserify).
## Usage
``` javascript
var createCopy = require( 'utils-copy' );
var cp = require( 'utils-copy' );
```
#### createCopy( value[, level] )
#### cp( value[, level] )

@@ -33,3 +31,3 @@ Copy or deep clone a `value` to an arbitrary depth. The `function` accepts both `objects` and `primitives`.

value = 'beep';
copy = createCopy( value );
copy = cp( value );
// returns 'beep'

@@ -39,3 +37,3 @@

value = [{'a':1,'b':true,'c':[1,2,3]}];
copy = createCopy( value );
copy = cp( value );
// returns [{'a':1,'b':true,'c':[1,2,3]}]

@@ -47,3 +45,3 @@

The default behavior returns a __full__ deep copy of any `objects`. To limit the copy depth, set the `level` option.
The default behavior returns a __full__ deep copy of any `object`. To limit the copy depth, set the `level` option.

@@ -56,3 +54,3 @@ ``` javascript

// Trivial case => return the same reference
copy = createCopy( value, 0 );
copy = cp( value, 0 );
// returns [{'a':1,'b':true,'c':[1,2,3]}]

@@ -64,3 +62,3 @@

// Shallow copy:
copy = createCopy( value, 1 );
copy = cp( value, 1 );

@@ -74,3 +72,3 @@ console.log( value[0] === copy[0] );

// Deep copy:
copy = createCopy( value, 2 );
copy = cp( value, 2 );

@@ -94,2 +92,11 @@ console.log( value[0].c === copy[0].c );

- `RegExp`
- `Set`
- `Map`
- `Error`
- `URIError`
- `ReferenceError`
- `SyntaxError`
- `RangeError`
- `EvalError`
- `TypeError`
- `Array`

@@ -105,17 +112,20 @@ - `Int8Array`

- `Float64Array`
- `Buffer` ([Node.js]((http://nodejs.org/api/buffer.html)))
- `Buffer` ([Node.js][node-buffer])
* List of __unsupported__ values/types:
- `DOMElement`: to copy DOM elements, use `.cloneNode()`.
- `Set`
- `Map`
- `Error`
- `URIError`
- `ReferenceError`
- `SyntaxError`
- `RangeError`
- `DOMElement`: to copy DOM elements, use `element.cloneNode()`.
- `Symbol`
- `WeakMap`
- `WeakSet`
- `Blob`
- `File`
- `FileList`
- `ImageData`
- `ImageBitmap`
- `ArrayBuffer`
* If you need support for any of the above types, feel free to file an issue or submit a pull request.
* The implementation can handle circular references.
* If a `Number`, `String`, or `Boolean` object is encountered, the value is cloned as a primitive. This behavior is intentional. The implementation is opinionated in wanting to __avoid__ creating `numbers`, `strings`, and `booleans` via the `new` operator and a constructor.
* The implementation __can__ handle circular references.
* If a `Number`, `String`, or `Boolean` object is encountered, the value is cloned as a __primitive__. This behavior is intentional. The implementation is opinionated in wanting to __avoid__ creating `numbers`, `strings`, and `booleans` via the `new` operator and a constructor.
* For `objects`, the implementation __only__ copies `enumerable` keys and their associated property descriptors.
* The implementation __only__ checks whether basic `Objects`, `Arrays`, and class instances are `extensible`, `sealed`, and/or `frozen`.
* `functions` are __not__ cloned; their reference is copied.

@@ -132,3 +142,3 @@ * 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.

var foo = new Foo();
var fooey = createCopy( foo );
var fooey = cp( foo );

@@ -145,16 +155,7 @@ console.log( foo._name === fooey._name );

* Re: __why__ this implementation and not the many other [copy](https://github.com/victusfate/copyjs/blob/master/lib/copy.js)/[deep copy](https://github.com/sasaplus1/deepcopy.js)/[clone](https://github.com/dankogai/js-object-clone)/[deep clone](https://github.com/evlun/copy/blob/master/copy.js) modules out there.
1. They are buggy. For example, circular references are not properly tracked.
2. They fail to account for `Number`, `String`, and `Boolean` objects.
3. They fail to properly validate if a value is a Node `Buffer` object. They assume, for instance, a Node environment.
4. They fail to clone class instances.
5. They do not allow limiting the copy depth.
6. They assume an `array` or `object` input value.
7. They are not sufficiently tested.
## Examples
``` javascript
var createCopy = require( 'utils-copy' );
var cp = require( 'utils-copy' );

@@ -176,3 +177,3 @@ var arr = [

var copy = createCopy( arr );
var copy = cp( arr );

@@ -186,3 +187,3 @@ console.log( arr[ 0 ] === copy[ 0 ] );

copy = createCopy( arr, 1 );
copy = cp( arr, 1 );

@@ -203,2 +204,3 @@ console.log( arr[ 0 ] === copy[ 0 ] );

---
## Tests

@@ -208,3 +210,3 @@

Unit tests use the [Mocha](http://mochajs.org) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory:
This repository uses [tape][tape] for unit tests. To run the tests, execute the following command in the top-level application directory:

@@ -220,3 +222,3 @@ ``` bash

This repository uses [Istanbul](https://github.com/gotwarlost/istanbul) as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
This repository uses [Istanbul][istanbul] as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

@@ -234,2 +236,19 @@ ``` bash

### Browser Support
This repository uses [Testling][testling] for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:
``` bash
$ make test-browsers
```
To view the tests in a local web browser,
``` bash
$ make view-browser-tests
```
<!-- [![browser support][browsers-image]][browsers-url] -->
---

@@ -249,7 +268,7 @@ ## License

[travis-image]: http://img.shields.io/travis/kgryte/utils-copy/master.svg
[travis-url]: https://travis-ci.org/kgryte/utils-copy
[build-image]: http://img.shields.io/travis/kgryte/utils-copy/master.svg
[build-url]: https://travis-ci.org/kgryte/utils-copy
[coveralls-image]: https://img.shields.io/coveralls/kgryte/utils-copy/master.svg
[coveralls-url]: https://coveralls.io/r/kgryte/utils-copy?branch=master
[coverage-image]: https://img.shields.io/codecov/c/github/kgryte/utils-copy/master.svg
[coverage-url]: https://codecov.io/github/kgryte/utils-copy?branch=master

@@ -264,1 +283,15 @@ [dependencies-image]: http://img.shields.io/david/kgryte/utils-copy.svg

[github-issues-url]: https://github.com/kgryte/utils-copy/issues
[browsers-image]: https://ci.testling.com/kgryte/utils-copy.png
[browsers-url]: https://ci.testling.com/kgryte/utils-copy
[tape]: https://github.com/substack/tape
[istanbul]: https://github.com/gotwarlost/istanbul
[testling]: https://ci.testling.com
[node-buffer]: http://nodejs.org/api/buffer.html
[victusfate-copyjs]: https://github.com/victusfate/copyjs/blob/master/lib/copy.js
[sasaplus1-deepcopy]: https://github.com/sasaplus1/deepcopy.js
[dankogai-clone]: https://github.com/dankogai/js-object-clone
[evlun-copy]: https://github.com/evlun/copy/blob/master/copy.js
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc