mirror of
https://github.com/lingble/Trool.git
synced 2025-10-30 20:17:57 +00:00
merged pull request
This commit is contained in:
@@ -210,3 +210,28 @@ with multiple values.
|
||||
|
||||
- Import property name rules are the same as for JavaScript keys. That means alphanumeric, underscores,
|
||||
and dashes. Anything other than characters will throw an error.
|
||||
|
||||
- In case you don't have local access to your rules file, or can't otherwise pass a file path, you can use
|
||||
`initFromString()` and pass the content of the CSV file as a string instead:
|
||||
```typescript
|
||||
const factsHolder = {
|
||||
Visitors: [new Visitor(), new Visitor()],
|
||||
Tickets: new Ticket(),
|
||||
};
|
||||
|
||||
const importsHolder = {
|
||||
VisitorTypes: {
|
||||
ADULT: 'Adult',
|
||||
CHILD: 'Child',
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
const s3 = new AWS.S3();
|
||||
const data = await s3.getObject({ Bucket: bucketName, Key: bucketKey });
|
||||
|
||||
const facts = this.setupFactsHolder(visitors, ticketOpt);
|
||||
const trool = new Trool();
|
||||
await trool.initFromString(data.Body, factsHolder, true, importsHolder);
|
||||
|
||||
```
|
||||
|
||||
@@ -10,6 +10,7 @@ import DecisionTable from './DecisionTable';
|
||||
import TableErrs from './TableErrs';
|
||||
|
||||
|
||||
|
||||
class Trool {
|
||||
|
||||
private readonly NO_TABLES_WARN = 'No decision tables found';
|
||||
@@ -24,7 +25,7 @@ class Trool {
|
||||
|
||||
|
||||
/**
|
||||
* Create the decision tables.
|
||||
* Initialize the engine.
|
||||
*
|
||||
* @param filePath
|
||||
* @param facts
|
||||
@@ -37,14 +38,47 @@ class Trool {
|
||||
showLogs?: boolean,
|
||||
imports?: IImportsHolder,
|
||||
) {
|
||||
try {
|
||||
this.logger = new Logger(showLogs);
|
||||
const jsonArr = await csvToJson().fromFile(filePath);
|
||||
this.createDecisionTables(jsonArr, facts, showLogs, imports);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initialize the engine with the CSV file content passed as string.
|
||||
*
|
||||
* @param fileContent
|
||||
* @param facts
|
||||
* @param imports
|
||||
* @param showLogs
|
||||
*/
|
||||
public async initFromString(
|
||||
fileContent: string,
|
||||
facts: IFactsHolder,
|
||||
showLogs?: boolean,
|
||||
imports?: IImportsHolder,
|
||||
) {
|
||||
const jsonArr = await csvToJson().fromString(fileContent);
|
||||
this.createDecisionTables(jsonArr, facts, showLogs, imports);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create the decision tables.
|
||||
*
|
||||
* @param filePath
|
||||
* @param facts
|
||||
* @param imports
|
||||
* @param showLogs
|
||||
*/
|
||||
private createDecisionTables(
|
||||
jsonArr: IRow[],
|
||||
facts: IFactsHolder,
|
||||
showLogs?: boolean,
|
||||
imports?: IImportsHolder,
|
||||
) {
|
||||
this.logger = new Logger(showLogs);
|
||||
const allImports = this.setupImports(jsonArr, imports || {});
|
||||
this._decisionTables = this.getTables(jsonArr, facts, allImports);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "trool",
|
||||
"version": "1.1.0",
|
||||
"version": "1.1.1",
|
||||
"description": "a rule engine for NodeJS written in TypeScript",
|
||||
"main": "./lib/index.js",
|
||||
"typings": "./lib/index.d.ts",
|
||||
|
||||
Reference in New Issue
Block a user