zer
Version:
Generate Gremlin queries by chaining JavaScript function calls
56 lines (42 loc) • 1.51 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createChainCreator = createChainCreator;
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _chain = require('./chain');
var _chainBuilder = require('./chain-builder');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Given a rendering function and a syntax, returns a Proxy which can intercept
* any property and creates a Chain that starts with that property 'name'.
*/
function createChainCreator(render, syntax) {
// This Proxy initiates the chain, and must return a new Chain
const handler = {
get(createProxiedChainTarget, name) {
return createProxiedChainTarget(name, render, syntax, {});
}
};
return new Proxy(createProxiedChain, handler);
}
;
/**
* Given a 'name', the Proxy intercepts and return a new ChainBuilder.
* 'createChainBuilder' is a function that returns a Proxy<Function>.
*/
function createProxiedChain(chainName, render, syntax) {
return new Proxy(_chainBuilder.createChainBuilder, {
get(target, name) {
const chain = (0, _chain.createChain)().startWith(chainName);
const builder = target(chain, render, syntax)[name];
return builder;
},
apply(target, thisArg, args) {
const chain = (0, _chain.createChain)().startWith(chainName);
const builder = target(chain, render, syntax)(...args);
return builder;
}
});
}
;