Skip to content

Commit

Permalink
add generics support
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Jun 9, 2018
1 parent 6d6e8ba commit 41364a7
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
11 changes: 7 additions & 4 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ export interface NotOp {
not: Expression;
}
export declare type Expression = FuncOp | AndOp | OrOp | NotOp | CompareOp;
export declare type Func = (param: any, context: any) => boolean;
export declare type FunctionsTable = {
[k: string]: Func;
export declare type Func<T> = (param: any, context: T) => boolean;
export declare type FunctionsTable<T> = {
[k: string]: Func<T>;
};
export declare const evaluate: (expression: Expression, context: any, functionsTable: FunctionsTable) => boolean;
export interface Context {
[index: string]: any;
}
export declare const evaluate: <T extends Context>(expression: Expression, context: T, functionsTable: FunctionsTable<T>) => boolean;
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function isNotOp(expression) {
return expression.not !== undefined;
}
const _isObject = (obj) => {
var type = typeof obj;
const type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
const _evaluateCompareOp = (op, param) => {
Expand Down
5 changes: 3 additions & 2 deletions example/lib/functionsFactory.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Func, FunctionsTable } from "../../dist";
import { ExpressionContext } from "./evaluator";
export interface FunctionDescription {
name: string;
evaluate: Func;
evaluate: Func<ExpressionContext>;
}
export declare type FuncFactory = () => FunctionDescription;
declare const functionsTable: FunctionsTable;
declare const functionsTable: FunctionsTable<ExpressionContext>;
export { functionsTable };
5 changes: 3 additions & 2 deletions example/lib/functionsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
import * as fs from 'fs';
import * as path from 'path';
import {Func, FunctionsTable} from "../../dist";
import {ExpressionContext} from "./evaluator";

export interface FunctionDescription {
name: string;
evaluate: Func;
evaluate: Func<ExpressionContext>;
}

export type FuncFactory = () => FunctionDescription;

const functionsDir = path.join(__dirname, '/functions');

const functionsTable: FunctionsTable = {};
const functionsTable: FunctionsTable<ExpressionContext> = {};

fs.readdirSync(functionsDir).forEach((file) => {
if (file.endsWith('.js')) {
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": "json-expression-eval",
"version": "1.1.0",
"version": "1.1.1",
"description": "evaluate a json described boolean expression using dynamic functions",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export interface NotOp {

export type Expression = FuncOp | AndOp | OrOp | NotOp | CompareOp;

export type Func = (param: any, context: any) => boolean;
export type Func<T> = (param: any, context: T) => boolean;

export type FunctionsTable = { [k: string]: Func };
export type FunctionsTable<T> = { [k: string]: Func<T> };

function isAndOp(expression: Expression): expression is AndOp {
return (<AndOp>expression).and !== undefined;
Expand All @@ -77,7 +77,7 @@ function isNotOp(expression: Expression): expression is NotOp {
}

const _isObject = (obj: any) => {
var type = typeof obj;
const type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};

Expand Down Expand Up @@ -108,7 +108,11 @@ const _evaluateCompareOp = (op: CompareOp, param: any) : boolean => {
throw new Error(`Invalid expression - unknown op ${key}`);
};

export const evaluate = (expression: Expression, context: any, functionsTable: FunctionsTable): boolean => {
export interface Context {
[index:string] : any;
}

export const evaluate = <T extends Context>(expression: Expression, context: T, functionsTable: FunctionsTable<T>): boolean => {
const _evaluate = (_expression: Expression): boolean => {
const keys = Object.keys(_expression);
if (keys.length !== 1) {
Expand Down

0 comments on commit 41364a7

Please sign in to comment.