deep-proxy-polyfill
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

Deep Proxy Polyfill Tweet

Acts as a deep or recursive Proxy for getter and setter attributes on an object.

version minified size minzipped size downloads build

Install

  • npm install deep-proxy-polyfill --save or
  • yarn add deep-proxy-polyfill

Getter

import deepProxy from 'deep-proxy-polyfill';
 
const rootObject = {
  a: {
    b: {
      c: 1
    }
  }
};
 
const getHandler = (obj, key, root, keys) => {
  assert(root === rootObject);
 
  // [ 'a', 'b' ]
  if (keys.length == 2) {
    assert(obj === rootObject.a.b);
    asset(key === 'c');
    return obj[key] + 2;
  }
 
  return obj[key];
};
 
const proxy = deepProxy(rootObject, { get: getHandler });
 
// Getter returns an additional 2.
assert(proxy.a.b.c === 3);

Setter

import deepProxy from 'deep-proxy-polyfill';
 
const rootObject = {
  a: {
    b: {
      c: 1
    }
  }
};
 
// Set values with an additional 2.
const setHandler = (obj, key, value, root, keys) => {
  assert(root === rootObject);
  obj[key] = value + 2;
};
 
const proxy = deepProxy(rootObject, { set: setHandler });
proxy.a.b.c = 3;
 
assert(proxy.a.b.c === 5);

Readme

Keywords

none

Package Sidebar

Install

npm i deep-proxy-polyfill

Weekly Downloads

6

Version

1.0.4

License

MIT

Unpacked Size

6.2 kB

Total Files

5

Last publish

Collaborators

  • charlesstover