Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Jun 9, 2018
1 parent c99513d commit ca69d87
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 7 deletions.
3 changes: 1 addition & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export interface NotOp {
not: Expression;
}
export declare type Expression = FuncOp | AndOp | OrOp | NotOp;
export declare type ExpressionContext = any;
export declare type Func = (param: any, context: ExpressionContext) => boolean;
export declare type Func = (param: any, context: any) => boolean;
export declare type FunctionsTable = {
[k: string]: Func;
};
Expand Down
1 change: 1 addition & 0 deletions example/example.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
37 changes: 37 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const evaluator_1 = require("./lib/evaluator");
const run = (expression, context) => {
const result = evaluator_1.evaluate(expression, context);
console.log(`Evaluating expression ${JSON.stringify(expression)} using context ${JSON.stringify(context)}`);
console.log(`Result: ${result}`);
};
let context = {
userId: 'a@b.com',
times: 3
};
let expression = {
user: 'a@b.com'
};
run(expression, context);
expression = {
and: [
{ user: 'a@b.com' },
{ maxCount: 5 },
]
};
run(expression, context);
expression = {
and: [
{ user: 'a@b.com' },
{ maxCount: 1 },
]
};
run(expression, context);
expression = {
or: [
{ user: 'a@b.com' },
{ maxCount: 6 },
]
};
run(expression, context);
51 changes: 51 additions & 0 deletions example/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use strict";

import {evaluate, ExpressionContext} from './lib/evaluator';
import {Expression} from "../dist";

const run = (expression: Expression, context: ExpressionContext) => {
const result = evaluate(expression, context);
console.log(`Evaluating expression ${JSON.stringify(expression)} using context ${JSON.stringify(context)}`);
console.log(`Result: ${result}`);
};

let context: ExpressionContext = {
userId: 'a@b.com',
times: 3
};

let expression: Expression = {
user: 'a@b.com'
};

run(expression, context);

expression = {
and: [
{ user: 'a@b.com'},
{ maxCount: 5},
]
};

run(expression, context);

expression = {
and: [
{ user: 'a@b.com'},
{ maxCount: 1},
]
};

run(expression, context);

expression = {
or: [
{ user: 'a@b.com'},
{ maxCount: 6},
]
};

run(expression, context);



6 changes: 6 additions & 0 deletions example/lib/evaluator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Expression } from "../../dist";
export interface ExpressionContext {
userId: string;
times: number;
}
export declare const evaluate: (expression: Expression, context: ExpressionContext) => boolean;
7 changes: 7 additions & 0 deletions example/lib/evaluator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const functionsFactory_1 = require("./functionsFactory");
const index_1 = require("./../../dist/index");
exports.evaluate = (expression, context) => {
return index_1.evaluate(expression, context, functionsFactory_1.functionsTable);
};
15 changes: 15 additions & 0 deletions example/lib/evaluator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

import {Expression} from "../../dist";

import { functionsTable } from './functionsFactory';
import { evaluate as _evaluate } from './../../dist/index';

export interface ExpressionContext {
userId: string;
times: number;
}

export const evaluate = (expression: Expression, context: ExpressionContext): boolean => {
return _evaluate(expression, context, functionsTable);
};
2 changes: 2 additions & 0 deletions example/lib/functions/counterFunc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { FuncFactory } from "../functionsFactory";
export declare const factory: FuncFactory;
11 changes: 11 additions & 0 deletions example/lib/functions/counterFunc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const desc = {
name: 'maxCount',
evaluate: (maxCount, context) => {
return context.times < maxCount;
},
};
exports.factory = () => {
return desc;
};
14 changes: 14 additions & 0 deletions example/lib/functions/counterFunc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

import {FuncFactory, FunctionDescription} from "../functionsFactory";

const desc = {
name: 'maxCount',
evaluate: (maxCount: number, context: { times: number }): boolean => {
return context.times < maxCount;
},
};

export const factory: FuncFactory = (): FunctionDescription => {
return desc;
};
2 changes: 2 additions & 0 deletions example/lib/functions/userFunc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { FuncFactory } from "../functionsFactory";
export declare const factory: FuncFactory;
11 changes: 11 additions & 0 deletions example/lib/functions/userFunc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const desc = {
name: 'user',
evaluate: (user, context) => {
return context.userId === user;
},
};
exports.factory = () => {
return desc;
};
14 changes: 14 additions & 0 deletions example/lib/functions/userFunc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

import {FuncFactory, FunctionDescription} from "../functionsFactory";

const desc = {
name: 'user',
evaluate: (user: string, context: { userId: string }): boolean => {
return context.userId === user;
},
};

export const factory: FuncFactory = (): FunctionDescription => {
return desc;
};
8 changes: 8 additions & 0 deletions example/lib/functionsFactory.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Func, FunctionsTable } from "../../dist";
export interface FunctionDescription {
name: string;
evaluate: Func;
}
export declare type FuncFactory = () => FunctionDescription;
declare const functionsTable: FunctionsTable;
export { functionsTable };
17 changes: 17 additions & 0 deletions example/lib/functionsFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const path = require("path");
const functionsDir = path.join(__dirname, '/functions');
const functionsTable = {};
exports.functionsTable = functionsTable;
fs.readdirSync(functionsDir).forEach((file) => {
if (file.endsWith('.js')) {
const data = require(path.join(functionsDir, file)).factory;
const description = data();
if (functionsTable[description.name]) {
throw new Error(`Function with name ${description.name} already exists`);
}
functionsTable[description.name] = description.evaluate;
}
});
29 changes: 29 additions & 0 deletions example/lib/functionsFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

import * as fs from 'fs';
import * as path from 'path';
import {Func, FunctionsTable} from "../../dist";

export interface FunctionDescription {
name: string;
evaluate: Func;
}

export type FuncFactory = () => FunctionDescription;

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

const functionsTable: FunctionsTable = {};

fs.readdirSync(functionsDir).forEach((file) => {
if (file.endsWith('.js')) {
const data = require(path.join(functionsDir, file)).factory as FuncFactory;
const description = data();
if (functionsTable[description.name]) {
throw new Error(`Function with name ${description.name} already exists`);
}
functionsTable[description.name] = description.evaluate;
}
});

export { functionsTable };
13 changes: 13 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es2016",
"module": "none",
"moduleResolution": "node",
"declaration": true,
"strict": true,
"lib": [
"es6",
"dom"
]
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"devDependencies": {
"@types/chai": "^4.1.3",
"@types/mocha": "^5.2.1",
"@types/node": "^10.3.2",
"@types/underscore": "^1.8.8",
"chai": "^4.1.2",
"coveralls": "^3.0.1",
Expand Down
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export interface NotOp {

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

export type ExpressionContext = any;

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

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

Expand All @@ -38,7 +36,7 @@ function isNotOp(expression: Expression): expression is NotOp {
return (<NotOp>expression).not !== undefined;
}

export const evaluate = (expression: Expression, context: ExpressionContext, functionsTable: FunctionsTable): boolean => {
export const evaluate = (expression: Expression, context: any, functionsTable: FunctionsTable): boolean => {
const _evaluate = (_expression: Expression): boolean => {
const keys = Object.keys(_expression);
if (keys.length !== 1) {
Expand Down
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"moduleResolution": "node",
"declaration": true,
"outDir": "./dist",
"strict": true
"strict": true,
"lib": [
"es6",
"dom"
]
},
"include": [
"src/**/*.ts"
Expand Down

0 comments on commit ca69d87

Please sign in to comment.