deepit is a predictable, safe deep-clone utility for JavaScript and TypeScript. It performs structural deep cloning of complex data types while preserving prototypes, property descriptors, circular references, Maps, Sets, Dates, RegExps, ArrayBuffers, typed arrays, and more.
The library intentionally does not attempt to clone functions or closures. Functions are always copied by reference, consistent with JavaScript semantics and the behavior of established libraries.
deepit is written in TypeScript, thoroughly tested, and built for real production workloads.
- Deep cloning of objects, arrays, and nested structures.
- Prototype preservation (supports classes and custom prototypes).
- Property descriptor preservation, including getters and setters.
- Circular reference handling.
- Full support for Map and Set.
- Full support for Date and RegExp.
- Support for ArrayBuffer and typed arrays.
- Typed, predictable API.
- Functions are never cloned; they are copied by reference.
- Zero dependencies.
- ESM and CJS output builds.
npm install deepit
or
yarn add deepit
or
pnpm add deepit
import { clone } from 'deepit';
const input = {
value: 1,
nested: { a: 2 },
map: new Map([['k', { x: 10 }]]),
date: new Date(),
};
const output = clone(input);The resulting object is deeply cloned, structurally identical, prototype-safe, and independent.
Performs a deep, structural clone of the provided value. Prototypes, descriptors, and supported built-in types are preserved.
value
The input value to clone.
options
Optional configuration.
interface CloneOptions {
maxDepth?: number | null;
skipKeys?: string[];
}maxDepth: Controls recursion depth. Whennull(default), there is no limit. When set to a number, the clone operation will throw if it exceeds that depth.skipKeys: An array of property names to omit from the cloned result. Useful for removing sensitive or unnecessary data while cloning deeply.
Returns a deep, independent copy of the input value, with the following rules:
- Objects and arrays are cloned deeply.
- Maps and Sets are cloned with their entries deeply cloned.
- Prototypes and property descriptors are preserved.
- Functions are copied by reference.
- WeakMap and WeakSet are not cloned (they are left as references).
- Circular references in the input are preserved.
| Type | Behavior |
|---|---|
| Object | Cloned with prototype + descriptors preserved |
| Array | Deep cloned |
| Map | Deep cloned (keys and values) |
| Set | Deep cloned (values) |
| Date | Cloned |
| RegExp | Cloned |
| ArrayBuffer | Copied |
| TypedArray | Copied |
| Error | Cloned (name, message, stack) |
| Function | Copied by reference |
| WeakMap | Reference preserved |
| WeakSet | Reference preserved |
deepit avoids any behavior that could create ambiguous or incorrect clones, such as replicating closures or attempting to re-generate function internals.
The clone matches the original object's shape, descriptors, and prototypes.
The implementation and API are written entirely in TypeScript and designed to maintain strong type guarantees.
The implementation is self-contained to avoid dependency security risks and reduce bundle size.
These are intentional constraints that maintain correctness:
- Functions are not cloned; they are copied by reference.
- Closures cannot be cloned because JavaScript does not expose closure environments.
- WeakMap and WeakSet cannot be cloned because their contents are not observable.
- DOM nodes are not handled; deepit focuses on JavaScript data structures.
Contributions are welcome. Please follow these steps:
- Fork the repository.
- Create a feature branch with a descriptive name.
- Add or update tests for all behavior changes.
- Ensure
npm testandnpm run lintpass. - Submit a pull request with a clear explanation of the changes.
Issues, feature requests, and bug reports should include reproducible examples where possible.
npm run build
npm test
npm run lint
npm run format
src/
index.ts
clone.ts
handlers/
object.ts
collection.ts
tests/
clone.spec.ts
deepit follows semantic versioning (semver):
- Patch versions for fixes and safe refactors.
- Minor versions for new capabilities.
- Major versions for breaking changes.
deepit is developed and maintained by Gautam Suthar. Focused on building reliable developer tooling, high-performance JavaScript utilities, and clean architectural patterns.
MIT License.
See the LICENSE file for details.