UNPKG

microcms_sdk_generator

Version:
26 lines (25 loc) 1.01 kB
import lowerCase from "./lowerCase.js"; import nonWordRegexp from "./vendor/nonWordRegexp.js"; import camelCaseRegexp from "./vendor/camelCaseRegexp.js"; import camelCaseUpperRegexp from "./vendor/camelCaseUpperRegexp.js"; export default function normalCase(str, locale, replacement) { if (str == null) { return ""; } replacement = typeof replacement !== "string" ? " " : replacement; function replace(match, index, value) { if (index === 0 || index === value.length - match.length) { return ""; } return replacement; } str = String(str) // Support camel case ("camelCase" -> "camel Case"). .replace(camelCaseRegexp, "$1 $2") // Support odd camel case ("CAMELCase" -> "CAMEL Case"). .replace(camelCaseUpperRegexp, "$1 $2") // Remove all non-word characters and replace with a single space. .replace(nonWordRegexp, replace); // Lower case the entire string. return lowerCase(str, locale); }