Comparing version 0.1.6 to 0.1.7
{ | ||
"name": "jsexp", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "scripts": { |
24
rules.js
@@ -13,2 +13,8 @@ 'use strict'; | ||
var isArray = require('lodash/isArray'); | ||
var match = function match(left, right) { | ||
return (isRegExp(right) ? right : new RegExp(right)).test(left); | ||
}; | ||
module.exports = { | ||
@@ -51,11 +57,21 @@ $eq: function $eq(left, right) { | ||
}, | ||
$in: function $in(left, right) { | ||
return includes(right, left); | ||
}, | ||
$empty: function $empty(value) { | ||
return !isNumber(value) && !isBoolean(value) && isEmpty(value); | ||
}, | ||
$in: function $in(left, right) { | ||
return includes(right, left); | ||
}, | ||
$match: function $match(left, right) { | ||
return (isRegExp(right) ? right : new RegExp(right)).test(left); | ||
if (isArray(right)) { | ||
for (var i = 0; i < right.length; i++) { | ||
if (!match(left, right[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return match(left, right); | ||
} | ||
}; |
@@ -46,5 +46,10 @@ 'use strict'; | ||
}; | ||
var fn = { | ||
isFunction: true | ||
var fnOneArg = { | ||
isFunction: true, | ||
argLen: 1 | ||
}; | ||
var fnTwoArg = { | ||
isFunction: true, | ||
argLen: 2 | ||
}; | ||
module.exports = { | ||
@@ -63,5 +68,5 @@ $eq: createOps('=='), | ||
$is: createOps('!!', unary), | ||
$empty: createOps('empty', fn), | ||
$in: createOps('in', fn), | ||
$match: createOps('match', fn) | ||
$empty: createOps('empty', fnOneArg), | ||
$in: createOps('in', fnTwoArg), | ||
$match: createOps('match', fnTwoArg) | ||
}; |
'use strict'; | ||
var isArray = require('lodash/isArray'); | ||
var rulesMap = require('./rulesMap'); | ||
@@ -23,2 +25,4 @@ | ||
isFunction = _rulesMap$type.isFunction, | ||
_rulesMap$type$argLen = _rulesMap$type.argLen, | ||
argLen = _rulesMap$type$argLen === void 0 ? 1 : _rulesMap$type$argLen, | ||
isUnary = _rulesMap$type.isUnary; | ||
@@ -41,3 +45,4 @@ | ||
if (isFunction) { | ||
return "".concat(operator, "(").concat(value.shift(), ")"); | ||
var appendArgs = argLen > 1 && value.length ? ",[".concat(value.join(','), "]") : ''; | ||
return "".concat(operator, "(").concat(value.shift()).concat(appendArgs, ")"); | ||
} | ||
@@ -44,0 +49,0 @@ |
@@ -34,2 +34,3 @@ 'use strict'; | ||
}); | ||
var COMPOUND = 'Compound'; // 暂不支持 | ||
@@ -39,8 +40,13 @@ var IDENTIFIER = 'Identifier'; | ||
var LITERAL = 'Literal'; | ||
var THIS_EXP = 'ThisExpression'; // 暂不支持 | ||
var CALL_EXP = 'CallExpression'; // 暂不支持 | ||
var UNARY_EXP = 'UnaryExpression'; | ||
var BINARY_EXP = 'BinaryExpression'; | ||
var LOGICAL_EXP = 'LogicalExpression'; | ||
var CONDITIONAL_EXP = 'ConditionalExpression'; // 暂不支持 | ||
var ARRAY_EXP = 'ArrayExpression'; | ||
var OPERATOR = 'operator'; | ||
var SUPPORT_EXP = [UNARY_EXP, LOGICAL_EXP, BINARY_EXP]; | ||
@@ -56,2 +62,15 @@ | ||
function throwError(type, value) { | ||
var error = new Error("No support ".concat(type, " `").concat(value, "`")); | ||
error.type = type; | ||
error.value = value; | ||
throw error; | ||
} | ||
function transformArray(array, callback) { | ||
return isArray(array) ? array.map(function (item) { | ||
return transform(item, callback); | ||
}) : []; | ||
} | ||
function transform(expression, callback) { | ||
@@ -76,5 +95,5 @@ if (expression) { | ||
var type = expression.type; | ||
var originOperator = expression.operator; | ||
var originOperator = expression[OPERATOR]; | ||
if (originOperator === '@') { | ||
if (type === UNARY_EXP && originOperator === '@') { | ||
var argument = transform(expression.argument, callback); | ||
@@ -91,5 +110,3 @@ return isValidate(argument) ? "@".concat(argument) : undefined; | ||
} else if (type === ARRAY_EXP) { | ||
return expression.elements.map(function (item) { | ||
return transform(item, callback); | ||
}); | ||
return transformArray(expression.elements, callback); | ||
} else { | ||
@@ -108,4 +125,45 @@ var _operator = rulesMapReverse[originOperator]; | ||
} | ||
} else if (type === CALL_EXP) { | ||
var callee = transform(expression.callee, callback); | ||
var _operator2 = rulesMap[callee] ? callee : rulesMapReverse[callee]; | ||
if (_operator2) { | ||
var rule = rulesMap[_operator2]; | ||
var _left = transform(expression.arguments.shift(), callback); | ||
if (rule.argLen > 1) { | ||
var _right = transformArray(expression.arguments, callback); | ||
return [_operator2, _left, _right]; | ||
} | ||
return [_operator2, _left]; | ||
} else { | ||
throwError(IDENTIFIER, callee); | ||
} | ||
} else if (type === COMPOUND) { | ||
var body = isArray(expression.body) ? expression.body : []; | ||
var identifier = body.find(function (item) { | ||
return !isArray(item) && item.name !== undefined; | ||
}); | ||
if (identifier) { | ||
throwError(IDENTIFIER, identifier.name); | ||
} | ||
var literal = body.find(function (item) { | ||
return !isArray(item) && item.value !== undefined; | ||
}); | ||
if (literal) { | ||
throwError(LITERAL, literal.value); | ||
} | ||
} else if (type === CONDITIONAL_EXP) { | ||
throwError(OPERATOR, '?:`'); | ||
} else if (type === THIS_EXP) { | ||
throwError(IDENTIFIER, 'this'); | ||
} else { | ||
throw new Error("no support operator `".concat(type, "`")); | ||
throwError(OPERATOR, type); | ||
} | ||
@@ -112,0 +170,0 @@ } |
@@ -26,6 +26,6 @@ 'use strict'; | ||
var parser = jsep.create(); | ||
[['OR', 11], ['AND', 12], ['in', 13], ['match', 14]].forEach(function (args) { | ||
[['OR', 11], ['AND', 12]].forEach(function (args) { | ||
parser.addBinaryOp.apply(parser, _toConsumableArray(args)); | ||
}); | ||
['empty', '!!', '@'].forEach(function (arg) { | ||
['!!', '@'].forEach(function (arg) { | ||
parser.addUnaryOp(arg); | ||
@@ -32,0 +32,0 @@ }); |
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
37702
1172