UNPKG

text-case-converter

Version:

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

1 lines 7.32 kB
{"version":3,"sources":["../src/index.ts","../src/utils.ts"],"sourcesContent":["import { splitWords, capitalize, lowerFirst } from './utils';\nimport type { CaseType } from './types';\n\n/**\n * Converts an input string to camelCase.\n * e.g., \"hello world\" -> \"helloWorld\"\n * @param input The string to convert.\n * @returns The camelCased string.\n * @throws {TypeError} If input is not a string.\n */\nexport function toCamelCase(input: string): string {\n if (typeof input !== 'string') throw new TypeError('Input must be a string');\n if (input === '') return '';\n const words = splitWords(input);\n if (words.length === 0) return '';\n return [lowerFirst(words[0]), ...words.slice(1).map(capitalize)].join('');\n}\n\n/**\n * Converts an input string to PascalCase (UpperCamelCase).\n * e.g., \"hello world\" -> \"HelloWorld\"\n * @param input The string to convert.\n * @returns The PascalCased string.\n * @throws {TypeError} If input is not a string.\n */\nexport function toPascalCase(input: string): string {\n if (typeof input !== 'string') throw new TypeError('Input must be a string');\n if (input === '') return '';\n const words = splitWords(input);\n if (words.length === 0) return '';\n return words.map(capitalize).join('');\n}\n\n/**\n * Converts an input string to snake_case.\n * e.g., \"hello world\" -> \"hello_world\"\n * @param input The string to convert.\n * @returns The snake_cased string.\n * @throws {TypeError} If input is not a string.\n */\nexport function toSnakeCase(input: string): string {\n if (typeof input !== 'string') throw new TypeError('Input must be a string');\n if (input === '') return '';\n const words = splitWords(input);\n if (words.length === 0) return '';\n return words.map((w) => w.toLowerCase()).join('_');\n}\n\n/**\n * Converts an input string to kebab-case.\n * e.g., \"hello world\" -> \"hello-world\"\n * @param input The string to convert.\n * @returns The kebab-cased string.\n * @throws {TypeError} If input is not a string.\n */\nexport function toKebabCase(input: string): string {\n if (typeof input !== 'string') throw new TypeError('Input must be a string');\n if (input === '') return '';\n const words = splitWords(input);\n if (words.length === 0) return '';\n return words.map((w) => w.toLowerCase()).join('-');\n}\n\n/**\n * Converts an input string to CONSTANT_CASE (SCREAMING_SNAKE_CASE).\n * e.g., \"hello world\" -> \"HELLO_WORLD\"\n * @param input The string to convert.\n * @returns The CONSTANT_CASED string.\n * @throws {TypeError} If input is not a string.\n */\nexport function toConstantCase(input: string): string {\n if (typeof input !== 'string') throw new TypeError('Input must be a string');\n if (input === '') return '';\n const words = splitWords(input);\n if (words.length === 0) return '';\n return words.map((w) => w.toUpperCase()).join('_');\n}\n\nexport type { CaseType };\n","/**\n * Splits a string into words based on spaces, hyphens, underscores, and case transitions.\n * Handles acronyms and numbers.\n * @param input The string to split.\n * @returns Array of words.\n */\nexport function splitWords(input: string): string[] {\n if (typeof input !== 'string') throw new TypeError('Input must be a string');\n // Normalize delimiters to spaces\n const processed = input\n .replace(/[_\\-\\s]+/g, ' ')\n .replace(/[^a-zA-Z0-9 ]+/g, ' ')\n .trim();\n\n // Split by spaces\n const words = processed.length ? processed.split(/\\s+/) : [];\n\n // Further split each word by camelCase, PascalCase, and acronym boundaries\n const result: string[] = [];\n for (const word of words) {\n // Split on transitions from lowercase to uppercase or acronym to normal word\n const parts = word\n .replace(/([a-z0-9])([A-Z])/g, '$1 $2')\n .replace(/([A-Z]+)([A-Z][a-z0-9]+)/g, '$1 $2')\n .split(' ');\n for (const part of parts) {\n // Lowercase all words except for single-character words\n if (part.length > 1) {\n result.push(part.toLowerCase());\n } else {\n result.push(part);\n }\n }\n }\n\n // For version1_2_3, split numbers after underscores only\n const final: string[] = [];\n for (const w of result) {\n // If the word is like 'version1', keep as is; if just numbers, keep as is\n if (/^[a-zA-Z]+[0-9]+$/.test(w) || /^[0-9]+$/.test(w)) {\n final.push(w);\n } else if (/^[a-zA-Z]+$/.test(w)) {\n final.push(w);\n } else {\n // For mixed, split numbers and letters\n const match = w.match(/[a-zA-Z]+|[0-9]+/g);\n if (match) final.push(...match);\n }\n }\n\n return final;\n}\n\n/**\n * Capitalizes the first character of a word and lowercases the rest.\n * @param word The word to capitalize.\n * @returns Capitalized word.\n */\nexport function capitalize(word: string): string {\n return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();\n}\n\n/**\n * Lowercases the first character of a word and leaves the rest as is.\n * @param word The word to modify.\n * @returns Word with first character lowercased.\n */\nexport function lowerFirst(word: string): string {\n return word.charAt(0).toLowerCase() + word.slice(1);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMO,SAAS,WAAW,OAAyB;AAClD,MAAI,OAAO,UAAU,SAAU,OAAM,IAAI,UAAU,wBAAwB;AAE3E,QAAM,YAAY,MACf,QAAQ,aAAa,GAAG,EACxB,QAAQ,mBAAmB,GAAG,EAC9B,KAAK;AAGR,QAAM,QAAQ,UAAU,SAAS,UAAU,MAAM,KAAK,IAAI,CAAC;AAG3D,QAAM,SAAmB,CAAC;AAC1B,aAAW,QAAQ,OAAO;AAExB,UAAM,QAAQ,KACX,QAAQ,sBAAsB,OAAO,EACrC,QAAQ,6BAA6B,OAAO,EAC5C,MAAM,GAAG;AACZ,eAAW,QAAQ,OAAO;AAExB,UAAI,KAAK,SAAS,GAAG;AACnB,eAAO,KAAK,KAAK,YAAY,CAAC;AAAA,MAChC,OAAO;AACL,eAAO,KAAK,IAAI;AAAA,MAClB;AAAA,IACF;AAAA,EACF;AAGA,QAAM,QAAkB,CAAC;AACzB,aAAW,KAAK,QAAQ;AAEtB,QAAI,oBAAoB,KAAK,CAAC,KAAK,WAAW,KAAK,CAAC,GAAG;AACrD,YAAM,KAAK,CAAC;AAAA,IACd,WAAW,cAAc,KAAK,CAAC,GAAG;AAChC,YAAM,KAAK,CAAC;AAAA,IACd,OAAO;AAEL,YAAM,QAAQ,EAAE,MAAM,mBAAmB;AACzC,UAAI,MAAO,OAAM,KAAK,GAAG,KAAK;AAAA,IAChC;AAAA,EACF;AAEA,SAAO;AACT;AAOO,SAAS,WAAW,MAAsB;AAC/C,SAAO,KAAK,OAAO,CAAC,EAAE,YAAY,IAAI,KAAK,MAAM,CAAC,EAAE,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