Shallow Clone Utility
raw JSON →shallow-clone is a JavaScript utility library designed for creating shallow copies of various data types. It supports a comprehensive range of values including primitives, plain objects, arrays, regular expressions, dates, buffers, array buffers, all JavaScript typed arrays (e.g., Int8Array, Uint8Array), Maps, and Sets. Currently at version 3.0.1, the package focuses exclusively on performing a shallow clone, which means only the top-level structure of the input value is copied. Any nested objects or arrays within the cloned structure will retain their original references. This characteristic is a key differentiator, distinguishing it from 'deep cloning' libraries like `clone-deep`, which recursively copy all nested structures. The library is considered stable and suitable for scenarios where a new top-level instance is required without altering the original, and shared references for nested data are acceptable.
Common errors
error Unexpected mutation: Changing a property on my cloned object also changed the original object! ↓
clone-deep) or manually recurse and clone nested structures. error TypeError: 'clone' is not a function or 'clone' is undefined (when using ES modules) ↓
import clone from 'shallow-clone';. Do not use { clone } or * as clone unless specifically configured for CJS interop with TypeScript or bundlers. Warnings
gotcha This library performs a *shallow* clone, not a deep clone. This is its fundamental design. Any nested objects or arrays within the cloned value will still refer to the *original* objects, not new copies. Mutating nested data in the clone will therefore affect the original data. ↓
gotcha When cloning primitive values (strings, numbers, booleans, null, undefined, symbols), `shallow-clone` simply returns the primitive itself, as primitives are immutable and cannot be 'cloned' in the traditional sense. ↓
gotcha The package's latest version 3.0.1 was published approximately 7 years ago (as of current date), and while still widely used and maintained sustainably, it has not seen recent feature updates or new versions. ↓
Install
npm install shallow-clone yarn add shallow-clone pnpm add shallow-clone Imports
- clone wrong
import { clone } from 'shallow-clone';correctimport clone from 'shallow-clone'; - clone
const clone = require('shallow-clone'); - clone (TypeScript) wrong
import * as clone from 'shallow-clone';correctimport clone from 'shallow-clone';
Quickstart
import clone from 'shallow-clone';
const originalArray = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
const clonedArray = clone(originalArray);
console.log('Original Array:', originalArray);
console.log('Cloned Array:', clonedArray);
// Prove array is a new instance (shallow copy)
console.log('Are arrays the same instance?', originalArray === clonedArray); // Expected: false
// Prove elements are NOT cloned (references are shared)
console.log('Are first elements the same instance?', originalArray[0] === clonedArray[0]); // Expected: true
// Mutating a nested object in the clone affects the original
clonedArray[0].name = 'Alicia';
console.log('Original after mutation:', originalArray); // Original array's first element will also be 'Alicia'
const originalObject = { a: 1, b: { c: 2 }, d: [3, 4] };
const clonedObject = clone(originalObject);
console.log('Original Object:', originalObject);
console.log('Cloned Object:', clonedObject);
console.log('Are objects the same instance?', originalObject === clonedObject); // Expected: false
console.log('Are nested objects the same instance?', originalObject.b === clonedObject.b); // Expected: true