1 | 'use strict';
|
2 |
|
3 | var _ = require('lodash');
|
4 |
|
5 | function helper(paper) {
|
6 | paper.handlebars.registerHelper('for', function (from, to, context) {
|
7 | const options = arguments[arguments.length - 1];
|
8 | const maxIterations = 100;
|
9 | var output = '';
|
10 |
|
11 | function isOptions(obj) {
|
12 | return _.isObject(obj) && obj.fn;
|
13 | }
|
14 |
|
15 | if (isOptions(to)) {
|
16 | context = {};
|
17 | to = from;
|
18 | from = 1;
|
19 |
|
20 | } else if (isOptions(context)) {
|
21 | if (_.isObject(to)) {
|
22 | context = to;
|
23 | to = from;
|
24 | from = 1;
|
25 | }
|
26 | }
|
27 |
|
28 | if (to < from) {
|
29 | return;
|
30 | }
|
31 |
|
32 | from = parseInt(from, 10);
|
33 | to = parseInt(to, 10);
|
34 |
|
35 | if ((to - from) >= maxIterations) {
|
36 | to = from + maxIterations - 1;
|
37 | }
|
38 |
|
39 | for (var i = from; i <= to; i++) {
|
40 | context.$index = i;
|
41 | output += options.fn(context);
|
42 | }
|
43 |
|
44 | return output;
|
45 | });
|
46 | }
|
47 |
|
48 | module.exports = helper;
|