@restorecommerce/handlebars-helperized 0.1.9 → 0.1.16
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 +71 -0
- package/LICENSE +2 -2
- package/README.md +2 -1
- package/lib/helpers/custom-helpers.d.ts +1 -0
- package/lib/helpers/custom-helpers.js +11 -0
- package/lib/helpers/l10n-helpers.d.ts +3 -0
- package/lib/helpers/l10n-helpers.js +20 -0
- package/lib/helpers/moment-helpers.d.ts +1 -0
- package/lib/helpers/moment-helpers.js +61 -0
- package/lib/helpers/numbro-helpers.d.ts +3 -0
- package/lib/helpers/numbro-helpers.js +47 -0
- package/lib/index.d.ts +19 -0
- package/lib/index.js +65 -0
- package/package.json +34 -16
- package/src/helpers/custom-helpers.ts +11 -0
- package/{lib/l10n-helpers.js → src/helpers/l10n-helpers.ts} +6 -6
- package/{lib/moment-helpers.js → src/helpers/moment-helpers.ts} +12 -12
- package/{lib/numbro-helpers.js → src/helpers/numbro-helpers.ts} +6 -6
- package/{index.js → src/index.ts} +17 -14
- package/tsconfig.json +25 -0
- package/lib/custom-helpers.js +0 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## [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)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @restorecommerce/handlebars-helperized
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## [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)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Bug Fixes
|
|
18
|
+
|
|
19
|
+
* **version:** up version to be in sync in package-lock ([b8f22c1](https://github.com/restorecommerce/handlebars-helperized/commit/b8f22c1268ee2af4beff7d88bda30f197896e3d2))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
## [0.1.14](https://github.com/restorecommerce/handlebars-helperized/compare/@restorecommerce/handlebars-helperized@0.1.13...@restorecommerce/handlebars-helperized@0.1.14) (2021-08-03)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### Bug Fixes
|
|
29
|
+
|
|
30
|
+
* up pkg locks ([8ed92d6](https://github.com/restorecommerce/handlebars-helperized/commit/8ed92d613b9a095e4b5066056ac566e5dbcf1472))
|
|
31
|
+
* updated githead ([2904d30](https://github.com/restorecommerce/handlebars-helperized/commit/2904d30e5773dc8a87c01a08ff6481f99d692354))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
## [0.1.13](https://github.com/restorecommerce/handlebars-helperized/compare/@restorecommerce/handlebars-helperized@0.1.12...@restorecommerce/handlebars-helperized@0.1.13) (2021-08-03)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
### Bug Fixes
|
|
41
|
+
|
|
42
|
+
* **koa-health-check:** added missing .eslintrc.js ([45af632](https://github.com/restorecommerce/handlebars-helperized/commit/45af632955d2dd448e7a27f4e8c4b971412cd004))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## [0.1.12](https://github.com/restorecommerce/handlebars-helperized/compare/@restorecommerce/handlebars-helperized@0.1.11...@restorecommerce/handlebars-helperized@0.1.12) (2021-06-26)
|
|
49
|
+
|
|
50
|
+
**Note:** Version bump only for package @restorecommerce/handlebars-helperized
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
## [0.1.11](https://github.com/restorecommerce/handlebars-helperized/compare/@restorecommerce/handlebars-helperized@0.1.10...@restorecommerce/handlebars-helperized@0.1.11) (2021-05-25)
|
|
57
|
+
|
|
58
|
+
**Note:** Version bump only for package @restorecommerce/handlebars-helperized
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
## 0.1.10 (2021-05-24)
|
|
65
|
+
|
|
66
|
+
**Note:** Version bump only for package @restorecommerce/handlebars-helperized
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
1
72
|
### 0.1.9 (April 15th, 2021)
|
|
2
73
|
|
|
3
74
|
- Upgrade deps.
|
package/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c)
|
|
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,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,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;
|
|
11
|
+
template: any;
|
|
12
|
+
constructor(template: string, layout: string, style: string, opts: object, 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.
|
|
3
|
+
"version": "0.1.16",
|
|
4
4
|
"description": "Opinionated handlebars based templating engine for rendering e-mail like content",
|
|
5
|
-
"main": "index",
|
|
6
|
-
"
|
|
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": "^
|
|
24
|
+
"juice": "^8.0.0",
|
|
24
25
|
"lodash": "^4.17.21",
|
|
25
|
-
"moment-timezone": "^0.5.
|
|
26
|
-
"numbro": "^2.3.
|
|
26
|
+
"moment-timezone": "^0.5.34",
|
|
27
|
+
"numbro": "^2.3.6"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
|
-
"@
|
|
30
|
-
"
|
|
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",
|
|
31
43
|
"is-generator": "^1.0.3",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
44
|
+
"mocha": "^9.2.0",
|
|
45
|
+
"npm-run-all": "^4.1.5",
|
|
34
46
|
"nyc": "^15.1.0",
|
|
35
|
-
"should": "^13.2.3"
|
|
47
|
+
"should": "^13.2.3",
|
|
48
|
+
"typescript": "^4.5.5"
|
|
36
49
|
},
|
|
37
50
|
"scripts": {
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
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"
|
|
42
59
|
},
|
|
43
60
|
"engines": {
|
|
44
61
|
"node": ">= 8.0.0"
|
|
45
|
-
}
|
|
62
|
+
},
|
|
63
|
+
"gitHead": "e97bbfe2fe8166dfe1cd47ae60bce54347a4f1c9"
|
|
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
|
-
|
|
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
|
-
|
|
1
|
+
import moment from 'moment-timezone';
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,16 +7,16 @@ const defaultOpts = {
|
|
|
9
7
|
};
|
|
10
8
|
|
|
11
9
|
// Initializes and configures a custom handlebars instance
|
|
12
|
-
|
|
10
|
+
const init = (options: object, customHelpersList: any) => {
|
|
13
11
|
// default values if nothing given
|
|
14
|
-
const opts =
|
|
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('./
|
|
19
|
-
require('./
|
|
20
|
-
require('./
|
|
21
|
-
require('./
|
|
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) {
|
|
@@ -31,7 +29,7 @@ function init(options, customHelpersList) {
|
|
|
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
|
-
|
|
42
|
+
|
|
43
|
+
hbs: any;
|
|
44
|
+
style: string;
|
|
45
|
+
template: any;
|
|
46
|
+
|
|
47
|
+
constructor(template: string, layout: string, style: string, opts: object, 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
|
-
|
|
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
|
+
}
|
package/lib/custom-helpers.js
DELETED
|
@@ -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
|
-
};
|