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

@aboutweb/proxyscope

v3.0.1

Published

Bootstrap for combining object be reference with ES proxy, and reusing them on other proxy-factories.

Downloads

35

Readme

ProxyScope

Bootstrap for combining object be reference with ES proxy, and reusing them on other proxy-factories. Which will also reflect changes made to the original objects. Proxies can also be useful when using immutables or mixins.

Caution this method uses Proxies and therefor will only work in modern enviroments. Please view CanIUse for more information.

Install

npm i @aboutweb/proxyscope

Caution this method uses Proxies and therefor will only work in modern enviroments. Please view CanIUse for more information.

ProxyFactory(`...(traps || factory))

traps chain of traps merged for the new Proxy

factory a previous created factory

returns a factory function with a traps property

factory(Object target | Array<Object target>, ...traps)

target an object or array of object to use as stack for lookups

traps to combine while creating a new proxy

Example


import { default as proxyFactory } from "@aboutweb/proxyscope";
import otherFactory from "./otherfactory.js";

const traps = {
  has(target, property, receiver) {
    return this.findHost(property);
  },
  /*helper method*/
  findHost(property, root) {
    let host = this.stack.find(function(level) {
      return property in level;
    });

    if(host) {
      return host;
    }

    if(root === true) {
      return this.stack[0];
    }
  }
}

const factory = proxyFactory(traps, otherfactory);

export {
  factory as default,
  traps
}

ProxyScope.read

Uses the target.stack to look up properties.

Example

import { read as proxyRead } from "@aboutweb/proxyscope";

let defaultConfig = {
  fu : "bar"
}

function someFn(config) {
  let proxy = proxyRead(config, defaultConfig);

  expect(proxy.fu).to.equal("bar");
  expect(proxy.bar).to.equal("baz");
}

someFn({
  bar : "baz"
});

ProxyScope.write

same as proxyRead but will also forward writes if they are defined somewhere in the level chain.

Example

const l1 = {
  some : "value"
}

const l2 = {
  fu : "bar"
}

let proxy = proxyWrite(l1, l2);

proxy.fu = "hallo";

expect(l2.fu).to.equal("hallo");

ProxyScope.readDeep

read even nested objects, extends proxy-read by returning a new proxy for every property that has multiple possible getters

Use wisely, because this will create a proxy for every property lookup that has more then one possible value. Therefor it is recommended to cache lookups when possible.

Example


import { readDeep as proxyReadDeep } from "@aboutweb/proxyscope";

const l1 = {
  nested : {
    some : "value",
  }
}

const l2 = {
  nested : {
    fu : "bar"
  }
}

const l3 = {
  other : true
}

let proxy = proxyReadDeep(l1, l2, l3);

//before caching or update cache afterwords
l3.nested = {
  deep : true
}

//cache the returned proxy
let nested = proxy.nested;

expect(nested.some).to.equal("value");
expect(nested.fu).to.equal("bar");
expect(nested.deep).to.equal(true);

ProxyScope.writeDeep

same as proxyReadDeep, but will also write to nested objects

Example


import { writeDeep as proxyWriteDeep } from "@aboutweb/proxyscope";

const l1 = {
  nested : {
    some : "value",
  }
}

const l2 = {
  nested : {
    fu : "bar"
  }
}

let proxy = proxyReadDeep(l1, l2);

//also use cache where possible
let nested = proxy.nested;

nested.some = "other";
nested.fu = "baz";

expect(l1.nested.some).to.equal("other");
expect(l2.nested.fu).to.equal("baz");

Licence

ISC