shallow-clone NPM version NPM monthly downloads NPM total downloads Linux Build Status

Make a shallow clone of an object, array or primitive.

Install

Install with npm:

$ npm install --save shallow-clone

Usage

var clone = require('shallow-clone');

shallow clones arrays

The array itself is cloned, but not the elements of the array. So any objects in the array will still not be cloned (e.g. they will be the same object as in the orginal array).

var arr = [{ 'a': 0 }, { 'b': 1 }]
var foo = clone(arr);
// foo =>  [{ 'a': 0 }, { 'b': 1 }]

// array is cloned
assert.equal(actual === expected, false);

// array elements are not
assert.deepEqual(actual[0], expected[0]); // true

returns primitives as-is

clone(0)
//=> 0

clone('foo')
//=> 'foo'

shallow clone a regex

clone(/foo/g)
//=> /foo/g

shallow clone an object

clone({a: 1, b: 2, c: 3 })
//=> {a: 1, b: 2, c: 3 }

About

Related projects

  • assign-deep: Deeply assign the enumerable properties and/or es6 Symbol properies of source objects to the target… more | homepage
  • clone-deep: Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives. | homepage
  • extend-shallow: Extend an object with the properties of additional objects. node.js/javascript util. | homepage
  • is-plain-object: Returns true if an object was created by the Object constructor. | homepage
  • isobject: Returns true if the value is an object and not an array or null. | homepage
  • kind-of: Get the native type of a value. | homepage
  • mixin-deep: Deeply mix the properties of objects into the first object. Like merge-deep, but doesn't clone. | homepage
  • mixin-object: Mixin the own and inherited properties of other objects onto the first object. Pass an… more | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Contributors

CommitsContributor
2doowb
2jonschlinkert

Building docs

(This project‘s readme.md is generated by verb, please don’t edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on July 16, 2017.