Skip to content
/ clone Public

Deeply clone arbitrary objects in Javascript, with the original prototype.

License

Notifications You must be signed in to change notification settings

wings-j/clone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

252 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deeply clone arbitrary objects in Javascript.

Non-enumerable properties will not be maintained. Objects will be cloned with same prototype, while following variables will not be cloned:

  • Basic types: undefined, null, bool, number, string, symbol
  • Function
  • WeakMap
  • WeakSet
  • Blob and File

Watch that:

  • This library use modern Javascript syntax such as ESM, Object.getPrototypeOf, so it can not be use in legacy environments.
  • If the original object is a custom class whose constructor may throw errors, this library may fail.
  • Only writable members can be copied.

Installation

npm install @wings-j/clone

Usage

import { clone } from '@wings-j/clone';

class A {
  constructor() {
    this.x = false;
    this.y = 0;
    this.z = '';
  }
}
let o = new A();
let r = clone(o);

console.log(r === o); // false
console.log(r instanceof A); // true
console.log(r.x === o.x); // true
console.log(r.y === o.y); // true
console.log(r.z === o.z); // true

About

Deeply clone arbitrary objects in Javascript, with the original prototype.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 30