npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ouroboros/clone

v1.1.0

Published

A function for cloning a piece of data completely

Downloads

266

Readme

@ouroboros/clone

npm version MIT License

A function for cloning a piece of data completely.

Installation

npm

npm install @ouroboros/clone

clone

Does a deep copy of pure data arrays, objects, and other simple types.

import clone from '@ouroboros/clone';

const o1 = {
	message: [
		{ greeting: ['h', 'e', 'l', 'l', 'o' ] },
		{ name: ['w', 'o', 'r', 'l', 'd'] },
		{ punctuation: ['!'] }
	]
};
const o2 = clone(o1)

// True. o2 now contains the same data as o1, but they are
//	different instances of the object
o1 !== o2

// All true. Every part of o2, at every level, is a copy, not a pointer, of the data in o2
o1.message !== o2.message
o1.message[0] !== o2.message[0]
o1.message[0].greeting !== o2.message[0].greeting
o1.message[1].name !== o2.message[1].name

Clone

Can be extending by any child class to add .clone() to it, which will be run by the library to handle cloning the instance of the object when it is encountered. The default .clone() method implemented by Class simply returns the value as is.

import clone, { Clone } from '@ouroboros/clone';

class MyClass extends Clone {
	// ... do class stuff
}

const o1 = { instance = new MyClass() };
const o2 = clone(o1);

// True. The objects are different despite containing the same data.
o1 !== o2

// False! The variables within the objects still point to the same instance of MyClass.
o1.instance !== o2.instance

Implementing .clone()

By overwriting .clone() you can handle implementing the copy yourself and be 100% sure you have a different instance.

Note, we don't actually extend Clone in the below example. It's recommended if you're using clone within your own library or project, but it's not necessary to create a dependency to clone in order to add support for clone. As long as clone finds a method to call on the instance, it will call it and use the return value.

import clone from '@ouroboros/clone';

class MyClass {
	constructor(one, two) {
		this.one = one;
		this.two = two;
	}
	clone() {
		return new MyClass(this.one, this.two);
	}
	// ... do class stuff
}

const o1 = { instance = new MyClass() };
const o2 = clone(o1);

// True. The objects are different despite containing the same data.
o1 !== o2

// True. The instances contain the same data, but they are now different.
o1.instance !== o2.instance

using cloneIgnore()

If you are running into a problem of a class instance being cloned as an object, thereby destroying the constructor associated, and for whatever reason you can't give it a custom .clone() method, you can pass the class' type to cloneIgnore and it will be skipped by the library when instances are discovered.

import clone, { cloneIgnore } from '@ouroboros/clone';

class MyClass {
	// ... do class stuff
}

// Add the type of class to be ignored
cloneIgnore(MyClass);

const o1 = { instance = new MyClass() };
const o2 = clone(o1);

// True
o1 !== o2

// False. All we did was ignore cloning the instance and return it as is.
o1.instance !== o2.instance