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

circ-clone

v2.7.11

Published

Simple lib to safely clone circular objects.

Downloads

158

Readme

Circ clone

Simple lib to safely (regarding prototype poisoning) clone circular (deep) objects. Exports three functions: cloneKeys, mergeDeep and cloneKeysButKeepSym, that do what they say and are not configurable. They are properly types and support tree-shaking. Each implementation is considerable simple (thus small). All functions that keep track of circular references, use WeakMap to do so.

Installation

 $ npm i circ-clone

Usage

Clone keys

cloneKeys<Ob extends Object>(ob: Ob): Ob

import { cloneKeys } from "circ-clone"

const obj = { a: 1, b: { c: 3 } }
obj.b.d = obj

const cloned = cloneKeys(obj)

Merge deep

mergeDeep<Into extends object, From extends object>(from: From, into: Into): Into & From

Merges the properties of from into into recursively with support for cyclic references. References from from to into (or any of its nested objects) are not supported, as the resulting behavior is not defined. Arrays are not treated specially (indexes are overwritten). The return value is === into (thus not cloned), only sub-branches (nested objects) of from that are new to into are cloned.

import { mergeDeep } from "circ-clone"

const into = { a: 2, b: { c: 4, d: { doesntMatter: "whats in here", asItGets: "overriden" } } }
const from = { a: 1, b: { c: 3, d: "see one line below" } }
from.b.d = from


const merged = mergeDeep(into, from)
// merged = into = {
//   a: 1,
//   b: {
//     c: 3,
//     d: [Circular]
//   }
// }

Importantly note, that the fields on into.b.d do not get copied over, as the reference to from.b.d is considered new. The reason for this decision is that it seems unintuitive for members of into to suddenly be written into a place of into. If you however need this behavior, please let me know by creating an issue.

Merge deep but not cyclic

mergeDeepButNotCyclic<Into extends object, From extends object>(from: From, into: Into): Into & From

Merges the properties of from into into without considering cyclic references! This is faster than mergeDeep, but has the drawback that cyclic references in both from and into (exclusively if in both at the same place) terminate the function with an (stackoverflow) exception, similar to how JSON.stringify would. Also note that references from from to into (or any of its nested objects) are not supported, as the resulting behavior is not defined. The return value is === into (thus not cloned), only sub-branches (nested objects) of from that are new to into are cloned.

import { mergeDeepButNotCyclic } from "circ-clone"

const into = { a: 2, b: { c: 4, e: 5 } }
const from = { a: 1, b: { c: 3 } }

const merged = mergeDeepButNotCyclic(into, from)
// merged = into = {
//   a: 1,
//   b: {
//     c: 3,
//     e: 5
//   }
// }

Clone keys but keep symbols

cloneKeysButKeepSym<Ob extends Object>(ob: Ob): Ob

Similar to cloneKeys but keeps symbols uncloned!

import { cloneKeysButKeepSym } from "circ-clone"

const obj = { a: 1, b: { c: 3 } }
obj.b.d = obj
const sym = Symbol("foo")
obj[sym] = {  }


const cloned = cloneKeysButKeepSym(obj)
cloned[sym] === obj[sym] // true

Contribute

All feedback is appreciated. Create a pull request or write an issue.