@polgubau/utils
Version:
A collection of utility functions for TypeScript
96 lines • 3.13 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var text_transform_exports = {};
__export(text_transform_exports, {
capitalize: () => capitalize,
formatString: () => formatString,
lowerAndNoSpace: () => lowerAndNoSpace,
randomString: () => randomString,
removeWhitespace: () => removeWhitespace,
reverseString: () => reverseString,
toCamelCase: () => toCamelCase,
toKebabCase: () => toKebabCase,
toLowerCase: () => toLowerCase,
toTitleCase: () => toTitleCase,
toUpperCase: () => toUpperCase,
truncateString: () => truncateString
});
module.exports = __toCommonJS(text_transform_exports);
function toCamelCase(str) {
return str.replace(/[-_](.)/g, (_, c) => c.toUpperCase());
}
function toTitleCase(str) {
return str.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.slice(1).toLowerCase());
}
function toKebabCase(str) {
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/\s+/g, "-").toLowerCase();
}
function toUpperCase(str) {
return str.toUpperCase();
}
function toLowerCase(str) {
return str.toLowerCase();
}
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function removeWhitespace(str) {
return str.replace(/\s/g, "");
}
function reverseString(str) {
return str.split("").reverse().join("");
}
function truncateString(str, length = 50, suffix = "...") {
if (str.length === 0) {
return str;
}
return str.length > length ? str.substring(0, length - suffix.length) + suffix : str;
}
function formatString(str) {
const result = str.replace(/([a-z])([A-Z])/g, "$1 $2");
return capitalize(result.replace(/([A-Z])([A-Z][a-z])/g, "$1 $2").trim());
}
const randomString = (length = 4) => {
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
let result = "";
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
result += characters.charAt(randomIndex);
}
return result;
};
const lowerAndNoSpace = (str) => {
return str.toLowerCase().replace(/\s/g, "");
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
capitalize,
formatString,
lowerAndNoSpace,
randomString,
removeWhitespace,
reverseString,
toCamelCase,
toKebabCase,
toLowerCase,
toTitleCase,
toUpperCase,
truncateString
});
//# sourceMappingURL=text-transform.js.map