Skip to content

Commit 65d410a

Browse files
committed
chore: apply code review
1 parent a11b7b3 commit 65d410a

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

domUtil/template.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ var EXPRESSION_REGEXP = /{{\s?(\/?[a-zA-Z0-9_.@[\] ]+)\s?}}/g;
1515
var BRACKET_REGEXP = /^([a-zA-Z0-9_@]+)\[([a-zA-Z0-9_@]+)\]$/;
1616
var NUMBER_REGEXP = /^-?\d+\.?\d*$/;
1717

18+
var EXPRESSION_INTERVAL = 2;
19+
1820
var BLOCK_HELPERS = {
1921
'if': handleIf,
2022
'each': handleEach,
@@ -25,7 +27,7 @@ var BLOCK_HELPERS = {
2527
* Find value in the context by an expression.
2628
* @param {string} exp - an expression
2729
* @param {object} context - context
28-
* @return {*}
30+
* @returns {*}
2931
* @private
3032
*/
3133
function getValueFromContext(exp, context) {
@@ -50,7 +52,7 @@ function getValueFromContext(exp, context) {
5052
* Extract elseif and else expressions.
5153
* @param {Array.<string>} ifExps - args of if expression
5254
* @param {Array.<string>} sourcesInsideBlock - sources inside if block
53-
* @return {object} - exps: expressions of if, elseif, and else / sourcesInsideIf: sources inside if, elseif, and else block.
55+
* @returns {object} - exps: expressions of if, elseif, and else / sourcesInsideIf: sources inside if, elseif, and else block.
5456
* @private
5557
*/
5658
function extractElseif(ifExps, sourcesInsideBlock) {
@@ -82,7 +84,7 @@ function extractElseif(ifExps, sourcesInsideBlock) {
8284
* @param {Array.<string>} exps - array of expressions split by spaces
8385
* @param {Array.<string>} sourcesInsideBlock - array of sources inside the if block
8486
* @param {object} context - context
85-
* @return {string}
87+
* @returns {string}
8688
* @private
8789
*/
8890
function handleIf(exps, sourcesInsideBlock, context) {
@@ -107,7 +109,7 @@ function handleIf(exps, sourcesInsideBlock, context) {
107109
* @param {Array.<string>} exps - array of expressions split by spaces
108110
* @param {Array.<string>} sourcesInsideBlock - array of sources inside the each block
109111
* @param {object} context - context
110-
* @return {string}
112+
* @returns {string}
111113
* @private
112114
*/
113115
function handleEach(exps, sourcesInsideBlock, context) {
@@ -132,7 +134,7 @@ function handleEach(exps, sourcesInsideBlock, context) {
132134
* @param {Array.<string>} exps - array of expressions split by spaces
133135
* @param {Array.<string>} sourcesInsideBlock - array of sources inside the with block
134136
* @param {object} context - context
135-
* @return {string}
137+
* @returns {string}
136138
* @private
137139
*/
138140
function handleWith(exps, sourcesInsideBlock, context) {
@@ -151,7 +153,7 @@ function handleWith(exps, sourcesInsideBlock, context) {
151153
* @param {Array.<string>} sources - array of sources
152154
* @param {number} start - index of start block
153155
* @param {number} end - index of end block
154-
* @return {Array.<string>}
156+
* @returns {Array.<string>}
155157
* @private
156158
*/
157159
function extractSourcesInsideBlock(sources, start, end) {
@@ -162,18 +164,18 @@ function extractSourcesInsideBlock(sources, start, end) {
162164
}
163165

164166
/**
165-
* Concatenate the elements between previous and next of the base element in place.
167+
* Concatenate the strings between previous and next of the base string in place.
166168
* @param {Array.<string>} sources - array of sources
167-
* @param {number} index - index of base element
169+
* @param {number} index - index of base string
168170
* @private
169171
*/
170-
function joinBetween(source, index) {
172+
function concatPrevAndNextString(source, index) {
171173
var start = Math.max(index - 1, 0);
172174
var end = Math.min(index + 1, source.length - 1);
173-
var deleteCount = end - start + 1;
174-
var result = source.splice(start, deleteCount).join('');
175+
var deletedCount = end - start + 1;
176+
var result = source.splice(start, deletedCount).join('');
175177

176-
if (deleteCount < 3) {
178+
if (deletedCount < 3) {
177179
source.splice(start, 0, '', result);
178180
} else {
179181
source.splice(start, 0, result);
@@ -185,7 +187,7 @@ function joinBetween(source, index) {
185187
* @param {string} helperKeyword - helper keyword (ex. if, each, with)
186188
* @param {Array.<string>} sourcesToEnd - array of sources after the starting block
187189
* @param {object} context - context
188-
* @return {Array.<string>}
190+
* @returns {Array.<string>}
189191
* @private
190192
*/
191193
function handleBlockHelper(helperKeyword, sourcesToEnd, context) {
@@ -209,15 +211,15 @@ function handleBlockHelper(helperKeyword, sourcesToEnd, context) {
209211
extractSourcesInsideBlock(sourcesToEnd, startBlockIndex, index),
210212
context
211213
);
212-
joinBetween(sourcesToEnd, startBlockIndex);
213-
index = startBlockIndex - 2;
214+
concatPrevAndNextString(sourcesToEnd, startBlockIndex);
215+
index = startBlockIndex - EXPRESSION_INTERVAL;
214216
}
215217

216-
index += 2;
218+
index += EXPRESSION_INTERVAL;
217219
expression = sourcesToEnd[index];
218-
} while (helperCount !== 0 && isString(expression));
220+
} while (helperCount && isString(expression));
219221

220-
if (helperCount !== 0) {
222+
if (helperCount) {
221223
throw Error(helperKeyword + ' needs {{/' + helperKeyword + '}} expression.');
222224
}
223225

@@ -229,14 +231,14 @@ function handleBlockHelper(helperKeyword, sourcesToEnd, context) {
229231
* If helper is not a function, return helper itself.
230232
* @param {Array.<string>} exps - array of expressions split by spaces (first element: helper)
231233
* @param {object} context - context
232-
* @return {string}
234+
* @returns {string}
233235
* @private
234236
*/
235237
function handleExpression(exps, context) {
236238
var result = getValueFromContext(exps[0], context);
237239

238240
if (result instanceof Function) {
239-
result = executeFunction(result, exps.slice(1), context);
241+
return executeFunction(result, exps.slice(1), context);
240242
}
241243

242244
return result;
@@ -247,7 +249,7 @@ function handleExpression(exps, context) {
247249
* @param {Function} helper - helper function
248250
* @param {Array.<string>} argExps - expressions of arguments
249251
* @param {object} context - context
250-
* @return {string} - result of executing the function with arguments
252+
* @returns {string} - result of executing the function with arguments
251253
* @private
252254
*/
253255
function executeFunction(helper, argExps, context) {
@@ -263,7 +265,7 @@ function executeFunction(helper, argExps, context) {
263265
* Get a result of compiling an expression with the context.
264266
* @param {Array.<string>} sources - array of sources split by regexp of expression.
265267
* @param {object} context - context
266-
* @return {Array.<string>} - array of sources that bind with its context
268+
* @returns {Array.<string>} - array of sources that bind with its context
267269
* @private
268270
*/
269271
function compile(sources, context) {
@@ -282,7 +284,7 @@ function compile(sources, context) {
282284
sources[index] = handleExpression(exps, context);
283285
}
284286

285-
index += 2;
287+
index += EXPRESSION_INTERVAL;
286288
expression = sources[index];
287289
}
288290

@@ -304,7 +306,7 @@ function compile(sources, context) {
304306
* 3) 'with ... as ...' provides an alias.
305307
* @param {string} text - text with expressions
306308
* @param {object} context - context
307-
* @return {string} - text that bind with its context
309+
* @returns {string} - text that bind with its context
308310
* @memberof module:domUtil
309311
* @example
310312
* var template = require('tui-code-snippet/domUtil/template');

0 commit comments

Comments
 (0)