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

pimped-proxy

v1.1.1

Published

Comprehensive and simple Proxy implementation

Downloads

15

Readme

pimped-proxy

Build Status Coveralls npm version npm downloads npm dependencies npm devDependencies npm license

Pimped Proxy is a comprehensive, simple, ES5+ compatible, lightweight (~2KB) and universal implementation of Proxy for JavaScript and TypeScript. It is not a replacement of the ES2015 Proxy object but it gives a simplest way to:

  • lookup both simple properties and more complex paths
  • transform data on the fly without altering objects
  • flatten complex nested objects to a single level
  • make aggregations (like arithmetical operations) on the fly
  • create new full-proxy objects
  • give an existing object the capability to proxy some properties

Install

The easiest way is to install pimped-proxy as dependency:

npm install pimped-proxy --save

Usage

Proxy a list of properties

Creating a proxy will forward the target object properties
var Proxy = require('pimped-proxy');

var car = {
  brand: 'Peugeot',
  model: '308',
  power: '112hp'
};

var carProxy = new Proxy(car, ['brand', 'model', 'power']);

console.log(`I bought a ${carProxy.brand} ${carProxy.model} of ${carProxy.power}`);
// Displays "I bought a Peugeot 308 of 112hp"
Updating a property on the target object will update it on the proxy
var car = {
  brand: 'Peugeot',
  model: '308',
  power: '112hp'
};

var carProxy = new Proxy(car, ['brand', 'model', 'power']);
car.power = '250hp'

console.log(`I bought a ${carProxy.brand} ${carProxy.model} of ${carProxy.power}`);
// Displays "I bought a Peugeot 308 of 250hp"
Property proxying is two-way binding, then updating the proxy will update the target object
var car = {
  brand: 'Peugeot',
  model: '308',
  power: '112hp'
};

var carProxy = new Proxy(car, ['brand', 'model', 'power']);
carProxy.brand = 'Renault';
carProxy.model = 'Clio';

console.log(`I bought a ${car.brand} ${car.model} of ${car.power}`);
// Displays "I bought a Renault Clio of 112hp"
If some properties are not specified, then they won't be overridden
var car = {
  brand: 'Peugeot',
  model: '308',
  power: '112hp'
};

var carProxy = new Proxy(car, ['brand', 'model']);

console.log(`I bought a ${carProxy.brand} ${carProxy.model} of ${carProxy.power||'??'}`);
// Displays "I bought a Peugeot 308 of ??"

Proxy a list of paths

Proxying with a path will just retrieve the value from the target object after evaluation of the path
var car = {
  brand: 'Peugeot',
  model: '308',
  engine: {
    power: '112hp'
  }
};

var carProxy = new Proxy(car, {
  brand: 'brand',
  model: 'model',
  power: 'engine.power'
);

console.log(`I bought a ${carProxy.brand} ${carProxy.model} of ${carProxy.power}`);
// Displays "I bought a Peugeot 308 of 112hp"

Path resolution uses Lo-Dash like get and set methods.

Proxy a list of accessors

Defining getters are useful to tranform the data on the fly
var car = {
  brand: 'Peugeot',
  model: '308',
  power: '112hp'
};

var carProxy = new Proxy(car, {
  brand: {
    get: function (value) {
      return value
    }
  },
  model: {
    get: function (value) {
      return value + ' GTI'
    }
  },
  power: {
    get: function (value) {
      return '270hp'
    }
  }
});

console.log(`I bought a ${carProxy.brand} ${carProxy.model} of ${carProxy.power}`);
// Displays "I bought a Peugeot 308 GTI of 270hp"
Creating setters give the possibility to dynamically alter the target object
var car = {
  brand: 'Peugeot',
  model: '308',
  power: '112hp'
};

var carProxy = new Proxy(car, {
  brand: {
    set: (value) => {
      return String(value).toUpperCase()
    }
  },
  model: {
    set: (value) => {
      return value + ' GTI'
    }
  },
  power: {
    set: (value) => {
      return '270hp'
    }
  }
});

carProxy.brand = 'Renault';
carProxy.model = 'Clio';
carProxy.power = '140hp';

console.log(`I bought a ${car.brand} ${car.model} of ${car.power}`);
// Displays "I bought a RENAULT Clio GTI of 270hp"

Other examples

Mix the ways to proxy
var car = {
  brand: 'Peugeot',
  model: '308',
  engine: {
    power: '270hp'
  }
};

var carProxy = new Proxy(car, {
  brand: 'brand',
  model: {
    get: function (value) {
      return value + ' GTI'
    }
  },
  power: 'engine.power'
});

console.log(`I bought a ${carProxy.brand} ${carProxy.model} of ${carProxy.power}`);
// Displays "I bought a Peugeot 308 GTI of 270hp"
Transform an existing object into a Proxy
var existingCar = {
  name: 'MyCar',
  power: '250hp'
};

var car = {
  brand: 'Peugeot',
  model: '308',
  power: '112hp'
};

Proxy.lookup(existingCar, car, ['brand', 'model']);

console.log(`I bought a ${carProxy.brand} ${carProxy.model} of ${carProxy.power} and I called it ${carProxy.name}`);
// Displays "I bought a Peugeot 308 of 250hp and I called it MyCar"
In TypeScript?
import { Proxy } from 'pimped-proxy'

const car: ICar = {
  brand: 'Peugeot',
  model: '308',
  power: '112hp'
}

const carProxy: ICar = <ICar> new Proxy(car, ['brand', 'model', 'power'])

console.log(`I bought a ${carProxy.brand} ${carProxy.model} of ${carProxy.power}`)
// Displays "I bought a Peugeot 308 of 112hp"
And in the browser?
<script src="pimped-proxy/dist/proxy.browser.js"></script>
<script>
  var car = {
    brand: 'Peugeot',
    model: '308',
    power: '112hp'
  };

  var carProxy = new Proxy(car, ['brand', 'model', 'power']);

  console.log(`I bought a ${carProxy.brand} ${carProxy.model} of ${carProxy.power}`);
  // Displays "I bought a Peugeot 308 of 112hp"
</script>

License

Code licensed under MIT License.