Skip to content

Commit

Permalink
functions refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Afsin Ustundag committed Jun 24, 2015
1 parent e392985 commit a075d28
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
35 changes: 12 additions & 23 deletions lib/jsonpath/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,44 +385,33 @@ var tracerAdapter = (function () {
return {
method: Tracer.traceRootPathPropertyGen(propertySteps, internalOpts)
};
}
};

adapter.handleJSExpression = function (expr) {
expr = expr.replace(/@path/g, PATH_VAR);
expr = expr.replace(/@/g, VALUE_VAR);
return expr;
};

adapter.reMap = [{
re: /\(\)$/,
obj: function (expr, opts) {
var fnName = expr.substring(0, expr.length - 2);
var fn = opts.functions[fnName];
},
fn: function (parameter, opts) {
var fn = opts.functions[parameter];
if (fn) {
return {
method: Tracer.tracePredefinedFunctionGen(fn, expr)
method: Tracer.tracePredefinedFunctionGen(fn, parameter + '()')
};
} else {
return {
method: Tracer.tracePostdefinedFunctionGen(expr)
method: Tracer.tracePostdefinedFunctionGen(parameter + '()')
};
}
}
}];
};

adapter.handleJSExpression = function (expr) {
expr = expr.replace(/@path/g, PATH_VAR);
expr = expr.replace(/@/g, VALUE_VAR);
return expr;
};

adapter.exprToMethod = function (expr, opts) {
if (typeof expr === 'object') {
var type = expr.type;
var parameter = expr.parameter;
return this.converter[type](parameter, opts);
}
for (var i = 0; i < this.reMap.length; ++i) {
var reItem = this.reMap[i];
if (reItem.re.test(expr)) {
return reItem.obj(expr, opts);
}
}
return {
method: Tracer.tracePropertyGen(expr)
};
Expand Down
23 changes: 20 additions & 3 deletions lib/jsonpath/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ var subscriptToNode = function (subscript) {
return subscript;
};

var updateNodesWithProperty = function (property, nodes) {
var n = property.length;
if (property.substring(n - 2, n) === '()') {
nodes.push({
type: 'fn',
parameter: property.substring(0, n - 2)
});
return;
}
nodes.push(property);
};

var normalize = exports.normalize = function (expr) {
var exprList = [];
var index = 0;
Expand All @@ -146,20 +158,25 @@ var normalize = exports.normalize = function (expr) {
var cp1 = expr.charAt(index + 1);
if (lastIndex !== index) {
var subExpr = expr.substring(lastIndex, index);
exprList.push(subExpr);
updateNodesWithProperty(subExpr, exprList);
}
if (cp1 === '.') {
exprList.push(types.RECURSIVE_DESCENT);
++index;
} else if (index + 1 === length) {
throw new Error('Incomplete path.');
}
++index;
if (expr.charAt(index) === '.') {
throw new Error('Invalid syntax "...".');
}
lastIndex = index;
continue;
}
if (c === '[') {
if (lastIndex !== index) {
var subExprLB = expr.substring(lastIndex, index);
exprList.push(subExprLB);
updateNodesWithProperty(subExprLB, exprList);
}
var openBrackets = 1;
++index;
Expand Down Expand Up @@ -214,7 +231,7 @@ var normalize = exports.normalize = function (expr) {
}
if (lastIndex < index) {
var subExprFinal = expr.substring(lastIndex, index);
exprList.push(subExprFinal);
updateNodesWithProperty(subExprFinal, exprList);
}
return exprList;
};
6 changes: 5 additions & 1 deletion test/jsonpath/test-normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ describe('jsonpath normalization', function () {

it('$.store..price.round()', function () {
var actual = jsonpath.normalize('$.store..price.round()');
expect(actual).to.deep.equal([jsonpath.types.ROOT, 'store', jsonpath.types.RECURSIVE_DESCENT, 'price', 'round()']);
var fnObject = {
type: 'fn',
parameter: 'round'
};
expect(actual).to.deep.equal([jsonpath.types.ROOT, 'store', jsonpath.types.RECURSIVE_DESCENT, 'price', fnObject]);
});

it('$.link[$.obj.library.books[0].references[*]].title', function () {
Expand Down

0 comments on commit a075d28

Please sign in to comment.