From dbc9275f374a8b232aaef10c5410bd0e08a2b36e Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Wed, 25 Sep 2019 19:48:34 -0400 Subject: [PATCH] json-rules-engine (#38613) * add types for turndown * use auto-genned tsconfig and tslint * add dom types * re-generate module * fix tests and typescript version * feature/json-rules-engine: add initial types for json-rules-engine * feature/json-rules-engine: update test * also satisfy linter * feature/json-rules-engine: update types * chore: pre-commit hook fixes * chore: fix missing optional param * feature/json-rules-engine: remove generic type --- types/json-rules-engine/index.d.ts | 106 ++++++++++++++++++ .../json-rules-engine-tests.ts | 50 +++++++++ types/json-rules-engine/tsconfig.json | 23 ++++ types/json-rules-engine/tslint.json | 1 + 4 files changed, 180 insertions(+) create mode 100644 types/json-rules-engine/index.d.ts create mode 100644 types/json-rules-engine/json-rules-engine-tests.ts create mode 100644 types/json-rules-engine/tsconfig.json create mode 100644 types/json-rules-engine/tslint.json diff --git a/types/json-rules-engine/index.d.ts b/types/json-rules-engine/index.d.ts new file mode 100644 index 0000000000..21ecb66c6a --- /dev/null +++ b/types/json-rules-engine/index.d.ts @@ -0,0 +1,106 @@ +// Type definitions for json-rules-engine 4.0 +// Project: https://github.com/cachecontrol/json-rules-engine +// Definitions by: Scott Jones +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +export interface Event { + type: string; + params: { + message: string; + [key: string]: string; + }; +} + +export interface RuleEngine { + conditions: Conditions; + event: Event; + name?: any[]; + priority?: number; + onSuccess?: (evt: Event, almanac: Almanac) => void; +} + +export interface Conditions { + any: AnyRule[]; +} + +export interface AnyRule { + all: Rule[]; +} + +export interface EngineResult { + events: Event[]; + almanac: Almanac; +} + +export interface Almanac { + factMap: Map; + factResultCache: Map>; + allowUndefinedFacts: boolean; + factValue: (fact: object, params: object, path: string) => Promise; + addRuntimeFact: (factId: string, value: any) => void; +} + +export interface EngineOptions { + allowUndefinedFacts: boolean; +} + +export interface RuleOptions { + conditions: Conditions; + events: Event[]; + priority?: number; + name?: any[]; + onSuccess?: DefinitionFunction; + onFailure?: DefinitionFunction; +} + +export interface FactOptions { + cache: boolean; + priority: number; +} + +export interface DefinitionFunction { + (params: Event['params'], almanac: Almanac): void; +} + +export interface OperatorEvaluateFunction { + (factValue: string, jsonValue: JSON): void; +} + +export interface EngineEventFunction { + (event: Event, almanac: Almanac, ruleResult: object): void; +} + +export class Rule { + constructor(options: RuleOptions | JSON); + fact: string; + operator: + | 'equal' + | 'notEqual' + | 'lessThan' + | 'greaterThan' + | 'greaterThanInclusive' + | 'in' + | 'notIn' + | 'contains' + | 'doesNotContain'; + value: number | string | string[] | number[]; + path?: string; + setConditions?: (conditions: Conditions) => void; + setEvent?: (event: Event) => void; + setPriority?: (priority: number) => void; + toJSON?: (stringify?: boolean) => void; +} + +export class Engine { + constructor(rules?: Rule[], options?: EngineOptions); + addRule(rules: RuleEngine): void; + removeRule(rule: Rule): void; + addFact(id: string, definitionFunc: DefinitionFunction, options: FactOptions): void; + removeFact(id: string): void; + addOperator(name: string, definitionFunc: OperatorEvaluateFunction): void; + removeOperator(id: string): void; + stop(): Engine; + on(eventName: 'success' | 'failure', engineEvent: EngineEventFunction): void; + run(facts: object): Promise; +} diff --git a/types/json-rules-engine/json-rules-engine-tests.ts b/types/json-rules-engine/json-rules-engine-tests.ts new file mode 100644 index 0000000000..43827f1de9 --- /dev/null +++ b/types/json-rules-engine/json-rules-engine-tests.ts @@ -0,0 +1,50 @@ +// NOTE: used from: https://github.com/CacheControl/json-rules-engine#basic-example +import { Engine } from "json-rules-engine"; + +interface PlayerStats { + personalFoulCount: number; + gameDuration: number; +} + +const engine = new Engine(); + +engine.addRule({ + conditions: { + any: [{ + all: [{ + fact: 'gameDuration', + operator: 'equal', + value: 40 + }, { + fact: 'personalFoulCount', + operator: 'greaterThanInclusive', + value: 5 + }] + }, { + all: [{ + fact: 'gameDuration', + operator: 'equal', + value: 48 + }, { + fact: 'personalFoulCount', + operator: 'greaterThanInclusive', + value: 6 + }] + }] + }, + event: { + type: 'fouledOut', + params: { + message: 'Player has fouled out!' + } + } + }); + +const facts: PlayerStats = { + gameDuration: 6, + personalFoulCount: 40, +}; + +engine.run(facts).then(results => { + results.events.map(event => event.params.message); +}); diff --git a/types/json-rules-engine/tsconfig.json b/types/json-rules-engine/tsconfig.json new file mode 100644 index 0000000000..1aa7c02eb8 --- /dev/null +++ b/types/json-rules-engine/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "json-rules-engine-tests.ts" + ] +} diff --git a/types/json-rules-engine/tslint.json b/types/json-rules-engine/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/json-rules-engine/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }