Skip to content

Commit

Permalink
proxyIndexer updated to hdlrIndexer
Browse files Browse the repository at this point in the history
proxyIndexer modified to return the actual handler that needs to be passed to the Proxy object. Handler is now represented in a functional/class context so it must be initialized with the new keyword before being used as the handler parameter. The object will also expose the createProxy() method to easily retrieve a proxied object on a data object. The change was made so that the user can determine how best to use the handler, whether that be in it's own proxy, or by passing it to another custom handler that combines multiple handles in a complex operation (like my use-case).

var pobj = new jsonQuery.hdlrIndexer().createProxy(obj);
OR
var pobj = new Proxy(obj, new jsonQuery.hdlrIndexer());
OR
var pobj = new Proxy(obj, new MyCustomHandler(myFirstHndl, new jsonQuery.hdlrIndexer());
  • Loading branch information
gerneio committed Jan 13, 2019
1 parent 50bf259 commit ed62341
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions query.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,18 +376,20 @@ jsonQuery.setObjProto = () => Object.prototype.query = function(q, ...args) { re
// The resulting proxied object will have a direct indexer access to .query() method;
// getter returns a recursive proxyIndexer, if applicable
// I.E. {first:1, second:2}["$.first"]
jsonQuery.proxyIndexer = (obj) => {
var handler = {
get: (o, p) => {
if (o[p] && (o[p].constructor.name == "Object" || o[p].constructor.name == "Array"))
return jsonQuery.proxyIndexer(o[p]);
else
return p[0] == "$" ? jsonQuery.query(p, o) : undefined
}
}
// Create new instance of handler and then call createProxy with your object as parameter
// I.E. new jsonQuery.hdlrIndexer().createProxy(obj)
jsonQuery.hdlrIndexer = function() {
this.createProxy = (o) => new Proxy(o, this);

return new Proxy(obj, handler);
}
var isNonObj = (o) => o != undefined && [ "Object", "Array" ].indexOf(o.constructor.name) == -1;

this.get = (o, p) => {
if (!isNonObj(o[p]) && o[p] != undefined)
return this.createProxy(o[p]);

return p[0] == "$" ? jsonQuery.query(p, o) : undefined;
};
};


// If this module is being used with node.js
Expand Down

0 comments on commit ed62341

Please sign in to comment.