UNPKG

text-case-converter

Version:

[DEPRECATED] A modern TypeScript library to convert strings between various case formats (camelCase, PascalCase, snake_case, etc.).

111 lines (108 loc) 3.79 kB
"use strict"; 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 index_exports = {}; __export(index_exports, { toCamelCase: () => toCamelCase, toConstantCase: () => toConstantCase, toKebabCase: () => toKebabCase, toPascalCase: () => toPascalCase, toSnakeCase: () => toSnakeCase }); module.exports = __toCommonJS(index_exports); // src/utils.ts function splitWords(input) { if (typeof input !== "string") throw new TypeError("Input must be a string"); const processed = input.replace(/[_\-\s]+/g, " ").replace(/[^a-zA-Z0-9 ]+/g, " ").trim(); const words = processed.length ? processed.split(/\s+/) : []; const result = []; for (const word of words) { const parts = word.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z0-9]+)/g, "$1 $2").split(" "); for (const part of parts) { if (part.length > 1) { result.push(part.toLowerCase()); } else { result.push(part); } } } const final = []; for (const w of result) { if (/^[a-zA-Z]+[0-9]+$/.test(w) || /^[0-9]+$/.test(w)) { final.push(w); } else if (/^[a-zA-Z]+$/.test(w)) { final.push(w); } else { const match = w.match(/[a-zA-Z]+|[0-9]+/g); if (match) final.push(...match); } } return final; } function capitalize(word) { return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); } function lowerFirst(word) { return word.charAt(0).toLowerCase() + word.slice(1); } // src/index.ts function toCamelCase(input) { if (typeof input !== "string") throw new TypeError("Input must be a string"); if (input === "") return ""; const words = splitWords(input); if (words.length === 0) return ""; return [lowerFirst(words[0]), ...words.slice(1).map(capitalize)].join(""); } function toPascalCase(input) { if (typeof input !== "string") throw new TypeError("Input must be a string"); if (input === "") return ""; const words = splitWords(input); if (words.length === 0) return ""; return words.map(capitalize).join(""); } function toSnakeCase(input) { if (typeof input !== "string") throw new TypeError("Input must be a string"); if (input === "") return ""; const words = splitWords(input); if (words.length === 0) return ""; return words.map((w) => w.toLowerCase()).join("_"); } function toKebabCase(input) { if (typeof input !== "string") throw new TypeError("Input must be a string"); if (input === "") return ""; const words = splitWords(input); if (words.length === 0) return ""; return words.map((w) => w.toLowerCase()).join("-"); } function toConstantCase(input) { if (typeof input !== "string") throw new TypeError("Input must be a string"); if (input === "") return ""; const words = splitWords(input); if (words.length === 0) return ""; return words.map((w) => w.toUpperCase()).join("_"); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { toCamelCase, toConstantCase, toKebabCase, toPascalCase, toSnakeCase }); //# sourceMappingURL=index.cjs.map