You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

shallow-clone

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

shallow-clone

Creates a shallow clone of any JavaScript value.


Version published
Maintainers
2
Created

Package description

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

Readme

Source

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

Creates a shallow clone of any JavaScript value.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

Install

Install with npm:

$ npm install --save shallow-clone

Usage

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

Supports

  • array buffers
  • arrays
  • buffers
  • dates
  • errors
  • float32 arrays
  • float64 arrays
  • int16 arrays
  • int32 arrays
  • int8 arrays
  • maps
  • objects
  • primitives
  • regular expressions
  • sets
  • symbols
  • uint16 arrays
  • uint32 arrays
  • uint8 arrays
  • uint8clamped arrays

Arrays

By default, only the array itself is cloned (shallow), use clone-deep if you also need the elements in the array to be cloned.

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

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

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

Objects

Only the object is shallow cloned, use clone-deep if you also need the values in the object to be cloned.

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

RegExp

Clones regular expressions and flags, and preserves the .lastIndex.

const regex = clone(/foo/g); //=> /foo/g
// you can manually reset lastIndex if necessary
regex.lastIndex = 0;

Primitives

Simply returns primitives unchanged.

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

About

Contributing

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

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
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

You might also be interested in these projects:

  • assign-deep: Deeply assign the values of all enumerable-own-properties and symbols from one or more source objects… more | homepage
  • clone-deep: Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives. | homepage
  • is-plain-object: Returns true if an object was created by the Object constructor. | homepage
  • kind-of: Get the native type of a value. | homepage

Contributors

CommitsContributor
20jonschlinkert
2doowb
1jakub-g

Author

Jon Schlinkert

License

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


This file was generated by verb-generate-readme, v0.8.0, on April 15, 2019.

Keywords

FAQs

Package last updated on 16 Apr 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc