ProxyMate
Introduction
This library provides a wrapper for ES6 Proxies that can be used to emulate protected class properties as well as provide data encapsulation.
Examples
{ thisdata = 'propA': 'this is prop a' 'propB': 'this is prop b' ; } // Simple data encapsulationlet obj1 = ;console; // 'this is prop a'console; // 'this is prop a'obj1propA = 'prop a has changed';console; // 'prop a has changed'console; // 'prop a has changed' // Data encapsulation with read-only memberslet obj2 = ;console; // 'this is prop a'console; // 'this is prop a'obj2propA = 'prop a is read-only'; // throws exceptionconsole; // 'this is prop a'console; // 'this is prop a' // Complex example with a nested proxylet obj3 = ;let obj4 = ;obj3nested = obj4; console; // 'this is prop a'console; // 'this is prop a'console; // 'this is prop a'console; // 'this is prop a' obj3propA = 'prop a is read-only'; // throws exceptionobj3nestedpropA = 'prop a of the inner proxy can be modified';console; // 'prop a of the inner proxy can be modified'console; // 'prop a of the inner proxy can be modified'
Classes can also be proxied automatically in the constructor.
// Typically, value objects should not be modifiable once created { thisdata = sourceData; return ; }
You don't have to use ES6 classes, either. Plain objects are ok too!
const getObj = propA: 'this is prop a' propB: 'this is prop b' nested: propC: 'this is nested.propC' ; // Make a pure value object by marking everything read-onlylet obj4 = ;obj4propA = 'prop a is read-only'; // throws exceptionobj4propB = 'prop b is read-only'; // throws exceptionobj4nestedpropC = 'even nested props are read-only'; // throws exception
License
MIT License. See LICENSE.md for the complete license text.