string-utils-lite
Version:
Tiny string helpers: capitalize, titleCase, kebab-case, snake_case, camelCase, and PascalCase.
59 lines (57 loc) • 2.16 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);
// src/index.ts
var src_exports = {};
__export(src_exports, {
capitalize: () => capitalize,
titleCase: () => titleCase,
toCamelCase: () => toCamelCase,
toKebabCase: () => toKebabCase,
toPascalCase: () => toPascalCase,
toSnakeCase: () => toSnakeCase
});
module.exports = __toCommonJS(src_exports);
function capitalize(str = "") {
if (!str) return "";
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
function titleCase(str = "") {
return str.toLowerCase().split(/(\s+)/).map((t) => t.trim() === "" ? t : t.charAt(0).toUpperCase() + t.slice(1)).join("");
}
function toKebabCase(str = "") {
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase().replace(/^-+|-+$/g, "");
}
function toSnakeCase(str = "") {
return str.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[\s-]+/g, "_").toLowerCase().replace(/^_+|_+$/g, "");
}
function toCamelCase(str = "") {
return str.toLowerCase().split(/[\s-_]+/).map((w, i) => i === 0 ? w : w.charAt(0).toUpperCase() + w.slice(1)).join("");
}
function toPascalCase(str = "") {
return str.toLowerCase().split(/[\s-_]+/).map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join("");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
capitalize,
titleCase,
toCamelCase,
toKebabCase,
toPascalCase,
toSnakeCase
});
;