1 |
|
2 | import { RunnerConfig } from "./types";
|
3 |
|
4 | const inflection = require('inflection');
|
5 | const changeCase = require('change-case');
|
6 |
|
7 |
|
8 | inflection.undasherize = str => str.split(/[-_]/).map(w => w[0].toUpperCase() + w.slice(1).toLowerCase()).join('');
|
9 |
|
10 | const helpers = {
|
11 | capitalize(string) {
|
12 | return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
|
13 | },
|
14 | inflection,
|
15 | changeCase
|
16 | };
|
17 |
|
18 | const doCapitalization = (hsh, [key, value]) => {
|
19 | hsh[key] = value;
|
20 |
|
21 | if (localsToCapitalize.includes(key)) hsh[helpers.capitalize(key)] = helpers.capitalize(value);
|
22 |
|
23 | return hsh;
|
24 | };
|
25 |
|
26 | const localsToCapitalize = ['name'];
|
27 | const localsDefaults = {
|
28 | name: 'unnamed'
|
29 | };
|
30 |
|
31 | const capitalizedLocals = (locals: any) => Object.entries(locals).reduce(doCapitalization, {});
|
32 |
|
33 | const context = (locals: any, config: RunnerConfig) => {
|
34 | const localsWithDefaults = Object.assign({}, localsDefaults, locals);
|
35 | const configHelpers = config && (typeof config.helpers === "function" ? config.helpers(locals, config) : config.helpers) || {};
|
36 | return Object.assign(localsWithDefaults, capitalizedLocals(localsWithDefaults), {
|
37 | h: { ...helpers, ...configHelpers }
|
38 | });
|
39 | };
|
40 | module.exports = context; |
\ | No newline at end of file |