@handfish/hygen
Version:
The scalable code generator that saves you time.
44 lines (31 loc) • 1.87 kB
JavaScript
;
function _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; }
function _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; }
const inflection = require('inflection');
const changeCase = require('change-case'); // supports kebab-case to KebabCase
inflection.undasherize = str => str.split(/[-_]/).map(w => w[0].toUpperCase() + w.slice(1).toLowerCase()).join('');
const helpers = {
capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
},
inflection,
changeCase
};
const doCapitalization = (hsh, [key, value]) => {
hsh[key] = value;
if (localsToCapitalize.includes(key)) hsh[helpers.capitalize(key)] = helpers.capitalize(value);
return hsh;
};
const localsToCapitalize = ['name'];
const localsDefaults = {
name: 'unnamed'
};
const capitalizedLocals = locals => Object.entries(locals).reduce(doCapitalization, {});
const context = (locals, config) => {
const localsWithDefaults = Object.assign({}, localsDefaults, locals);
const configHelpers = config && (typeof config.helpers === "function" ? config.helpers(locals, config) : config.helpers) || {};
return Object.assign(localsWithDefaults, capitalizedLocals(localsWithDefaults), {
h: _objectSpread({}, helpers, configHelpers)
});
};
module.exports = context;