1 | 'use strict';
|
2 |
|
3 | function helper(paper) {
|
4 | paper.handlebars.registerHelper('replace', function (needle, haystack) {
|
5 | const options = arguments[arguments.length - 1];
|
6 |
|
7 | if (typeof needle !== 'string') {
|
8 | return options.inverse(this);
|
9 | }
|
10 |
|
11 | const regex = new RegExp(escapeRegex(needle), 'g');
|
12 |
|
13 |
|
14 | if (typeof haystack === 'string' && regex.test(haystack)) {
|
15 | return haystack.replace(regex, options.fn(this));
|
16 | } else {
|
17 | return options.inverse(this);
|
18 | }
|
19 | });
|
20 | }
|
21 |
|
22 |
|
23 | function escapeRegex(string) {
|
24 | return string.replace(/[-\\^$*+?.()|[\]{}]/g, "\\$&");
|
25 | }
|
26 |
|
27 | module.exports = helper;
|