detect-case
Version:
Detects the casing of the input string (camelcase, lowercase, snakecase etc).
81 lines (80 loc) • 3.21 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
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);
// index.ts
var index_exports = {};
__export(index_exports, {
WORD_SPLIT_REGEX: () => WORD_SPLIT_REGEX,
WORD_SPLIT_REGEX_STRICT: () => WORD_SPLIT_REGEX_STRICT,
default: () => index_default,
detectCase: () => detectCase,
splitString: () => splitString
});
module.exports = __toCommonJS(index_exports);
var WORD_SPLIT_REGEX_STRICT = /([\p{Lu}]+[\p{Ll}]*|[\p{Ll}]+|[\p{N}]+)/gu;
var WORD_SPLIT_REGEX = /([\p{Lu}(.,-]+[\p{Ll})]*|[\p{Ll}()]+|[\p{N}()]+)/gu;
var splitString = /* @__PURE__ */ __name((input, strict = true) => {
const regex = strict ? WORD_SPLIT_REGEX_STRICT : WORD_SPLIT_REGEX;
return input ? (String(input).match(regex) || []).filter(Boolean) : [];
}, "splitString");
var isPascalCase = /* @__PURE__ */ __name((input) => {
return /^([A-Z][a-zA-Z]*)+$/.test(input);
}, "isPascalCase");
var detectCase = /* @__PURE__ */ __name((input) => {
if (typeof input !== "string") {
throw new TypeError("Expected input to be a string");
}
if (!input) return "unknown";
if (!/[a-z]/i.test(input)) return "unknown";
if (input.includes("_")) {
if (/^([A-Z][A-Z0-9_]+)+$/.test(input)) return "uppersnake";
if (/^([a-z][a-z0-9_]+)+$/.test(input)) return "snakecase";
return "mixedcase";
}
if (input.includes("-")) {
if (/^([a-z][a-z0-9-]+)+$/.test(input)) return "kebabcase";
return "mixedcase";
}
if (/^[a-z ]+$/.test(input)) return "lowercase";
if (/^[A-Z][A-Z0-9_]*$/.test(input)) return "uppercase";
const words = input.split(/\s+/);
if (words.length > 1 && words.every((word) => isPascalCase(word))) {
return "titlecase";
}
if (words.length > 1 && (/^[0-9]+$/.test(words[0]) && isPascalCase(words[1]))) {
return "titlecase";
}
if (words.length > 1 && isPascalCase(words[0])) {
return "sentencecase";
}
if (isPascalCase(input)) return "pascalcase";
if (/^(?:[0-9]+\s+)?(?:[A-Z]\w*\s+)+$/.test(input)) return "titlecase";
if (/^([a-z][a-z0-9]*[A-Z]+[a-z0-9]*)+$/.test(input)) return "camelcase";
if (/[.!?;:,](?!(?:\s|$))/.test(input)) return "unknown";
return "mixedcase";
}, "detectCase");
var index_default = detectCase;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
WORD_SPLIT_REGEX,
WORD_SPLIT_REGEX_STRICT,
detectCase,
splitString
});
//# sourceMappingURL=index.js.map
;