Introduce ESLint and Prettier
This commit is contained in:
1 parent
9c943d0bbf
commit
e494bdca8a
26 files changed
+1585
-1594
No files matched your search
+62
-98
@@ -21,21 +21,16 @@
|
||||
const common = require("./common");
|
||||
|
||||
function stringToRegexp(input, flags) {
|
||||
if (input.indexOf("*") === -1)
|
||||
return false;
|
||||
if (input.indexOf("*") === -1) return false;
|
||||
|
||||
let output = input.replace(/[\[\]\\\^\$\.\|\?\+\(\)]/, "\\$&");
|
||||
if (output[0] === "*")
|
||||
output = output.replace(/^\*+/g, "");
|
||||
else
|
||||
output = "^" + output;
|
||||
let output = input.replace(/[[\]\\^$.|?+()]/, "\\$&");
|
||||
if (output[0] === "*") output = output.replace(/^\*+/g, "");
|
||||
else output = "^" + output;
|
||||
|
||||
if (output[output.length - 1] === "*")
|
||||
output = output.replace(/\*+$/g, "");
|
||||
else
|
||||
output = output + "$";
|
||||
if (output[output.length - 1] === "*") output = output.replace(/\*+$/g, "");
|
||||
else output = output + "$";
|
||||
|
||||
output = output.replace(/[\*]/, ".*");
|
||||
output = output.replace(/[*]/, ".*");
|
||||
return new RegExp(output, flags);
|
||||
}
|
||||
|
||||
@@ -43,19 +38,15 @@ function normalize(input) {
|
||||
if (common.typeOf(input) === common.STRING_TYPE) {
|
||||
const vals = [input];
|
||||
let m = /^\/(.*?)\/(g?i?m?y?)$/.exec(input);
|
||||
if (m)
|
||||
vals.push({"$regex": new RegExp(m[1], m[2])});
|
||||
if (m) vals.push({ $regex: new RegExp(m[1], m[2]) });
|
||||
|
||||
if (+input === parseFloat(input))
|
||||
vals.push(+input);
|
||||
if (+input === parseFloat(input)) vals.push(+input);
|
||||
|
||||
let d = new Date(input);
|
||||
if (input.length >= 8 && d.getFullYear() > 1983)
|
||||
vals.push(d);
|
||||
if (input.length >= 8 && d.getFullYear() > 1983) vals.push(d);
|
||||
|
||||
let r = stringToRegexp(input);
|
||||
if (r !== false)
|
||||
vals.push({"$regex": r});
|
||||
if (r !== false) vals.push({ $regex: r });
|
||||
|
||||
return vals;
|
||||
}
|
||||
@@ -64,16 +55,13 @@ function normalize(input) {
|
||||
|
||||
function expandValue(value) {
|
||||
if (common.typeOf(value) === common.ARRAY_TYPE) {
|
||||
const a = [];
|
||||
for (let j of value)
|
||||
a = a.conact(expandValue(j));
|
||||
let a = [];
|
||||
for (let j of value) a = a.conact(expandValue(j));
|
||||
return [a];
|
||||
} else if (common.typeOf(value) !== common.OBJECT_TYPE) {
|
||||
const n = normalize(value);
|
||||
if (common.typeOf(n) !== common.ARRAY_TYPE)
|
||||
return [n];
|
||||
else
|
||||
return n;
|
||||
if (common.typeOf(n) !== common.ARRAY_TYPE) return [n];
|
||||
else return n;
|
||||
}
|
||||
|
||||
const objs = [];
|
||||
@@ -89,14 +77,12 @@ function expandValue(value) {
|
||||
let i = 0;
|
||||
while (i < indices.length) {
|
||||
const obj = {};
|
||||
for (let j = 0; j < keys.length; ++ j)
|
||||
obj[keys[j]] = values[j][indices[j]];
|
||||
for (let j = 0; j < keys.length; ++j) obj[keys[j]] = values[j][indices[j]];
|
||||
objs.push(obj);
|
||||
|
||||
for (i = 0; i < indices.length; ++ i) {
|
||||
for (i = 0; i < indices.length; ++i) {
|
||||
indices[i] += 1;
|
||||
if (indices[i] < values[i].length)
|
||||
break;
|
||||
if (indices[i] < values[i].length) break;
|
||||
indices[i] = 0;
|
||||
}
|
||||
}
|
||||
@@ -107,11 +93,10 @@ function permute(param, val) {
|
||||
const conditions = [];
|
||||
const values = expandValue(val);
|
||||
|
||||
if (param[param.lastIndexOf(".") + 1] !== "_")
|
||||
param += "._value";
|
||||
if (param[param.lastIndexOf(".") + 1] !== "_") param += "._value";
|
||||
|
||||
for (let v of values) {
|
||||
const obj = {}
|
||||
const obj = {};
|
||||
obj[param] = v;
|
||||
conditions.push(obj);
|
||||
}
|
||||
@@ -122,7 +107,8 @@ function permute(param, val) {
|
||||
function expand(query) {
|
||||
const newQuery = {};
|
||||
for (let [k, v] of Object.entries(query)) {
|
||||
if (k[0] === "$") { // Operator
|
||||
if (k[0] === "$") {
|
||||
// Operator
|
||||
newQuery[k] = v.map(e => expand(e));
|
||||
} else {
|
||||
const conditions = permute(k, v);
|
||||
@@ -131,16 +117,16 @@ function expand(query) {
|
||||
if (v && (v["$ne"] != null || v["$not"] != null)) {
|
||||
if (Object.keys(v).length > 1)
|
||||
throw new Error("Cannot mix $ne or $not with other operators");
|
||||
for (let c of conditions)
|
||||
newQuery["$an"].push(c);
|
||||
for (let c of conditions) newQuery["$an"].push(c);
|
||||
} else {
|
||||
newQuery["$and"].push({"$or": conditions});
|
||||
newQuery["$and"].push({ $or: conditions });
|
||||
}
|
||||
} else {
|
||||
Object.assign(newQuery, conditions[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newQuery;
|
||||
}
|
||||
|
||||
@@ -149,16 +135,13 @@ function testExpressions(params, expressions, lop) {
|
||||
const res = test(params, f);
|
||||
switch (lop) {
|
||||
case "$and":
|
||||
if (!res)
|
||||
return false;
|
||||
if (!res) return false;
|
||||
break;
|
||||
case "$or":
|
||||
if (res)
|
||||
return true;
|
||||
if (res) return true;
|
||||
break;
|
||||
case "$nor":
|
||||
if (res)
|
||||
return false;
|
||||
if (res) return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,20 +160,18 @@ function testExpressions(params, expressions, lop) {
|
||||
function test(params, query) {
|
||||
let res;
|
||||
for (let [k, v] of Object.entries(query)) {
|
||||
if (k.charAt(0) === "$") { // Logical operator
|
||||
if (k.charAt(0) === "$") {
|
||||
// Logical operator
|
||||
res = testExpressions(params, v, k);
|
||||
} else {
|
||||
const value = params[k];
|
||||
if (common.typeOf(v) !== common.OBJECT_TYPE) {
|
||||
// TODO comparing array to regex, array to array, and object to object
|
||||
if (common.typeOf(value) === common.ARRAY_TYPE) {
|
||||
// TODO comparing array to regex, array to array, and object to object
|
||||
res = value.indexOf(v) !== -1;
|
||||
} else {
|
||||
if (common.typeOf(v) === common.REGEXP_TYPE) {
|
||||
res = v.test(value);
|
||||
} else {
|
||||
res = v === value;
|
||||
}
|
||||
if (common.typeOf(v) === common.REGEXP_TYPE) res = v.test(value);
|
||||
else res = v === value;
|
||||
}
|
||||
} else {
|
||||
for (let [k2, v2] of Object.entries(v)) {
|
||||
@@ -198,8 +179,7 @@ function test(params, query) {
|
||||
case "$ne":
|
||||
if (common.typeOf(value) === common.ARRAY_TYPE)
|
||||
res = value.indexOf(v2) === -1;
|
||||
else
|
||||
res = value !== v2;
|
||||
else res = value !== v2;
|
||||
break;
|
||||
case "$lt":
|
||||
res = value < v2;
|
||||
@@ -218,16 +198,12 @@ function test(params, query) {
|
||||
break;
|
||||
case "$in":
|
||||
throw new Error("Operator not supported");
|
||||
break;
|
||||
case "$nin":
|
||||
throw new Error("Operator not supported");
|
||||
break;
|
||||
case "$all":
|
||||
throw new Error("Operator not supported");
|
||||
break;
|
||||
case "$exists":
|
||||
throw new Error("Operator not supported");
|
||||
break;
|
||||
default:
|
||||
throw new Error("Operator not supported");
|
||||
}
|
||||
@@ -235,8 +211,7 @@ function test(params, query) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!res)
|
||||
return false;
|
||||
if (!res) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -246,49 +221,41 @@ function matchType(src, dst) {
|
||||
if (type === "string") {
|
||||
return String(dst);
|
||||
} else if (type === "number") {
|
||||
if (+dst === parseFloat(dst))
|
||||
return +dst;
|
||||
else if (!isNaN(Date.parse(dst)))
|
||||
return Date.parse(dst);
|
||||
if (+dst === parseFloat(dst)) return +dst;
|
||||
else if (!isNaN(Date.parse(dst))) return Date.parse(dst);
|
||||
} else if (type === "boolean") {
|
||||
const v = String(dst).trim().toLowerCase();
|
||||
if (v === "true" || v === "1")
|
||||
return true;
|
||||
else if (v === "false" || v === "0")
|
||||
return false;
|
||||
const v = String(dst)
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
if (v === "true" || v === "1") return true;
|
||||
else if (v === "false" || v === "0") return false;
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
function testFilter(obj, filter) {
|
||||
for (let [k, v] of Object.entries(filter)) {
|
||||
const [param, op] = k.split(/([^a-zA-Z0-9\-\_\.].*)/, 2)
|
||||
const [param, op] = k.split(/([^a-zA-Z0-9\-_.].*)/, 2);
|
||||
const val = matchType(obj[param], v);
|
||||
switch (op) {
|
||||
case "=":
|
||||
case undefined:
|
||||
if (obj[param] !== val)
|
||||
return false;
|
||||
if (obj[param] !== val) return false;
|
||||
break;
|
||||
case ">":
|
||||
if (!(obj[param] > val))
|
||||
return false;
|
||||
if (!(obj[param] > val)) return false;
|
||||
break;
|
||||
case ">=":
|
||||
if (!(obj[param] >= val))
|
||||
return false;
|
||||
if (!(obj[param] >= val)) return false;
|
||||
break;
|
||||
case "<":
|
||||
if (!(obj[param] < val))
|
||||
return false;
|
||||
if (!(obj[param] < val)) return false;
|
||||
break;
|
||||
case "<=":
|
||||
if (!(obj[param] <= val))
|
||||
return false;
|
||||
if (!(obj[param] <= val)) return false;
|
||||
break;
|
||||
case "!":
|
||||
if (obj[param] === val)
|
||||
return false;
|
||||
if (obj[param] === val) return false;
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unrecognized operator ${op}`);
|
||||
@@ -298,25 +265,21 @@ function testFilter(obj, filter) {
|
||||
}
|
||||
|
||||
function convertMongoQueryToFilters(query, filters) {
|
||||
filters = filters || {}
|
||||
filters = filters || {};
|
||||
for (let [k, v] of Object.entries(query)) {
|
||||
if (k[0] === "$") {
|
||||
if (k === "$and")
|
||||
for (let vv of v)
|
||||
convertMongoQueryToFilters(vv, filters);
|
||||
else
|
||||
throw new Error(`Operator ${k} not supported`);
|
||||
for (let vv of v) convertMongoQueryToFilters(vv, filters);
|
||||
else throw new Error(`Operator ${k} not supported`);
|
||||
} else if (k === "_tags") {
|
||||
if (common.typeOf(v) === common.OBJECT_TYPE) {
|
||||
for (let [kk, vv] of Object.entries(v)) {
|
||||
vv = vv.replace(/[^a-zA-Z0-9\-]+/g, "_");
|
||||
if (kk === "$ne")
|
||||
filters[`Tags.${vv}!`] = true;
|
||||
else
|
||||
throw new Error(`Operator ${kk} not supported`);
|
||||
vv = vv.replace(/[^a-zA-Z0-9-]+/g, "_");
|
||||
if (kk === "$ne") filters[`Tags.${vv}!`] = true;
|
||||
else throw new Error(`Operator ${kk} not supported`);
|
||||
}
|
||||
} else {
|
||||
v = v.replace(/[^a-zA-Z0-9\-]+/g, "_");
|
||||
v = v.replace(/[^a-zA-Z0-9-]+/g, "_");
|
||||
filters[`Tags.${v}`] = true;
|
||||
}
|
||||
} else {
|
||||
@@ -379,22 +342,22 @@ function convertMongoQueryToFilters(query, filters) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return filters;
|
||||
}
|
||||
|
||||
function sanitizeQueryTypes(query, types) {
|
||||
for (let [k, v] of Object.entries(query)) {
|
||||
if (k[0] === "$") { // Logical operator
|
||||
for (let vv of v)
|
||||
sanitizeQueryTypes(vv, types);
|
||||
if (k[0] === "$") {
|
||||
// Logical operator
|
||||
for (let vv of v) sanitizeQueryTypes(vv, types);
|
||||
} else if (k in types) {
|
||||
if (common.typeOf(v) === common.OBJECT_TYPE) {
|
||||
for (let [kk, vv] of Object.entries(v)) {
|
||||
switch (kk) {
|
||||
case "$in":
|
||||
case "$nin":
|
||||
for (let i = 0; i < vv.length; ++ i)
|
||||
vv[i] = types[k](vv[i]);
|
||||
for (let i = 0; i < vv.length; ++i) vv[i] = types[k](vv[i]);
|
||||
break;
|
||||
case "$eq":
|
||||
case "$gt":
|
||||
@@ -417,6 +380,7 @@ function sanitizeQueryTypes(query, types) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user