115 lines
5.1 KiB
Markdown
115 lines
5.1 KiB
Markdown
# shallow-clone [![NPM version](https://img.shields.io/npm/v/shallow-clone.svg?style=flat)](https://www.npmjs.com/package/shallow-clone) [![NPM monthly downloads](https://img.shields.io/npm/dm/shallow-clone.svg?style=flat)](https://npmjs.org/package/shallow-clone) [![NPM total downloads](https://img.shields.io/npm/dt/shallow-clone.svg?style=flat)](https://npmjs.org/package/shallow-clone) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/shallow-clone.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/shallow-clone)
|
|
|
|
> Make a shallow clone of an object, array or primitive.
|
|
|
|
## Install
|
|
|
|
Install with [npm](https://www.npmjs.com/):
|
|
|
|
```sh
|
|
$ npm install --save shallow-clone
|
|
```
|
|
|
|
## Usage
|
|
|
|
```js
|
|
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).
|
|
|
|
```js
|
|
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
|
|
|
|
```js
|
|
clone(0)
|
|
//=> 0
|
|
|
|
clone('foo')
|
|
//=> 'foo'
|
|
```
|
|
|
|
## shallow clone a regex
|
|
|
|
```js
|
|
clone(/foo/g)
|
|
//=> /foo/g
|
|
```
|
|
|
|
## shallow clone an object
|
|
|
|
```js
|
|
clone({a: 1, b: 2, c: 3 })
|
|
//=> {a: 1, b: 2, c: 3 }
|
|
```
|
|
|
|
## About
|
|
|
|
### Related projects
|
|
|
|
* [assign-deep](https://www.npmjs.com/package/assign-deep): Deeply assign the enumerable properties and/or es6 Symbol properies of source objects to the target… [more](https://github.com/jonschlinkert/assign-deep) | [homepage](https://github.com/jonschlinkert/assign-deep "Deeply assign the enumerable properties and/or es6 Symbol properies of source objects to the target (first) object.")
|
|
* [clone-deep](https://www.npmjs.com/package/clone-deep): Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives. | [homepage](https://github.com/jonschlinkert/clone-deep "Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives.")
|
|
* [extend-shallow](https://www.npmjs.com/package/extend-shallow): Extend an object with the properties of additional objects. node.js/javascript util. | [homepage](https://github.com/jonschlinkert/extend-shallow "Extend an object with the properties of additional objects. node.js/javascript util.")
|
|
* [is-plain-object](https://www.npmjs.com/package/is-plain-object): Returns true if an object was created by the `Object` constructor. | [homepage](https://github.com/jonschlinkert/is-plain-object "Returns true if an object was created by the `Object` constructor.")
|
|
* [isobject](https://www.npmjs.com/package/isobject): Returns true if the value is an object and not an array or null. | [homepage](https://github.com/jonschlinkert/isobject "Returns true if the value is an object and not an array or null.")
|
|
* [kind-of](https://www.npmjs.com/package/kind-of): Get the native type of a value. | [homepage](https://github.com/jonschlinkert/kind-of "Get the native type of a value.")
|
|
* [mixin-deep](https://www.npmjs.com/package/mixin-deep): Deeply mix the properties of objects into the first object. Like merge-deep, but doesn't clone. | [homepage](https://github.com/jonschlinkert/mixin-deep "Deeply mix the properties of objects into the first object. Like merge-deep, but doesn't clone.")
|
|
* [mixin-object](https://www.npmjs.com/package/mixin-object): Mixin the own and inherited properties of other objects onto the first object. Pass an… [more](https://github.com/jonschlinkert/mixin-object) | [homepage](https://github.com/jonschlinkert/mixin-object "Mixin the own and inherited properties of other objects onto the first object. Pass an empty object as the first arg to shallow clone.")
|
|
|
|
### Contributing
|
|
|
|
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
|
|
|
### Contributors
|
|
|
|
| **Commits** | **Contributor** |
|
|
| --- | --- |
|
|
| 2 | [doowb](https://github.com/doowb) |
|
|
| 2 | [jonschlinkert](https://github.com/jonschlinkert) |
|
|
|
|
### Building docs
|
|
|
|
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
|
|
|
|
To generate the readme, run the following command:
|
|
|
|
```sh
|
|
$ 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:
|
|
|
|
```sh
|
|
$ npm install && npm test
|
|
```
|
|
|
|
### Author
|
|
|
|
**Jon Schlinkert**
|
|
|
|
* [github/jonschlinkert](https://github.com/jonschlinkert)
|
|
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
|
|
|
|
### License
|
|
|
|
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
|
|
Released under the [MIT License](LICENSE).
|
|
|
|
***
|
|
|
|
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on July 16, 2017._ |