precedent 1.0.13 → 1.0.14
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/.browserslistrc +1 -1
- package/dist/precedent.compatible.js +307 -3975
- package/dist/precedent.compatible.min.js +2 -22
- package/dist/precedent.compatible.min.js.map +1 -1
- package/dist/precedent.js +307 -3975
- package/dist/precedent.min.js +2 -22
- package/dist/precedent.min.js.map +1 -1
- package/gulpfile-config.json +2 -2
- package/package.json +2 -4
- package/source/Precedent.js +2 -7
- package/source/StringParser.js +13 -130
- package/source/WordTree.js +0 -36
- package/test/Precedent_tests.js +0 -23
package/gulpfile-config.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "precedent",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"description": "Precedent Meta-Templating",
|
|
5
5
|
"main": "source/Precedent.js",
|
|
6
6
|
"bin": {
|
|
@@ -61,7 +61,5 @@
|
|
|
61
61
|
"vinyl-buffer": "^1.0.1",
|
|
62
62
|
"vinyl-source-stream": "^2.0.0"
|
|
63
63
|
},
|
|
64
|
-
"dependencies": {
|
|
65
|
-
"async": "^3.2.4"
|
|
66
|
-
}
|
|
64
|
+
"dependencies": {}
|
|
67
65
|
}
|
package/source/Precedent.js
CHANGED
|
@@ -37,11 +37,6 @@ class Precedent
|
|
|
37
37
|
return this.WordTree.addPattern(pPatternStart, pPatternEnd, pParser);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
addPatternAsync(pPatternStart, pPatternEnd, pParserPromise)
|
|
41
|
-
{
|
|
42
|
-
return this.WordTree.addPatternAsync(pPatternStart, pPatternEnd, pParserPromise);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
40
|
/**
|
|
46
41
|
* Parse a string with the existing parse tree
|
|
47
42
|
* @method parseString
|
|
@@ -49,9 +44,9 @@ class Precedent
|
|
|
49
44
|
* @param {object} pData - Data to pass in as the second argument
|
|
50
45
|
* @return {string} The result from the parser
|
|
51
46
|
*/
|
|
52
|
-
parseString(pString, pData
|
|
47
|
+
parseString(pString, pData)
|
|
53
48
|
{
|
|
54
|
-
return this.StringParser.parseString(pString, this.ParseTree, pData
|
|
49
|
+
return this.StringParser.parseString(pString, this.ParseTree, pData);
|
|
55
50
|
}
|
|
56
51
|
}
|
|
57
52
|
|
package/source/StringParser.js
CHANGED
|
@@ -1,15 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* String Parser
|
|
3
|
-
*
|
|
4
|
-
* @license MIT
|
|
5
|
-
*
|
|
6
3
|
* @author Steven Velozo <steven@velozo.com>
|
|
7
|
-
*
|
|
8
4
|
* @description Parse a string, properly processing each matched token in the word tree.
|
|
9
5
|
*/
|
|
10
6
|
|
|
11
|
-
let libAsync = require('async');
|
|
12
|
-
|
|
13
7
|
class StringParser
|
|
14
8
|
{
|
|
15
9
|
/**
|
|
@@ -102,69 +96,15 @@ class StringParser
|
|
|
102
96
|
{
|
|
103
97
|
// ... this is the end of a pattern, cut off the end tag and parse it.
|
|
104
98
|
// Trim the start and end tags off the output buffer now
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
// End pattern mode
|
|
112
|
-
pParserState.Pattern = false;
|
|
113
|
-
pParserState.PatternMatch = false;
|
|
114
|
-
}
|
|
115
|
-
else
|
|
116
|
-
{
|
|
117
|
-
pParserState.OutputBuffer = pParserState.Pattern.Parse(pParserState.OutputBuffer.substr(pParserState.Pattern.PatternStart.length, pParserState.OutputBuffer.length - (pParserState.Pattern.PatternStart.length+pParserState.Pattern.PatternEnd.length)), pData);
|
|
118
|
-
// Flush the output buffer.
|
|
119
|
-
this.flushOutputBuffer(pParserState);
|
|
120
|
-
// End pattern mode
|
|
121
|
-
pParserState.Pattern = false;
|
|
122
|
-
pParserState.PatternMatch = false;
|
|
123
|
-
}
|
|
99
|
+
pParserState.OutputBuffer = pParserState.Pattern.Parse(pParserState.OutputBuffer.substr(pParserState.Pattern.PatternStart.length, pParserState.OutputBuffer.length - (pParserState.Pattern.PatternStart.length+pParserState.Pattern.PatternEnd.length)), pData);
|
|
100
|
+
// Flush the output buffer.
|
|
101
|
+
this.flushOutputBuffer(pParserState);
|
|
102
|
+
// End pattern mode
|
|
103
|
+
pParserState.Pattern = false;
|
|
104
|
+
pParserState.PatternMatch = false;
|
|
124
105
|
}
|
|
125
106
|
}
|
|
126
107
|
|
|
127
|
-
checkPatternEndAsync (pParserState, pData, fCallback)
|
|
128
|
-
{
|
|
129
|
-
if ((pParserState.OutputBuffer.length >= pParserState.Pattern.PatternEnd.length+pParserState.Pattern.PatternStart.length) &&
|
|
130
|
-
(pParserState.OutputBuffer.substr(-pParserState.Pattern.PatternEnd.length) === pParserState.Pattern.PatternEnd))
|
|
131
|
-
{
|
|
132
|
-
// ... this is the end of a pattern, cut off the end tag and parse it.
|
|
133
|
-
// Trim the start and end tags off the output buffer now
|
|
134
|
-
if (pParserState.Pattern.isAsync)
|
|
135
|
-
{
|
|
136
|
-
return pParserState.Pattern.Parse(pParserState.OutputBuffer.substr(pParserState.Pattern.PatternStart.length, pParserState.OutputBuffer.length - (pParserState.Pattern.PatternStart.length+pParserState.Pattern.PatternEnd.length)), pData,
|
|
137
|
-
(pError, pAsyncOutput) =>
|
|
138
|
-
{
|
|
139
|
-
if (pError)
|
|
140
|
-
{
|
|
141
|
-
console.log(`Precedent ERROR: Async template error happened parsing ${pParserState.Pattern.PatternStart} ... ${pParserState.Pattern.PatternEnd}: ${pError}`);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
pParserState.OutputBuffer = pAsyncOutput;
|
|
145
|
-
// Flush the output buffer.
|
|
146
|
-
this.flushOutputBuffer(pParserState);
|
|
147
|
-
// End pattern mode
|
|
148
|
-
pParserState.Pattern = false;
|
|
149
|
-
pParserState.PatternMatch = false;
|
|
150
|
-
|
|
151
|
-
return fCallback();
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
else
|
|
155
|
-
{
|
|
156
|
-
pParserState.OutputBuffer = pParserState.Pattern.Parse(pParserState.OutputBuffer.substr(pParserState.Pattern.PatternStart.length, pParserState.OutputBuffer.length - (pParserState.Pattern.PatternStart.length+pParserState.Pattern.PatternEnd.length)), pData);
|
|
157
|
-
// Flush the output buffer.
|
|
158
|
-
this.flushOutputBuffer(pParserState);
|
|
159
|
-
// End pattern mode
|
|
160
|
-
pParserState.Pattern = false;
|
|
161
|
-
pParserState.PatternMatch = false;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
return fCallback();
|
|
166
|
-
}
|
|
167
|
-
|
|
168
108
|
/**
|
|
169
109
|
* Parse a character in the buffer.
|
|
170
110
|
* @method parseCharacter
|
|
@@ -204,82 +144,25 @@ class StringParser
|
|
|
204
144
|
}
|
|
205
145
|
}
|
|
206
146
|
|
|
207
|
-
parseCharacterAsync (pCharacter, pParserState, pData, fCallback)
|
|
208
|
-
{
|
|
209
|
-
// (1) If we aren't in a pattern match, and we aren't potentially matching, and this may be the start of a new pattern....
|
|
210
|
-
if (!pParserState.PatternMatch && pParserState.ParseTree.hasOwnProperty(pCharacter))
|
|
211
|
-
{
|
|
212
|
-
// ... assign the node as the matched node.
|
|
213
|
-
this.assignNode(pParserState.ParseTree[pCharacter], pParserState);
|
|
214
|
-
this.appendOutputBuffer(pCharacter, pParserState);
|
|
215
|
-
}
|
|
216
|
-
// (2) If we are in a pattern match (actively seeing if this is part of a new pattern token)
|
|
217
|
-
else if (pParserState.PatternMatch)
|
|
218
|
-
{
|
|
219
|
-
// If the pattern has a subpattern with this key
|
|
220
|
-
if (pParserState.PatternMatch.hasOwnProperty(pCharacter))
|
|
221
|
-
{
|
|
222
|
-
// Continue matching patterns.
|
|
223
|
-
this.assignNode(pParserState.PatternMatch[pCharacter], pParserState);
|
|
224
|
-
}
|
|
225
|
-
this.appendOutputBuffer(pCharacter, pParserState);
|
|
226
|
-
if (pParserState.Pattern)
|
|
227
|
-
{
|
|
228
|
-
// ... Check if this is the end of the pattern (if we are matching a valid pattern)...
|
|
229
|
-
return this.checkPatternEndAsync(pParserState, pData, fCallback);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
// (3) If we aren't in a pattern match or pattern, and this isn't the start of a new pattern (RAW mode)....
|
|
233
|
-
else
|
|
234
|
-
{
|
|
235
|
-
pParserState.Output += pCharacter;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
return fCallback(null);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
147
|
/**
|
|
242
148
|
* Parse a string for matches, and process any template segments that occur.
|
|
243
149
|
* @method parseString
|
|
244
150
|
* @param {string} pString - The string to parse.
|
|
245
151
|
* @param {Object} pParseTree - The parse tree to begin parsing from (usually root)
|
|
246
152
|
* @param {Object} pData - The data to pass to the function as a second parameter
|
|
247
|
-
* @param {function} fCallback - The callback function to call when the parse is complete
|
|
248
153
|
*/
|
|
249
|
-
parseString (pString, pParseTree, pData
|
|
154
|
+
parseString (pString, pParseTree, pData)
|
|
250
155
|
{
|
|
251
|
-
|
|
252
|
-
{
|
|
253
|
-
let tmpParserState = this.newParserState(pParseTree);
|
|
156
|
+
let tmpParserState = this.newParserState(pParseTree);
|
|
254
157
|
|
|
255
|
-
|
|
256
|
-
{
|
|
257
|
-
// TODO: This is not fast.
|
|
258
|
-
this.parseCharacter(pString[i], tmpParserState, pData, fCallback);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
this.flushOutputBuffer(tmpParserState);
|
|
262
|
-
|
|
263
|
-
return tmpParserState.Output;
|
|
264
|
-
}
|
|
265
|
-
else
|
|
158
|
+
for (var i = 0; i < pString.length; i++)
|
|
266
159
|
{
|
|
267
|
-
|
|
268
|
-
|
|
160
|
+
this.parseCharacter(pString[i], tmpParserState, pData);
|
|
161
|
+
}
|
|
269
162
|
|
|
270
|
-
|
|
271
|
-
(pCharacter, fCharacterCallback) =>
|
|
272
|
-
{
|
|
273
|
-
this.parseCharacterAsync(pCharacter, tmpParserState, pData, fCharacterCallback);
|
|
274
|
-
},
|
|
275
|
-
(pError) =>
|
|
276
|
-
{
|
|
277
|
-
// Flush the remaining data
|
|
278
|
-
this.flushOutputBuffer(tmpParserState);
|
|
279
|
-
fCallback(pError, tmpParserState.Output);
|
|
280
|
-
});
|
|
163
|
+
this.flushOutputBuffer(tmpParserState);
|
|
281
164
|
|
|
282
|
-
|
|
165
|
+
return tmpParserState.Output;
|
|
283
166
|
}
|
|
284
167
|
}
|
|
285
168
|
|
package/source/WordTree.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Word Tree
|
|
3
|
-
*
|
|
4
|
-
* @license MIT
|
|
5
|
-
*
|
|
6
3
|
* @author Steven Velozo <steven@velozo.com>
|
|
7
|
-
*
|
|
8
4
|
* @description Create a tree (directed graph) of Javascript objects, one character per object.
|
|
9
5
|
*/
|
|
10
6
|
|
|
@@ -61,38 +57,6 @@ class WordTree
|
|
|
61
57
|
tmpLeaf.Parse = (typeof(pParser) === 'function') ? pParser :
|
|
62
58
|
(typeof(pParser) === 'string') ? () => { return pParser; } :
|
|
63
59
|
(pData) => { return pData; };
|
|
64
|
-
tmpLeaf.isPromise = false;
|
|
65
|
-
|
|
66
|
-
return true;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/** Add a Pattern to the Parse Tree (asynchronous)
|
|
70
|
-
* @method addPattern
|
|
71
|
-
* @param {Object} pPatternStart - The starting string for the pattern (e.g. "${")
|
|
72
|
-
* @param {string} pPatternEnd - The ending string for the pattern (e.g. "}")
|
|
73
|
-
* @param {number} pParserAsync - The function (with an asynchronous callback) to parse if this is the matched pattern, once the Pattern End is met. If this is a string, a simple replacement occurs.
|
|
74
|
-
* @return {bool} True if adding the pattern was successful
|
|
75
|
-
*/
|
|
76
|
-
addPatternAsync (pPatternStart, pPatternEnd, pParserAsync)
|
|
77
|
-
{
|
|
78
|
-
if (pPatternStart.length < 1)
|
|
79
|
-
return false;
|
|
80
|
-
|
|
81
|
-
if ((typeof(pPatternEnd) === 'string') && (pPatternEnd.length < 1))
|
|
82
|
-
return false;
|
|
83
|
-
|
|
84
|
-
let tmpLeaf = this.ParseTree;
|
|
85
|
-
|
|
86
|
-
// Add the tree of leaves iteratively
|
|
87
|
-
for (var i = 0; i < pPatternStart.length; i++)
|
|
88
|
-
tmpLeaf = this.addChild(tmpLeaf, pPatternStart, i);
|
|
89
|
-
|
|
90
|
-
tmpLeaf.PatternStart = pPatternStart;
|
|
91
|
-
tmpLeaf.PatternEnd = ((typeof(pPatternEnd) === 'string') && (pPatternEnd.length > 0)) ? pPatternEnd : pPatternStart;
|
|
92
|
-
tmpLeaf.Parse = (typeof(pParserAsync) === 'function') ? pParserAsync :
|
|
93
|
-
(typeof(pParserAsync) === 'string') ? (pHash, pData, fCallback) => { fCallback(pParserPromise); } :
|
|
94
|
-
(pHash, pData, fCallback) => { return fCallback(pHash); };
|
|
95
|
-
tmpLeaf.isAsync = true;
|
|
96
60
|
|
|
97
61
|
return true;
|
|
98
62
|
}
|
package/test/Precedent_tests.js
CHANGED
|
@@ -30,12 +30,6 @@ var configPrecedent = (pModule) =>
|
|
|
30
30
|
pModule.addPattern('<^', '^>', (pHash, pData)=>{return `hash of [${pHash}] from pData is ${pData[pHash]}`});
|
|
31
31
|
// This just escapes out pairs of $
|
|
32
32
|
pModule.addPattern('$');
|
|
33
|
-
|
|
34
|
-
pModule.addPatternAsync('<%Async', '%>',
|
|
35
|
-
(pHash, pData, fCallback)=>
|
|
36
|
-
{
|
|
37
|
-
return fCallback(null, `ASYNC DATA IS [${pHash}]`);
|
|
38
|
-
});
|
|
39
33
|
};
|
|
40
34
|
|
|
41
35
|
suite
|
|
@@ -159,23 +153,6 @@ suite
|
|
|
159
153
|
}
|
|
160
154
|
);
|
|
161
155
|
test
|
|
162
|
-
(
|
|
163
|
-
'Async Function',
|
|
164
|
-
(fDone) =>
|
|
165
|
-
{
|
|
166
|
-
var tmpTestString = 'The <^SomeValue^> pData and Async <%AsyncThe Funny String%> up in here and a $comment$ as well.';
|
|
167
|
-
var tmpExpectedResult = 'The hash of [SomeValue] from pData is AirbornLight pData up in here and a comment as well.';
|
|
168
|
-
var testPrecedent = loadPrecedentModule();
|
|
169
|
-
configPrecedent(testPrecedent);
|
|
170
|
-
var tmpResult = testPrecedent.parseString(tmpTestString, {SomeValue:'AirbornLight'},
|
|
171
|
-
(pError, pValue) =>
|
|
172
|
-
{
|
|
173
|
-
Expect(pValue).to.equal('The hash of [SomeValue] from pData is AirbornLight pData and Async ASYNC DATA IS [The Funny String] up in here and a comment as well.');
|
|
174
|
-
return fDone();
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
);
|
|
178
|
-
test
|
|
179
156
|
(
|
|
180
157
|
'Bad pattern start parameter...',
|
|
181
158
|
(fDone) =>
|