{"id":18062,"library":"shallow-clone","title":"Shallow Clone Utility","description":"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.","status":"active","version":"3.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/jonschlinkert/shallow-clone","tags":["javascript","array","clone","copy","extend","mixin","object","primitive","shallow"],"install":[{"cmd":"npm install shallow-clone","lang":"bash","label":"npm"},{"cmd":"yarn add shallow-clone","lang":"bash","label":"yarn"},{"cmd":"pnpm add shallow-clone","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library exports a default function. When using ES modules, import it directly as a default.","wrong":"import { clone } from 'shallow-clone';","symbol":"clone","correct":"import clone from 'shallow-clone';"},{"note":"This is the standard CommonJS import pattern, as shown in the package's documentation.","symbol":"clone","correct":"const clone = require('shallow-clone');"},{"note":"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.","wrong":"import * as clone from 'shallow-clone';","symbol":"clone (TypeScript)","correct":"import clone from 'shallow-clone';"}],"quickstart":{"code":"import clone from 'shallow-clone';\n\nconst originalArray = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];\nconst clonedArray = clone(originalArray);\n\nconsole.log('Original Array:', originalArray);\nconsole.log('Cloned Array:', clonedArray);\n\n// Prove array is a new instance (shallow copy)\nconsole.log('Are arrays the same instance?', originalArray === clonedArray); // Expected: false\n\n// Prove elements are NOT cloned (references are shared)\nconsole.log('Are first elements the same instance?', originalArray[0] === clonedArray[0]); // Expected: true\n\n// Mutating a nested object in the clone affects the original\nclonedArray[0].name = 'Alicia';\nconsole.log('Original after mutation:', originalArray); // Original array's first element will also be 'Alicia'\n\nconst originalObject = { a: 1, b: { c: 2 }, d: [3, 4] };\nconst clonedObject = clone(originalObject);\n\nconsole.log('Original Object:', originalObject);\nconsole.log('Cloned Object:', clonedObject);\nconsole.log('Are objects the same instance?', originalObject === clonedObject); // Expected: false\nconsole.log('Are nested objects the same instance?', originalObject.b === clonedObject.b); // Expected: true\n","lang":"javascript","description":"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."},"warnings":[{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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'`.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":"<=3.0.1"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"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.","cause":"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.","error":"Unexpected mutation: Changing a property on my cloned object also changed the original object!"},{"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.","cause":"Incorrect import statement for ES Modules. `shallow-clone` exports a default function, but users might attempt a named import or incorrect CJS interop.","error":"TypeError: 'clone' is not a function or 'clone' is undefined (when using ES modules)"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}