fable 3.0.20 → 3.0.22

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fable",
3
- "version": "3.0.20",
3
+ "version": "3.0.22",
4
4
  "description": "An entity behavior management and API bundling library.",
5
5
  "main": "source/Fable.js",
6
6
  "scripts": {
@@ -63,8 +63,10 @@
63
63
  "dependencies": {
64
64
  "async.eachlimit": "^0.5.2",
65
65
  "async.waterfall": "^0.5.2",
66
+ "data-arithmatic": "^1.0.3",
66
67
  "fable-log": "^3.0.6",
67
68
  "fable-settings": "^3.0.3",
68
- "fable-uuid": "^3.0.2"
69
+ "fable-uuid": "^3.0.2",
70
+ "precedent": "^1.0.9"
69
71
  }
70
72
  }
@@ -0,0 +1,16 @@
1
+ const libFableServiceBase = require('./Fable-ServiceProviderBase.js');
2
+ const libDataArithmatic = require('data-arithmatic');
3
+
4
+ class FableServiceDataArithmatic extends libFableServiceBase
5
+ {
6
+ constructor(pFable, pOptions, pServiceHash)
7
+ {
8
+ super(pFable, pOptions, pServiceHash);
9
+
10
+ this.serviceType = 'DataArithmatic';
11
+
12
+ this._DataArithmaticLibrary = new libDataArithmatic();
13
+ }
14
+ }
15
+
16
+ module.exports = FableServiceDataArithmatic;
@@ -0,0 +1,41 @@
1
+ const libFableServiceBase = require('./Fable-ServiceProviderBase.js');
2
+
3
+ const libPrecedent = require('precedent');
4
+
5
+ class FableServiceMetaTemplate extends libFableServiceBase
6
+ {
7
+ constructor(pFable, pOptions, pServiceHash)
8
+ {
9
+ super(pFable, pOptions, pServiceHash);
10
+
11
+ this.serviceType = 'MetaTemplate';
12
+
13
+ this._MetaTemplateLibrary = new libPrecedent(this.options);
14
+ }
15
+
16
+ /**
17
+ * Add a Pattern to the Parse Tree
18
+ * @method addPattern
19
+ * @param {Object} pTree - A node on the parse tree to push the characters into
20
+ * @param {string} pPattern - The string to add to the tree
21
+ * @param {number} pIndex - callback function
22
+ * @return {bool} True if adding the pattern was successful
23
+ */
24
+ addPattern(pPatternStart, pPatternEnd, pParser)
25
+ {
26
+ return this._MetaTemplateLibrary.addPattern(pPatternStart, pPatternEnd, pParser);
27
+ }
28
+
29
+ /**
30
+ * Parse a string with the existing parse tree
31
+ * @method parseString
32
+ * @param {string} pString - The string to parse
33
+ * @return {string} The result from the parser
34
+ */
35
+ parseString(pString)
36
+ {
37
+ return this._MetaTemplateLibrary.parseString(pString, this.ParseTree);
38
+ }
39
+ }
40
+
41
+ module.exports = FableServiceMetaTemplate;
@@ -1,12 +1,24 @@
1
+ const libFableServiceBase = require('./Fable-ServiceProviderBase.js');
2
+
3
+
1
4
  // TODO: These are still pretty big -- consider the smaller polyfills
2
5
  const libAsyncWaterfall = require('async.waterfall');
3
6
  const libAsyncEachLimit = require('async.eachlimit');
4
7
 
5
- class FableUtility
8
+ class FableServiceUtility extends libFableServiceBase
6
9
  {
7
- constructor(pFable)
10
+ // Underscore and lodash have a behavior, _.template, which compiles a
11
+ // string-based template with code snippets into simple executable pieces,
12
+ // with the added twist of returning a precompiled function ready to go.
13
+ //
14
+ // NOTE: This does not implement underscore escape expressions
15
+ // NOTE: This does not implement underscore magic browser variable assignment
16
+ //
17
+ // This is an implementation of that.
18
+ // TODO: Make this use precedent, add configuration, add debugging.
19
+ constructor(pFable, pOptions, pServiceHash)
8
20
  {
9
- this.fable = pFable;
21
+ super(pFable, pOptions, pServiceHash);
10
22
 
11
23
  this.templates = {};
12
24
 
@@ -68,4 +80,4 @@ class FableUtility
68
80
  }
69
81
  }
70
82
 
71
- module.exports = FableUtility;
83
+ module.exports = FableServiceUtility;
package/source/Fable.js CHANGED
@@ -7,10 +7,12 @@ const libFableSettings = require('fable-settings');
7
7
  const libFableUUID = require('fable-uuid');
8
8
  const libFableLog = require('fable-log');
9
9
 
10
- const libFableUtility = require('./Fable-Utility.js');
11
10
  const libFableServiceManager = require('./Fable-ServiceManager.js');
12
11
 
12
+ const libFableServiceDataArithmatic = require('./Fable-Service-DataArithmatic.js');
13
13
  const libFableServiceTemplate = require('./Fable-Service-Template.js');
14
+ const libFableServiceMetaTemplate = require('./Fable-Service-MetaTemplate.js');
15
+ const libFableServiceUtility = require('./Fable-Service-Utility.js');
14
16
 
15
17
  const libFableOperation = require('./Fable-Operation.js');
16
18
 
@@ -28,9 +30,6 @@ class Fable
28
30
  this.log = new libFableLog(this.settingsManager.settings);
29
31
  this.log.initialize();
30
32
 
31
- // Built-in utility belt functions
32
- this.Utility = new libFableUtility(this);
33
-
34
33
  // Built-in dependencies
35
34
  this.Dependencies = (
36
35
  {
@@ -42,8 +41,23 @@ class Fable
42
41
 
43
42
  this.serviceManager = new libFableServiceManager(this);
44
43
 
44
+ // Initialize and instantiate the default baked-in Data Arithmatic service
45
+ this.serviceManager.addServiceType('DataArithmatic', libFableServiceDataArithmatic);
46
+ this.fable.serviceManager.instantiateServiceProvider('DataArithmatic', {}, 'Default-Service-DataArithmatic');
47
+ // This service is passing through the data arithmatic library
48
+ this.DataArithmatic = this.serviceManager.defaultServices.DataArithmatic._DataArithmaticLibrary;
49
+
50
+ // Initialize the template service
45
51
  this.serviceManager.addServiceType('Template', libFableServiceTemplate);
46
52
 
53
+ // Initialize the metatemplate service
54
+ this.serviceManager.addServiceType('MetaTemplate', libFableServiceMetaTemplate);
55
+
56
+ // Initialize and instantiate the default baked-in Utility service
57
+ this.serviceManager.addServiceType('Utility', libFableServiceUtility)
58
+ this.fable.serviceManager.instantiateServiceProvider('Utility', {}, 'Default-Service-Utility');
59
+ this.Utility = this.serviceManager.defaultServices.Utility;
60
+
47
61
  this.services = this.serviceManager.services;
48
62
  this.defaultServices = this.serviceManager.defaultServices;
49
63
  }
@@ -0,0 +1,223 @@
1
+ /**
2
+ * Unit tests for Fable's DataArithmatic service
3
+ *
4
+ * @license MIT
5
+ *
6
+ * @author Steven Velozo <steven@velozo.com>
7
+ */
8
+
9
+ var libFable = require('../source/Fable.js');
10
+
11
+ var Chai = require("chai");
12
+ var Expect = Chai.expect;
13
+
14
+ suite
15
+ (
16
+ 'DataArithmatic String',
17
+ function()
18
+ {
19
+ setup (()=> {} );
20
+
21
+ suite
22
+ (
23
+ 'Manipulate Strings',
24
+ ()=>
25
+ {
26
+ test
27
+ (
28
+ 'Reverse a String',
29
+ (fTestComplete)=>
30
+ {
31
+ let testFable = new libFable({LogStreams: false});
32
+ let _DataArithmatic = testFable.DataArithmatic;
33
+ Expect(_DataArithmatic
34
+ .stringReverse('Dogs'))
35
+ .to.equal('sgoD');
36
+ Expect(_DataArithmatic
37
+ .stringReverse('Florence and the Machine'))
38
+ .to.equal('enihcaM eht dna ecnerolF');
39
+ return fTestComplete();
40
+ }
41
+ )
42
+ test
43
+ (
44
+ 'Insecure String Hash',
45
+ (fTestComplete)=>
46
+ {
47
+ let testFable = new libFable({LogStreams: false});
48
+ let _DataArithmatic = testFable.DataArithmatic;
49
+ Expect(_DataArithmatic
50
+ .insecureStringHash('Dogs'))
51
+ .to.equal('HSH2135767');
52
+ Expect(_DataArithmatic
53
+ .insecureStringHash('Dogs1'))
54
+ .to.equal('HSH66208826');
55
+ Expect(_DataArithmatic
56
+ .insecureStringHash('This string is longer'))
57
+ .to.equal('HSH53260210');
58
+ // This repeat is intentional, to ensure stable hash generation after multiple runs.
59
+ Expect(_DataArithmatic
60
+ .insecureStringHash('Dogs'))
61
+ .to.equal('HSH2135767');
62
+ return fTestComplete();
63
+ }
64
+ )
65
+ test
66
+ (
67
+ 'Clean wrapping characters from a valid enclosure.',
68
+ (fTestComplete)=>
69
+ {
70
+ let testFable = new libFable({LogStreams: false});
71
+ let _DataArithmatic = testFable.DataArithmatic;
72
+ // Test the enclosure cleaning function
73
+ Expect(_DataArithmatic
74
+ .cleanEnclosureWrapCharacters('`', '`Dogs`'))
75
+ .to.equal('Dogs');
76
+ // Tests a cleaning where the enclosure is not wrapped with the expected character
77
+ Expect(_DataArithmatic
78
+ .cleanEnclosureWrapCharacters('"', '`Cats`'))
79
+ .to.equal('`Cats`');
80
+ // Tests a cleaning where the enclosure is not wrapped with the expected character
81
+ Expect(_DataArithmatic
82
+ .cleanEnclosureWrapCharacters('"', '"Dogs"'))
83
+ .to.equal('Dogs');
84
+ // Test cleaning an enclosure with multiple enclosures of the same type which are expected to stay intact
85
+ Expect(_DataArithmatic
86
+ .cleanEnclosureWrapCharacters('[', '[Array[with]weird other] Dogs in it['))
87
+ .to.equal('Array[with]weird other] Dogs in it');
88
+ // Test cleaning a string where the enclosure character is on one side but not the other
89
+ Expect(_DataArithmatic
90
+ .cleanEnclosureWrapCharacters('"', '"Dogs'))
91
+ .to.equal('"Dogs');
92
+ // Test cleaning a string where the enclosure character is on one side but not the other
93
+ Expect(_DataArithmatic
94
+ .cleanEnclosureWrapCharacters('"', 'Dogs"'))
95
+ .to.equal('Dogs"');
96
+ return fTestComplete();
97
+ }
98
+ );
99
+ test
100
+ (
101
+ 'Test if a string starts with a given substring - Javascript Engine startsWith',
102
+ (fTestComplete)=>
103
+ {
104
+ let testFable = new libFable({LogStreams: false});
105
+ let _DataArithmatic = testFable.DataArithmatic;
106
+ Expect(_DataArithmatic
107
+ .stringStartsWith('Dogs', 'Do'))
108
+ .to.equal(true);
109
+ Expect(_DataArithmatic
110
+ .stringStartsWith('Bats', 'Bats'))
111
+ .to.equal(true);
112
+ Expect(_DataArithmatic
113
+ .stringStartsWith('Dogs', 'Dogs are cool'))
114
+ .to.equal(false);
115
+ Expect(_DataArithmatic
116
+ .stringStartsWith('Dogs', 'Cats'))
117
+ .to.equal(false);
118
+ return fTestComplete();
119
+ }
120
+ );
121
+ test
122
+ (
123
+ 'Test if a string starts with a given substring - Internal startsWith',
124
+ (fTestComplete)=>
125
+ {
126
+ let testFable = new libFable({LogStreams: false});
127
+ let _DataArithmatic = testFable.DataArithmatic;
128
+ _DataArithmatic._UseEngineStringStartsWith = false;
129
+ Expect(_DataArithmatic
130
+ .stringStartsWith('Dogs', 'Do'))
131
+ .to.equal(true);
132
+ Expect(_DataArithmatic
133
+ .stringStartsWith('Bats', 'Bats'))
134
+ .to.equal(true);
135
+ Expect(_DataArithmatic
136
+ .stringStartsWith('Dogs', 'Dogs are cool'))
137
+ .to.equal(false);
138
+ Expect(_DataArithmatic
139
+ .stringStartsWith('Dogs', 'Cats'))
140
+ .to.equal(false);
141
+ return fTestComplete();
142
+ }
143
+ );
144
+ test
145
+ (
146
+ 'Test if a string ends with a given substring - Javascript Engine endsWith',
147
+ (fTestComplete)=>
148
+ {
149
+ let testFable = new libFable({LogStreams: false});
150
+ let _DataArithmatic = testFable.DataArithmatic;
151
+ Expect(_DataArithmatic
152
+ .stringEndsWith('Dogs', 'gs'))
153
+ .to.equal(true);
154
+ Expect(_DataArithmatic
155
+ .stringEndsWith('Bats', 'Bats'))
156
+ .to.equal(true);
157
+ Expect(_DataArithmatic
158
+ .stringEndsWith('Dogs', 'Dogs are cool'))
159
+ .to.equal(false);
160
+ Expect(_DataArithmatic
161
+ .stringEndsWith('Dogs', 'Cats'))
162
+ .to.equal(false);
163
+ return fTestComplete();
164
+ }
165
+ );
166
+ test
167
+ (
168
+ 'Test if a string ends with a given substring - Internal endsWith',
169
+ (fTestComplete)=>
170
+ {
171
+ let testFable = new libFable({LogStreams: false});
172
+ let _DataArithmatic = testFable.DataArithmatic;
173
+ _DataArithmatic._UseEngineStringEndsWith = false;
174
+ Expect(_DataArithmatic
175
+ .stringEndsWith('Dogs', 'gs'))
176
+ .to.equal(true);
177
+ Expect(_DataArithmatic
178
+ .stringEndsWith('Bats', 'Bats'))
179
+ .to.equal(true);
180
+ Expect(_DataArithmatic
181
+ .stringEndsWith('Dogs', 'Dogs are cool'))
182
+ .to.equal(false);
183
+ // We should be able to tell it a midpoint to "end" the string at
184
+ Expect(_DataArithmatic
185
+ .stringEndsWith('Start from a median point', 'median', 19))
186
+ .to.equal(true);
187
+ Expect(_DataArithmatic
188
+ .stringEndsWith('Start from a median point', 'median', 20))
189
+ .to.equal(false);
190
+ Expect(_DataArithmatic
191
+ .stringEndsWith('Dogs', 'Cats'))
192
+ .to.equal(false);
193
+ return fTestComplete();
194
+ }
195
+ );
196
+ test
197
+ (
198
+ 'Test cleaning nonalpha characters from a string',
199
+ (fTestComplete)=>
200
+ {
201
+ let testFable = new libFable({LogStreams: false});
202
+ let _DataArithmatic = testFable.DataArithmatic;
203
+ Expect(_DataArithmatic
204
+ .cleanNonAlphaCharacters('Dogs'))
205
+ .to.equal('Dogs');
206
+ Expect(_DataArithmatic
207
+ .cleanNonAlphaCharacters('Dogs are cool'))
208
+ .to.equal('Dogs_are_cool');
209
+ Expect(_DataArithmatic
210
+ .cleanNonAlphaCharacters('Dogs are cool!'))
211
+ .to.equal('Dogs_are_cool_');
212
+ // Test cleaning with no character
213
+ _DataArithmatic._Value_Clean_formatterCleanNonAlpha = '';
214
+ Expect(_DataArithmatic
215
+ .cleanNonAlphaCharacters('Dogs are cool!'))
216
+ .to.equal('Dogsarecool');
217
+ return fTestComplete();
218
+ }
219
+ );
220
+ }
221
+ );
222
+ }
223
+ );
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Unit tests for Fable
3
+ *
4
+ * @license MIT
5
+ *
6
+ * @author Steven Velozo <steven@velozo.com>
7
+ */
8
+
9
+ var libFable = require('../source/Fable.js');
10
+
11
+ var Chai = require("chai");
12
+ var Expect = Chai.expect;
13
+
14
+
15
+ var loadPrecedentModule = () =>
16
+ {
17
+ let tmpFable = new libFable();
18
+ return tmpFable.serviceManager.instantiateServiceProviderWithoutRegistration('MetaTemplate', {});
19
+ };
20
+
21
+ var configPrecedent = (pModule) =>
22
+ {
23
+ pModule.addPattern('<%', '%>', 'JUNKED_THIS_DATA');
24
+ // This one gets the count of the inner string...
25
+ pModule.addPattern('<%#', '%>', (pData)=>{return pData.length});
26
+ // Replaces the string with the settings object...
27
+ pModule.addPattern('<%=', '%>', (pData)=>{return JSON.stringify(pModule.settings);});
28
+ // This just escapes out pairs of $
29
+ pModule.addPattern('$');
30
+ };
31
+
32
+ suite
33
+ (
34
+ 'Fable MetaTemplating',
35
+ function()
36
+ {
37
+ var testFable = false;
38
+
39
+ setup
40
+ (
41
+ function()
42
+ {
43
+ }
44
+ );
45
+
46
+ suite
47
+ (
48
+ 'MetaTemplating',
49
+ function()
50
+ {
51
+ test
52
+ (
53
+ 'No Matches...',
54
+ (fDone) =>
55
+ {
56
+ var tmpTestString = 'ABC123';
57
+ var tmpExpectedResult = tmpTestString;
58
+ var testPrecedent = loadPrecedentModule();
59
+ configPrecedent(testPrecedent);
60
+ var tmpResult = testPrecedent.parseString(tmpTestString);
61
+ Expect(tmpResult).to.equal(tmpExpectedResult);
62
+ fDone();
63
+ }
64
+ );
65
+ test
66
+ (
67
+ 'Count function...',
68
+ (fDone) =>
69
+ {
70
+ var tmpTestString = 'There are <%#0123456789%> characters in here';
71
+ var tmpExpectedResult = 'There are 10 characters in here';
72
+ var testPrecedent = loadPrecedentModule();
73
+ configPrecedent(testPrecedent);
74
+ var tmpResult = testPrecedent.parseString(tmpTestString);
75
+ Expect(tmpResult).to.equal(tmpExpectedResult);
76
+ fDone();
77
+ }
78
+ );
79
+ test
80
+ (
81
+ 'Multiple terms...',
82
+ (fDone) =>
83
+ {
84
+ var tmpTestString = 'There are <%#12345%> characters in here and a $comment$ as well. And we <% Some data in here %> right up.';
85
+ var tmpExpectedResult = 'There are 5 characters in here and a comment as well. And we JUNKED_THIS_DATA right up.';
86
+ var testPrecedent = loadPrecedentModule();
87
+ configPrecedent(testPrecedent);
88
+ var tmpResult = testPrecedent.parseString(tmpTestString);
89
+ Expect(tmpResult).to.equal(tmpExpectedResult);
90
+ fDone();
91
+ }
92
+ );
93
+ }
94
+ );
95
+ }
96
+ );
@@ -10,7 +10,6 @@ var libFable = require('../source/Fable.js');
10
10
 
11
11
  var Chai = require("chai");
12
12
  var Expect = Chai.expect;
13
- var Assert = Chai.assert;
14
13
 
15
14
  suite
16
15
  (
@@ -10,7 +10,6 @@ var libFable = require('../source/Fable.js');
10
10
 
11
11
  var Chai = require("chai");
12
12
  var Expect = Chai.expect;
13
- var Assert = Chai.assert;
14
13
 
15
14
  suite
16
15
  (
@@ -10,7 +10,6 @@ var libFable = require('../source/Fable.js');
10
10
 
11
11
  var Chai = require("chai");
12
12
  var Expect = Chai.expect;
13
- var Assert = Chai.assert;
14
13
 
15
14
  suite
16
15
  (