boats
Version:
Beautiful Open / Async Template System - Write less yaml with BOATS and Nunjucks.
40 lines (39 loc) • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = buildIndexFromPath;
const tslib_1 = require("tslib");
const upath_1 = tslib_1.__importDefault(require("upath"));
const getMethodFromFileName_1 = tslib_1.__importDefault(require("../utils/getMethodFromFileName"));
const ucFirst_1 = tslib_1.__importDefault(require("../ucFirst"));
const removeFileExtension_1 = tslib_1.__importDefault(require("../removeFileExtension"));
const lodash_1 = tslib_1.__importDefault(require("lodash"));
const pluralize_1 = tslib_1.__importDefault(require("pluralize"));
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function buildIndexFromPath(input) {
const { cleanPath, autoComponentIndexerOptions, enableFancyPluralization, dontUcFirst } = input;
const dir = upath_1.default.dirname(cleanPath);
const filename = upath_1.default.basename(cleanPath);
const method = (0, getMethodFromFileName_1.default)(filename);
let _path = cleanPath;
if (autoComponentIndexerOptions && autoComponentIndexerOptions.dropBaseName && new RegExp(method + '$', 'i').test(lodash_1.default.camelCase(dir))) {
_path = cleanPath.replace(filename, '');
}
const trim = typeof autoComponentIndexerOptions === 'string' ? autoComponentIndexerOptions : '';
let rawIndex = lodash_1.default.camelCase((0, removeFileExtension_1.default)(_path));
if (!dontUcFirst) {
rawIndex = (0, ucFirst_1.default)(rawIndex);
}
const isPlural = trim && rawIndex.endsWith(`${trim}s`);
if (!isPlural) {
return rawIndex.replace(new RegExp(`${trim}$`), '');
}
if (!enableFancyPluralization) {
return rawIndex.replace(new RegExp(`${trim}s$`), 's');
}
const trimmedIndex = rawIndex.replace(new RegExp(`${trim}s$`), '');
const pluralIndex = pluralize_1.default.plural(trimmedIndex);
// Add an extra "s" to words which are the same in both plural and non-plural form (i.e. sheep)
return isPlural && trimmedIndex === pluralIndex
? `${pluralIndex}s`
: pluralIndex;
}