fable 3.0.12 → 3.0.17

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.
@@ -1,108 +0,0 @@
1
- class FableUtility
2
- {
3
- // Underscore and lodash have a behavior, _.template, which compiles a
4
- // string-based template with code snippets into simple executable pieces,
5
- // with the added twist of returning a precompiled function ready to go.
6
- //
7
- // NOTE: This does not implement underscore escape expressions
8
- // NOTE: This does not implement underscore magic browser variable assignment
9
- //
10
- // This is an implementation of that.
11
- // TODO: Make this use precedent, add configuration, add debugging.
12
- constructor(pFable, pTemplateText)
13
- {
14
- this.fable = pFable;
15
-
16
- // These are the exact regex's used in lodash/underscore
17
- // TODO: Switch this to precedent
18
- this.Matchers = (
19
- {
20
- Evaluate: /<%([\s\S]+?)%>/g,
21
- Interpolate: /<%=([\s\S]+?)%>/g,
22
- Escaper: /\\|'|\r|\n|\t|\u2028|\u2029/g,
23
- Unescaper: /\\(\\|'|r|n|t|u2028|u2029)/g,
24
- // This is how underscore does it, so we are keeping it for now.
25
- GuaranteedNonMatch: /.^/
26
- });
27
-
28
- // This is a helper for the escaper and unescaper functions.
29
- // Right now we are going to keep what underscore is doing, but, not forever.
30
- this.templateEscapes = {
31
- '\\': '\\',
32
- "'": "'",
33
- 'r': '\r',
34
- '\r': 'r',
35
- 'n': '\n',
36
- '\n': 'n',
37
- 't': '\t',
38
- '\t': 't',
39
- 'u2028': '\u2028',
40
- '\u2028': 'u2028',
41
- 'u2029': '\u2029',
42
- '\u2029': 'u2029'
43
- };
44
-
45
- // This is defined as such to underscore that it is a dynamic programming
46
- // function on this class.
47
- this.renderFunction = ()=>{return ``};
48
- }
49
-
50
- // Underscore and lodash have a behavior, _.extend, which merges objects.
51
- // Now that es6 gives us this, use the native thingy.
52
- extend(pDestinationObject, ...pSourceObjects)
53
- {
54
- return Object.assign(pDestinationObject, ...pSourceObjects);
55
- }
56
-
57
- renderTemplate(pData)
58
- {
59
- return this.renderFunction(pData);
60
- }
61
-
62
- templateFunction(pData)
63
- {
64
- let fRenderTemplateBound = this.renderTemplate.bind(this);
65
- return fRenderTemplateBound;
66
- }
67
-
68
- buildTemplateFunction(pTemplateText, pData)
69
- {
70
- // For now this is being kept in a weird form ... this is to mimic the old
71
- // underscore code until this is rewritten using precedent.
72
- this.TemplateSource = "__p+='" + pTemplateText
73
- .replace(this.Matchers.Escaper,
74
- (pMatch)=>
75
- {
76
- return `\\${this.templateEscapes[pMatch]}`;
77
- })
78
- .replace(this.Matchers.Interpolate || this.Matchers.GuaranteedNonMatch,
79
- (pMatch, pCode) =>
80
- {
81
- return `'+\n(${decodeURIComponent(pCode)})+\n'`;
82
- })
83
- .replace(this.Matchers.Evaluate || this.Matchers.GuaranteedNonMatch,
84
- (pMatch, pCode) =>
85
- {
86
- return `';\n${decodeURIComponent(pCode)}\n;__p+='`;
87
- }) + `';\n`;
88
-
89
-
90
- this.TemplateSource = `with(pTemplateDataObject||{}){\n${this.TemplateSource}}\n`;
91
- this.TemplateSource = `var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};\n${this.TemplateSource}return __p;\n`;
92
-
93
- this.renderFunction = new Function('pTemplateDataObject', this.TemplateSource);
94
-
95
- if (typeof(pData) != 'undefined')
96
- {
97
- return this.renderFunction(pData);
98
- }
99
-
100
- // Provide the compiled function source as a convenience for build time
101
- // precompilation.
102
- this.TemplateSourceCompiled = 'function(obj){\n' + this.TemplateSource + '}';
103
-
104
- return this.templateFunction();
105
- }
106
- }
107
-
108
- module.exports = FableUtility;