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-helpers

v0.2.0

Published

Extend objects with sugar methods without touching their prototypes

Downloads

5

Readme

proxy-helpers

Extend objects with sugar methods without touching their prototypes

Usage

Function makeProxy returns a proxy to its argument coated with many sugar methods allowing to manipulate it and to get to its internals. Only object types are handled though (including Array-like structures);

An actual proxy

makeProxy uses the Proxy constructor to create its proxy before coating it with sugar, making sure the returned object behaves in every respect like the original object when sugar methods are not called.

import makeProxy from 'proxy-helpers';

const obj = {
  a: 1,
  b: 'foo',
};
const o = makeProxy(obj);

o.a; // 1
o.b; // 'foo'
o.a = 2;
obj.a; // 2
o.c = 3;
obj.c; // 3

const arr = [0, 1, 2];
const a = makeProxy(arr);

a[0]; // 0
a[1] = 6;
arr[1]; // 6
a.push(9);
arr[3]; // 9

Getting properties

A few methods are provided to streamline getting at the original object internals.

  • keys(): Returns all enumerable keys of instance.
  • ownPropertyKeys(): Returns all property keys of instance.
  • attributeKeys(): keys() restricted to non functions.
  • stateKeys(): Non enumerable, non function keys of instance.
  • methodKeys(): all function keys, up the prototype chain of instance, Object.prototype excluded, instance included.
  • allMethodKeys(): all function keys, up the prototype chain of instance, including Object.prototype, instance included.
import makeProxy from 'proxy-helpers';

class Parent {
  constructor (x, y) {
    this.x = x;
    Object.defineProperty(this, 'y', {
      value: y,
      writable: true,
    });
  }

  hi () {
    return 'hi parent';
  }

  hello () {
    return 'hello parent';
  }
}

class Child extends Parent {
  hi () {
    return 'hi child';
  }
}

class OtherChild extends Parent {
  constructor (x, y) {
    super(x, y);

    this.hi = function () {
      return 'hi other child';
    };
  }
}

const c1 = new Child(1, 2);
const c2 = new OtherChild(1, 2);

const p1 = makeProxy(c1);
const p2 = makeProxy(c2);

p1.keys(); // ['x'];
p1.ownPropertyKeys(); // ['x', 'y'];
p1.attributeKeys(); // ['x'];
p1.stateKeys(); // ['y'];
p1.methodKeys(); // ['hi', 'hello'];
p1.allMethodKeys(); // ['hi', 'hello', 'toString', 'valueOf', ...];

p2.keys(); // ['x', 'hi'];
p2.ownPropertyKeys(); // ['x', 'y', 'hi'];
p2.attributeKeys(); // ['x'];
p2.stateKeys(); // ['y'];
p2.methodKeys(); // ['hi', 'hello'];
p2.allMethodKeys(); // ['hi', 'hello', 'toString', 'valueOf', ...];

Looping over properties

  • forEach((value, key, obj) => {...}): like Array forEach, applied to enumerable properties of instance.
  • map((value, key, obj) => {...}): like Array map, applied to enumerable properties of instance.
  • some((value, key, obj) => {...}): like Array some, applied to enumerable properties of instance.
  • every((value, key, obj) => {...}): like Array every, applied to enumerable properties of instance.

There exist corresponding counterparts for different sets of keys (see Getting properties):

  • forEachOwnProperty/mapOwnProperties/someOwnProperty/everyOwnProperty
  • forEachAttribute/mapAttributes/someAttribute/everyAttribute
  • forEachState/mapStates/someState/everyState
  • forEachMethod/mapMethods/someMethod/everyMethod
  • forEachMethodAll/mapMethodsAll/someMethodAll/everyMethodAll

Testing descriptors

  • propertyIsWritable(key): Whether or not key property is writable.
  • propertyIsEnumerable(key): Whether or not key property is enumerable.
  • propertyIsConfigurable(key): Whether or not key property is configurable.

Taking a snapshot

Any time you can take a snapshot of a set of properties, calling with no arguments methods snapshot/snapshotOwnProperties/snapshotAttributes/snapshotStates/snapshotMethods/snapshotMethodsAll.

Comparing

Any time you can compare a set of properties to expected values calling equiv/equivOwnProperties/equivAttributes/equivStates/equivMethods/equivMethodsAll.

License

proxy-helpers is MIT licensed.

© 2017-2019 Jason Lenoble