1 |
|
2 |
|
3 | /**
|
4 | * @description Given a "loop" parameter from an "each" tag, parses out the param names and expression to be looped.
|
5 | *
|
6 | * @method parseLoopStatement
|
7 | *
|
8 | * @param {String} input Input
|
9 | *
|
10 | * @return {Object} {} Keys && Expression
|
11 | */
|
12 | function parseLoopStatement (input) {
|
13 | // try to find ` in ` keyword
|
14 | const inKeywordIndex = input.search(/\sin\s/)
|
15 |
|
16 | // if we reach the end of the string without getting "in", it's an error
|
17 | if (inKeywordIndex === -1) {
|
18 | throw new Error("Loop statement lacking 'in' keyword")
|
19 | }
|
20 |
|
21 | // expression is always after `in` keyword
|
22 | const expression = input.substr(inKeywordIndex + 4)
|
23 |
|
24 | // keys is always before `in` keyword
|
25 | const keys = input.substr(0, inKeywordIndex).split(',')
|
26 |
|
27 | for (let i = 0; i < keys.length; i++) {
|
28 | keys[i] = keys[i].trim()
|
29 | }
|
30 |
|
31 | return { keys, expression }
|
32 | }
|
33 |
|
34 | /**
|
35 | * @module loops
|
36 | *
|
37 | * @type {Function}
|
38 | */
|
39 | module.exports = parseLoopStatement
|