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

proxyme-js

v1.2.1

Published

Generates a new Proxy that adds listeners for changes in objects

Downloads

3

Readme

ProxyMe

Generates a Proxy for clasess and objects and uses listeners to warn about any change to the elements.

Install

  npm intall proxyme-js

Require

For NodeJS in module or Browser:

  import ProxyMe from "proxyme-js";

Use with objects

  let objPrueba={
    a:"A",
    b:"B",
    subLevel:{
      a:"A2",
      b:"B2"
    }
  }

  let proxied=ProxyMe(objPrueba);
  proxied.on("set",
    (obj,prop,newVal,oldVal,receiver)=>{
      console.group(`Setted ${prop}`);
      console.log("Old Value: ",oldVal);
      console.log("New Value: ",newVal);
      console.log("Object:",obj);
      console.log("Receiver:",receiver);
      console.groupEnd();
    }
  );

  proxied.a="New a";

  /* Console
    Setted a
      Old Value:  A
      New Value:  New A
      Object: { a: 'New A', b: 'B', subLevel: { a: 'A2', b: 'B2' } }  
      Receiver: { a: 'New A', b: 'B', subLevel: { a: 'A2', b: 'B2' } }
  */

  proxied.subLevel.a="New A2";

  /* Console
    Setted subLevel.a
      Old Value:  A2
      New Value:  New A2
      Object: { a: 'New A', b: 'B', subLevel: { a: 'New A2', b: 'B2' } }
      Receiver: { a: 'New A2', b: 'B2' }
  */
  

If the object is changed by a function inside the object the prop value is going to be: "byFunction: ${functionName}" and all the individual changes will be discarded. (This is because the addition of this functionality can make the code so much bigger and more complex to debug).

Access sublevel object with string

Continuing with previous example.

In this example it's interesting to note that assigning value

  proxied["subLevel.a"]="New new A2";

  /* Console
      Setted subLevel.a
        Old Value:  New A2
        New Value:  New new A2
        Object: { a: 'New A', b: 'B', subLevel: { a: 'New new A2', b: 'B2' } }
        Receiver: { a: 'New new A2', b: 'B2' }
  */

  console.log(proxied["subLevel.a"]);

  /* Console
      New new A2
  */

Cancel Set Action

Continuing with previous example.

If the set listener returns a string "#cancel" the set action will be canceled and the element will matain the oldVal.

  proxied.on("set",(obj,prop,newVal,oldVal,receiver)=>{
    if(/b$/.exec(prop)){
      console.log(`Unable to modify ${prop}`);
      return "#cancel";
    }
  });
  proxied.b="This value should not be changed";
  proxied.subLevel.b="This value neither changes";
  proxied.a="I should be modified";
  console.log(proxied);
  /* Console
      Unable to modify b
      Unable to modify subLevel.b
      { a: 'I should be modified', b: 'B', subLevel: { a: 'A2', b: 'B2' } }
  */

Proxy Class

Proxied Classes works the same as proxied objects.

  class A{
    constructor(){
      this.a="A";
      this.secondLevel={a:"A2"};
    }
    changeA(newName){
      this.a="changeA A";
      this.secondLevel.a="changeA A2";
    }
  }
  let AProxied=ProxyMe(new A());
  AProxied.on("set",
    (obj,prop,newVal,oldVal,receiver)=>{
      console.group(`Setted ${prop}`);
      console.log("Old Value: ",oldVal);
      console.log("New Value: ",newVal);
      console.log("Object:",obj);
      console.log("Receiver:",receiver);
      console.groupEnd();
    }
  );

  AProxied.a="New A";

  /* Console
      Setted a
        Old Value:  A
        New Value:  New A
        Object: A { a: 'New A', secondLevel: { a: 'A2' } }  
        Receiver: A { a: 'New A', secondLevel: { a: 'A2' } }
  */

But we can extend them:

  class A{
    constructor(){
      this.a="A";
      this.secondLevel={a:"A2"};
    }
    changeA(newName){
      this.a="changeA A";
      this.secondLevel.a="changeA A2";
    }
    //You can assign the listeners inside the constructor class with the function __onProxied()
    __onProxied(){
      this.on("set",
        ()=>{
          console.log("From A __onProxied set listener");
        }
      );
    }
  }
  let AProxied=ProxyMe(A);

  class B extends AProxied{
    constructor(){
      super();
      this.a="B->A";
      this.b="B";
      this.secondLevel.a="B->A2";
    }
  }
  let BProxied=new B();
  BProxied.on("set",
    (obj,prop,newVal,oldVal,receiver)=>{
      console.group(`Setted ${prop}`);
      console.log("Old Value: ",oldVal);
      console.log("New Value: ",newVal);
      console.log("Object:",obj);
      console.log("Receiver:",receiver);
      console.groupEnd();
    }
  );

  BProxied.a="New A";

  /* Console
      From A __onProxied set listener

      Setted a
        Old Value:  B->A
        New Value:  New A
        Object: B { a: 'New A', secondLevel: { a: 'B->A2' }, b: 'B' }  
        Receiver: B { a: 'New A', secondLevel: { a: 'B->A2' }, b: 'B' }
  */

 BProxied.changeA("New Custom A");

 /* Console
      From A __onProxied set listener

      Setted byFunction: changeA
        Old Value:  {"a":"New A","secondLevel":{"a":"B->A2"},"b":"B"}
        New Value:  {"a":"changeA A","secondLevel":{"a":"changeA A2"},"b":"B"}  
        Object: B { a: 'changeA A', secondLevel: { a: 'changeA A2' }, b: 'B' }  
        Receiver: B { a: 'changeA A', secondLevel: { a: 'changeA A2' }, b: 'B' }
  */

The proxied class works the same as a proxied object.

Proxy the get

    let objPrueba={
      a:"A",
      b:"B",
      subLevel:{
        a:"A2",
        b:"B2"
      }
    }

    let proxied=ProxyMe(objPrueba);
    proxied.on("get",
      (obj,prop,value,receiver)=>{
        console.group(`Getted ${prop}`);
        console.log("Value:",value);
        console.log("Object:",obj);
        console.log("Receiver:",receiver);
        console.groupEnd();
      }
    );

    proxied.a;

    /* Console
      Getted a
        Value: A
        Object: { a: 'A', b: 'B', subLevel: { a: 'A2', b: 'B2' } }  
        Receiver: { a: 'A', b: 'B', subLevel: { a: 'A2', b: 'B2' } }
    */

   proxied.subLevel.a;

    /* Console
      Getted subLevel
        Value: { a: 'A2', b: 'B2' }
        Object: { a: 'A', b: 'B', subLevel: { a: 'A2', b: 'B2' } }  
        Receiver: { a: 'A', b: 'B', subLevel: { a: 'A2', b: 'B2' } }
      Getted subLevel.a
        Value: A2
        Object: { a: 'A', b: 'B', subLevel: { a: 'A2', b: 'B2' } }  
        Receiver: { a: 'A2', b: 'B2' }
    */

   proxied["subLevel.a"];

   /* Console
      Getted subLevel.a
        Value: A2
        Object: { a: 'A', b: 'B', subLevel: { a: 'A2', b: 'B2' } }  
        Receiver: { a: 'A', b: 'B', subLevel: { a: 'A2', b: 'B2' } }
    */

NICE DAY AND WONDERFUL CODING

Author: Adrián Mercado Martínez.

Last modification: 2021-10-19