1 | # shallow-clone [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W8YFZ425KND68) [![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)
|
---|
2 |
|
---|
3 | > Creates a shallow clone of any JavaScript value.
|
---|
4 |
|
---|
5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
|
---|
6 |
|
---|
7 | ## Install
|
---|
8 |
|
---|
9 | Install with [npm](https://www.npmjs.com/):
|
---|
10 |
|
---|
11 | ```sh
|
---|
12 | $ npm install --save shallow-clone
|
---|
13 | ```
|
---|
14 |
|
---|
15 | ## Usage
|
---|
16 |
|
---|
17 | ```js
|
---|
18 | const clone = require('shallow-clone');
|
---|
19 | ```
|
---|
20 |
|
---|
21 | **Supports**
|
---|
22 |
|
---|
23 | * array buffers
|
---|
24 | * arrays
|
---|
25 | * buffers
|
---|
26 | * dates
|
---|
27 | * errors
|
---|
28 | * float32 arrays
|
---|
29 | * float64 arrays
|
---|
30 | * int16 arrays
|
---|
31 | * int32 arrays
|
---|
32 | * int8 arrays
|
---|
33 | * maps
|
---|
34 | * objects
|
---|
35 | * primitives
|
---|
36 | * regular expressions
|
---|
37 | * sets
|
---|
38 | * symbols
|
---|
39 | * uint16 arrays
|
---|
40 | * uint32 arrays
|
---|
41 | * uint8 arrays
|
---|
42 | * uint8clamped arrays
|
---|
43 |
|
---|
44 | ## Arrays
|
---|
45 |
|
---|
46 | By default, only the array itself is cloned (shallow), use [clone-deep](https://github.com/jonschlinkert/clone-deep) if you also need the elements in the array to be cloned.
|
---|
47 |
|
---|
48 | ```js
|
---|
49 | const arr = [{ a: 0 }, { b: 1 }];
|
---|
50 | const foo = clone(arr);
|
---|
51 | // foo => [{ 'a': 0 }, { 'b': 1 }]
|
---|
52 |
|
---|
53 | // array is cloned
|
---|
54 | assert(actual === expected); // false
|
---|
55 |
|
---|
56 | // array elements are not
|
---|
57 | assert.deepEqual(actual[0], expected[0]); // true
|
---|
58 | ```
|
---|
59 |
|
---|
60 | ## Objects
|
---|
61 |
|
---|
62 | Only the object is shallow cloned, use [clone-deep](https://github.com/jonschlinkert/clone-deep) if you also need the values in the object to be cloned.
|
---|
63 |
|
---|
64 | ```js
|
---|
65 | console.log(clone({ a: 1, b: 2, c: 3 }));
|
---|
66 | //=> {a: 1, b: 2, c: 3 }
|
---|
67 | ```
|
---|
68 |
|
---|
69 | ## RegExp
|
---|
70 |
|
---|
71 | Clones regular expressions and flags, and preserves the `.lastIndex`.
|
---|
72 |
|
---|
73 | ```js
|
---|
74 | const regex = clone(/foo/g); //=> /foo/g
|
---|
75 | // you can manually reset lastIndex if necessary
|
---|
76 | regex.lastIndex = 0;
|
---|
77 | ```
|
---|
78 |
|
---|
79 | ## Primitives
|
---|
80 |
|
---|
81 | Simply returns primitives unchanged.
|
---|
82 |
|
---|
83 | ```js
|
---|
84 | clone(0); //=> 0
|
---|
85 | clone('foo'); //=> 'foo'
|
---|
86 | ```
|
---|
87 |
|
---|
88 | ## About
|
---|
89 |
|
---|
90 | <details>
|
---|
91 | <summary><strong>Contributing</strong></summary>
|
---|
92 |
|
---|
93 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
---|
94 |
|
---|
95 | </details>
|
---|
96 |
|
---|
97 | <details>
|
---|
98 | <summary><strong>Running Tests</strong></summary>
|
---|
99 |
|
---|
100 | 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:
|
---|
101 |
|
---|
102 | ```sh
|
---|
103 | $ npm install && npm test
|
---|
104 | ```
|
---|
105 |
|
---|
106 | </details>
|
---|
107 |
|
---|
108 | <details>
|
---|
109 | <summary><strong>Building docs</strong></summary>
|
---|
110 |
|
---|
111 | _(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.)_
|
---|
112 |
|
---|
113 | To generate the readme, run the following command:
|
---|
114 |
|
---|
115 | ```sh
|
---|
116 | $ npm install -g verbose/verb#dev verb-generate-readme && verb
|
---|
117 | ```
|
---|
118 |
|
---|
119 | </details>
|
---|
120 |
|
---|
121 | ### Related projects
|
---|
122 |
|
---|
123 | You might also be interested in these projects:
|
---|
124 |
|
---|
125 | * [assign-deep](https://www.npmjs.com/package/assign-deep): Deeply assign the values of all enumerable-own-properties and symbols from one or more source objects… [more](https://github.com/jonschlinkert/assign-deep) | [homepage](https://github.com/jonschlinkert/assign-deep "Deeply assign the values of all enumerable-own-properties and symbols from one or more source objects to a target object. Returns the target object.")
|
---|
126 | * [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.")
|
---|
127 | * [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.")
|
---|
128 | * [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.")
|
---|
129 |
|
---|
130 | ### Contributors
|
---|
131 |
|
---|
132 | | **Commits** | **Contributor** |
|
---|
133 | | --- | --- |
|
---|
134 | | 20 | [jonschlinkert](https://github.com/jonschlinkert) |
|
---|
135 | | 2 | [doowb](https://github.com/doowb) |
|
---|
136 | | 1 | [jakub-g](https://github.com/jakub-g) |
|
---|
137 |
|
---|
138 | ### Author
|
---|
139 |
|
---|
140 | **Jon Schlinkert**
|
---|
141 |
|
---|
142 | * [GitHub Profile](https://github.com/jonschlinkert)
|
---|
143 | * [Twitter Profile](https://twitter.com/jonschlinkert)
|
---|
144 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
|
---|
145 |
|
---|
146 | ### License
|
---|
147 |
|
---|
148 | Copyright © 2019, [Jon Schlinkert](https://github.com/jonschlinkert).
|
---|
149 | Released under the [MIT License](LICENSE).
|
---|
150 |
|
---|
151 | ***
|
---|
152 |
|
---|
153 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on April 15, 2019._ |
---|