@restorecommerce/handlebars-helperized 0.1.15 → 0.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,33 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [0.1.18](https://github.com/restorecommerce/handlebars-helperized/compare/@restorecommerce/handlebars-helperized@0.1.17...@restorecommerce/handlebars-helperized@0.1.18) (2022-02-22)
7
+
8
+ **Note:** Version bump only for package @restorecommerce/handlebars-helperized
9
+
10
+
11
+
12
+
13
+
14
+ ## [0.1.17](https://github.com/restorecommerce/handlebars-helperized/compare/@restorecommerce/handlebars-helperized@0.1.16...@restorecommerce/handlebars-helperized@0.1.17) (2022-02-15)
15
+
16
+
17
+ ### Bug Fixes
18
+
19
+ * **handlebars-helperized:** made layout, style, opts and customhelpers as optional parameters ([a4e3f6d](https://github.com/restorecommerce/handlebars-helperized/commit/a4e3f6d8879f5f6581ab8cac27c8fa3f588a231c))
20
+
21
+
22
+
23
+
24
+
25
+ ## [0.1.16](https://github.com/restorecommerce/handlebars-helperized/compare/@restorecommerce/handlebars-helperized@0.1.15...@restorecommerce/handlebars-helperized@0.1.16) (2022-02-14)
26
+
27
+ **Note:** Version bump only for package @restorecommerce/handlebars-helperized
28
+
29
+
30
+
31
+
32
+
6
33
  ## [0.1.15](https://github.com/restorecommerce/handlebars-helperized/compare/@restorecommerce/handlebars-helperized@0.1.14...@restorecommerce/handlebars-helperized@0.1.15) (2021-08-23)
7
34
 
8
35
 
package/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) Invend GmbH and other contributors.
1
+ Copyright (c) n-fuse GmbH and other contributors.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of
4
4
  this software and associated documentation files (the "Software"), to deal in
@@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
16
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
17
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
18
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
- SOFTWARE.
19
+ SOFTWARE.
package/README.md CHANGED
@@ -15,7 +15,8 @@ extension `t`.
15
15
 
16
16
  Additional custom helpers can be provided from the service which makes use of
17
17
  this module by passing an array which contains the file's path relative to the
18
- root folder on class initialization.
18
+ root folder on class initialization.
19
+
19
20
  ```js
20
21
  // example
21
22
  const filePathList = ['./test/handlebars/helper-loud.js'];
@@ -0,0 +1 @@
1
+ declare let customHandlebarsExtensions: (hbs: any, opts: any) => void;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ let customHandlebarsExtensions = (hbs, opts) => {
3
+ // increment a given numerical string by one
4
+ hbs.registerHelper('increment', (value, hash) => {
5
+ const toIncrement = parseInt(value, 10);
6
+ if (isNaN(toIncrement))
7
+ return '0';
8
+ return toIncrement + 1;
9
+ });
10
+ };
11
+ module.exports = customHandlebarsExtensions;
@@ -0,0 +1,3 @@
1
+ declare const tripleStache: RegExp;
2
+ declare const doubleStache: RegExp;
3
+ declare let localizationHandlebarsExtension: (hbs: any, opts: any) => void;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ const tripleStache = /\{\{\{\s*(.*?)\s*\}\}\}/g;
3
+ const doubleStache = /\{\{\s*(.*?)\s*\}\}/g;
4
+ let localizationHandlebarsExtension = (hbs, opts) => {
5
+ hbs.registerHelper('t', (key, hash = {}) => {
6
+ const locale = opts.locale;
7
+ let result = opts.texts[key] || key;
8
+ result = (typeof result === 'object') ? result[locale] : result;
9
+ if (!result)
10
+ return 'Missing translation for ' + key;
11
+ const data = hash.data.root;
12
+ return result.replace(doubleStache, (i, match) => {
13
+ return data[match] ? data[match] : ('{{' + match + '}}');
14
+ }).replace(tripleStache, (i, match) => {
15
+ // TODO: escaping
16
+ return data[match] ? data[match] : ('{{{' + match + '}}}');
17
+ });
18
+ });
19
+ };
20
+ module.exports = localizationHandlebarsExtension;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const moment_timezone_1 = __importDefault(require("moment-timezone"));
7
+ let momentHandlebarsExtension = (hbs, opts) => {
8
+ // Output point in time relative to current point in time
9
+ // for example: '1h ago'
10
+ hbs.registerHelper('ago', (value, options) => {
11
+ let v = value;
12
+ if (options.isSeconds) {
13
+ // the given property represents seconds since UNIX epoch, so we
14
+ // multiply by 1000 to get the date in milliseconds since UNIX epoch
15
+ v *= 1000;
16
+ }
17
+ const tz = options.hash.timezone || moment_timezone_1.default.tz.guess();
18
+ return moment_timezone_1.default.tz(v, tz).locale(opts.locale).fromNow();
19
+ });
20
+ // Date format short
21
+ hbs.registerHelper('df', (value, options) => {
22
+ const tz = options.hash.timezone || moment_timezone_1.default.tz.guess();
23
+ return moment_timezone_1.default.tz(value, tz).locale(opts.locale).format('L');
24
+ });
25
+ // Date format Long
26
+ hbs.registerHelper('dfl', (value, options) => {
27
+ const tz = options.hash.timezone || moment_timezone_1.default.tz.guess();
28
+ return moment_timezone_1.default.tz(value, tz).locale(opts.locale).format('LL');
29
+ });
30
+ // Time format
31
+ hbs.registerHelper('tf', (value, options) => {
32
+ const tz = options.hash.timezone || moment_timezone_1.default.tz.guess();
33
+ return moment_timezone_1.default.tz(value, tz).locale(opts.locale).format('LT');
34
+ });
35
+ // Date-Time format
36
+ hbs.registerHelper('dtf', (value, options) => {
37
+ const tz = options.hash.timezone || moment_timezone_1.default.tz.guess();
38
+ return moment_timezone_1.default.tz(value, tz).locale(opts.locale).format('LLL');
39
+ });
40
+ // Date-Time format with given format
41
+ hbs.registerHelper('dff', (value, options) => {
42
+ const format = options.hash.format || '';
43
+ const tz = options.hash.timezone || moment_timezone_1.default.tz.guess();
44
+ return (0, moment_timezone_1.default)(value).tz(tz).format(format);
45
+ });
46
+ // Duration formatting
47
+ // Warning, localization should not be used with this:
48
+ // While the pattern `D` yields the number of days
49
+ // `dddd` would yield to a name of a weekday which is
50
+ // of course not applicapble for a duration.
51
+ //
52
+ // A format can be a template string with this syntax:
53
+ // '[it\'s] D [days and] h [hours]'
54
+ hbs.registerHelper('duf', (value, options) => {
55
+ const format = options.hash.format || '';
56
+ const dur = moment_timezone_1.default.duration(value);
57
+ // eslint-disable-next-line
58
+ return (0, moment_timezone_1.default)(dur._data).format(format);
59
+ });
60
+ };
61
+ module.exports = momentHandlebarsExtension;
@@ -0,0 +1,3 @@
1
+ declare const numbro: any;
2
+ declare const allLanguages: any;
3
+ declare let numbroHandlebarsExtension: (hbs: any, opts: any) => void;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ const numbro = require('numbro');
3
+ const allLanguages = require('numbro/dist/languages.min');
4
+ Object.values(allLanguages).forEach((data) => {
5
+ numbro.registerLanguage(data);
6
+ });
7
+ let numbroHandlebarsExtension = (hbs, opts) => {
8
+ const locale = opts.locale.replace('_', '-');
9
+ // For numeric values without decimals
10
+ hbs.registerHelper('nfn', (value, hash) => {
11
+ numbro.setLanguage(locale);
12
+ return numbro(value).format({
13
+ thousandSeparated: true,
14
+ mantissa: 0
15
+ });
16
+ });
17
+ // For currency based numeric values
18
+ hbs.registerHelper('nfc', (value, hash) => {
19
+ const lhash = hash.hash;
20
+ numbro.setLanguage(locale);
21
+ // Don't use formatCurrency as it does not allow control over the currency
22
+ // symbol in confunction with locales
23
+ let formatted = numbro(value).format({
24
+ mantissa: 2,
25
+ thousandSeparated: true
26
+ });
27
+ const cs = lhash.cs;
28
+ if (cs === undefined)
29
+ return formatted;
30
+ if (lhash.csPos === 'postfix') {
31
+ formatted += cs;
32
+ }
33
+ else {
34
+ formatted = cs + formatted;
35
+ }
36
+ return formatted;
37
+ });
38
+ // For byte based numeric values
39
+ hbs.registerHelper('nfb', (value, hash) => {
40
+ numbro.setLanguage(locale);
41
+ return numbro(value).format({
42
+ output: 'byte',
43
+ base: 'binary'
44
+ });
45
+ });
46
+ };
47
+ module.exports = numbroHandlebarsExtension;
package/lib/index.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ declare class Renderer {
2
+ /**
3
+ @param {String} template the template
4
+ @param {String} layout the optional layout
5
+ @param {String} style the style
6
+ @param {Object} opts handlebars options
7
+ @param {Array} customHelpersList contains a list of custom helpers (optional)
8
+ */
9
+ hbs: any;
10
+ style: string | undefined;
11
+ template: any;
12
+ constructor(template: string, layout?: string | undefined, style?: string | undefined, opts?: object | undefined, customHelpersList?: any);
13
+ /**
14
+ @param {Object} context: required data for the placeholders
15
+ @return {String} html
16
+ */
17
+ render(context: Object): any;
18
+ }
19
+ export default Renderer;
package/lib/index.js ADDED
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const juice_1 = __importDefault(require("juice"));
7
+ const defaults_1 = __importDefault(require("lodash/defaults"));
8
+ const defaultOpts = {
9
+ locale: 'en_US',
10
+ texts: {}
11
+ };
12
+ // Initializes and configures a custom handlebars instance
13
+ const init = (options, customHelpersList) => {
14
+ // default values if nothing given
15
+ const opts = (0, defaults_1.default)(options, defaultOpts);
16
+ // the basic building block is the handlebars rendering engine
17
+ const hbs = require('handlebars');
18
+ // more functionality directly added via custom plugins from ./lib
19
+ require('./helpers/l10n-helpers.js')(hbs, opts); // localization
20
+ require('./helpers/numbro-helpers.js')(hbs, opts); // numbers & currencies
21
+ require('./helpers/moment-helpers.js')(hbs, opts); // dates, times & durations
22
+ require('./helpers/custom-helpers.js')(hbs, opts); // everything else
23
+ // add custom helpers from rendering-srv
24
+ if (customHelpersList) {
25
+ for (let customHelper of customHelpersList) {
26
+ const filePath = customHelper;
27
+ require(filePath)(hbs, opts);
28
+ }
29
+ }
30
+ // extend rendering with layout functionality
31
+ const handlebarsLayouts = require('handlebars-layouts');
32
+ handlebarsLayouts.register(hbs);
33
+ return hbs;
34
+ };
35
+ class Renderer {
36
+ constructor(template, layout, style, opts, customHelpersList) {
37
+ this.hbs = init(opts, customHelpersList);
38
+ this.style = style;
39
+ if (layout) {
40
+ this.hbs.registerPartial('layout', layout);
41
+ }
42
+ this.template = this.hbs.compile(template);
43
+ }
44
+ /**
45
+ @param {Object} context: required data for the placeholders
46
+ @return {String} html
47
+ */
48
+ render(context) {
49
+ let html = this.template(context);
50
+ if (this.style) {
51
+ html = juice_1.default.inlineContent(html, this.style, {
52
+ inlinePseudoElements: true,
53
+ preserveImportant: true,
54
+ preserveMediaQueries: true,
55
+ preserveFontFaces: true,
56
+ applyWidthAttributes: true,
57
+ applyHeightAttributes: true,
58
+ insertPreservedExtraCss: true,
59
+ extraCss: this.style // to enable inlining of media queries
60
+ });
61
+ }
62
+ return html;
63
+ }
64
+ }
65
+ exports.default = Renderer;
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@restorecommerce/handlebars-helperized",
3
- "version": "0.1.15",
3
+ "version": "0.1.18",
4
4
  "description": "Opinionated handlebars based templating engine for rendering e-mail like content",
5
- "main": "index",
6
- "author": "Invend GmbH",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
7
+ "author": "n-fuse GmbH",
7
8
  "repository": {
8
9
  "type": "git",
9
10
  "url": "https://github.com/restorecommerce/handlebars-helperized.git"
@@ -20,27 +21,44 @@
20
21
  "dependencies": {
21
22
  "handlebars": "^4.7.7",
22
23
  "handlebars-layouts": "^3.1.4",
23
- "juice": "^7.0.0",
24
+ "juice": "^8.0.0",
24
25
  "lodash": "^4.17.21",
25
- "moment-timezone": "^0.5.33",
26
- "numbro": "^2.3.2"
26
+ "moment-timezone": "^0.5.34",
27
+ "numbro": "^2.3.6"
27
28
  },
28
29
  "devDependencies": {
29
- "@restorecommerce/eslint-config-restorecommerce": "^0.1.5",
30
+ "@types/lodash": "^4.14.178",
31
+ "@types/mocha": "^9.1.0",
32
+ "@types/node": "^17.0.16",
33
+ "@types/nodemailer": "^6.4.4",
34
+ "@types/nodemailer-html-to-text": "^3.1.0",
35
+ "@types/nodemailer-stub-transport": "^1.1.5",
36
+ "@typescript-eslint/eslint-plugin": "^5.11.0",
37
+ "@typescript-eslint/eslint-plugin-tslint": "^5.11.0",
38
+ "@typescript-eslint/parser": "^5.11.0",
39
+ "assert": "^2.0.0",
40
+ "coveralls": "^3.1.1",
41
+ "eslint": "^8.8.0",
42
+ "eslint-plugin-prefer-arrow-functions": "^3.1.4",
30
43
  "is-generator": "^1.0.3",
31
- "jshint": "^2.12.0",
32
- "mocha": "^8.3.2",
44
+ "mocha": "^9.2.0",
45
+ "npm-run-all": "^4.1.5",
33
46
  "nyc": "^15.1.0",
34
- "should": "^13.2.3"
47
+ "should": "^13.2.3",
48
+ "typescript": "^4.5.5"
35
49
  },
36
50
  "scripts": {
37
- "test": "nyc mocha",
38
- "lint": "eslint *.json *.js",
39
- "mocha": "mocha ./test/*",
40
- "lcov-report": "nyc report --reporter=lcov"
51
+ "lint": "eslint src --ext .ts ",
52
+ "pretest": "npm run build",
53
+ "test": "nyc mocha ./test/* --exit",
54
+ "build:tsc": "tsc --strict",
55
+ "build:clean": "rimraf lib",
56
+ "build": "npm-run-all lint build:clean build:tsc",
57
+ "lcov-report": "nyc report --reporter=lcov",
58
+ "coveralls": "nyc --reporter text --reporter lcov npm test"
41
59
  },
42
60
  "engines": {
43
61
  "node": ">= 8.0.0"
44
62
  },
45
- "gitHead": "40168951ced3ee5a0eb7c52251044ef7b392dcbd"
63
+ "gitHead": "48d54d4a52972fa4c7f03a105241ea0662454e83"
46
64
  }
@@ -0,0 +1,11 @@
1
+
2
+ let customHandlebarsExtensions = (hbs: any, opts: any) => {
3
+ // increment a given numerical string by one
4
+ hbs.registerHelper('increment', (value: any, hash: any) => {
5
+ const toIncrement = parseInt(value, 10);
6
+ if (isNaN(toIncrement)) return '0';
7
+ return toIncrement + 1;
8
+ });
9
+ };
10
+
11
+ module.exports = customHandlebarsExtensions;
@@ -1,20 +1,20 @@
1
- 'use strict';
2
-
3
1
  const tripleStache = /\{\{\{\s*(.*?)\s*\}\}\}/g;
4
2
  const doubleStache = /\{\{\s*(.*?)\s*\}\}/g;
5
3
 
6
- module.exports = function localizationHandlebarsExtension(hbs, opts) {
7
- hbs.registerHelper('t', (key, hash = {}) => {
4
+ let localizationHandlebarsExtension = (hbs: any, opts: any) => {
5
+ hbs.registerHelper('t', (key: string, hash = {} as any) => {
8
6
  const locale = opts.locale;
9
7
  let result = opts.texts[key] || key;
10
8
  result = (typeof result === 'object') ? result[locale] : result;
11
9
  if (!result) return 'Missing translation for ' + key;
12
10
  const data = hash.data.root;
13
- return result.replace(doubleStache, (i, match) => {
11
+ return result.replace(doubleStache, (i: any, match: any) => {
14
12
  return data[match] ? data[match] : ('{{' + match + '}}');
15
- }).replace(tripleStache, (i, match) => {
13
+ }).replace(tripleStache, (i: any, match: any) => {
16
14
  // TODO: escaping
17
15
  return data[match] ? data[match] : ('{{{' + match + '}}}');
18
16
  });
19
17
  });
20
18
  };
19
+
20
+ module.exports = localizationHandlebarsExtension;
@@ -1,11 +1,9 @@
1
- 'use strict';
1
+ import moment from 'moment-timezone';
2
2
 
3
- const moment = require('moment-timezone');
4
-
5
- module.exports = function momentHandlebarsExtension(hbs, opts) {
3
+ let momentHandlebarsExtension = (hbs: any, opts: any) => {
6
4
  // Output point in time relative to current point in time
7
5
  // for example: '1h ago'
8
- hbs.registerHelper('ago', (value, options) => {
6
+ hbs.registerHelper('ago', (value: any, options: any) => {
9
7
  let v = value;
10
8
  if (options.isSeconds) {
11
9
  // the given property represents seconds since UNIX epoch, so we
@@ -17,31 +15,31 @@ module.exports = function momentHandlebarsExtension(hbs, opts) {
17
15
  });
18
16
 
19
17
  // Date format short
20
- hbs.registerHelper('df', (value, options) => {
18
+ hbs.registerHelper('df', (value: any, options: any) => {
21
19
  const tz = options.hash.timezone || moment.tz.guess();
22
20
  return moment.tz(value, tz).locale(opts.locale).format('L');
23
21
  });
24
22
 
25
23
  // Date format Long
26
- hbs.registerHelper('dfl', (value, options) => {
24
+ hbs.registerHelper('dfl', (value: any, options: any) => {
27
25
  const tz = options.hash.timezone || moment.tz.guess();
28
26
  return moment.tz(value, tz).locale(opts.locale).format('LL');
29
27
  });
30
28
 
31
29
  // Time format
32
- hbs.registerHelper('tf', (value, options) => {
30
+ hbs.registerHelper('tf', (value: any, options: any) => {
33
31
  const tz = options.hash.timezone || moment.tz.guess();
34
32
  return moment.tz(value, tz).locale(opts.locale).format('LT');
35
33
  });
36
34
 
37
35
  // Date-Time format
38
- hbs.registerHelper('dtf', (value, options) => {
36
+ hbs.registerHelper('dtf', (value: any, options: any) => {
39
37
  const tz = options.hash.timezone || moment.tz.guess();
40
38
  return moment.tz(value, tz).locale(opts.locale).format('LLL');
41
39
  });
42
40
 
43
41
  // Date-Time format with given format
44
- hbs.registerHelper('dff', (value, options) => {
42
+ hbs.registerHelper('dff', (value: any, options: any) => {
45
43
  const format = options.hash.format || '';
46
44
  const tz = options.hash.timezone || moment.tz.guess();
47
45
  return moment(value).tz(tz).format(format);
@@ -55,10 +53,12 @@ module.exports = function momentHandlebarsExtension(hbs, opts) {
55
53
  //
56
54
  // A format can be a template string with this syntax:
57
55
  // '[it\'s] D [days and] h [hours]'
58
- hbs.registerHelper('duf', (value, options) => {
56
+ hbs.registerHelper('duf', (value: any, options: any) => {
59
57
  const format = options.hash.format || '';
60
- const dur = moment.duration(value);
58
+ const dur: any = moment.duration(value);
61
59
  // eslint-disable-next-line
62
60
  return moment(dur._data).format(format);
63
61
  });
64
62
  };
63
+
64
+ module.exports = momentHandlebarsExtension;
@@ -1,5 +1,3 @@
1
- 'use strict';
2
-
3
1
  const numbro = require('numbro');
4
2
  const allLanguages = require('numbro/dist/languages.min');
5
3
 
@@ -7,11 +5,11 @@ Object.values(allLanguages).forEach((data) => {
7
5
  numbro.registerLanguage(data);
8
6
  });
9
7
 
10
- module.exports = function numbroHandlebarsExtension(hbs, opts) {
8
+ let numbroHandlebarsExtension = (hbs: any, opts: any) => {
11
9
  const locale = opts.locale.replace('_', '-');
12
10
 
13
11
  // For numeric values without decimals
14
- hbs.registerHelper('nfn', (value, hash) => {
12
+ hbs.registerHelper('nfn', (value: any, hash: any) => {
15
13
  numbro.setLanguage(locale);
16
14
  return numbro(value).format({
17
15
  thousandSeparated: true,
@@ -20,7 +18,7 @@ module.exports = function numbroHandlebarsExtension(hbs, opts) {
20
18
  });
21
19
 
22
20
  // For currency based numeric values
23
- hbs.registerHelper('nfc', (value, hash) => {
21
+ hbs.registerHelper('nfc', (value: any, hash: any) => {
24
22
  const lhash = hash.hash;
25
23
  numbro.setLanguage(locale);
26
24
  // Don't use formatCurrency as it does not allow control over the currency
@@ -43,7 +41,7 @@ module.exports = function numbroHandlebarsExtension(hbs, opts) {
43
41
  });
44
42
 
45
43
  // For byte based numeric values
46
- hbs.registerHelper('nfb', (value, hash) => {
44
+ hbs.registerHelper('nfb', (value: any, hash: any) => {
47
45
  numbro.setLanguage(locale);
48
46
  return numbro(value).format({
49
47
  output: 'byte',
@@ -51,3 +49,5 @@ module.exports = function numbroHandlebarsExtension(hbs, opts) {
51
49
  });
52
50
  });
53
51
  };
52
+
53
+ module.exports = numbroHandlebarsExtension;
@@ -1,7 +1,5 @@
1
- 'use strict';
2
-
3
- const _ = require('lodash');
4
- const juice = require('juice');
1
+ import juice from 'juice';
2
+ import defaults from 'lodash/defaults';
5
3
 
6
4
  const defaultOpts = {
7
5
  locale: 'en_US',
@@ -9,29 +7,29 @@ const defaultOpts = {
9
7
  };
10
8
 
11
9
  // Initializes and configures a custom handlebars instance
12
- function init(options, customHelpersList) {
10
+ const init = (options: object | undefined, customHelpersList: any) => {
13
11
  // default values if nothing given
14
- const opts = _.defaults(options, defaultOpts);
12
+ const opts = defaults(options, defaultOpts);
15
13
  // the basic building block is the handlebars rendering engine
16
14
  const hbs = require('handlebars');
17
15
  // more functionality directly added via custom plugins from ./lib
18
- require('./lib/l10n-helpers')(hbs, opts); // localization
19
- require('./lib/numbro-helpers')(hbs, opts); // numbers & currencies
20
- require('./lib/moment-helpers')(hbs, opts); // dates, times & durations
21
- require('./lib/custom-helpers')(hbs, opts); // everything else
16
+ require('./helpers/l10n-helpers.js')(hbs, opts); // localization
17
+ require('./helpers/numbro-helpers.js')(hbs, opts); // numbers & currencies
18
+ require('./helpers/moment-helpers.js')(hbs, opts); // dates, times & durations
19
+ require('./helpers/custom-helpers.js')(hbs, opts); // everything else
22
20
 
23
21
  // add custom helpers from rendering-srv
24
22
  if (customHelpersList) {
25
23
  for (let customHelper of customHelpersList) {
26
24
  const filePath = customHelper;
27
- require(filePath) (hbs, opts);
25
+ require(filePath)(hbs, opts);
28
26
  }
29
27
  }
30
28
  // extend rendering with layout functionality
31
29
  const handlebarsLayouts = require('handlebars-layouts');
32
30
  handlebarsLayouts.register(hbs);
33
31
  return hbs;
34
- }
32
+ };
35
33
 
36
34
  class Renderer {
37
35
  /**
@@ -41,7 +39,12 @@ class Renderer {
41
39
  @param {Object} opts handlebars options
42
40
  @param {Array} customHelpersList contains a list of custom helpers (optional)
43
41
  */
44
- constructor(template, layout, style, opts, customHelpersList) {
42
+
43
+ hbs: any;
44
+ style: string | undefined;
45
+ template: any;
46
+
47
+ constructor(template: string, layout?: string | undefined, style?: string | undefined, opts?: object | undefined, customHelpersList?: any) {
45
48
  this.hbs = init(opts, customHelpersList);
46
49
  this.style = style;
47
50
  if (layout) {
@@ -54,7 +57,7 @@ class Renderer {
54
57
  @param {Object} context: required data for the placeholders
55
58
  @return {String} html
56
59
  */
57
- render(context) {
60
+ render(context: Object) {
58
61
  let html = this.template(context);
59
62
 
60
63
  if (this.style) {
@@ -74,4 +77,4 @@ class Renderer {
74
77
  }
75
78
  }
76
79
 
77
- module.exports = Renderer;
80
+ export default Renderer;
package/tsconfig.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES6",
4
+ "module": "commonjs",
5
+ "moduleResolution": "node",
6
+ "emitDecoratorMetadata": true,
7
+ "experimentalDecorators": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "declaration": true,
10
+ "esModuleInterop": true,
11
+ "skipLibCheck": true,
12
+ "outDir": "lib",
13
+ "typeRoots": [
14
+ "node_modules/@types"
15
+ ],
16
+ },
17
+ "include": [
18
+ "src/**/*.ts"
19
+ ],
20
+ "exclude": [
21
+ "node_modules",
22
+ "lib",
23
+ "doc"
24
+ ]
25
+ }
@@ -1,8 +0,0 @@
1
- module.exports = function customHandlebarsExtensions(hbs, opts) {
2
- // increment a given numerical string by one
3
- hbs.registerHelper('increment', (value, hash) => {
4
- const toIncrement = parseInt(value, 10);
5
- if (isNaN(toIncrement)) return '0';
6
- return toIncrement + 1;
7
- });
8
- };