1 | 'use strict';
|
2 | const utils = require('handlebars-utils');
|
3 | const max_length = 1024;
|
4 | const max_keys = 50;
|
5 |
|
6 | function helper(paper) {
|
7 | paper.handlebars.registerHelper('getVar', function (key) {
|
8 | if (!utils.isString(key)) {
|
9 | throw new Error("getVar helper key must be a string");
|
10 | }
|
11 |
|
12 | return paper.variables[key]
|
13 | });
|
14 |
|
15 | paper.handlebars.registerHelper('assignVar', function (key, value) {
|
16 |
|
17 | if (!utils.isString(key)) {
|
18 | throw new Error("assignVar helper key must be a string");
|
19 | }
|
20 |
|
21 |
|
22 | if (!utils.isString(value) && !Number.isInteger(value)) {
|
23 | throw new Error("assignVar helper value must be a string or a number (integer)");
|
24 | }
|
25 |
|
26 |
|
27 | if (utils.isString(value) && value.length >= max_length) {
|
28 | throw new Error(`assignVar helper value must be less than ${max_length} characters,
|
29 | but a ${value.length} character value was set to ${key}`);
|
30 | }
|
31 |
|
32 |
|
33 | if (Object.keys(paper.variables).length >= max_keys) {
|
34 | throw new Error(`Unique keys in variable storage may not exceed ${max_keys} in total`);
|
35 | }
|
36 |
|
37 |
|
38 | paper.variables[key] = value;
|
39 | });
|
40 |
|
41 | paper.handlebars.registerHelper('incrementVar', function (key) {
|
42 | if (!utils.isString(key)) {
|
43 | throw new Error("incrementVar helper key must be a string");
|
44 | }
|
45 |
|
46 | if (Number.isInteger(paper.variables[key])) {
|
47 |
|
48 | paper.variables[key] += 1;
|
49 | } else {
|
50 |
|
51 | if (Object.keys(paper.variables).length >= max_keys) {
|
52 | throw new Error(`Unique keys in variable storage may not exceed ${max_keys} in total`);
|
53 | }
|
54 |
|
55 | paper.variables[key] = 0;
|
56 | }
|
57 |
|
58 |
|
59 | return paper.variables[key];
|
60 | });
|
61 |
|
62 | paper.handlebars.registerHelper('decrementVar', function (key) {
|
63 | if (!utils.isString(key)) {
|
64 | throw new Error("decrementVar helper key must be a string");
|
65 | }
|
66 |
|
67 | if (Number.isInteger(paper.variables[key])) {
|
68 |
|
69 | paper.variables[key] -= 1;
|
70 | } else {
|
71 |
|
72 | if (Object.keys(paper.variables).length >= max_keys) {
|
73 | throw new Error(`Unique keys in variable storage may not exceed ${max_keys} in total`);
|
74 | }
|
75 |
|
76 | paper.variables[key] = 0;
|
77 | }
|
78 |
|
79 |
|
80 | return paper.variables[key];
|
81 | });
|
82 | }
|
83 |
|
84 | module.exports = helper;
|