decision_table
Advanced tools
Comparing version 0.1.5 to 0.1.6
Object.defineProperty(exports, "__esModule", { value: true }); | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn('[decision_table] You are using non-production version of library. ' + | ||
'It\'s provides some additional checks which mades library some slower.'); | ||
} | ||
/** Action testing sandbox */ | ||
var ActionTest = (function () { | ||
function ActionTest() { | ||
} | ||
/** Run testing */ | ||
ActionTest.run = function (action) { | ||
var result = { | ||
passed: true, | ||
reasons: [] | ||
}; | ||
if (ActionTest.isNotNil_(action.whenTrue) && !ActionTest.isArray_(action.whenTrue)) { | ||
result.passed = false; | ||
result.reasons.push('Action field "whenTrue" is not array of strings.'); | ||
} | ||
if (ActionTest.isNotNil_(action.whenFalse) && !ActionTest.isArray_(action.whenFalse)) { | ||
result.passed = false; | ||
result.reasons.push('Action field "whenFalse" is not array of strings.'); | ||
} | ||
if (ActionTest.isNotNil_(action.whenTrue) && !ActionTest.isArrayOfStrings_(action.whenTrue)) { | ||
result.passed = false; | ||
result.reasons.push('Action field "whenTrue" is not array of strings.'); | ||
} | ||
if (ActionTest.isNotNil_(action.whenFalse) && !ActionTest.isArrayOfStrings_(action.whenFalse)) { | ||
result.passed = false; | ||
result.reasons.push('Action field "whenFalse" is not array of strings.'); | ||
} | ||
if (!ActionTest.isFunction_(action.execute)) { | ||
result.passed = false; | ||
result.reasons.push('Action field "execute" is required and must be a function.'); | ||
} | ||
return result; | ||
}; | ||
/** Returns "true" if entity is not "null" or "undefined" */ | ||
ActionTest.isNotNil_ = function (value) { | ||
return (value !== null) && (value !== undefined); | ||
}; | ||
/** Returns "true" if entity is array */ | ||
ActionTest.isArray_ = function (value) { | ||
return (Object.prototype.toString.call(value) === '[object Array]'); | ||
}; | ||
/** Returns "true" if entity is a function */ | ||
ActionTest.isFunction_ = function (value) { | ||
return (value instanceof Function); | ||
}; | ||
/** Returns "true" if entity is array of strings */ | ||
ActionTest.isArrayOfStrings_ = function (value) { | ||
var result = ActionTest.isNotNil_(value) && ActionTest.isArray_(value); | ||
if (result) { | ||
for (var i = 0; i < value.length; i++) { | ||
if (typeof value[i] !== 'string') { | ||
result = false; | ||
break; | ||
} | ||
} | ||
} | ||
return result; | ||
}; | ||
return ActionTest; | ||
}()); | ||
/** Condition testing sandbox */ | ||
var ConditionTest = (function () { | ||
function ConditionTest() { | ||
} | ||
/** Run testing */ | ||
ConditionTest.run = function (name, func) { | ||
var result = { | ||
passed: true, | ||
reasons: [] | ||
}; | ||
if (typeof name !== 'string') { | ||
result.passed = false; | ||
result.reasons.push('Condition name must be a string'); | ||
} | ||
if (!(func instanceof Function)) { | ||
result.passed = false; | ||
result.reasons.push('Condition must be a function'); | ||
} | ||
return result; | ||
}; | ||
return ConditionTest; | ||
}()); | ||
/** Common class of decision table */ | ||
@@ -24,4 +109,34 @@ var DecisionTable = (function () { | ||
}; | ||
/** | ||
* Special static method which provides simple testing | ||
* for your actions. In development environment this method | ||
* will be run for each adding action. | ||
*/ | ||
DecisionTable.testAction = function (action) { | ||
var result = ActionTest.run(action); | ||
if (!result.passed) { | ||
console.error("[decision_table] Action test failed:\n\t" + result.reasons.join('\t\n')); | ||
} | ||
return result; | ||
}; | ||
/** | ||
* Special static method which provides simple testing | ||
* for your conditions. In development environment this method | ||
* will be run for each adding condition. | ||
*/ | ||
DecisionTable.testCondition = function (name, condition) { | ||
var result = ConditionTest.run(name, condition); | ||
if (!result.passed) { | ||
console.error("[decision_table] Condition test failed:\n\t" + result.reasons.join('\t\n')); | ||
} | ||
return result; | ||
}; | ||
/** Add new condition to table (will be override if exists) */ | ||
DecisionTable.prototype.addCondition = function (name, func) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
var result = DecisionTable.testCondition(name, func); | ||
if (!result.passed) { | ||
throw new Error(result.reasons.join('\t\n')); | ||
} | ||
} | ||
this.conditions_[name] = func; | ||
@@ -38,3 +153,8 @@ }; | ||
DecisionTable.prototype.addAction = function (action) { | ||
this.lintAction_(action); | ||
if (process.env.NODE_ENV !== 'production') { | ||
var result = DecisionTable.testAction(action); | ||
if (!result.passed) { | ||
throw new Error(result.reasons.join('\t\n')); | ||
} | ||
} | ||
this.actions_.push(action); | ||
@@ -58,14 +178,2 @@ }; | ||
}; | ||
DecisionTable.prototype.lintAction_ = function (action) { | ||
var isNotNilAndNotArray = function (item) { return ((item !== null && item !== undefined && !Array.isArray(item))); }; | ||
if (isNotNilAndNotArray(action.whenTrue)) { | ||
throw new Error('Action field "whenTrue" must be array of strings!'); | ||
} | ||
if (isNotNilAndNotArray(action.whenFalse)) { | ||
throw new Error('Action field "whenFalse" must be array of strings!'); | ||
} | ||
if (!(action.execute instanceof Function)) { | ||
throw new Error('Action field "execute" is required and must be a function!'); | ||
} | ||
}; | ||
/** Action execition function */ | ||
@@ -72,0 +180,0 @@ DecisionTable.prototype.doAction_ = function (action, conditionsResults) { |
{ | ||
"name": "decision_table", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"main": "dist/DecisionTable.js", | ||
"license": "BSD-3-Clause", | ||
"repository": { | ||
"type" : "git", | ||
"url" : "https://github.com/AlexanderSychev/decision-table" | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/AlexanderSychev/decision-table" | ||
}, | ||
"scripts": { | ||
"dev:prepare": "npm i typescript@2.4.2 webpack@2.2.1", | ||
"dev:build": "tsc && webpack", | ||
"dev:clean": "rm -rf lib/* && rm -rf dist/*", | ||
"dev:test": "node tests/node-test.js" | ||
"prepare:dev": "npm i typescript@2.4.2 webpack@2.2.1 webpack-merge@4.1.0 @types/node@8.0.53", | ||
"compile:dev": "tsc", | ||
"bundle:dev": "NODE_ENV=development webpack", | ||
"bundle:prod": "NODE_ENV=production webpack", | ||
"build:dev": "npm run compile:dev && npm run bundle:dev", | ||
"build:prod": "npm run compile:dev && npm run bundle:dev", | ||
"clean:dev": "rm -rf lib/* && rm -rf dist/*", | ||
"test:dev": "NODE_ENV=development node tests/node-test.js", | ||
"test:prod": "NODE_ENV=production node tests/node-test.js", | ||
"prepublish": "npm run clean:dev && npm run compile:dev && npm run bundle:dev && npm run bundle:prod && npm run test:prod" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# decision-table | ||
# decision_table | ||
@@ -11,5 +11,5 @@ Simple implementation of Decision Tables (https://en.wikipedia.org/wiki/Decision_table). | ||
# By NPM | ||
npm i decision-table --save | ||
npm i decision_table --save | ||
# By Yarn | ||
yarn add decision-table --save | ||
yarn add decision_table --save | ||
``` | ||
@@ -20,3 +20,6 @@ | ||
```html | ||
<script src="path/to/decision-table.js"></script> | ||
<!-- Development environment version --> | ||
<script src="path/to/decision-table.dev.js"></script> | ||
<!-- Production environment version --> | ||
<script src="path/to/decision-table.prod.js"></script> | ||
``` | ||
@@ -42,2 +45,12 @@ | ||
### Environment | ||
Library uses standard Node.JS environment variable (`NODE_ENV`). | ||
In development mode (`NODE_ENV !== 'production'`) library runs some | ||
tests for each adding action and condition. | ||
Browser versions of library has defined environment: | ||
* `lib/decision-table.dev.js` - development environment (all test running always, not compressed); | ||
* `lib/decision-table.prod.js` - development environment (all test running always, compressed); | ||
### API | ||
@@ -67,2 +80,14 @@ | ||
#### DecisionTable.testAction(action) | ||
Special static method which provides simple testing | ||
for your actions. In "development" environment this method | ||
will be run for each adding action. | ||
#### DecisionTable.testCondition(name, func) | ||
Special static method which provides simple testing | ||
for your conditions. In development environment this method | ||
will be run for each adding condition. | ||
#### DecisionTable.prototype.addCondition(name, func) | ||
@@ -250,2 +275,7 @@ | ||
table.run(); | ||
``` | ||
``` | ||
## Roadmap | ||
* Version *0.5.0* - optional conditions ("OR" logic); | ||
* Version *0.9.0* - asynchronous conditions; |
@@ -27,2 +27,97 @@ /** | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn( | ||
'[decision_table] You are using non-production version of library. ' + | ||
'It\'s provides some additional checks which mades library some slower.' | ||
); | ||
} | ||
/** Action testing result */ | ||
interface TestResult { | ||
/** Passed tests action flag */ | ||
passed: boolean; | ||
/** Reason of failed test */ | ||
reasons: string[]; | ||
} | ||
/** Action testing sandbox */ | ||
class ActionTest { | ||
/** Run testing */ | ||
public static run(action: Action): TestResult { | ||
let result: TestResult = { | ||
passed: true, | ||
reasons: [] | ||
}; | ||
if (ActionTest.isNotNil_(action.whenTrue) && !ActionTest.isArray_(action.whenTrue)) { | ||
result.passed = false; | ||
result.reasons.push('Action field "whenTrue" is not array of strings.'); | ||
} | ||
if (ActionTest.isNotNil_(action.whenFalse) && !ActionTest.isArray_(action.whenFalse)) { | ||
result.passed = false; | ||
result.reasons.push('Action field "whenFalse" is not array of strings.'); | ||
} | ||
if (ActionTest.isNotNil_(action.whenTrue) && !ActionTest.isArrayOfStrings_(action.whenTrue)) { | ||
result.passed = false; | ||
result.reasons.push('Action field "whenTrue" is not array of strings.'); | ||
} | ||
if (ActionTest.isNotNil_(action.whenFalse) && !ActionTest.isArrayOfStrings_(action.whenFalse)) { | ||
result.passed = false; | ||
result.reasons.push('Action field "whenFalse" is not array of strings.'); | ||
} | ||
if (!ActionTest.isFunction_(action.execute)) { | ||
result.passed = false; | ||
result.reasons.push('Action field "execute" is required and must be a function.'); | ||
} | ||
return result; | ||
} | ||
/** Returns "true" if entity is not "null" or "undefined" */ | ||
private static isNotNil_(value: any): value is undefined { | ||
return (value !== null) && (value !== undefined); | ||
} | ||
/** Returns "true" if entity is array */ | ||
private static isArray_(value: any): value is any[] { | ||
return (Object.prototype.toString.call(value) === '[object Array]') | ||
} | ||
/** Returns "true" if entity is a function */ | ||
private static isFunction_(value: any): value is Function { | ||
return (value instanceof Function); | ||
} | ||
/** Returns "true" if entity is array of strings */ | ||
private static isArrayOfStrings_(value: any): value is string[] { | ||
let result: boolean = ActionTest.isNotNil_(value) && ActionTest.isArray_(value); | ||
if (result) { | ||
for (let i = 0; i < value.length; i++) { | ||
if (typeof value[i] !== 'string') { | ||
result = false; | ||
break; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
/** Condition testing sandbox */ | ||
class ConditionTest { | ||
/** Run testing */ | ||
public static run(name: string, func: Condition): TestResult { | ||
let result: TestResult = { | ||
passed: true, | ||
reasons: [] | ||
}; | ||
if(typeof name !== 'string') { | ||
result.passed = false; | ||
result.reasons.push('Condition name must be a string'); | ||
} | ||
if(!(func instanceof Function)) { | ||
result.passed = false; | ||
result.reasons.push('Condition must be a function') | ||
} | ||
return result; | ||
} | ||
} | ||
/** Common class of decision table */ | ||
@@ -65,4 +160,34 @@ export class DecisionTable { | ||
} | ||
/** | ||
* Special static method which provides simple testing | ||
* for your actions. In development environment this method | ||
* will be run for each adding action. | ||
*/ | ||
public static testAction(action: Action): TestResult { | ||
const result: TestResult = ActionTest.run(action); | ||
if (!result.passed) { | ||
console.error(`[decision_table] Action test failed:\n\t${result.reasons.join('\t\n')}`); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Special static method which provides simple testing | ||
* for your conditions. In development environment this method | ||
* will be run for each adding condition. | ||
*/ | ||
public static testCondition(name: string, condition: Condition): TestResult { | ||
const result: TestResult = ConditionTest.run(name, condition); | ||
if (!result.passed) { | ||
console.error(`[decision_table] Condition test failed:\n\t${result.reasons.join('\t\n')}`); | ||
} | ||
return result; | ||
} | ||
/** Add new condition to table (will be override if exists) */ | ||
public addCondition(name: string, func: Condition): void { | ||
if (process.env.NODE_ENV !== 'production') { | ||
const result: TestResult = DecisionTable.testCondition(name, func); | ||
if (!result.passed) { | ||
throw new Error(result.reasons.join('\t\n')); | ||
} | ||
} | ||
this.conditions_[name] = func; | ||
@@ -79,3 +204,8 @@ } | ||
public addAction(action: Action): void { | ||
this.lintAction_(action); | ||
if (process.env.NODE_ENV !== 'production') { | ||
const result: TestResult = DecisionTable.testAction(action); | ||
if (!result.passed) { | ||
throw new Error(result.reasons.join('\t\n')); | ||
} | ||
} | ||
this.actions_.push(action); | ||
@@ -98,17 +228,2 @@ } | ||
} | ||
/** Analise and check action for correct signature */ | ||
private lintAction_(action: Action): void { | ||
const isNotNilAndNotArray = (item: any) => ( | ||
(item !== null && item !== undefined && !Array.isArray(item)) | ||
); | ||
if (isNotNilAndNotArray(action.whenTrue)) { | ||
throw new Error('Action field "whenTrue" must be array of strings!'); | ||
} | ||
if (isNotNilAndNotArray(action.whenFalse)) { | ||
throw new Error('Action field "whenFalse" must be array of strings!'); | ||
} | ||
if (!(action.execute instanceof Function)) { | ||
throw new Error('Action field "execute" is required and must be a function!'); | ||
} | ||
} | ||
/** Action execition function */ | ||
@@ -115,0 +230,0 @@ private doAction_(action: Action, conditionsResults: Dictionary<boolean>): void { |
window.addEventListener('load', function() { | ||
var startedAt = Date.now(); | ||
var table = new window.dt.DecisionTable(); | ||
@@ -74,2 +76,6 @@ var A, B; | ||
table.run(); | ||
var endedAt = Date.now(); | ||
console.log('Executed in ' + (endedAt - startedAt) + 'ms'); | ||
}); |
'use strict'; | ||
const DecisionTable = require('../dist/DecisionTable').DecisionTable; | ||
const startedAt = Date.now(); | ||
let table = new DecisionTable(); | ||
@@ -76,1 +78,5 @@ let A, B; | ||
table.run(); | ||
const endedAt = Date.now(); | ||
console.log(`Executed in ${endedAt - startedAt}ms`); |
const path = require('path'); | ||
const webpack = require('webpack'); | ||
const merge = require('webpack-merge'); | ||
const baseConfig = require('./build/webpack.base'); | ||
const targetConfig = ( | ||
process.env.NODE_ENV === 'production' ? require('./build/webpack.prod') : require('./build/webpack.dev') | ||
); | ||
module.exports = { | ||
entry: path.join(__dirname, 'dist', 'DecisionTable.js'), | ||
output: { | ||
filename: 'decision-table.js', | ||
path: path.join(__dirname, 'lib'), | ||
library: 'dt' | ||
}, | ||
plugins: [ | ||
new webpack.optimize.UglifyJsPlugin(), | ||
new webpack.DefinePlugin({ | ||
'process.env.NODE_ENV': JSON.stringify('production') | ||
}) | ||
] | ||
}; | ||
module.exports = merge(baseConfig, targetConfig); |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
45811
18
936
276
2