boats
Version:
Beautiful Open / Async Template System - Write less yaml with BOATS and Nunjucks.
58 lines (57 loc) • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const lodash_1 = require("lodash");
const upath_1 = require("upath");
const getMethodFromFileName_1 = tslib_1.__importDefault(require("./utils/getMethodFromFileName"));
const removeFileExtension_1 = tslib_1.__importDefault(require("./removeFileExtension"));
const ucFirst_1 = tslib_1.__importDefault(require("./ucFirst"));
const summaryVerbReplacements = {
get: 'Get',
post: 'Create a',
put: 'Update a',
patch: 'Update part of a',
delete: 'Delete a'
};
const isVar = (part) => {
return part.includes('{');
};
exports.default = (filePath, options) => {
const parts = filePath.split(upath_1.sep);
// @ts-ignore
const method = summaryVerbReplacements[(0, getMethodFromFileName_1.default)(
// if we use the filename governed by the options, don't pop the last part, leave it in place.
options && options.useFileName ? parts[parts.length - 1] : parts.pop())];
if (options && options.useFileName) {
parts[parts.length - 1] = (0, ucFirst_1.default)((0, removeFileExtension_1.default)(parts[parts.length - 1])).trim();
}
let out = `${(0, lodash_1.upperFirst)(method)}`;
if (!parts.length) {
return out;
}
parts.reverse();
for (let i = 0; i < parts.length; i++) {
const first = i === 0;
if (isVar(parts[i])) {
//
const variable = parts[i];
++i;
const word = parts[i];
if (first) {
out += ` ${word} based on ${variable}`;
}
else {
out += `, from ${word} ${variable}`;
}
}
else {
if (first) {
out += ' ' + parts[i];
}
else {
out += ', from ' + parts[i];
}
}
}
return out;
};