UNPKG

@adonisjs/ace

Version:

Commandline apps framework used by AdonisJs

115 lines (114 loc) 3.12 kB
"use strict"; /* * @adonisjs/ace * * (c) Harminder Virk <virk@adonisjs.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.StringTransformer = void 0; const path_1 = require("path"); const helpers_1 = require("@poppinss/utils/build/helpers"); /** * Exposes the API to transform a string */ class StringTransformer { constructor(input) { this.input = input; } /** * Cleans suffix from the input. */ cleanSuffix(suffix) { if (!suffix) { return this; } this.input = this.input.replace(new RegExp(`[-_]?${suffix}$`, 'i'), ''); return this; } /** * Cleans prefix from the input. */ cleanPrefix(prefix) { if (!prefix) { return this; } this.input = this.input.replace(new RegExp(`^${prefix}[-_]?`, 'i'), ''); return this; } /** * Add suffix to the file name */ addSuffix(suffix) { if (!suffix) { return this; } this.input = `${this.input}_${suffix}`; return this; } /** * Add prefix to the file name */ addPrefix(prefix) { if (!prefix) { return this; } this.input = `${prefix}_${this.input}`; return this; } /** * Changes the name form by converting it to singular * or plural case */ changeForm(form, ignoreList) { if (!form) { return this; } /** * Do not change form when word is in ignore list */ if ((ignoreList || []).find((word) => word.toLowerCase() === this.input.toLowerCase())) { return this; } this.input = form === 'singular' ? helpers_1.string.singularize(this.input) : helpers_1.string.pluralize(this.input); return this; } /** * Changes the input case */ changeCase(pattern) { switch (pattern) { case 'camelcase': this.input = helpers_1.string.camelCase(this.input); return this; case 'pascalcase': const camelCase = helpers_1.string.camelCase(this.input); this.input = `${camelCase.charAt(0).toUpperCase()}${camelCase.slice(1)}`; return this; case 'snakecase': this.input = helpers_1.string.snakeCase(this.input); return this; case 'dashcase': this.input = helpers_1.string.dashCase(this.input); return this; default: return this; } } /** * Drops the extension from the input */ dropExtension() { this.input = this.input.replace(new RegExp(`${(0, path_1.extname)(this.input)}$`), ''); return this; } /** * Returns the transformed value */ toValue() { return this.input; } } exports.StringTransformer = StringTransformer;