mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
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
This commit is contained in:
parent
008bc5cc70
commit
dbc9275f37
106
types/json-rules-engine/index.d.ts
vendored
Normal file
106
types/json-rules-engine/index.d.ts
vendored
Normal file
@ -0,0 +1,106 @@
|
||||
// Type definitions for json-rules-engine 4.0
|
||||
// Project: https://github.com/cachecontrol/json-rules-engine
|
||||
// Definitions by: Scott Jones <https://github.com/scottdj92>
|
||||
// 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<string | 'success-events', Event>;
|
||||
factResultCache: Map<number, Promise<Event>>;
|
||||
allowUndefinedFacts: boolean;
|
||||
factValue: (fact: object, params: object, path: string) => Promise<void>;
|
||||
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<EngineResult>;
|
||||
}
|
||||
50
types/json-rules-engine/json-rules-engine-tests.ts
Normal file
50
types/json-rules-engine/json-rules-engine-tests.ts
Normal file
@ -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);
|
||||
});
|
||||
23
types/json-rules-engine/tsconfig.json
Normal file
23
types/json-rules-engine/tsconfig.json
Normal file
@ -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"
|
||||
]
|
||||
}
|
||||
1
types/json-rules-engine/tslint.json
Normal file
1
types/json-rules-engine/tslint.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Loading…
Reference in New Issue
Block a user