UNPKG

1.22 kBPlain TextView Raw
1
2import { RunnerConfig } from "./types";
3
4const inflection = require('inflection');
5const changeCase = require('change-case');
6
7// supports kebab-case to KebabCase
8inflection.undasherize = str => str.split(/[-_]/).map(w => w[0].toUpperCase() + w.slice(1).toLowerCase()).join('');
9
10const helpers = {
11 capitalize(string) {
12 return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
13 },
14 inflection,
15 changeCase
16};
17
18const 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
26const localsToCapitalize = ['name'];
27const localsDefaults = {
28 name: 'unnamed'
29};
30
31const capitalizedLocals = (locals: any) => Object.entries(locals).reduce(doCapitalization, {});
32
33const 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};
40module.exports = context;
\No newline at end of file