1
- import * as Utils from './utils' ;
1
+ import { createFrame , extend , toString } from './utils' ;
2
2
import Exception from './exception' ;
3
+ import { registerDefaultHelpers } from './helpers' ;
3
4
4
5
export const VERSION = '3.0.1' ;
5
6
export const COMPILER_REVISION = 6 ;
@@ -13,10 +14,7 @@ export const REVISION_CHANGES = {
13
14
6 : '>= 2.0.0-beta.1'
14
15
} ;
15
16
16
- const isArray = Utils . isArray ,
17
- isFunction = Utils . isFunction ,
18
- toString = Utils . toString ,
19
- objectType = '[object Object]' ;
17
+ const objectType = '[object Object]' ;
20
18
21
19
export function HandlebarsEnvironment ( helpers , partials ) {
22
20
this . helpers = helpers || { } ;
@@ -34,7 +32,7 @@ HandlebarsEnvironment.prototype = {
34
32
registerHelper : function ( name , fn ) {
35
33
if ( toString . call ( name ) === objectType ) {
36
34
if ( fn ) { throw new Exception ( 'Arg not supported with multiple helpers' ) ; }
37
- Utils . extend ( this . helpers , name ) ;
35
+ extend ( this . helpers , name ) ;
38
36
} else {
39
37
this . helpers [ name ] = fn ;
40
38
}
@@ -45,7 +43,7 @@ HandlebarsEnvironment.prototype = {
45
43
46
44
registerPartial : function ( name , partial ) {
47
45
if ( toString . call ( name ) === objectType ) {
48
- Utils . extend ( this . partials , name ) ;
46
+ extend ( this . partials , name ) ;
49
47
} else {
50
48
if ( typeof partial === 'undefined' ) {
51
49
throw new Exception ( 'Attempting to register a partial as undefined' ) ;
@@ -58,167 +56,6 @@ HandlebarsEnvironment.prototype = {
58
56
}
59
57
} ;
60
58
61
- function registerDefaultHelpers ( instance ) {
62
- instance . registerHelper ( 'helperMissing' , function ( /* [args, ]options */ ) {
63
- if ( arguments . length === 1 ) {
64
- // A missing field in a {{foo}} construct.
65
- return undefined ;
66
- } else {
67
- // Someone is actually trying to call something, blow up.
68
- throw new Exception ( 'Missing helper: "' + arguments [ arguments . length - 1 ] . name + '"' ) ;
69
- }
70
- } ) ;
71
-
72
- instance . registerHelper ( 'blockHelperMissing' , function ( context , options ) {
73
- let inverse = options . inverse ,
74
- fn = options . fn ;
75
-
76
- if ( context === true ) {
77
- return fn ( this ) ;
78
- } else if ( context === false || context == null ) {
79
- return inverse ( this ) ;
80
- } else if ( isArray ( context ) ) {
81
- if ( context . length > 0 ) {
82
- if ( options . ids ) {
83
- options . ids = [ options . name ] ;
84
- }
85
-
86
- return instance . helpers . each ( context , options ) ;
87
- } else {
88
- return inverse ( this ) ;
89
- }
90
- } else {
91
- if ( options . data && options . ids ) {
92
- let data = createFrame ( options . data ) ;
93
- data . contextPath = Utils . appendContextPath ( options . data . contextPath , options . name ) ;
94
- options = { data : data } ;
95
- }
96
-
97
- return fn ( context , options ) ;
98
- }
99
- } ) ;
100
-
101
- instance . registerHelper ( 'each' , function ( context , options ) {
102
- if ( ! options ) {
103
- throw new Exception ( 'Must pass iterator to #each' ) ;
104
- }
105
-
106
- let fn = options . fn ,
107
- inverse = options . inverse ,
108
- i = 0 ,
109
- ret = '' ,
110
- data ,
111
- contextPath ;
112
-
113
- if ( options . data && options . ids ) {
114
- contextPath = Utils . appendContextPath ( options . data . contextPath , options . ids [ 0 ] ) + '.' ;
115
- }
116
-
117
- if ( isFunction ( context ) ) { context = context . call ( this ) ; }
118
-
119
- if ( options . data ) {
120
- data = createFrame ( options . data ) ;
121
- }
122
-
123
- function execIteration ( field , index , last ) {
124
- if ( data ) {
125
- data . key = field ;
126
- data . index = index ;
127
- data . first = index === 0 ;
128
- data . last = ! ! last ;
129
-
130
- if ( contextPath ) {
131
- data . contextPath = contextPath + field ;
132
- }
133
- }
134
-
135
- ret = ret + fn ( context [ field ] , {
136
- data : data ,
137
- blockParams : Utils . blockParams ( [ context [ field ] , field ] , [ contextPath + field , null ] )
138
- } ) ;
139
- }
140
-
141
- if ( context && typeof context === 'object' ) {
142
- if ( isArray ( context ) ) {
143
- for ( let j = context . length ; i < j ; i ++ ) {
144
- execIteration ( i , i , i === context . length - 1 ) ;
145
- }
146
- } else {
147
- let priorKey ;
148
-
149
- for ( let key in context ) {
150
- if ( context . hasOwnProperty ( key ) ) {
151
- // We're running the iterations one step out of sync so we can detect
152
- // the last iteration without have to scan the object twice and create
153
- // an itermediate keys array.
154
- if ( priorKey !== undefined ) {
155
- execIteration ( priorKey , i - 1 ) ;
156
- }
157
- priorKey = key ;
158
- i ++ ;
159
- }
160
- }
161
- if ( priorKey ) {
162
- execIteration ( priorKey , i - 1 , true ) ;
163
- }
164
- }
165
- }
166
-
167
- if ( i === 0 ) {
168
- ret = inverse ( this ) ;
169
- }
170
-
171
- return ret ;
172
- } ) ;
173
-
174
- instance . registerHelper ( 'if' , function ( conditional , options ) {
175
- if ( isFunction ( conditional ) ) { conditional = conditional . call ( this ) ; }
176
-
177
- // Default behavior is to render the positive path if the value is truthy and not empty.
178
- // The `includeZero` option may be set to treat the condtional as purely not empty based on the
179
- // behavior of isEmpty. Effectively this determines if 0 is handled by the positive path or negative.
180
- if ( ( ! options . hash . includeZero && ! conditional ) || Utils . isEmpty ( conditional ) ) {
181
- return options . inverse ( this ) ;
182
- } else {
183
- return options . fn ( this ) ;
184
- }
185
- } ) ;
186
-
187
- instance . registerHelper ( 'unless' , function ( conditional , options ) {
188
- return instance . helpers [ 'if' ] . call ( this , conditional , { fn : options . inverse , inverse : options . fn , hash : options . hash } ) ;
189
- } ) ;
190
-
191
- instance . registerHelper ( 'with' , function ( context , options ) {
192
- if ( isFunction ( context ) ) { context = context . call ( this ) ; }
193
-
194
- let fn = options . fn ;
195
-
196
- if ( ! Utils . isEmpty ( context ) ) {
197
- let data = options . data ;
198
- if ( options . data && options . ids ) {
199
- data = createFrame ( options . data ) ;
200
- data . contextPath = Utils . appendContextPath ( options . data . contextPath , options . ids [ 0 ] ) ;
201
- }
202
-
203
- return fn ( context , {
204
- data : data ,
205
- blockParams : Utils . blockParams ( [ context ] , [ data && data . contextPath ] )
206
- } ) ;
207
- } else {
208
- return options . inverse ( this ) ;
209
- }
210
- } ) ;
211
-
212
- instance . registerHelper ( 'log' , function ( message , options ) {
213
- let level = options . data && options . data . level != null ? parseInt ( options . data . level , 10 ) : 1 ;
214
- instance . log ( level , message ) ;
215
- } ) ;
216
-
217
- instance . registerHelper ( 'lookup' , function ( obj , field ) {
218
- return obj && obj [ field ] ;
219
- } ) ;
220
- }
221
-
222
59
export let logger = {
223
60
methodMap : { 0 : 'debug' , 1 : 'info' , 2 : 'warn' , 3 : 'error' } ,
224
61
@@ -240,8 +77,4 @@ export let logger = {
240
77
241
78
export let log = logger . log ;
242
79
243
- export function createFrame ( object ) {
244
- let frame = Utils . extend ( { } , object ) ;
245
- frame . _parent = object ;
246
- return frame ;
247
- }
80
+ export { createFrame } ;
0 commit comments