What is shallow-clone?
The shallow-clone npm package is designed to create shallow copies of various JavaScript data types, such as objects, arrays, and other native types. It is useful for cases where you need a new object with the same properties as the original but without deep copying nested structures.
What are shallow-clone's main functionalities?
Cloning Objects
Creates a shallow copy of an object. Changes to nested objects in the original will reflect in the clone.
const shallowClone = require('shallow-clone');
const obj = { a: 1, b: { c: 2 } };
const clonedObj = shallowClone(obj);
console.log(clonedObj); // { a: 1, b: { c: 2 } }
Cloning Arrays
Creates a shallow copy of an array. Changes to nested arrays in the original will reflect in the clone.
const shallowClone = require('shallow-clone');
const arr = [1, 2, [3, 4]];
const clonedArr = shallowClone(arr);
console.log(clonedArr); // [1, 2, [3, 4]]
Cloning Dates
Creates a shallow copy of a Date object, resulting in a new Date object with the same time.
const shallowClone = require('shallow-clone');
const date = new Date();
const clonedDate = shallowClone(date);
console.log(clonedDate); // outputs cloned date object
Cloning Regular Expressions
Creates a shallow copy of a RegExp object, including its flags and lastIndex property.
const shallowClone = require('shallow-clone');
const regex = /test/gi;
const clonedRegex = shallowClone(regex);
console.log(clonedRegex); // outputs cloned RegExp object
Other packages similar to shallow-clone
lodash.clonedeep
Lodash's clonedeep method provides deep cloning capabilities, unlike shallow-clone which only performs shallow cloning. This is useful for more complex data structures where nested objects and arrays also need to be independently cloned.
clone
The clone package offers both deep and shallow cloning functionalities. It can be configured to perform either, making it more flexible compared to shallow-clone which only supports shallow cloning.
shallow-clone
Make a shallow clone of an object, array or primitive.
Install
Install with npm
$ npm i shallow-clone --save
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);
assert.equal(actual === expected, false);
assert.deepEqual(actual[0], expected[0]);
returns primitives as-is
clone(0)
clone('foo')
shallow clone a regex
clone(/foo/g)
shallow clone an object
clone({a: 1, b: 2, c: 3 })
Related projects
- assign-deep: Deeply assign the enumerable properties of source objects to a destination object.
- clone-deep: Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives.
- extend-shallow: Extend an object with the properties of additional objects. node.js/javascript util.
- is-plain-object: Returns true if an object was created by the
Object
constructor. - mixin-object: Mixin the own and inherited properties of other objects onto the first object. Pass an… more
- mixin-deep: Deeply mix the properties of objects into the first object. Like merge-deep, but doesn't clone.
Running tests
Install dev dependencies:
$ npm i -d && npm test
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Author
Jon Schlinkert
License
Copyright © 2015 Jon Schlinkert
Released under the MIT license.
This file was generated by verb-cli on August 10, 2015.