UNPKG

text-no-case

Version:

Convert text to all lowercase letters with spaces between words

41 lines (40 loc) 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.noCase = noCase; const text_lower_case_1 = require("text-lower-case"); // Support camel case ("camelCase" -> "camel Case" and "CAMELCase" -> "CAMEL Case"). // Updated to support Unicode letters const DEFAULT_SPLIT_REGEXP = [ /(\p{Ll}|\p{N})(\p{Lu})/gu, /(\p{Lu})(\p{Lu}\p{Ll})/gu, ]; // Remove all non-word characters, but preserve Unicode letters and numbers. // Updated to use Unicode-aware character classes const DEFAULT_STRIP_REGEXP = /[^\p{L}\p{N}]+/gu; /** * Normalize text into something other libraries can manipulate easier. */ function noCase(input, options = {}) { // Handle null/undefined inputs gracefully if (!input) return ""; const { splitRegexp = DEFAULT_SPLIT_REGEXP, stripRegexp = DEFAULT_STRIP_REGEXP, transform = text_lower_case_1.lowerCase, delimiter = " ", } = options; let result = replace(replace(input, splitRegexp, "$1\0$2"), stripRegexp, "\0"); let start = 0; let end = result.length; // Trim the delimiter from around the output string. while (result.charAt(start) === "\0") start++; while (result.charAt(end - 1) === "\0") end--; // Transform each token independently. return result.slice(start, end).split("\0").map(transform).join(delimiter); } /** * Replace `re` in the input string with the replacement value. */ function replace(input, re, value) { if (re instanceof RegExp) return input.replace(re, value); return re.reduce((input, re) => input.replace(re, value), input); }