smart-case
Version:
JavaScript library that provides advanced string casing capabilities with intelligent capitalization handling. It offers a comprehensive set of features to convert strings to various casing styles, including title case, sentence case, camel case, snake ca
203 lines • 8.8 kB
JavaScript
/**
* Converts a string to title case.
* @param inputString - The string to convert.
* @returns The title case string.
* @example
* titleCase("hello world"); // "Hello World"
* titleCase("HELLO WORLD"); // "Hello World"
* titleCase("hello world", { wordsToAlwaysUpperCase: ["world"] }); // "Hello WORLD"
*/
export var titleCase = function (inputString, customRules) {
var transformWord = function (word, _, rulesAsSets) {
var newWord = rulesAsSets
? applyCustomRules(word, rulesAsSets)
: undefined;
return newWord !== null && newWord !== void 0 ? newWord : word[0].toUpperCase() + word.slice(1).toLowerCase();
};
return applyRulesAndJoin(inputString, transformWord, " ", customRules);
};
/**
* Converts a string to sentence case.
* @param inputString - The string to convert.
* @returns The sentence case string.
* @example
* sentenceCase("hello world"); // "Hello world"
* sentenceCase("HELLO WORLD"); // "Hello world"
* sentenceCase("hello world", { wordsToAlwaysUpperCase: ["world"] }); // "Hello WORLD"
* sentenceCase("hello world", { wordsToAlwaysLowerCase: ["hello"] }); // "hello world"
*/
export var sentenceCase = function (inputString, customRules) {
if (!inputString)
return ""; // Handle empty string
var transformWord = function (word, index, rulesAsSets) {
if (index === 0) {
var newWord = rulesAsSets
? applyCustomRules(word.toLowerCase(), rulesAsSets)
: undefined;
return ((newWord !== null && newWord !== void 0 ? newWord : word).charAt(0).toUpperCase() +
(newWord !== null && newWord !== void 0 ? newWord : word).slice(1).toLowerCase());
}
else {
var newWord = rulesAsSets
? applyCustomRules(word.toLowerCase(), rulesAsSets)
: word.toLowerCase();
return newWord !== null && newWord !== void 0 ? newWord : word;
}
};
return applyRulesAndJoin(inputString, transformWord, " ", customRules);
};
/**
* Converts a string to camel case.
* @param inputString - The string to convert.
* @returns The camel case string.
* @example
* camelCase("hello world"); // "helloWorld"
* camelCase("HELLO WORLD"); // "helloWorld"
* camelCase("hello world", { wordsToAlwaysUpperCase: ["world"] }); // "helloWORLD"
*/
export var camelCase = function (inputString, customRules) {
var transformWord = function (word, index, rulesAsSets) {
var newWord = rulesAsSets
? applyCustomRules(word, rulesAsSets)
: undefined;
return index === 0
? word.toLowerCase()
: newWord !== null && newWord !== void 0 ? newWord : word[0].toUpperCase() + word.slice(1).toLowerCase();
};
return applyRulesAndJoin(inputString, transformWord, "", customRules);
};
/**
* Converts a string to pascal case.
* @param inputString - The string to convert.
* @returns The pascal case string.
* @example
* pascalCase("hello world"); // "HelloWorld"
* pascalCase("HELLO WORLD"); // "HelloWorld"
* pascalCase("hello world", { wordsToAlwaysUpperCase: ["world"] }); // "HelloWORLD"
*/
export var pascalCase = function (inputString, customRules) {
var transformWord = function (word, _, rulesAsSets) {
var newWord = rulesAsSets
? applyCustomRules(word, rulesAsSets)
: undefined;
return newWord !== null && newWord !== void 0 ? newWord : word[0].toUpperCase() + word.slice(1).toLowerCase();
};
return applyRulesAndJoin(inputString, transformWord, "", customRules);
};
/**
* Converts a string to snake case.
* @param inputString - The string to convert.
* @returns The snake case string.
* @example
* snakeCase("hello world"); // "hello_world"
* snakeCase("HELLO WORLD"); // "hello_world"
* snakeCase("hello world", { wordsToAlwaysUpperCase: ["world"] }); // "hello_WORLD"
*/
export var snakeCase = function (inputString, customRules) {
var transformWord = function (word, _, rulesAsSets) {
var newWord = rulesAsSets
? applyCustomRules(word, rulesAsSets)
: undefined;
return newWord !== null && newWord !== void 0 ? newWord : word.toLowerCase();
};
return applyRulesAndJoin(inputString, transformWord, "_", customRules);
};
/**
* Converts a string to kebab case.
* @param inputString - The string to convert.
* @returns The kebab case string.
* @example
* kebabCase("hello world"); // "hello-world"
* kebabCase("HELLO WORLD"); // "hello-world"
* kebabCase("hello world", { wordsToAlwaysUpperCase: ["world"] }); // "hello-WORLD"
*/
export var kebabCase = function (inputString, customRules) {
var transformWord = function (word, _, rulesAsSets) {
var newWord = rulesAsSets
? applyCustomRules(word, rulesAsSets)
: undefined;
return newWord !== null && newWord !== void 0 ? newWord : word.toLowerCase();
};
return applyRulesAndJoin(inputString, transformWord, "-", customRules);
};
/**
* Converts a string to constant case.
* @param inputString - The string to convert.
* @returns The constant case string.
* @example
* constantCase("hello world"); // "HELLO_WORLD"
* constantCase("HELLO WORLD"); // "HELLO_WORLD"
* constantCase("hello world", { wordsToAlwaysUpperCase: ["world"] }); // "HELLO_WORLD"
*/
export var constantCase = function (inputString, customRules) {
var transformWord = function (word, _, rulesAsSets) {
var newWord = rulesAsSets
? applyCustomRules(word, rulesAsSets)
: undefined;
return newWord !== null && newWord !== void 0 ? newWord : word.toUpperCase();
};
return applyRulesAndJoin(inputString, transformWord, "_", customRules);
};
/**
* Converts a string to dot case.
* @param inputString - The string to convert.
* @returns The dot case string.
* @example
* dotCase("hello world"); // "hello.world"
* dotCase("HELLO WORLD"); // "hello.world"
* dotCase("hello world", { wordsToAlwaysUpperCase: ["world"] }); // "hello.WORLD"
*/
export var dotCase = function (inputString, customRules) {
var transformWord = function (word, _, rulesAsSets) {
var newWord = rulesAsSets
? applyCustomRules(word, rulesAsSets)
: undefined;
return newWord !== null && newWord !== void 0 ? newWord : word.toLowerCase();
};
return applyRulesAndJoin(inputString, transformWord, ".", customRules);
};
/**
* Converts a string to path case.
* @param inputString - The string to convert.
* @returns The path case string.
* @example
* pathCase("hello world"); // "hello/world"
* pathCase("HELLO WORLD"); // "hello/world"
* pathCase("hello world", { wordsToAlwaysUpperCase: ["world"] }); // "hello/WORLD"
*/
export var pathCase = function (inputString, customRules) {
var transformWord = function (word, _, rulesAsSets) {
var newWord = rulesAsSets
? applyCustomRules(word, rulesAsSets)
: undefined;
return newWord !== null && newWord !== void 0 ? newWord : word.toLowerCase();
};
return applyRulesAndJoin(inputString, transformWord, "/", customRules);
};
var applyCustomRules = function (word, customRules) {
var wordsToAlwaysCapitalize = customRules.wordsToAlwaysCapitalize, wordsToAlwaysLowerCase = customRules.wordsToAlwaysLowerCase, wordsToAlwaysUpperCase = customRules.wordsToAlwaysUpperCase, wordsToNeverTransform = customRules.wordsToNeverTransform;
if (wordsToAlwaysCapitalize === null || wordsToAlwaysCapitalize === void 0 ? void 0 : wordsToAlwaysCapitalize.has(word))
return "".concat(word[0].toUpperCase()).concat(word.slice(1).toLowerCase());
if (wordsToAlwaysLowerCase === null || wordsToAlwaysLowerCase === void 0 ? void 0 : wordsToAlwaysLowerCase.has(word))
return word.toLowerCase();
if (wordsToAlwaysUpperCase === null || wordsToAlwaysUpperCase === void 0 ? void 0 : wordsToAlwaysUpperCase.has(word))
return word.toUpperCase();
if (wordsToNeverTransform === null || wordsToNeverTransform === void 0 ? void 0 : wordsToNeverTransform.has(word))
return word;
return undefined;
};
var convertRulesToSets = function (customRulesAsArray) {
var rulesWithSets = {};
Object.keys(customRulesAsArray).forEach(function (key) {
rulesWithSets[key] = new Set(customRulesAsArray[key]);
});
return rulesWithSets;
};
var applyRulesAndJoin = function (inputString, transform, delimiter, customRules) {
var rulesAsSets = customRules ? convertRulesToSets(customRules) : undefined;
return inputString
.split(" ")
.map(function (word, index) { return transform(word, index, rulesAsSets); })
.join(delimiter);
};
//# sourceMappingURL=smart-case.js.map