Convert backend code to ES modules

Bundling code now with Rollup for its flat bundling and tree shaking
support. Front end code still goes through Webpack for browser support.
This commit is contained in:
Zaid Abdulla committed 2019-02-04 23:47:54 -08:00
1 parent 824639976f
commit 1e41c72d60
62 files changed
+718 -898

No files matched your search

+15 -25
View File
@@ -1,8 +1,8 @@
"use strict";
const expressionParser = require("./expression-parser");
const expressionCnf = require("./expression-cnf");
const satSolver = require("./sat-solver");
import { map, parseLikePattern } from "./expression-parser";
import { booleanCnf } from "./expression-cnf";
import { naiveDpll } from "./sat-solver";
const isArray = Array.isArray;
@@ -26,7 +26,7 @@ function reduce(exp, callback) {
return exp;
}
function evaluate(exp, obj, now, cb) {
export function evaluate(exp, obj, now, cb) {
function getRegExp(pat, esc) {
const k = `${esc || ""}:${pat}`;
let c = regExpCache.get(exp);
@@ -35,7 +35,7 @@ function evaluate(exp, obj, now, cb) {
return c[k];
}
return expressionParser.map(exp, e => {
return map(exp, e => {
if (cb) e = cb(e);
if (!isArray(e)) return e;
@@ -162,7 +162,7 @@ function evaluate(exp, obj, now, cb) {
});
}
function and(exp1, exp2) {
export function and(exp1, exp2) {
if (!isArray(exp1)) return exp1 ? exp2 : exp1;
if (!isArray(exp2)) return exp2 ? exp1 : exp2;
@@ -177,7 +177,7 @@ function and(exp1, exp2) {
return res;
}
function or(exp1, exp2) {
export function or(exp1, exp2) {
if (!isArray(exp1)) return exp1 ? exp1 : exp2;
if (!isArray(exp2)) return exp2 ? exp2 : exp1;
@@ -192,19 +192,19 @@ function or(exp1, exp2) {
return res;
}
function not(exp) {
export function not(exp) {
if (isArray(exp) && exp[0] === "NOT") return exp[1];
return ["NOT", exp];
}
function subset(exp1, exp2) {
export function subset(exp1, exp2) {
const e = evaluate(["NOT", ["OR", ["NOT", exp1], exp2]]);
if (!isArray(e)) return !e;
const { vars, clauses } = expressionCnf.booleanCnf(e);
return !satSolver.naiveDpll(clauses, vars);
const { vars, clauses } = booleanCnf(e);
return !naiveDpll(clauses, vars);
}
function likePatternToRegExp(pat, esc = "", flags = "") {
export function likePatternToRegExp(pat, esc = "", flags = "") {
const convChars = {
"-": "\\-",
"/": "\\/",
@@ -225,7 +225,7 @@ function likePatternToRegExp(pat, esc = "", flags = "") {
"\\%": ".*",
"\\_": "."
};
let chars = expressionParser.parseLikePattern(pat, esc);
let chars = parseLikePattern(pat, esc);
if (!chars.length) return new RegExp("^$", flags);
chars = chars.map(c => convChars[c] || c);
chars[0] = chars[0] === ".*" ? "" : "^" + chars[0];
@@ -234,21 +234,11 @@ function likePatternToRegExp(pat, esc = "", flags = "") {
return new RegExp(chars.join(""), flags);
}
function extractParams(exp) {
export function extractParams(exp) {
const params = new Set();
expressionParser.map(exp, e => {
map(exp, e => {
if (isArray(e) && e[0] === "PARAM") params.add(e[1]);
return e;
});
return Array.from(params);
}
exports.evaluate = evaluate;
exports.and = and;
exports.or = or;
exports.not = not;
exports.subset = subset;
exports.parse = expressionParser.parse;
exports.stringify = expressionParser.stringify;
exports.likePatternToRegExp = likePatternToRegExp;
exports.extractParams = extractParams;