Shadow Copy
Zero dependency deep proxy creation tool.
usage
This deep proxy tool offers by default nesting, property chain path and a cache to avoid Proxy re-instanciation without preventing carbage collection.
yarn add shadowcopy-js
;
Example 1 DSL query building
In this example we let the user create property chains by nesting functions and log the path when applied.
const db = {} return ShadowCopy; { return ShadowCopypath argArray; }; console;// [['select', 'from', 'where'], ['a === b']]
Example 2 watch
In this exemple we create a watcher logging the modification of an object and his childrens via the handler 'set'.
{ { return stack3; } return watched console; targetp = value; return true; ;} // create an object to watch and name it 'my_foo'const p = ; { val'foo''bar' = 'sneak!'; //logs: modified property "[ 'foo', 'bar' ]" of object "my_foo" at "at fn (file:///var/www/test.js:165:35)" from "val" to "sneak!"}p;
Example 3 Property protection
This third example protects private properties from being set.
const p = 'foo': 'bar': 'val' '_private': null if p0 === '_' throw `Invalid attempt to set private "" property`; targetp = value; return true; ; p'foo''bar' = 'val2';p'foo''_private' = 'val2'; // Error
api
class ShadowCopy
Create a deep proxy with nesting by default.
contructor (target, handler, path = [])
- target
object
original to copy/proxy - handler
object
proxy handler (full doc at MDN) - path
string[]
root path of the original object, will be prepended to ShadowCopy.path
static ShadowCopy.path
An array of string which value correspond to the chain of properties called to proc a handler.
It should only be used in a handler.
Example:
const p = '1': '2': '3': '4': '5' console; // ['1', '2', '3'] targetp = value; return true; ; p'1''2''3' = {};
static nest(original = currentTarget[currentP])
Create a nested Proxy.
It should only be used in a handler.
The default get handler returns a ShadowCopy to offer by default nesting, if you wish to implement the 'get' handler and to keep the by default nesting, return ShadowCopy.nest():
const p = {} return ShadowCopy; // same as: return ShadowCopy.nest(target[p]); ;
test
Running the tests requires node at least node 14 and yarn.
yarn # install the development dependencies yarn test # run the tests
license
MIT