UNPKG

1.87 kBJavaScriptView Raw
1"use strict";
2
3function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
4
5function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
7const inflection = require('inflection');
8
9const changeCase = require('change-case'); // supports kebab-case to KebabCase
10
11
12inflection.undasherize = str => str.split(/[-_]/).map(w => w[0].toUpperCase() + w.slice(1).toLowerCase()).join('');
13
14const helpers = {
15 capitalize(string) {
16 return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
17 },
18
19 inflection,
20 changeCase
21};
22
23const doCapitalization = (hsh, [key, value]) => {
24 hsh[key] = value;
25 if (localsToCapitalize.includes(key)) hsh[helpers.capitalize(key)] = helpers.capitalize(value);
26 return hsh;
27};
28
29const localsToCapitalize = ['name'];
30const localsDefaults = {
31 name: 'unnamed'
32};
33
34const capitalizedLocals = locals => Object.entries(locals).reduce(doCapitalization, {});
35
36const context = (locals, config) => {
37 const localsWithDefaults = Object.assign({}, localsDefaults, locals);
38 const configHelpers = config && (typeof config.helpers === "function" ? config.helpers(locals, config) : config.helpers) || {};
39 return Object.assign(localsWithDefaults, capitalizedLocals(localsWithDefaults), {
40 h: _objectSpread({}, helpers, configHelpers)
41 });
42};
43
44module.exports = context;
\No newline at end of file