16 Commits

Author SHA1 Message Date
875c06f86c 2.0.0 2022-08-07 11:18:02 +02:00
0a8e4a3517 BREAKING CHANGE(core): switch to esm 2022-08-07 11:18:02 +02:00
8b9dec976c 1.0.10 2020-01-24 07:11:26 +00:00
1493848dc8 fix(core): update 2020-01-24 07:11:25 +00:00
17287a9ba3 1.0.9 2020-01-23 18:09:10 +00:00
11921a2864 fix(core): update 2020-01-23 18:09:09 +00:00
9035fafdc2 1.0.8 2020-01-23 16:52:22 +00:00
773ae00517 fix(core): update 2020-01-23 16:52:22 +00:00
e67fbfebf6 1.0.7 2020-01-23 15:24:47 +00:00
b2478d79f2 fix(core): update 2020-01-23 15:24:46 +00:00
917e630554 1.0.6 2020-01-23 15:12:55 +00:00
b3b02fee70 fix(core): update 2020-01-23 15:12:55 +00:00
8c257bc0fd 1.0.5 2020-01-23 15:11:30 +00:00
a4ff5c26e2 fix(core): update 2020-01-23 15:11:29 +00:00
7d08f9bdf6 1.0.4 2020-01-20 14:41:27 +00:00
5fabe776f5 fix(core): update 2020-01-20 14:41:27 +00:00
19 changed files with 12951 additions and 904 deletions

View File

@@ -11,7 +11,13 @@
},
"gitzone": {
"type": "object",
"description": "settings for gitzone"
"description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm"]
}
}
}
}
}

1
dist_ts/index.d.ts vendored Normal file
View File

@@ -0,0 +1 @@
export * from './smartrule.classes.smartrule.js';

2
dist_ts/index.js Normal file
View File

@@ -0,0 +1,2 @@
export * from './smartrule.classes.smartrule.js';
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLGtDQUFrQyxDQUFDIn0=

11
dist_ts/smartrule.classes.rule.d.ts vendored Normal file
View File

@@ -0,0 +1,11 @@
import { SmartRule } from './smartrule.classes.smartrule.js';
export declare type TTreeActionResult = 'continue' | 'apply-continue' | 'apply-stop' | 'stop';
export declare type TActionFunc<T = any> = (objectArg: T) => Promise<any>;
export declare type TCheckFunc<T> = (objectArg: T) => Promise<TTreeActionResult>;
export declare class Rule<T> {
smartRuleRef: SmartRule<T>;
priority: number;
checkFunction: TCheckFunc<T>;
actionFunction: TActionFunc;
constructor(smartRuleRef: SmartRule<T>, priorityArg: number, checkFunctionArg: TCheckFunc<T>, actionFunctionArg: TActionFunc);
}

View File

@@ -0,0 +1,11 @@
import './smartrule.plugins.js';
import './smartrule.classes.smartrule.js';
export class Rule {
constructor(smartRuleRef, priorityArg, checkFunctionArg, actionFunctionArg) {
this.smartRuleRef = smartRuleRef;
this.priority = priorityArg;
this.checkFunction = checkFunctionArg;
this.actionFunction = actionFunctionArg;
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRydWxlLmNsYXNzZXMucnVsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0cnVsZS5jbGFzc2VzLnJ1bGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBeUIsd0JBQXdCLENBQUM7QUFDbEQsT0FBMEIsa0NBQWtDLENBQUM7QUFPN0QsTUFBTSxPQUFPLElBQUk7SUFNZixZQUNFLFlBQTBCLEVBQzFCLFdBQW1CLEVBQ25CLGdCQUErQixFQUMvQixpQkFBOEI7UUFFOUIsSUFBSSxDQUFDLFlBQVksR0FBRyxZQUFZLENBQUM7UUFDakMsSUFBSSxDQUFDLFFBQVEsR0FBRyxXQUFXLENBQUM7UUFDNUIsSUFBSSxDQUFDLGFBQWEsR0FBRyxnQkFBZ0IsQ0FBQztRQUN0QyxJQUFJLENBQUMsY0FBYyxHQUFHLGlCQUFpQixDQUFDO0lBQzFDLENBQUM7Q0FDRiJ9

View File

@@ -0,0 +1,10 @@
import { Rule, TCheckFunc, TActionFunc } from './smartrule.classes.rule.js';
export declare class SmartRule<T> {
rules: Array<Rule<T>>;
/**
* makes a decision based on the given obect and the given rules
* @param objectArg
*/
makeDecision(objectArg: T): Promise<void>;
createRule(priorityArg: number, checkFunctionArg: TCheckFunc<T>, actionFunctionArg: TActionFunc): void;
}

View File

@@ -0,0 +1,69 @@
import './smartrule.plugins.js';
import { Rule } from './smartrule.classes.rule.js';
export class SmartRule {
constructor() {
this.rules = [];
}
/**
* makes a decision based on the given obect and the given rules
* @param objectArg
*/
async makeDecision(objectArg) {
// lets sort the rules
this.rules = this.rules.sort((a, b) => {
if (a.priority > b.priority) {
return 1;
}
else {
return 0;
}
});
// gets the next batch with the same priority
const getNextParallelBatch = (priorityStart) => {
return this.rules.filter(rule => {
return rule.priority === priorityStart;
});
};
// lets run the checks
const runNextBatch = async (startPriority, runRulesAmount) => {
const nextBatch = getNextParallelBatch(startPriority);
runRulesAmount = runRulesAmount + nextBatch.length;
const outcomes = [];
for (const rule of nextBatch) {
const checkResult = await rule.checkFunction(objectArg);
checkResult
? null
: console.log('WARNING!!! Please make sure your rule always returns a statement of how to continue!');
if (checkResult.startsWith('apply')) {
await rule.actionFunction(objectArg); // here the action function is run
}
outcomes.push(checkResult);
}
if (outcomes.length > 0) {
const finalOutcomeOfBatch = outcomes.reduce((previous, current, index, array) => {
if (current.includes('continue') || previous.includes('continue')) {
return 'continue';
}
else {
return 'stop';
}
});
if (finalOutcomeOfBatch === 'stop') {
return;
}
}
if (runRulesAmount < this.rules.length) {
await runNextBatch(startPriority + 1, runRulesAmount);
}
else {
return;
}
};
await runNextBatch(0, 0);
}
createRule(priorityArg, checkFunctionArg, actionFunctionArg) {
const rule = new Rule(this, priorityArg, checkFunctionArg, actionFunctionArg);
this.rules.push(rule);
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRydWxlLmNsYXNzZXMuc21hcnRydWxlLmpzIiwic291cmNlUm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PO2lCQUNSO2FBQ0Y7WUFFRCxJQUFJLGNBQWMsR0FBRyxJQUFJLENBQUMsS0FBSyxDQUFDLE1BQU0sRUFBRTtnQkFDdEMsTUFBTSxZQUFZLENBQUMsYUFBYSxHQUFHLENBQUMsRUFBRSxjQUFjLENBQUMsQ0FBQzthQUN2RDtpQkFBTTtnQkFDTCxPQUFPO2FBQ1I7UUFDSCxDQUFDLENBQUM7UUFFRixNQUFNLFlBQVksQ0FBQyxDQUFDLEVBQUUsQ0FBQyxDQUFDLENBQUM7SUFDM0IsQ0FBQztJQUVNLFVBQVUsQ0FDZixXQUFtQixFQUNuQixnQkFBK0IsRUFDL0IsaUJBQThCO1FBRTlCLE1BQU0sSUFBSSxHQUFHLElBQUksSUFBSSxDQUFJLElBQUksRUFBRSxXQUFXLEVBQUUsZ0JBQWdCLEVBQUUsaUJBQWlCLENBQUMsQ0FBQztRQUNqRixJQUFJLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQztJQUN4QixDQUFDO0NBQ0YifQ==

2
dist_ts/smartrule.plugins.d.ts vendored Normal file
View File

@@ -0,0 +1,2 @@
declare const removeme: {};
export { removeme };

View File

@@ -0,0 +1,3 @@
const removeme = {};
export { removeme };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRydWxlLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHJ1bGUucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxNQUFNLFFBQVEsR0FBRyxFQUFFLENBQUM7QUFDcEIsT0FBTyxFQUFFLFFBQVEsRUFBRSxDQUFDIn0=

View File

@@ -1,5 +1,6 @@
{
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",

13547
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,26 +1,24 @@
{
"name": "@pushrocks/smartrule",
"version": "1.0.3",
"version": "2.0.0",
"private": false,
"description": "a smart rule library for handling decision trees.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"type": "module",
"author": "Lossless GmbH",
"license": "MIT",
"scripts": {
"test": "(tstest test/)",
"build": "(tsbuild)",
"build": "(tsbuild --web)",
"format": "(gitzone format)"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.0.22",
"@gitzone/tstest": "^1.0.15",
"@pushrocks/tapbundle": "^3.0.7",
"@types/node": "^10.11.7",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.0"
"@gitzone/tsbuild": "^2.1.65",
"@gitzone/tstest": "^1.0.73",
"@pushrocks/tapbundle": "^5.0.4",
"@types/node": "^18.6.4"
},
"dependencies": {},
"files": [
"ts/**/*",
"ts_web/**/*",

View File

@@ -1,8 +1,33 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartrule from '../ts/index';
import * as smartrule from '../ts/index.js';
interface ITestMessage {
id: string;
body: string;
}
let testSmartruleInstance: smartrule.SmartRule<ITestMessage>;
tap.test('first test', async () => {
console.log(smartrule.standardExport);
testSmartruleInstance = new smartrule.SmartRule<ITestMessage>();
testSmartruleInstance.createRule(
2,
async messageArg => {
if (messageArg.body.startsWith('hello')) {
return 'apply-stop';
}
},
async messageArg => {
console.log(`rule triggered for message with body ${messageArg.body}`);
}
);
});
tap.test('make a decision based on an object', async () => {
testSmartruleInstance.makeDecision({
id: '123456',
body: 'hello, there. This is a cool message!'
});
});
tap.start();

8
ts/00_commitinfo_data.ts Normal file
View File

@@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@pushrocks/smartrule',
version: '2.0.0',
description: 'a smart rule library for handling decision trees.'
}

View File

@@ -1,3 +1 @@
import * as plugins from './smartrule.plugins';
export let standardExport = 'Hi there! :) This is an exported string';
export * from './smartrule.classes.smartrule.js';

View File

@@ -0,0 +1,26 @@
import * as plugins from './smartrule.plugins.js';
import { SmartRule } from './smartrule.classes.smartrule.js';
export type TTreeActionResult = 'continue' | 'apply-continue' | 'apply-stop' | 'stop';
export type TActionFunc<T = any> = (objectArg: T) => Promise<any>;
export type TCheckFunc<T> = (objectArg: T) => Promise<TTreeActionResult>;
export class Rule<T> {
public smartRuleRef: SmartRule<T>;
public priority: number;
public checkFunction: TCheckFunc<T>;
public actionFunction: TActionFunc;
constructor(
smartRuleRef: SmartRule<T>,
priorityArg: number,
checkFunctionArg: TCheckFunc<T>,
actionFunctionArg: TActionFunc
) {
this.smartRuleRef = smartRuleRef;
this.priority = priorityArg;
this.checkFunction = checkFunctionArg;
this.actionFunction = actionFunctionArg;
}
}

View File

@@ -0,0 +1,80 @@
import * as plugins from './smartrule.plugins.js';
import { Rule, TCheckFunc, TActionFunc, TTreeActionResult } from './smartrule.classes.rule.js';
export class SmartRule<T> {
public rules: Array<Rule<T>> = [];
/**
* makes a decision based on the given obect and the given rules
* @param objectArg
*/
public async makeDecision(objectArg: T) {
// lets sort the rules
this.rules = this.rules.sort((a, b) => {
if (a.priority > b.priority) {
return 1;
} else {
return 0;
}
});
// gets the next batch with the same priority
const getNextParallelBatch = (priorityStart: number) => {
return this.rules.filter(rule => {
return rule.priority === priorityStart;
});
};
// lets run the checks
const runNextBatch = async (startPriority: number, runRulesAmount: number): Promise<void> => {
const nextBatch = getNextParallelBatch(startPriority);
runRulesAmount = runRulesAmount + nextBatch.length;
const outcomes: TTreeActionResult[] = [];
for (const rule of nextBatch) {
const checkResult = await rule.checkFunction(objectArg);
checkResult
? null
: console.log(
'WARNING!!! Please make sure your rule always returns a statement of how to continue!'
);
if (checkResult.startsWith('apply')) {
await rule.actionFunction(objectArg); // here the action function is run
}
outcomes.push(checkResult);
}
if (outcomes.length > 0) {
const finalOutcomeOfBatch: TTreeActionResult = outcomes.reduce(
(previous, current, index, array) => {
if (current.includes('continue') || previous.includes('continue')) {
return 'continue';
} else {
return 'stop';
}
}
);
if (finalOutcomeOfBatch === 'stop') {
return;
}
}
if (runRulesAmount < this.rules.length) {
await runNextBatch(startPriority + 1, runRulesAmount);
} else {
return;
}
};
await runNextBatch(0, 0);
}
public createRule(
priorityArg: number,
checkFunctionArg: TCheckFunc<T>,
actionFunctionArg: TActionFunc
) {
const rule = new Rule<T>(this, priorityArg, checkFunctionArg, actionFunctionArg);
this.rules.push(rule);
}
}

10
tsconfig.json Normal file
View File

@@ -0,0 +1,10 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "nodenext",
"esModuleInterop": true
}
}

View File

@@ -1,17 +0,0 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}