es6-fuzz 3.0.5 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.jscsrc CHANGED
@@ -14,7 +14,6 @@
14
14
  "disallowEmptyBlocks": true,
15
15
  "disallowSpacesInsideParentheses": true,
16
16
  "disallowSpaceAfterObjectKeys": true,
17
- "requireSpaceAfterBinaryOperators": true,
18
17
  "disallowSpaceBeforePostfixUnaryOperators": ["++", "--"],
19
18
  "requireSpaceBeforeBinaryOperators": [
20
19
  "=",
package/docs/CHANGELOG.md CHANGED
@@ -1,6 +1,33 @@
1
+ 2022-06-14
2
+ ==========
3
+
4
+ * 4.0.0
5
+ * feature: make the api work with boonJsInputs
6
+ * feature: add a test for outoput names only to consist of alphabet
7
+ letters
8
+
9
+ 2022-06-11
10
+ ==========
11
+
12
+ * (churn): changelog
13
+ * 3.0.7
14
+ * fix: revert dep change
15
+ * refactor: make dependencies lighter
16
+
17
+ 2022-06-02
18
+ ==========
19
+
20
+ * 3.0.6
21
+ * (churn): changelog
22
+ * feature: chehck if init was called before any logic os performed
23
+ * test: add another example to the tests
24
+ * chore: remove travis config
25
+ * chore: update dependencies
26
+
1
27
  2019-01-15
2
28
  ==========
3
29
 
30
+ * (churn): changelog
4
31
  * 3.0.5
5
32
  * Updated docs gen
6
33
 
@@ -123,34 +150,3 @@
123
150
  * Fix output ght pages
124
151
  * DOcs
125
152
  * Ghpages
126
- * 2.5.5
127
- * Autorelease
128
-
129
- 2017-01-27
130
- ==========
131
-
132
- * (churn): changelog
133
- * 2.5.4
134
- * Merge branch 'master' of github.com:sebs/es6-fuzz
135
- * Merge pull request [#64](https://github.com/sebs/es6-fuzz/issues/64) from sebs/greenkeeper-browserify-14.0.0
136
- Update browserify to version 14.0.0 🚀
137
-
138
- 2017-01-25
139
- ==========
140
-
141
- * chore(package): update browserify to version 14.0.0
142
- https://greenkeeper.io/
143
-
144
- 2017-01-09
145
- ==========
146
-
147
- * (churn): changelog
148
- * 2.5.3
149
- * Update deps
150
-
151
- 2016-10-14
152
- ==========
153
-
154
- * (churn): changelog
155
- * 2.5.2
156
- * Sigmoid as well
package/lib/logic.js CHANGED
@@ -27,6 +27,7 @@ const ruleEngine = {
27
27
  /** Class helping with FuzzyLogic. */
28
28
 
29
29
  class Logic {
30
+ initCalled = false;
30
31
  constructor() {
31
32
  this.c = {
32
33
  Shape,
@@ -40,36 +41,58 @@ class Logic {
40
41
  };
41
42
  this.rules = [];
42
43
  }
44
+
45
+ checkInitCalled() {
46
+ if (!this.initCalled) {
47
+ throw Error('init needs to be called before performing logic');
48
+ }
49
+ }
50
+
51
+ checkOutputName(name) {
52
+ const reg = /^[a-z]+$/i;
53
+ if (!reg.test(name)) {
54
+ throw Error('Output names can only be strings without space, without numbers and without special chars');
55
+ }
56
+ }
57
+
58
+
59
+
43
60
  init(output, shape) {
61
+ this.checkOutputName(output);
62
+ this.initCalled = true;
44
63
  let type = TYPE_INIT;
45
64
  this.rules.push({ output, shape, type });
46
65
  return this;
47
66
  }
48
67
  and(output, shape) {
68
+ this.checkOutputName(output);
69
+ this.checkInitCalled();
49
70
  let type = TYPE_AND;
50
71
  this.rules.push({ output, shape, type });
51
72
  return this;
52
73
  }
53
- or(output, shape) {
74
+ or(output, shape) {+
75
+ this.checkOutputName(output);
76
+ this.checkInitCalled();
54
77
  let type = TYPE_OR;
55
78
  this.rules.push({ output, shape, type });
56
79
  return this;
57
80
  }
58
81
  not(output, shape) {
82
+ this.checkOutputName(output);
83
+ this.checkInitCalled();
59
84
  let type = TYPE_NOT;
60
85
  this.rules.push({ output, shape, type });
61
86
  return this;
62
87
  }
63
- defuzzify(value) {
64
-
88
+ defuzzify(value, as=undefined) {
89
+ this.checkInitCalled();
65
90
  let defuzzified = 'none';
66
91
  let fuzzified = 0;
67
92
  let lastShape;
68
-
69
93
  this.rules.forEach(rule => {
70
94
  rule.fuzzy = rule.shape.fuzzify(value);
71
95
  // lets keep the initial value
72
-
73
96
  if (rule.type === TYPE_INIT) {
74
97
  defuzzified = rule.output;
75
98
  fuzzified = rule.fuzzy;
@@ -85,14 +108,28 @@ class Logic {
85
108
  defuzzified = rule.output;
86
109
  fuzzified = rule.fuzzy;
87
110
  // if there is no shape, like for example for a NOT keep the last one
88
- lastShape = rule.shape || lastShape;
111
+ lastShape = rule.shape || lastShape;
89
112
  }
90
113
  });
114
+
115
+
116
+
117
+ var namePrefix = '';
118
+ if (!!as && typeof as === 'string') {
119
+ namePrefix = as + '.';
120
+ }
121
+
122
+ const boonJsInputs = {};
123
+ this.rules.forEach(rule => {
124
+ boonJsInputs[`${namePrefix}${rule.output}`] = rule.output === defuzzified;
125
+ });
126
+
91
127
  /**
92
128
  *
93
129
  * @example fuzzy.defuzzify(10)
94
130
  */
95
131
  return {
132
+ boonJsInputs,
96
133
  fuzzified: fuzzified,
97
134
  defuzzified: defuzzified,
98
135
  rules: this.rules,
package/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "es6-fuzz",
3
3
  "description": "fuzzy logic with and for es6",
4
- "version": "3.0.5",
4
+ "version": "4.0.0",
5
5
  "main": "lib/logic.js",
6
6
  "repository": "git@github.com:sebs/es6-fuzz.git",
7
7
  "homepage": "https://github.com/sebs/es6-fuzz",
8
8
  "scripts": {
9
- "env": "env",
10
- "changelog": "rm ./docs/CHANGELOG.md && changelog https://github.com/sebs/es6-fuzz all > ./docs/CHANGELOG.md && git add . && git commit . -m '(churn): changelog'",
11
- "test": "mocha -r blanket",
9
+ "changelog": "rm ./docs/CHANGELOG.md && npx changelog https://github.com/sebs/es6-fuzz all > ./docs/CHANGELOG.md && git add . && git commit . -m '(churn): changelog'",
10
+ "test": "npx mocha",
12
11
  "test:watch": "npm test -- -w",
13
12
  "addpush": "git add . && git commit . -m 'docs'",
14
13
  "pages": "node ./scripts/publish-gh.js",
@@ -23,32 +22,14 @@
23
22
  "javascript",
24
23
  "es6"
25
24
  ],
26
- "config": {
27
- "blanket": {
28
- "pattern": "lib/*",
29
- "data-cover-never": [
30
- "node_modules",
31
- "tests"
32
- ]
33
- }
34
- },
35
25
  "author": "Sebastian Schürmann",
36
26
  "license": "MIT",
37
27
  "devDependencies": {
38
- "browserify": "^16.2.2",
39
- "changelog": "^1.0.7",
40
- "documentation": "^9.1.1",
41
- "es6ify": "^1.6.0",
42
- "eslint": "^5.6.0",
43
- "gh-pages": "^2.0.0",
44
- "ink-docstrap": "^1.3.0",
45
- "jscs": "^3.0.7",
46
- "jsdoc": "^3.4.3",
47
- "mocha": "^5.2.0",
48
- "mocha-traceur": "^2.1.0",
49
- "traceur": "0.0.111"
28
+ "gh-pages": "4.0.0",
29
+ "ink-docstrap": "1.3.2",
30
+ "jsdoc": "3.6.10"
50
31
  },
51
32
  "dependencies": {
52
- "blanket": "^1.2.3"
33
+ "boon-js": "2.0.3"
53
34
  }
54
35
  }
@@ -0,0 +1,25 @@
1
+ const assert = require('assert');
2
+ const Logic = require('../lib/logic');
3
+ const Triangle = require('../lib/curve/triangle');
4
+
5
+ describe('Example Test', function() {
6
+ var fuzzyvariable_pageSize;
7
+ beforeEach(()=>{
8
+ const logic = new Logic();
9
+ fuzzyvariable_pageSize = logic
10
+ .init('Excellent', new Triangle(20, 40, 60))
11
+ .or('Medium', new Triangle(40,60,80))
12
+ .or('Bad', new Triangle(60,80,100))
13
+
14
+ })
15
+
16
+ it('40 is excellent', ()=>{
17
+ assert.equal(fuzzyvariable_pageSize.defuzzify(40).defuzzified, 'Excellent');
18
+ })
19
+ it('60 is medium', ()=>{
20
+ assert.equal(fuzzyvariable_pageSize.defuzzify(60).defuzzified, 'Medium');
21
+ })
22
+ it('80 is bad', ()=>{
23
+ assert.equal(fuzzyvariable_pageSize.defuzzify(80).defuzzified, 'Bad');
24
+ });
25
+ });
@@ -0,0 +1,16 @@
1
+ const assert = require('assert');
2
+ const Logic = require('../lib/logic');
3
+ const Triangle = require('../lib/curve/triangle');
4
+
5
+ describe('Example Test', function() {
6
+
7
+ it('init needs to be called before using logic', ()=>{
8
+
9
+ assert.throws(()=>{
10
+ const logic = new Logic();
11
+ fuzzyvariable_pageSize = logic
12
+ .or('Excellent', new Triangle(20, 40, 60))
13
+ });
14
+ });
15
+
16
+ });
@@ -0,0 +1,86 @@
1
+ 'use strict';
2
+ const Logic = require('../lib/logic');
3
+ const Triangle = require('../lib/curve/triangle');
4
+ const Trapezoid = require('../lib/curve/trapezoid');
5
+ const assert = require('assert');
6
+ const getEvaluator = require('boon-js').getEvaluator;
7
+
8
+ describe('js-bool test', () => {
9
+ var logicHeat = new Logic();
10
+ const optimalTemperature = new Triangle(10, 20, 30);
11
+ const toColdTemperature = new Triangle(0, 10, 15);
12
+ const toHotTemperature = new Triangle(25, 40, 60);
13
+
14
+ logicHeat.init('cold', toColdTemperature)
15
+ logicHeat.or('optimal', optimalTemperature)
16
+ logicHeat.or('hot', toHotTemperature)
17
+
18
+ var logicDistance = new Logic();
19
+ const close = new Triangle(0, 10, 20);
20
+ const far = new Triangle(5, 50, 100);
21
+
22
+ logicDistance.init('close', close)
23
+ logicDistance.or('far', far)
24
+
25
+
26
+ it('provides output for js-bool', () => {
27
+ const res = logicHeat.defuzzify(20, 'heat');
28
+ assert.ok(res.boonJsInputs);
29
+ })
30
+
31
+ it('jsbool output cold is false', () => {
32
+ const res = logicHeat.defuzzify(20);
33
+ assert.equal(res.boonJsInputs.cold, false);
34
+ })
35
+
36
+ it('jsbool output hot is false', () => {
37
+ const res = logicHeat.defuzzify(20);
38
+ assert.equal(res.boonJsInputs.cold, false);
39
+ })
40
+
41
+ it('jsbool output optimal is false', () => {
42
+ const res = logicHeat.defuzzify(20);
43
+ assert.equal(res.boonJsInputs.optimal, true);
44
+ })
45
+
46
+ it('jsbool output cold as heat is false', () => {
47
+ const res = logicHeat.defuzzify(20, 'heat');
48
+ assert.equal(res.boonJsInputs['heat.cold'], false);
49
+ })
50
+
51
+ it('jsbool output hot as heat is false', () => {
52
+ const res = logicHeat.defuzzify(20, 'heat');
53
+ assert.equal(res.boonJsInputs['heat.hot'], false);
54
+ })
55
+
56
+ it('jsbool output optimal as heat is true', () => {
57
+ const res = logicHeat.defuzzify(20, 'heat');
58
+ assert.equal(res.boonJsInputs['heat.optimal'], true);
59
+ });
60
+
61
+
62
+ describe('can combine 2 fuzzy decisions via boonJs', ()=>{
63
+ it.only('monster bites when heat is cold and distance is close', ()=>{
64
+
65
+ const resHeat = logicHeat.defuzzify(2, 'heat');
66
+ const resClose = logicDistance.defuzzify(2, 'distance');
67
+
68
+ const jsBoonInput = { ...resHeat.boonJsInputs, ...resClose.boonJsInputs }
69
+ console.log(jsBoonInput);
70
+
71
+ const monsterBiteTest = getEvaluator(
72
+ 'heat.cold AND distance.close',
73
+ );
74
+
75
+ const monsterSleepTest = getEvaluator(
76
+ 'heat.hot AND distance.far',
77
+ );
78
+
79
+ const resBite = monsterBiteTest(jsBoonInput)
80
+ assert.equal(resBite, true);
81
+
82
+ const resSleep = monsterSleepTest(jsBoonInput)
83
+ assert.equal(resSleep, false);
84
+ });
85
+ });
86
+ });
@@ -43,9 +43,9 @@ describe('logic', () => {
43
43
  var logic = new Logic();
44
44
  var res = logic
45
45
  .init('rage', rageRange)
46
- .not('no rage', rageRange)
46
+ .not('norage', rageRange)
47
47
  .defuzzify(20);
48
- assert.equal(res.toString(), 'no rage');
48
+ assert.equal(res.toString(), 'norage');
49
49
  });
50
50
  });
51
51
 
package/.travis.yml DELETED
@@ -1,4 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - '9'
4
- - '10'