Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
shallow-clone
Advanced tools
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.
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
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.
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.
Make a shallow clone of an object, array or primitive.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install with npm:
$ npm install --save shallow-clone
var clone = require('shallow-clone');
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
clone(0)
//=> 0
clone('foo')
//=> 'foo'
clone(/foo/g)
//=> /foo/g
clone({a: 1, b: 2, c: 3 })
//=> {a: 1, b: 2, c: 3 }
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
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
(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
You might also be interested in these projects:
Object
constructor. | homepageCommits | Contributor |
---|---|
9 | jonschlinkert |
2 | doowb |
Jon Schlinkert
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on December 28, 2017.
FAQs
Creates a shallow clone of any JavaScript value.
The npm package shallow-clone receives a total of 14,908,651 weekly downloads. As such, shallow-clone popularity was classified as popular.
We found that shallow-clone demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.