-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
247 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { FuncFactory } from "../functionsFactory"; | ||
export declare const factory: FuncFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { FuncFactory } from "../functionsFactory"; | ||
export declare const factory: FuncFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters