Git Product home page Git Product logo

proxly's Introduction

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

proxly's People

Contributors

pshihn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.