Registry / serialization / shallow-clone

shallow-clone

JSON →
library3.0.1jsnpmunverified

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.

npm install shallow-clone
INSTALL
IMPORT
SIG · SHALLOW-CLONE
S
shallow-clone
serializationjavascriptv3.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

clone
import clone from 'shallow-clone';
import { clone } from 'shallow-clone';
The library exports a default function. When using ES modules, import it directly as a default.
clone
const clone = require('shallow-clone');
This is the standard CommonJS import pattern, as shown in the package's documentation.
clone (TypeScript)
import clone from 'shallow-clone';
import * as clone from 'shallow-clone';
With `esModuleInterop` enabled in TypeScript (which is common), the default export can be imported directly. If `esModuleInterop` is false, `import clone = require('shallow-clone');` might be necessary.

Demonstrates how to import and use `shallow-clone` with arrays and objects, highlighting the key behavior of shallow copying where top-level structures are new instances, but nested objects retain original references.

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
Debug
Known issues
gotchaThis 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.
fix
If deep cloning is required, use a library specifically designed for it, such as `clone-deep`, or implement a recursive cloning function. Do not assume `shallow-clone` provides full immutability for complex data structures.
affects: >=1.0.0
gotchaWhen 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.
fix
No fix is needed, this is the expected behavior for primitives. Developers should be aware that `clone(1)` will yield `1` and `clone('foo')` will yield `'foo'`.
affects: >=1.0.0
gotchaThe 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.
fix
Evaluate if the package's stable feature set meets current project requirements. For projects requiring active development, newer language features, or potentially more robust TypeScript support out-of-the-box, consider alternatives, though for its specific shallow-clone purpose, it remains effective.
affects: <=3.0.1
Errors
Common errors & fixes
Unexpected mutation: Changing a property on my cloned object also changed the original object!
The developer expected a deep clone but used `shallow-clone`. Nested objects/arrays are copied by reference, not value, so modifying them in the clone affects the original.
fix
Understand the difference between shallow and deep cloning. If nested data must be independent, use a deep cloning utility (e.g., `clone-deep`) or manually recurse and clone nested structures.
TypeError: 'clone' is not a function or 'clone' is undefined (when using ES modules)
Incorrect import statement for ES Modules. `shallow-clone` exports a default function, but users might attempt a named import or incorrect CJS interop.
fix
For ES Modules, use `import clone from 'shallow-clone';`. Do not use `{ clone }` or `* as clone` unless specifically configured for CJS interop with TypeScript or bundlers.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
shallow-clone — npm install shallow-clone · libregistry