fable 3.0.91 → 3.0.92

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.91",
3
+ "version": "3.0.92",
4
4
  "description": "An entity behavior management and API bundling library.",
5
5
  "main": "source/Fable.js",
6
6
  "scripts": {
@@ -105,12 +105,17 @@ class StringParser
105
105
  // Flush the output buffer.
106
106
  this.appendOutputBuffer(pCharacter, pParserState);
107
107
  // If this last character is the end of the pattern, parse it.
108
- if (pParserState.Pattern.hasOwnProperty('Parse'))
108
+ if (pParserState.Pattern.hasOwnProperty('Parse') && (!pParserState.Pattern.isAsync || pParserState.Pattern.isBoth))
109
109
  {
110
110
  // Run the function
111
111
  pParserState.OutputBuffer = pParserState.Pattern.Parse(pParserState.OutputBuffer.substr(pParserState.Pattern.PatternStartString.length, pParserState.OutputBuffer.length - (pParserState.Pattern.PatternStartString.length+pParserState.Pattern.PatternEndString.length)), pData);
112
112
  return this.resetOutputBuffer(pParserState);
113
113
  }
114
+ else
115
+ {
116
+ console.log(`MetaTemplate: The pattern ${pParserState.Pattern.PatternStartString} is asynchronous and cannot be used in a synchronous parser.`);
117
+ return this.resetOutputBuffer(pParserState);
118
+ }
114
119
  }
115
120
  else if (pParserState.PatternStartNode.PatternEnd.hasOwnProperty(pCharacter))
116
121
  {
@@ -143,9 +148,9 @@ class StringParser
143
148
  // If this last character is the end of the pattern, parse it.
144
149
  if (pParserState.Pattern.hasOwnProperty('Parse'))
145
150
  {
146
- if (pParserState.Pattern.isAsync)
151
+ if (pParserState.Pattern.isAsync && !pParserState.Pattern.isBoth)
147
152
  {
148
- this.log.error(`MetaTemplate: The pattern ${pParserState.Pattern.PatternStartString} is asynchronous and cannot be used in a synchronous parser.`);
153
+ console.log(`MetaTemplate: The pattern ${pParserState.Pattern.PatternStartString} is asynchronous and cannot be used in a synchronous parser.`);
149
154
  this.resetOutputBuffer(pParserState);
150
155
  }
151
156
  else
@@ -215,7 +220,7 @@ class StringParser
215
220
  {
216
221
  // ... this is the end of a pattern, cut off the end tag and parse it.
217
222
  // Trim the start and end tags off the output buffer now
218
- if (pParserState.Pattern.isAsync)
223
+ if (pParserState.Pattern.isAsync && !pParserState.Pattern.isBoth)
219
224
  {
220
225
  // Run the function
221
226
  return pParserState.Pattern.Parse(pParserState.OutputBuffer.substr(pParserState.Pattern.PatternStartString.length, pParserState.OutputBuffer.length - (pParserState.Pattern.PatternStartString.length+pParserState.Pattern.PatternEndString.length)), pData,
@@ -231,6 +236,22 @@ class StringParser
231
236
  return fCallback();
232
237
  });
233
238
  }
239
+ else if (pParserState.Pattern.isAsync && pParserState.Pattern.isBoth)
240
+ {
241
+ // Run the function when both async and non async were provided with the pattern
242
+ return pParserState.Pattern.ParseAsync(pParserState.OutputBuffer.substr(pParserState.Pattern.PatternStartString.length, pParserState.OutputBuffer.length - (pParserState.Pattern.PatternStartString.length+pParserState.Pattern.PatternEndString.length)), pData,
243
+ (pError, pAsyncOutput) =>
244
+ {
245
+ if (pError)
246
+ {
247
+ console.log(`Precedent ERROR: Async template error happened parsing ${pParserState.Pattern.PatternStart} ... ${pParserState.Pattern.PatternEnd}: ${pError}`);
248
+ }
249
+
250
+ pParserState.OutputBuffer = pAsyncOutput;
251
+ this.resetOutputBuffer(pParserState);
252
+ return fCallback();
253
+ });
254
+ }
234
255
  else
235
256
  {
236
257
  // Run the t*mplate function
@@ -101,18 +101,37 @@ class WordTree
101
101
 
102
102
 
103
103
  /** Add a Pattern to the Parse Tree
104
- * @method addPattern
104
+ * @method addPatternAsync
105
+ * @param {Object} pPatternStart - The starting string for the pattern (e.g. "${")
106
+ * @param {string} pPatternEnd - The ending string for the pattern (e.g. "}")
107
+ * @param {function} fParserAsync - The function to parse if this is the matched pattern, once the Pattern End is met. If this is a string, a simple replacement occurs.
108
+ * @return {bool} True if adding the pattern was successful
109
+ */
110
+ addPatternAsync (pPatternStart, pPatternEnd, fParserAsync)
111
+ {
112
+ let tmpLeaf = this.addPattern(pPatternStart, pPatternEnd, fParserAsync);
113
+ if (tmpLeaf)
114
+ {
115
+ tmpLeaf.isAsync = true;
116
+ }
117
+ }
118
+
119
+ /** Add a Pattern to the Parse Tree
120
+ * @method addPatternBoth
105
121
  * @param {Object} pPatternStart - The starting string for the pattern (e.g. "${")
106
122
  * @param {string} pPatternEnd - The ending string for the pattern (e.g. "}")
107
123
  * @param {function} fParser - The function to parse if this is the matched pattern, once the Pattern End is met. If this is a string, a simple replacement occurs.
108
124
  * @return {bool} True if adding the pattern was successful
109
125
  */
110
- addPatternAsync (pPatternStart, pPatternEnd, fParser)
126
+ addPatternBoth (pPatternStart, pPatternEnd, fParser, fParserAsync)
111
127
  {
112
128
  let tmpLeaf = this.addPattern(pPatternStart, pPatternEnd, fParser);
113
129
  if (tmpLeaf)
114
130
  {
115
131
  tmpLeaf.isAsync = true;
132
+ tmpLeaf.isBoth = true;
133
+ // When a leaf has both async and non-async versions of the functions, we store the async in fParserAsync.
134
+ tmpLeaf.ParseAsync = fParserAsync;
116
135
  }
117
136
  }
118
137
  }
@@ -44,6 +44,11 @@ class FableServiceMetaTemplate extends libFableServiceBase
44
44
  return this.WordTree.addPatternAsync(pPatternStart, pPatternEnd, pParserPromise);
45
45
  }
46
46
 
47
+ addPatternBoth(pPatternStart, pPatternEnd, pParser, pParserPromise)
48
+ {
49
+ return this.WordTree.addPatternBoth(pPatternStart, pPatternEnd, pParser, pParserPromise);
50
+ }
51
+
47
52
  /**
48
53
  * Parse a string with the existing parse tree
49
54
  * @method parseString
@@ -37,6 +37,16 @@ const configMetaTemplate = (pModule) =>
37
37
  {
38
38
  return fCallback(null, `ASYNC DATA IS [${pHash}]`);
39
39
  });
40
+
41
+ pModule.addPatternBoth('<~', '~>',
42
+ (pHash, pData) =>
43
+ {
44
+ return `Non-Async Jellyfish called for pData which is [${pData}] with a hash of [${pHash}]`
45
+ },
46
+ (pHash, pData, fCallback)=>
47
+ {
48
+ return fCallback(null, `Async Jellyfish called for pData which is [${pData}] with a hash of [${pHash}]`);
49
+ });
40
50
 
41
51
  };
42
52
 
@@ -160,6 +170,25 @@ suite
160
170
  }
161
171
  );
162
172
  test
173
+ (
174
+ 'Passing both Async and Non-async Function',
175
+ (fDone) =>
176
+ {
177
+ let tmpTestString = 'The <^SomeValue^> and <~JELLY FISH~> pData and Async <%AsyncThe Funny String%> up in here and a $comment$ as well.';
178
+ let tmpExpectedResultAsync = 'The hash of [SomeValue] from pData is AirbornLight and Async Jellyfish called for pData which is [[object Object]] with a hash of [JELLY FISH] pData and Async ASYNC DATA IS [The Funny String] up in here and a comment as well.';
179
+ let tmpExpectedResult = 'The hash of [SomeValue] from pData is AirbornLight and Non-Async Jellyfish called for pData which is [[object Object]] with a hash of [JELLY FISH] pData and Async <%AsyncThe Funny String%> up in here and a comment as well.';
180
+ let testMetaTemplate = loadMetaTemplateModule();
181
+ configMetaTemplate(testMetaTemplate);
182
+ let tmpNonAsyncResult = testMetaTemplate.parseString(tmpTestString, {SomeValue:'AirbornLight'});
183
+ Expect(tmpNonAsyncResult).to.equal(tmpExpectedResult);
184
+ let tmpResult = testMetaTemplate.parseString(tmpTestString, {SomeValue:'AirbornLight'},
185
+ (pError, pValue) =>
186
+ {
187
+ Expect(pValue).to.equal(tmpExpectedResultAsync);
188
+ return fDone();
189
+ });
190
+ }
191
+ ); test
163
192
  (
164
193
  'Bad pattern start parameter...',
165
194
  (fDone) =>