Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert mozjexl output to an mjs module #33

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version: 2

shared:
node12: &node12
node: &node
docker:
- image: cimg/node:12.22
- image: cimg/node:16.16

restore-yarn-cache: &restore-yarn-cache
restore_cache:
Expand Down Expand Up @@ -39,28 +39,28 @@ shared:
command: yarn lint

jobs:
build-node12:
build-node:
<<: *build
<<: *node12
<<: *node

test-node12:
test-node:
<<: *test
<<: *node12
<<: *node

lint-node12:
lint-node:
<<: *lint
<<: *node12
<<: *node

workflows:
version: 2
pr:
jobs:
- build-node12
- build-node

- lint-node12:
- lint-node:
requires:
- build-node12
- build-node

- test-node12:
- test-node:
requires:
- build-node12
- build-node
12 changes: 10 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ module.exports = {
env: {
browser: true,
node: true,
es6: true
es6: true,
},
extends: ["eslint:recommended", "plugin:prettier/recommended"]
overrides: [
{
files: "*.mjs",
parserOptions: {
sourceType: "module",
},
},
],
extends: ["eslint:recommended", "plugin:prettier/recommended"],
};
34 changes: 17 additions & 17 deletions lib/Jexl.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ function Jexl() {
* on either side of the operator. It should return either the resulting
* value, or a Promise that resolves with the resulting value.
*/
Jexl.prototype.addBinaryOp = function(operator, precedence, fn) {
Jexl.prototype.addBinaryOp = function (operator, precedence, fn) {
this._addGrammarElement(operator, {
type: "binaryOp",
precedence: precedence,
eval: fn
eval: fn,
});
};

Expand All @@ -53,11 +53,11 @@ Jexl.prototype.addBinaryOp = function(operator, precedence, fn) {
* operator. It should return either the resulting value, or a Promise
* that resolves with the resulting value.
*/
Jexl.prototype.addUnaryOp = function(operator, fn) {
Jexl.prototype.addUnaryOp = function (operator, fn) {
this._addGrammarElement(operator, {
type: "unaryOp",
weight: Infinity,
eval: fn
eval: fn,
});
};

Expand All @@ -73,7 +73,7 @@ Jexl.prototype.addUnaryOp = function(operator, fn) {
* if the transform fails, or a null first argument and the
* transformed value as the second argument on success.
*/
Jexl.prototype.addTransform = function(name, fn) {
Jexl.prototype.addTransform = function (name, fn) {
this._transforms[name] = fn;
};

Expand All @@ -82,7 +82,7 @@ Jexl.prototype.addTransform = function(name, fn) {
* accepts a map of one or more transform names to their transform function.
* @param {{}} map A map of transform names to transform functions
*/
Jexl.prototype.addTransforms = function(map) {
Jexl.prototype.addTransforms = function (map) {
for (var key in map) {
if (Object.prototype.hasOwnProperty.call(map, key))
this._transforms[key] = map[key];
Expand All @@ -94,7 +94,7 @@ Jexl.prototype.addTransforms = function(map) {
* @param {string} name The name of the transform function
* @returns {function} The transform function
*/
Jexl.prototype.getTransform = function(name) {
Jexl.prototype.getTransform = function (name) {
return this._transforms[name];
};

Expand All @@ -111,7 +111,7 @@ Jexl.prototype.getTransform = function(name) {
* if a callback is supplied, the returned promise will already have
* a '.catch' attached to it in order to pass the error to the callback.
*/
Jexl.prototype.eval = function(expression, context, cb) {
Jexl.prototype.eval = function (expression, context, cb) {
if (typeof context === "function") {
cb = context;
context = {};
Expand All @@ -122,11 +122,11 @@ Jexl.prototype.eval = function(expression, context, cb) {
// try/catch in case the callback throws.
var called = false;
return valPromise
.then(function(val) {
.then(function (val) {
called = true;
setTimeout(cb.bind(null, null, val), 0);
})
.catch(function(err) {
.catch(function (err) {
if (!called) setTimeout(cb.bind(null, err), 0);
});
}
Expand All @@ -137,7 +137,7 @@ Jexl.prototype.eval = function(expression, context, cb) {
* Removes a binary or unary operator from the Jexl grammar.
* @param {string} operator The operator string to be removed
*/
Jexl.prototype.removeOp = function(operator) {
Jexl.prototype.removeOp = function (operator) {
var grammar = this._getCustomGrammar();
if (
grammar[operator] &&
Expand All @@ -157,7 +157,7 @@ Jexl.prototype.removeOp = function(operator) {
* grammar element
* @private
*/
Jexl.prototype._addGrammarElement = function(str, obj) {
Jexl.prototype._addGrammarElement = function (str, obj) {
var grammar = this._getCustomGrammar();
grammar[str] = obj;
this._lexer = null;
Expand All @@ -171,12 +171,12 @@ Jexl.prototype._addGrammarElement = function(str, obj) {
* @returns {Promise<*>} resolves with the result of the evaluation.
* @private
*/
Jexl.prototype._eval = function(exp, context) {
Jexl.prototype._eval = function (exp, context) {
var self = this,
grammar = this._getGrammar(),
parser = new Parser(grammar),
evaluator = new Evaluator(grammar, this._transforms, context);
return Promise.resolve().then(function() {
return Promise.resolve().then(function () {
parser.addTokens(self._getLexer().tokenize(exp));
return evaluator.eval(parser.complete());
});
Expand All @@ -189,7 +189,7 @@ Jexl.prototype._eval = function(exp, context) {
* @returns {{}} a customizable grammar map.
* @private
*/
Jexl.prototype._getCustomGrammar = function() {
Jexl.prototype._getCustomGrammar = function () {
if (!this._customGrammar) {
this._customGrammar = {};
for (var key in defaultGrammar) {
Expand All @@ -207,7 +207,7 @@ Jexl.prototype._getCustomGrammar = function() {
* @returns {{}} the grammar map currently in use.
* @private
*/
Jexl.prototype._getGrammar = function() {
Jexl.prototype._getGrammar = function () {
return this._customGrammar || defaultGrammar;
};

Expand All @@ -217,7 +217,7 @@ Jexl.prototype._getGrammar = function() {
* appropriate to this Jexl instance.
* @private
*/
Jexl.prototype._getLexer = function() {
Jexl.prototype._getLexer = function () {
if (!this._lexer) this._lexer = new Lexer(this._getGrammar());
return this._lexer;
};
Expand Down
38 changes: 19 additions & 19 deletions lib/Lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ var numericRegex = /^-?(?:(?:[0-9]*\.[0-9]+)|[0-9]+)$/,
"\\btrue\\b",
"\\bfalse\\b",
// Octothorpe comments
"#.*[\\n\\4]?"
"#.*[\\n\\4]?",
],
postOpRegexElems = [
// Identifiers
"\\b[a-zA-Z_\\$][a-zA-Z0-9_\\$]*\\b",
// Numerics (without negative symbol)
"(?:(?:[0-9]*\\.[0-9]+)|[0-9]+)"
"(?:(?:[0-9]*\\.[0-9]+)|[0-9]+)",
],
minusNegatesAfter = [
"binaryOp",
"unaryOp",
"openParen",
"openBracket",
"question",
"colon"
"colon",
];

/**
Expand All @@ -53,9 +53,9 @@ function Lexer(grammar) {
* @returns {Array<string>} An array of substrings defining the functional
* elements of the expression.
*/
Lexer.prototype.getElements = function(str) {
Lexer.prototype.getElements = function (str) {
var regex = this._getSplitRegex();
return str.split(regex).filter(function(elem) {
return str.split(regex).filter(function (elem) {
// Remove empty strings
return elem;
});
Expand All @@ -71,7 +71,7 @@ Lexer.prototype.getElements = function(str) {
* converted to tokens
* @returns {Array<{type, value, raw}>} an array of token objects.
*/
Lexer.prototype.getTokens = function(elements) {
Lexer.prototype.getTokens = function (elements) {
var tokens = [],
negate = false;
for (var i = 0; i < elements.length; i++) {
Expand Down Expand Up @@ -120,7 +120,7 @@ Lexer.prototype.getTokens = function(elements) {
* @returns {Array<{type, value, raw}>} an array of token objects.
* @throws {Error} if the provided string contains an invalid token.
*/
Lexer.prototype.tokenize = function(str) {
Lexer.prototype.tokenize = function (str) {
var elements = this.getElements(str);
return this.getTokens(elements);
};
Expand All @@ -134,11 +134,11 @@ Lexer.prototype.tokenize = function(str) {
* @throws {Error} if the provided string is not a valid expression element.
* @private
*/
Lexer.prototype._createToken = function(element) {
Lexer.prototype._createToken = function (element) {
var token = {
type: "literal",
value: element,
raw: element
raw: element,
};
if (element[0] == '"' || element[0] == "'")
token.value = this._unquote(element);
Expand All @@ -159,7 +159,7 @@ Lexer.prototype._createToken = function(element) {
* @see https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
* @private
*/
Lexer.prototype._escapeRegExp = function(str) {
Lexer.prototype._escapeRegExp = function (str) {
str = str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
if (str.match(identRegex)) str = "\\b" + str + "\\b";
return str;
Expand All @@ -171,23 +171,23 @@ Lexer.prototype._escapeRegExp = function(str) {
* @returns {RegExp} An element-splitting RegExp object
* @private
*/
Lexer.prototype._getSplitRegex = function() {
Lexer.prototype._getSplitRegex = function () {
if (!this._splitRegex) {
var elemArray = Object.keys(this._grammar);
// Sort by most characters to least, then regex escape each
elemArray = elemArray
.sort(function(a, b) {
.sort(function (a, b) {
return b.length - a.length;
})
.map(function(elem) {
.map(function (elem) {
return this._escapeRegExp(elem);
}, this);
this._splitRegex = new RegExp(
"(" +
[
preOpRegexElems.join("|"),
elemArray.join("|"),
postOpRegexElems.join("|")
postOpRegexElems.join("|"),
].join("|") +
")"
);
Expand All @@ -204,9 +204,9 @@ Lexer.prototype._getSplitRegex = function() {
* symbol; false otherwise
* @private
*/
Lexer.prototype._isNegative = function(tokens) {
Lexer.prototype._isNegative = function (tokens) {
if (!tokens.length) return true;
return minusNegatesAfter.some(function(type) {
return minusNegatesAfter.some(function (type) {
return type === tokens[tokens.length - 1].type;
});
};
Expand All @@ -220,7 +220,7 @@ Lexer.prototype._isNegative = function(tokens) {
* @private
*/
var _whitespaceRegex = /^\s*$/;
Lexer.prototype._isWhitespace = function(str) {
Lexer.prototype._isWhitespace = function (str) {
return _whitespaceRegex.test(str);
};

Expand All @@ -230,7 +230,7 @@ Lexer.prototype._isWhitespace = function(str) {
* @returns {boolean} true if the string is a comment, false otherwise.
* @private
*/
Lexer.prototype._isComment = function(str) {
Lexer.prototype._isComment = function (str) {
return str.startsWith("#");
};

Expand All @@ -245,7 +245,7 @@ Lexer.prototype._isComment = function(str) {
* properly processed.
* @private
*/
Lexer.prototype._unquote = function(str) {
Lexer.prototype._unquote = function (str) {
var quote = str[0],
escQuoteRegex = new RegExp("\\\\" + quote, "g");
return str
Expand Down
Loading