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

proxy-compare

v3.0.0

Published

Compare two objects using accessed properties with Proxy

Downloads

2,176,966

Readme

proxy-compare

CI npm size discord

Compare two objects using accessed properties with Proxy

Introduction

This is an internal library used in state management libraries such as React Tracked and Valtio.

Install

npm install proxy-compare

Usage

$ node
> const { createProxy, isChanged } = require('proxy-compare')
undefined
> state = { a: 1, b: 2 }
{ a: 1, b: 2 }
> affected = new WeakMap()
WeakMap { [items unknown] }
> proxy = createProxy(state, affected)
Proxy [
  { a: 1, b: 2 },
  {
    get: [Function: get],
    has: [Function: has],
    getOwnPropertyDescriptor: [Function: getOwnPropertyDescriptor],
    ownKeys: [Function: ownKeys]
  }
]
> proxy.a
1
> isChanged(state, { a: 1, b: 22 }, affected)
false
> isChanged(state, { a: 11, b: 2 }, affected)
true

API

createProxy

Create a proxy.

This function will create a proxy at top level and proxy nested objects as you access them, in order to keep track of which properties were accessed via get/has proxy handlers:

NOTE: Printing of WeakMap is hard to inspect and not very readable for this purpose you can use the affectedToPathList helper.

Parameters

  • obj object Object that will be wrapped on the proxy.
  • affected WeakMap<object, unknown> WeakMap that will hold the tracking of which properties in the proxied object were accessed.
  • proxyCache WeakMap<object, unknown>? WeakMap that will help keep referential identity for proxies.
  • targetCache WeakMap<object, any>?

Examples

import { createProxy } from 'proxy-compare';

const original = { a: "1", c: "2", d: { e: "3" } };
const affected = new WeakMap();
const proxy = createProxy(original, affected);

proxy.a // Will mark as used and track its value.
// This will update the affected WeakMap with original as key
// and a Set with "a"

proxy.d // Will mark "d" as accessed to track and proxy itself ({ e: "3" }).
// This will update the affected WeakMap with original as key
// and a Set with "d"

Returns Proxy<object> Object wrapped in a proxy.

isChanged

Compare changes on objects.

This will compare the affected properties on tracked objects inside the proxy to check if there were any changes made to it, by default if no property was accessed on the proxy it will attempt to do a reference equality check for the objects provided (Object.is(a, b)). If you access a property on the proxy, then isChanged will only compare the affected properties.

Parameters

  • prevObj object The previous object to compare.
  • nextObj object Object to compare with the previous one.
  • affected WeakMap<object, unknown> WeakMap that holds the tracking of which properties in the proxied object were accessed.
  • cache WeakMap<object, unknown>? WeakMap that holds a cache of the comparisons for better performance with repetitive comparisons, and to avoid infinite loop with circular structures.
  • isEqual function (a: any, b: any): boolean (optional, default Object.is)

Examples

import { createProxy, isChanged } from 'proxy-compare';

const obj = { a: "1", c: "2", d: { e: "3" } };
const affected = new WeakMap();

const proxy = createProxy(obj, affected);

proxy.a

isChanged(obj, { a: "1" }, affected) // false

proxy.a = "2"

isChanged(obj, { a: "1" }, affected) // true

Returns boolean Boolean indicating if the affected property on the object has changed.

getUntracked

Unwrap proxy to get the original object.

Used to retrieve the original object used to create the proxy instance with createProxy.

Parameters

  • obj Proxy<object> The proxy wrapper of the originial object.

Examples

import { createProxy, getUntracked } from 'proxy-compare';

const original = { a: "1", c: "2", d: { e: "3" } };
const affected = new WeakMap();

const proxy = createProxy(original, affected);
const originalFromProxy = getUntracked(proxy)

Object.is(original, originalFromProxy) // true
isChanged(original, originalFromProxy, affected) // false

Returns (object | null) Return either the unwrapped object if exists.

markToTrack

Mark object to be tracked.

This function marks an object that will be passed into createProxy as marked to track or not. By default only Array and Object are marked to track, so this is useful for example to mark a class instance to track or to mark a object to be untracked when creating your proxy.

Parameters

  • obj object Object to mark as tracked or not.
  • mark Boolean indicating whether you want to track this object or not. (optional, default true)

Examples

import { createProxy, markToTrack, isChanged } from 'proxy-compare';

const nested = { e: "3" }

markToTrack(nested, false)

const original = { a: "1", c: "2", d: nested };
const affected = new WeakMap();

const proxy = createProxy(original, affected);

proxy.d.e

isChanged(original, { d: { e: "3" } }, affected) // true

Returns any No return.

affectedToPathList

Convert affected to path list

affected is a weak map which is not printable. This function is can convert it to printable path list. It's for debugging purpose.

Parameters

  • obj any An object that is used with createProxy.
  • affected WeakMap<object, any> A weak map that is used with createProxy.
  • onlyWithValues boolean? An optional boolean to exclude object getters.

Returns any An array of paths.

replaceNewProxy

replace newProxy function.

This can be used if you want to use proxy-polyfill. Note that proxy-polyfill can't polyfill everything. Use it at your own risk.

Parameters

  • fn any

Projects using this library

Similar libraries