fallout-utility
Version:
<h1 align="center"> fallout-utility <br> </h1>
89 lines • 3.24 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceAll = exports.removeUnecessarySpaces = exports.splitString = exports.limitString = exports.startsWith = exports.trimChars = exports.escapeRegExp = exports.kleur = void 0;
const objects_1 = require("./objects");
const kleur_1 = __importDefault(require("kleur"));
exports.kleur = kleur_1.default;
const split_string_1 = __importDefault(require("split-string"));
/**
* Escape a string to be used as a regular expression
* @param string Escape regex from this string
*/
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
exports.escapeRegExp = escapeRegExp;
/**
* Trim characters from string
* @param string Trim characters from this string
* @param chars Trim this characters from string
*/
function trimChars(string, ...chars) {
chars = (0, objects_1.normalizeArray)(chars);
for (let char of chars) {
string = string.replace(new RegExp(`^${char}+|${char}+$`, 'g'), '');
}
return string;
}
exports.trimChars = trimChars;
/**
* Detects the string if has the given prefixes
* @param string Check this string for prefixes
* @param find Check if string starts with at least one of these strings
*/
function startsWith(string, ...find) {
find = (0, objects_1.normalizeArray)(find);
for (const prefix of find) {
if (string.startsWith(prefix))
return true;
}
return false;
}
exports.startsWith = startsWith;
/**
* Limit text to a certain number of characters with a suffix
*/
function limitString(string = '', limit = 0, endsWith = '...') {
return string.length >= limit ? string.slice(0, limit) + (endsWith ?? '') : string;
}
exports.limitString = limitString;
/**
* Split a string into an array of strings
* @param string String to split
* @param removeQuotations Whether to trim quotation marks from the string if they exist
* @param separator Split separator
*/
function splitString(string, removeQuotations = false, separator = ' ') {
return (0, split_string_1.default)(string, {
brackets: true,
quotes: true,
separator,
keep: (value, state) => value !== '\\' && (value !== '"' || state.prev() === '\\')
});
}
exports.splitString = splitString;
/**
* Removes extra spaces from text
* @param text Text to remove extra spaces from
*/
function removeUnecessarySpaces(text) {
return text.trim().split(' ').filter(Boolean).join(' ');
}
exports.removeUnecessarySpaces = removeUnecessarySpaces;
function replaceAll(string, find, replace) {
if (typeof find === 'string')
find = [find];
if (typeof replace === 'string')
replace = [replace];
if (find.length !== replace.length)
throw new TypeError(`${kleur_1.default.cyan('find')} and ${kleur_1.default.cyan('replace')} parameters must be of the same length.`);
for (let i = 0; i < find.length; i++) {
string = string.replace(new RegExp(find[i], 'g'), replace[i]);
}
return string;
}
exports.replaceAll = replaceAll;
//# sourceMappingURL=strings.js.map