This is a library which enables users to "trap" deeply nested objects into
proxies.
The API is identical to the proxy API, except that keys are now paths in the
object, and that a special nest()
-procedure is added to the get-trap as
a parameter. The default behavior is to mimick the object that has been passed
through as first argument.
A simple example for DSL language building:
const db = proxyDeep({}, {
get(target, path, nest) {
return nest()
},
apply(target, path, thisArg, argumentsList) {
return path
}
})
console.log(db.select.from.where) // outputs ['select', 'from', 'where']
Another example using Node's process object:
const proxyDeep = require('proxy-deep')
const _ = require('lodash')
const { EventEmitter } = require('events')
const emitter = new EventEmitter()
const pp = proxyDeep(process, {
get(p, path, nest) {
const val = _.get(p, path)
if (typeof val !== 'object') {
emitter.emit('access', path)
return val
} else {
return nest()
}
}
})
emitter.on('access', path => {
console.log(`${path} was accessed.`)
})
pp.argv[0] // trapped!
Currently, no additional options are supported.
All handlers are called with the first and second argument root
and path
.
The rest is identical to JavaScript's Proxy handler arguments.
A trap for the property accessor.
A trap for the property setter.
A trap for the in
-keyword.
A trap for the delete
keyword.
A trap for a function application.
A trap for the new
-keyword.
A trap for Object.getOwnPropertyDescriptor()
.
A trap for Object.getOwnPropertyNames
.
A trap for Object.getPrototypeOf()
.
A trap for Object.setPrototypeOf()
.
A trap for Object.isExtensible()
.
A trap for Object.preventExtensions()
.
If anyone is willing to write some good tests I would greatly appreciate it. Open a pull request and I'll merge when I have the time!