Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "jsexp", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "scripts": { |
70
parse.js
'use strict'; | ||
var rulesMap = require('./rulesMap'); | ||
var transform = require('./transform'); | ||
var withScope = require('./withScope'); | ||
var rulesMapReverse = Object.keys(rulesMap).reduce(function (map, rule) { | ||
map[rulesMap[rule].operator] = rule; | ||
return map; | ||
}, { | ||
AND: '$and', | ||
OR: '$or', | ||
empty: '$empty', | ||
"in": '$in', | ||
match: '$match' | ||
}); | ||
var IDENTIFIER = 'Identifier'; | ||
var MEMBER_EXP = 'MemberExpression'; | ||
var LITERAL = 'Literal'; | ||
var UNARY_EXP = 'UnaryExpression'; | ||
var BINARY_EXP = 'BinaryExpression'; | ||
var LOGICAL_EXP = 'LogicalExpression'; | ||
var ARRAY_EXP = 'ArrayExpression'; | ||
function isValidate(value) { | ||
if (Array.isArray(value)) { | ||
return !!value.length; | ||
} | ||
return value !== undefined; | ||
} | ||
function transform(expression) { | ||
if (expression) { | ||
var type = expression.type; | ||
var originOperator = expression.operator; | ||
if (originOperator === '@') { | ||
var argument = transform(expression.argument); | ||
return isValidate(argument) ? "@".concat(argument) : undefined; | ||
} else if (type === LITERAL) { | ||
return expression.value; | ||
} else if (type === IDENTIFIER) { | ||
return expression.name; | ||
} else if (type === MEMBER_EXP) { | ||
var object = transform(expression.object); | ||
var property = transform(expression.property); | ||
return isValidate(object) && isValidate(property) ? "".concat(object, ".").concat(property) : undefined; | ||
} else if (type === ARRAY_EXP) { | ||
return expression.elements.map(function (item) { | ||
return transform(item); | ||
}); | ||
} else { | ||
var operator = rulesMapReverse[originOperator]; | ||
if (operator) { | ||
if (type === UNARY_EXP) { | ||
var _argument = transform(expression.argument); | ||
return isValidate(_argument) ? [operator, _argument] : []; | ||
} else if (type === LOGICAL_EXP || type === BINARY_EXP) { | ||
var left = transform(expression.left); | ||
var right = transform(expression.right); | ||
return isValidate(left) && isValidate(right) ? [operator, left, right] : []; | ||
} | ||
} else { | ||
throw new Error("no support operator `".concat(operator, "`")); | ||
} | ||
} | ||
} | ||
return []; | ||
} | ||
function parse(expression) { | ||
@@ -76,0 +8,0 @@ return withScope(function (exec) { |
@@ -5,5 +5,10 @@ 'use strict'; | ||
function validate(expression) { | ||
function validate(expression, callback) { | ||
return withScope(function (exec) { | ||
var result = exec(expression); | ||
if (callback) { | ||
return callback(result); | ||
} | ||
return Array.isArray(result) ? !!result.length : !!result; | ||
@@ -10,0 +15,0 @@ }, false); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33506
13
1058