Skip to content

Commit

Permalink
Better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenThuriot committed Dec 29, 2022
1 parent aa4713a commit cf7ac12
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export declare const executeExpression: (model: {
[key: string]: any;
}, expression: string) => boolean;
}, expression: string | boolean) => boolean;
12 changes: 7 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ exports.executeExpression = (function () {
return value1 < value2;
};
var contains = function (value1, value2) {
value1 = value1;
if (typeof value1 === 'string') {
return value1.indexOf(value2) >= 0;
}
Expand Down Expand Up @@ -71,7 +70,7 @@ exports.executeExpression = (function () {
break;
}
}
throw new Error("Invalid Expression");
throw new Error("Invalid Expression Part: " + value);
};
var parseEquals = function (value) {
var parameters = resolveParameters(value);
Expand Down Expand Up @@ -177,13 +176,16 @@ exports.executeExpression = (function () {
return toReturn;
};
return function (m, e) {
if (!e) {
throw new Error("Invalid Expression: null");
if (typeof e === 'boolean') {
return function () { return e; };
}
e = e.trim();
if (!e) {
throw new Error("Invalid Expression: empty");
}
e = ('' + e).trim();
if (!e) {
throw new Error("Invalid Expression: whitespace");
}
if (e.indexOf('.') > 0) {
m = ___flattenObject(m);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-expressions",
"version": "0.0.3",
"version": "0.0.4",
"main": "lib/index.js",
"devDependencies": {
"typescript": "^4.9.4"
Expand Down
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const executeExpression = (function (): (model: { [key: string]: any; }, expression: string) => boolean {
export const executeExpression = (function (): (model: { [key: string]: any; }, expression: string | boolean) => boolean {
const ___expressionCache: { [key: string]: any; } = {};
const ___parseExpression = (expression: string): (model: { [key: string]: any; }) => any => {
const cachedExpression = ___expressionCache[expression];
Expand All @@ -19,8 +19,6 @@ export const executeExpression = (function (): (model: { [key: string]: any; },
}

const contains = (value1: any, value2: any): boolean => {
value1 = value1;

if (typeof value1 === 'string') {
return value1.indexOf(value2) >= 0;
}
Expand Down Expand Up @@ -85,7 +83,7 @@ export const executeExpression = (function (): (model: { [key: string]: any; },
break;
}
}
throw new Error("Invalid Expression");
throw new Error("Invalid Expression Part: " + value);
}

const parseEquals = (value: string): (model: { [key: string]: any; }) => boolean => {
Expand Down Expand Up @@ -217,14 +215,18 @@ export const executeExpression = (function (): (model: { [key: string]: any; },
}

return (m, e) => {
if (typeof e === 'boolean') {
return () => e;
}

if (!e) {
throw new Error("Invalid Expression: null");
throw new Error("Invalid Expression: empty");
}

e = e.trim();
e = (''+e).trim();

if (!e) {
throw new Error("Invalid Expression: empty");
throw new Error("Invalid Expression: whitespace");
}

if (e.indexOf('.') > 0) {
Expand Down

0 comments on commit cf7ac12

Please sign in to comment.