UNPKG

camote-utils

Version:

A comprehensive TypeScript utility library featuring advanced string and number formatting, data structures, and algorithms

203 lines (202 loc) 6.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.swapCase = exports.toHtmlEntities = exports.toUnicodes = exports.explode = exports.trim = exports.pad = exports.mask = exports.capitalizeWords = exports.chopEnd = exports.chopStart = exports.toLowerCase = exports.toUpperCase = exports.pluralize = exports.clean = exports.reverse = exports.format = exports.wordCount = exports.slugifyRevert = exports.slugify = exports.toSnakeCase = exports.toKebabCase = exports.toCamelCase = exports.truncate = exports.capitalize = void 0; const capitalize = (str) => { if (!str) return str; return str.charAt(0).toUpperCase() + str.slice(1); }; exports.capitalize = capitalize; const truncate = (str, length, ellipsis = "...") => { if (str.length <= length) return str; return str.slice(0, length - ellipsis.length) + ellipsis; }; exports.truncate = truncate; const toCamelCase = (str) => { return str .replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => index === 0 ? letter.toLowerCase() : letter.toUpperCase()) .replace(/[\s-_]+(\w)/g, (_, letter) => letter.toUpperCase()); }; exports.toCamelCase = toCamelCase; const toKebabCase = (str) => { return str .replace(/([a-z])([A-Z])/g, "$1-$2") .replace(/[\s_]+/g, "-") .toLowerCase(); }; exports.toKebabCase = toKebabCase; const toSnakeCase = (str) => { return str .replace(/([a-z])([A-Z])/g, "$1_$2") .replace(/[\s-]+/g, "_") .toLowerCase(); }; exports.toSnakeCase = toSnakeCase; const slugify = (str) => { return str .toLowerCase() .trim() .replace(/[^\w\s-]/g, "") .replace(/[\s_-]+/g, "-") .replace(/^-+|-+$/g, ""); }; exports.slugify = slugify; const slugifyRevert = (str) => { return str .replace(/[-_]+/g, " ") .replace(/\s+/g, " ") .split(" ") .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) .join(" "); }; exports.slugifyRevert = slugifyRevert; const wordCount = (str) => { return str.trim() === '' ? 0 : str.trim().split(/\s+/).length; }; exports.wordCount = wordCount; const format = (template, values) => { return template.replace(/{(\w+)}/g, (match, key) => { var _a, _b; return (_b = (_a = values[key]) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : match; }); }; exports.format = format; const reverse = (str) => { return str.split("").reverse().join(""); }; exports.reverse = reverse; const clean = (str) => { return str.trim().replace(/\s+/g, " "); }; exports.clean = clean; const pluralize = (word, count, customPlural) => { if (count === 1) return word; if (customPlural) return customPlural; if (word.endsWith('y')) { if (!/[aeiou]y$/i.test(word)) { return word.slice(0, -1) + 'ies'; } } else if (word.endsWith('s') || word.endsWith('sh') || word.endsWith('ch') || word.endsWith('x') || word.endsWith('z')) { return word + 'es'; } return word + 's'; }; exports.pluralize = pluralize; const toUpperCase = (str, locale) => { return locale ? str.toLocaleUpperCase(locale) : str.toUpperCase(); }; exports.toUpperCase = toUpperCase; const toLowerCase = (str, locale) => { return locale ? str.toLocaleLowerCase(locale) : str.toLowerCase(); }; exports.toLowerCase = toLowerCase; const chopStart = (str, count = 1) => { if (!str || count <= 0) return str; return str.slice(count > str.length ? str.length : count); }; exports.chopStart = chopStart; const chopEnd = (str, count = 1) => { if (!str || count <= 0) return str; return str.slice(0, -(count > str.length ? str.length : count)); }; exports.chopEnd = chopEnd; const capitalizeWords = (str) => { return str.replace(/\b\w/g, char => char.toUpperCase()); }; exports.capitalizeWords = capitalizeWords; const mask = (str, maskChar = '*', visibleCount = 6, position = 'end', active = true) => { if (!active) return str; if (!str) return str; visibleCount = Math.min(visibleCount, str.length); const maskedLength = Math.max(0, str.length - visibleCount); switch (position) { case 'start': return maskChar.repeat(maskedLength) + str.slice(-visibleCount); case 'end': return str.slice(0, visibleCount) + maskChar.repeat(maskedLength); default: return str; } }; exports.mask = mask; const pad = (str, length, char = " ", position = "end") => { const padChar = char.charAt(0); const padLength = length - str.length; if (padLength <= 0) return str; switch (position) { case "start": return padChar.repeat(padLength) + str; case "both": const startPad = Math.floor(padLength / 2); const endPad = padLength - startPad; return padChar.repeat(startPad) + str + padChar.repeat(endPad); default: return str + padChar.repeat(padLength); } }; exports.pad = pad; const trim = (str) => str.trim(); exports.trim = trim; const explode = (str, delimiter, limit) => { if (!str) return []; const parts = str.split(delimiter); if (limit && limit < parts.length) { const lastPart = parts.slice(limit).join(delimiter); return [...parts.slice(0, limit), lastPart]; } return parts; }; exports.explode = explode; const toUnicodes = (str, exclude = "") => { if (!str) return str; const stringArray = Array.from(str); for (let i = 0; i < stringArray.length; i++) { if (exclude.length > 0) { exclude = typeof (exclude) == "object" ? exclude.join('') : exclude; if (exclude.includes(stringArray[i])) continue; } const codePoint = stringArray[i].codePointAt(0); if (codePoint) { const hex = codePoint.toString(16).toUpperCase(); stringArray[i] = `\\u{${hex.padStart(4, "0")}}`; } } return stringArray.join(''); }; exports.toUnicodes = toUnicodes; const toHtmlEntities = (str, exclude = "") => { if (!str) return str; const stringArray = Array.from(str); for (let i = 0; i < stringArray.length; i++) { if (exclude.length > 0) { exclude = typeof (exclude) == "object" ? exclude.join('') : exclude; if (exclude.includes(stringArray[i])) continue; } const codePoint = stringArray[i].codePointAt(0); if (codePoint) { stringArray[i] = `&#${codePoint};`; } } return stringArray.join(''); }; exports.toHtmlEntities = toHtmlEntities; const swapCase = (str) => { return str.replace(/[a-zA-Z]/g, match => { return match === match.toUpperCase() ? match.toLowerCase() : match.toUpperCase(); }); }; exports.swapCase = swapCase; // ADD FORMAT FILE SIZE