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

proxly

v1.0.0

Published

Easiest way to create a single proxy to a list of functions or objects

Downloads

9

Readme

Proxly

Proxy any list of objects or functions to a single entity. All common properties and methods are automatically reflected.

  • Objects can be heterogeneous with a shared interface
  • Can proxy a set of functions (sync/async) to a single call
  • Works with Arrays
  • Supports callbacks as arguments
  • Excellent with async/await
  • Tiny in size. Only 444 bytes gzipped

Install

Download the latest from dist folder or from npm:

npm install --save proxly

Usage/Examples

Proxy Functions

function add(a, b) { return a + b; }
function subtract(a, b) { return a - b; }
async function multiply(a, b) { return a * b; }

(async () => {
  let proxy = proxly(add, subtract, multiply);
  let result = await proxy(4, 2);
  console.log(result); // [6, 2, 8]
})();

Proxy Objects

Objects could be instances of the same class or just any two objects with a common interface.

class Operation {
  constructor(name) {
    this.name = name;
    this.count = 0;
  }
  run(a, b) {
    this.count++;
    if (this.name === 'add') return a + b;
    if (this.name === 'subtract') return a - b;
  }
}
let adder = new Operation('add');
let subtractor = new Operation('subtract');

(async () => {
  let proxy = proxly(adder, subtractor);
  console.log(await proxy.name); // ["add", "subtract"]
  console.log(await proxy.count); // [0, 0]
  console.log(await proxy.run(10, 4)); // [14, 6]
  console.log(await proxy.count); // [1, 1]
})();

Proxy Arrays

Of course it works with arrays

let fruits = ["apple", "banana", "grape"];
let colors = ["red", "yellow", "green"];
(async () => {
  let proxy = proxly(fruits, colors);
  console.log(await proxy[1]); // ["banana", "yellow"]
  console.log(await proxy.length); // [3, 3]
  proxy.push('orange');
  console.log(await proxy.length); // [4, 4]
  console.log(fruits); // ["apple", "banana", "grape", "orange"]
  console.log(colors); // ["red", "yellow", "green", "orange"]
})();

Callbacks

If a callback is passed into a proxied set of functions (or a method in a proxied set of objects), it is called back sequentially in the order the proxy was defined.

function add(a, b, cb) {
  cb(a + b);
}
function sub(a, b, cb) {
  setTimeout(() => {
    cb(a - b);
  }, 1000);
}
(async () => {
  let cb = (result) => {
    console.log("result", result);
  };
  let proxy = proxly(add, sub);
  proxy(6, 4, cb);
})();

Output:

result 10
result 2

Examples

See the examples folder

License

MIT License (c) Preet Shihn