case-anything
Advanced tools
Comparing version 1.1.5 to 2.0.0
@@ -6,144 +6,309 @@ // Latin-1 Supplement | ||
// [à-öø-ÿ] | ||
const magicSplit = /^[a-zà-öø-ÿ]+|[A-ZÀ-ÖØ-ß][a-zà-öø-ÿ]+|[a-zà-öø-ÿ]+|[0-9]+|[A-ZÀ-ÖØ-ß]+(?![a-zà-öø-ÿ])/g; | ||
const spaceSplit = /\S+/g; | ||
/** | ||
* A string.match function that will return an array of "string parts" | ||
* | ||
* @param {string} string | ||
* @returns {string[]} | ||
* A string.matchAll function that will return an array of "string parts" and the indexes at which it split each part | ||
*/ | ||
function splitOnSpecialChars(string) { | ||
return string.match(/^[a-zà-öø-ÿ]+|[A-ZÀ-ÖØ-ß][a-zà-öø-ÿ]+|[a-zà-öø-ÿ]+|[0-9]+|[A-ZÀ-ÖØ-ß]+(?![a-zà-öø-ÿ])/g); | ||
function getPartsAndIndexes(string, splitRegex) { | ||
const result = { parts: [], prefixes: [] }; | ||
const matches = string.matchAll(splitRegex); | ||
let lastWordEndIndex = 0; | ||
for (const match of matches) { | ||
result.parts.push(match[0]); | ||
const prefix = string.slice(lastWordEndIndex, match.index).trim(); | ||
result.prefixes.push(prefix); | ||
lastWordEndIndex = match.index + match[0].length; | ||
} | ||
return result; | ||
} | ||
/** | ||
* A string.match function that will return an array of "string parts" | ||
* | ||
* @param {string} string | ||
* @returns {string[]} | ||
* A function that splits a string on words and returns an array of words. | ||
* - It can prefix each word with a given character | ||
* - It can strip or keep special characters, this affects the logic for adding a prefix as well | ||
*/ | ||
function getParts(string, noSpecialChars) { | ||
if (noSpecialChars === void 0) { noSpecialChars = false; } | ||
var target = string.trim().normalize('NFC'); | ||
var parts = target.includes(' ') ? target.split(' ').filter(Boolean) : splitOnSpecialChars(target); | ||
return noSpecialChars ? parts.map(function (part) { return part.normalize('NFD').replace(/[^a-zA-ZØßø0-9]/g, ''); }) : parts; | ||
function splitAndPrefix(string, options) { | ||
const { keepSpecialCharacters = true, prefix = '' } = options; | ||
const normalString = string.trim().normalize('NFC'); | ||
const hasSpaces = normalString.includes(' '); | ||
const split = hasSpaces ? spaceSplit : magicSplit; | ||
const partsAndIndexes = getPartsAndIndexes(normalString, split); | ||
return keepSpecialCharacters | ||
? partsAndIndexes.parts.map((part, i) => { | ||
const _prefix = partsAndIndexes.prefixes[i]; | ||
// the first word doesn't need a prefix, so only return the original prefix | ||
if (i === 0) { | ||
return _prefix + part; | ||
} | ||
// space based sentence was split on spaces, so only return original prefixes | ||
if (hasSpaces) { | ||
if (!_prefix && prefix.match(/\s/)) { | ||
// in this case we have no more original _prefix, it was trimmed, but we're looking to add a space | ||
// so let's return that space | ||
return prefix + part; | ||
} | ||
return _prefix + part; | ||
} | ||
// return the original prefix OR fall back to a given prefix | ||
return (_prefix || prefix) + part; | ||
}) | ||
: partsAndIndexes.parts.map((part, i) => { | ||
const _part = part.normalize('NFD').replace(/[^a-zA-ZØßø0-9]/g, ''); | ||
if (i === 0) { | ||
return _part; | ||
} | ||
return prefix + _part; | ||
}); | ||
} | ||
/** | ||
* Capitalises a single word | ||
* | ||
* @export | ||
* @param {string} string the word | ||
* @returns {string} the word with the first character in uppercase and the rest in lowercase | ||
* @returns the word with the first character in uppercase and the rest in lowercase | ||
*/ | ||
function capitaliseWord(string) { | ||
return string[0].toUpperCase() + string.slice(1).toLowerCase(); | ||
var _a; | ||
const firstLetterIndex = ((_a = string.matchAll(magicSplit).next().value) === null || _a === void 0 ? void 0 : _a.index) || 0; | ||
return string.slice(0, firstLetterIndex + 1).toUpperCase() + string.slice(firstLetterIndex + 1).toLowerCase(); | ||
} | ||
var noSpecialChars = true; | ||
/** | ||
* converts strings to camelCase | ||
* # 🐪 camelCase | ||
* converts a string to camelCase | ||
* - first lowercase then all capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in camelCase | ||
* @example | ||
* camelCase('$catDog') === 'catDog' | ||
* @example | ||
* camelCase('$catDog', { keepSpecialCharacters: true }) === '$catDog' | ||
*/ | ||
function camelCase(string) { | ||
return getParts(string, noSpecialChars).reduce(function (result, match, index) { | ||
return index === 0 ? match.toLowerCase() : result + capitaliseWord(match); | ||
function camelCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, options).reduce((result, word, index) => { | ||
return index === 0 || !(word[0] || '').match(magicSplit) | ||
? result + word.toLowerCase() | ||
: result + capitaliseWord(word); | ||
}, ''); | ||
} | ||
/** | ||
* converts strings to PascalCase | ||
* # 🐫 PascalCase | ||
* converts a string to PascalCase (also called UpperCamelCase) | ||
* - all capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in PascalCase | ||
* @example | ||
* pascalCase('$catDog') === 'CatDog' | ||
* @example | ||
* pascalCase('$catDog', { keepSpecialCharacters: true }) === '$CatDog' | ||
*/ | ||
function pascalCase(string) { | ||
return getParts(string, noSpecialChars).reduce(function (result, match) { | ||
return result + capitaliseWord(match); | ||
function pascalCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, options).reduce((result, word) => { | ||
return result + capitaliseWord(word); | ||
}, ''); | ||
} | ||
/** | ||
* converts strings to kebab-case | ||
* # 🐫 UpperCamelCase | ||
* converts a string to UpperCamelCase (also called PascalCase) | ||
* - all capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in kebab-case | ||
* @example | ||
* upperCamelCase('$catDog') === 'CatDog' | ||
* @example | ||
* upperCamelCase('$catDog', { keepSpecialCharacters: true }) === '$CatDog' | ||
*/ | ||
function kebabCase(string) { | ||
return getParts(string, noSpecialChars).join('-').toLowerCase(); | ||
const upperCamelCase = pascalCase; | ||
/** | ||
* # 🥙 kebab-case | ||
* converts a string to kebab-case | ||
* - hyphenated lowercase | ||
* - *strips away* special characters by default | ||
* | ||
* @example | ||
* kebabCase('$catDog') === 'cat-dog' | ||
* @example | ||
* kebabCase('$catDog', { keepSpecialCharacters: true }) === '$cat-dog' | ||
*/ | ||
function kebabCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '-' })) | ||
.join('') | ||
.toLowerCase(); | ||
} | ||
/** | ||
* converts strings to snake_case | ||
* # 🐍 snake_case | ||
* converts a string to snake_case | ||
* - underscored lowercase | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in snake_case | ||
* @example | ||
* snakeCase('$catDog') === 'cat_dog' | ||
* @example | ||
* snakeCase('$catDog', { keepSpecialCharacters: true }) === '$cat_dog' | ||
*/ | ||
function snakeCase(string) { | ||
return getParts(string, noSpecialChars).join('_').toLowerCase(); | ||
function snakeCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '_' })) | ||
.join('') | ||
.toLowerCase(); | ||
} | ||
/** | ||
* converts strings to CONSTANT_CASE | ||
* # 📣 CONSTANT_CASE | ||
* converts a string to CONSTANT_CASE | ||
* - underscored uppercase | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in CONSTANT_CASE | ||
* @example | ||
* constantCase('$catDog') === 'CAT_DOG' | ||
* @example | ||
* constantCase('$catDog', { keepSpecialCharacters: true }) === '$CAT_DOG' | ||
*/ | ||
function constantCase(string) { | ||
return getParts(string, noSpecialChars).join('_').toUpperCase(); | ||
function constantCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '_' })) | ||
.join('') | ||
.toUpperCase(); | ||
} | ||
/** | ||
* converts strings to path/case | ||
* # 🚂 Train-Case | ||
* converts strings to Train-Case | ||
* - hyphenated & capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in path/case | ||
* @example | ||
* trainCase('$catDog') === 'Cat-Dog' | ||
* @example | ||
* trainCase('$catDog', { keepSpecialCharacters: true }) === '$Cat-Dog' | ||
*/ | ||
function pathCase(string) { | ||
return getParts(string).join('/'); | ||
function trainCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '-' })) | ||
.map((word) => capitaliseWord(word)) | ||
.join(''); | ||
} | ||
/** | ||
* converts strings to space case (will add spaces but not change casing) | ||
* # 🕊 Ada_Case | ||
* converts a string to Ada_Case | ||
* - underscored & capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in path case | ||
* @example | ||
* adaCase('$catDog') === 'Cat_Dog' | ||
* @example | ||
* adaCase('$catDog', { keepSpecialCharacters: true }) === '$Cat_Dog' | ||
*/ | ||
function spaceCase(string) { | ||
return getParts(string).join(' '); | ||
function adaCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '_' })) | ||
.map((part) => capitaliseWord(part)) | ||
.join(''); | ||
} | ||
/** | ||
* converts strings to Capital Case (with spaces) | ||
* # 👔 COBOL-CASE | ||
* converts a string to COBOL-CASE | ||
* - hyphenated uppercase | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in Capital Case (with spaces) | ||
* @example | ||
* cobolCase('$catDog') === 'CAT-DOG' | ||
* @example | ||
* cobolCase('$catDog', { keepSpecialCharacters: true }) === '$CAT-DOG' | ||
*/ | ||
function capitalCase(string) { | ||
return getParts(string) | ||
.reduce(function (result, match) { | ||
return "".concat(result, " ").concat(capitaliseWord(match)); | ||
}, '') | ||
.trim(); | ||
function cobolCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '-' })) | ||
.join('') | ||
.toUpperCase(); | ||
} | ||
/** | ||
* converts strings to lower case (with spaces) | ||
* # 📍 Dot.notation | ||
* converts a string to dot.notation | ||
* - adds dots, does not change casing | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in lower case (with spaces) | ||
* @example | ||
* dotNotation('$catDog') === 'cat.Dog' | ||
* @example | ||
* dotNotation('$catDog', { keepSpecialCharacters: true }) === '$cat.Dog' | ||
*/ | ||
function lowerCase(string) { | ||
return getParts(string).join(' ').toLowerCase(); | ||
function dotNotation(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '.' })).join(''); | ||
} | ||
/** | ||
* converts strings to UPPER CASE (with spaces) | ||
* # 📂 Path/case | ||
* converts a string to path/case | ||
* - adds slashes, does not change casing | ||
* - *keeps* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in UPPER CASE (with spaces) | ||
* @example | ||
* pathCase('$catDog') === '$cat/Dog' | ||
* @example | ||
* pathCase('$catDog', { keepSpecialCharacters: false }) === 'cat/Dog' | ||
*/ | ||
function upperCase(string) { | ||
return getParts(string).join(' ').toUpperCase(); | ||
function pathCase(string, options = { keepSpecialCharacters: true }) { | ||
return splitAndPrefix(string, options).reduce((result, word, i) => { | ||
const prefix = i === 0 || word[0] === '/' ? '' : '/'; | ||
return result + prefix + word; | ||
}, ''); | ||
} | ||
/** | ||
* # 🛰 Space case | ||
* converts a string to space case | ||
* - adds spaces, does not change casing | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* spaceCase('$catDog') === '$cat Dog' | ||
* @example | ||
* spaceCase('$catDog', { keepSpecialCharacters: false }) === 'cat Dog' | ||
*/ | ||
function spaceCase(string, options = { keepSpecialCharacters: true }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: ' ' })).join(''); | ||
} | ||
/** | ||
* # 🏛 Capital Case | ||
* converts a string to Capital Case | ||
* - capitalizes words and adds spaces | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* capitalCase('$catDog') === '$Cat Dog' | ||
* @example | ||
* capitalCase('$catDog', { keepSpecialCharacters: false }) === 'Cat Dog' | ||
* | ||
* ⟪ if you do not want to add spaces, use `pascalCase()` ⟫ | ||
*/ | ||
function capitalCase(string, options = { keepSpecialCharacters: true }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: ' ' })).reduce((result, word) => { | ||
return result + capitaliseWord(word); | ||
}, ''); | ||
} | ||
/** | ||
* # 🔡 lower case | ||
* converts a string to lower case | ||
* - makes words lowercase and adds spaces | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* lowerCase('$catDog') === '$cat dog' | ||
* @example | ||
* lowerCase('$catDog', { keepSpecialCharacters: false }) === 'cat dog' | ||
* | ||
* ⟪ if you do not want to add spaces, use the native JS `toLowerCase()` ⟫ | ||
*/ | ||
function lowerCase(string, options = { keepSpecialCharacters: true }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: ' ' })) | ||
.join('') | ||
.toLowerCase(); | ||
} | ||
/** | ||
* # 🔠 UPPER CASE | ||
* converts a string to UPPER CASE | ||
* - makes words upper case and adds spaces | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* upperCase('$catDog') === '$CAT DOG' | ||
* @example | ||
* upperCase('$catDog', { keepSpecialCharacters: false }) === 'CAT DOG' | ||
* | ||
* ⟪ if you do not want to add spaces, use the native JS `toUpperCase()` ⟫ | ||
*/ | ||
function upperCase(string, options = { keepSpecialCharacters: true }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: ' ' })) | ||
.join('') | ||
.toUpperCase(); | ||
} | ||
export { camelCase, capitalCase, constantCase, kebabCase, lowerCase, pascalCase, pathCase, snakeCase, spaceCase, upperCase }; | ||
export { adaCase, camelCase, capitalCase, cobolCase, constantCase, dotNotation, kebabCase, lowerCase, pascalCase, pathCase, snakeCase, spaceCase, trainCase, upperCamelCase, upperCase }; |
@@ -12,147 +12,315 @@ (function (global, factory) { | ||
// [à-öø-ÿ] | ||
const magicSplit = /^[a-zà-öø-ÿ]+|[A-ZÀ-ÖØ-ß][a-zà-öø-ÿ]+|[a-zà-öø-ÿ]+|[0-9]+|[A-ZÀ-ÖØ-ß]+(?![a-zà-öø-ÿ])/g; | ||
const spaceSplit = /\S+/g; | ||
/** | ||
* A string.match function that will return an array of "string parts" | ||
* | ||
* @param {string} string | ||
* @returns {string[]} | ||
* A string.matchAll function that will return an array of "string parts" and the indexes at which it split each part | ||
*/ | ||
function splitOnSpecialChars(string) { | ||
return string.match(/^[a-zà-öø-ÿ]+|[A-ZÀ-ÖØ-ß][a-zà-öø-ÿ]+|[a-zà-öø-ÿ]+|[0-9]+|[A-ZÀ-ÖØ-ß]+(?![a-zà-öø-ÿ])/g); | ||
function getPartsAndIndexes(string, splitRegex) { | ||
const result = { parts: [], prefixes: [] }; | ||
const matches = string.matchAll(splitRegex); | ||
let lastWordEndIndex = 0; | ||
for (const match of matches) { | ||
result.parts.push(match[0]); | ||
const prefix = string.slice(lastWordEndIndex, match.index).trim(); | ||
result.prefixes.push(prefix); | ||
lastWordEndIndex = match.index + match[0].length; | ||
} | ||
return result; | ||
} | ||
/** | ||
* A string.match function that will return an array of "string parts" | ||
* | ||
* @param {string} string | ||
* @returns {string[]} | ||
* A function that splits a string on words and returns an array of words. | ||
* - It can prefix each word with a given character | ||
* - It can strip or keep special characters, this affects the logic for adding a prefix as well | ||
*/ | ||
function getParts(string, noSpecialChars) { | ||
if (noSpecialChars === void 0) { noSpecialChars = false; } | ||
var target = string.trim().normalize('NFC'); | ||
var parts = target.includes(' ') ? target.split(' ').filter(Boolean) : splitOnSpecialChars(target); | ||
return noSpecialChars ? parts.map(function (part) { return part.normalize('NFD').replace(/[^a-zA-ZØßø0-9]/g, ''); }) : parts; | ||
function splitAndPrefix(string, options) { | ||
const { keepSpecialCharacters = true, prefix = '' } = options; | ||
const normalString = string.trim().normalize('NFC'); | ||
const hasSpaces = normalString.includes(' '); | ||
const split = hasSpaces ? spaceSplit : magicSplit; | ||
const partsAndIndexes = getPartsAndIndexes(normalString, split); | ||
return keepSpecialCharacters | ||
? partsAndIndexes.parts.map((part, i) => { | ||
const _prefix = partsAndIndexes.prefixes[i]; | ||
// the first word doesn't need a prefix, so only return the original prefix | ||
if (i === 0) { | ||
return _prefix + part; | ||
} | ||
// space based sentence was split on spaces, so only return original prefixes | ||
if (hasSpaces) { | ||
if (!_prefix && prefix.match(/\s/)) { | ||
// in this case we have no more original _prefix, it was trimmed, but we're looking to add a space | ||
// so let's return that space | ||
return prefix + part; | ||
} | ||
return _prefix + part; | ||
} | ||
// return the original prefix OR fall back to a given prefix | ||
return (_prefix || prefix) + part; | ||
}) | ||
: partsAndIndexes.parts.map((part, i) => { | ||
const _part = part.normalize('NFD').replace(/[^a-zA-ZØßø0-9]/g, ''); | ||
if (i === 0) { | ||
return _part; | ||
} | ||
return prefix + _part; | ||
}); | ||
} | ||
/** | ||
* Capitalises a single word | ||
* | ||
* @export | ||
* @param {string} string the word | ||
* @returns {string} the word with the first character in uppercase and the rest in lowercase | ||
* @returns the word with the first character in uppercase and the rest in lowercase | ||
*/ | ||
function capitaliseWord(string) { | ||
return string[0].toUpperCase() + string.slice(1).toLowerCase(); | ||
var _a; | ||
const firstLetterIndex = ((_a = string.matchAll(magicSplit).next().value) === null || _a === void 0 ? void 0 : _a.index) || 0; | ||
return string.slice(0, firstLetterIndex + 1).toUpperCase() + string.slice(firstLetterIndex + 1).toLowerCase(); | ||
} | ||
var noSpecialChars = true; | ||
/** | ||
* converts strings to camelCase | ||
* # 🐪 camelCase | ||
* converts a string to camelCase | ||
* - first lowercase then all capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in camelCase | ||
* @example | ||
* camelCase('$catDog') === 'catDog' | ||
* @example | ||
* camelCase('$catDog', { keepSpecialCharacters: true }) === '$catDog' | ||
*/ | ||
function camelCase(string) { | ||
return getParts(string, noSpecialChars).reduce(function (result, match, index) { | ||
return index === 0 ? match.toLowerCase() : result + capitaliseWord(match); | ||
function camelCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, options).reduce((result, word, index) => { | ||
return index === 0 || !(word[0] || '').match(magicSplit) | ||
? result + word.toLowerCase() | ||
: result + capitaliseWord(word); | ||
}, ''); | ||
} | ||
/** | ||
* converts strings to PascalCase | ||
* # 🐫 PascalCase | ||
* converts a string to PascalCase (also called UpperCamelCase) | ||
* - all capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in PascalCase | ||
* @example | ||
* pascalCase('$catDog') === 'CatDog' | ||
* @example | ||
* pascalCase('$catDog', { keepSpecialCharacters: true }) === '$CatDog' | ||
*/ | ||
function pascalCase(string) { | ||
return getParts(string, noSpecialChars).reduce(function (result, match) { | ||
return result + capitaliseWord(match); | ||
function pascalCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, options).reduce((result, word) => { | ||
return result + capitaliseWord(word); | ||
}, ''); | ||
} | ||
/** | ||
* converts strings to kebab-case | ||
* # 🐫 UpperCamelCase | ||
* converts a string to UpperCamelCase (also called PascalCase) | ||
* - all capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in kebab-case | ||
* @example | ||
* upperCamelCase('$catDog') === 'CatDog' | ||
* @example | ||
* upperCamelCase('$catDog', { keepSpecialCharacters: true }) === '$CatDog' | ||
*/ | ||
function kebabCase(string) { | ||
return getParts(string, noSpecialChars).join('-').toLowerCase(); | ||
const upperCamelCase = pascalCase; | ||
/** | ||
* # 🥙 kebab-case | ||
* converts a string to kebab-case | ||
* - hyphenated lowercase | ||
* - *strips away* special characters by default | ||
* | ||
* @example | ||
* kebabCase('$catDog') === 'cat-dog' | ||
* @example | ||
* kebabCase('$catDog', { keepSpecialCharacters: true }) === '$cat-dog' | ||
*/ | ||
function kebabCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '-' })) | ||
.join('') | ||
.toLowerCase(); | ||
} | ||
/** | ||
* converts strings to snake_case | ||
* # 🐍 snake_case | ||
* converts a string to snake_case | ||
* - underscored lowercase | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in snake_case | ||
* @example | ||
* snakeCase('$catDog') === 'cat_dog' | ||
* @example | ||
* snakeCase('$catDog', { keepSpecialCharacters: true }) === '$cat_dog' | ||
*/ | ||
function snakeCase(string) { | ||
return getParts(string, noSpecialChars).join('_').toLowerCase(); | ||
function snakeCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '_' })) | ||
.join('') | ||
.toLowerCase(); | ||
} | ||
/** | ||
* converts strings to CONSTANT_CASE | ||
* # 📣 CONSTANT_CASE | ||
* converts a string to CONSTANT_CASE | ||
* - underscored uppercase | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in CONSTANT_CASE | ||
* @example | ||
* constantCase('$catDog') === 'CAT_DOG' | ||
* @example | ||
* constantCase('$catDog', { keepSpecialCharacters: true }) === '$CAT_DOG' | ||
*/ | ||
function constantCase(string) { | ||
return getParts(string, noSpecialChars).join('_').toUpperCase(); | ||
function constantCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '_' })) | ||
.join('') | ||
.toUpperCase(); | ||
} | ||
/** | ||
* converts strings to path/case | ||
* # 🚂 Train-Case | ||
* converts strings to Train-Case | ||
* - hyphenated & capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in path/case | ||
* @example | ||
* trainCase('$catDog') === 'Cat-Dog' | ||
* @example | ||
* trainCase('$catDog', { keepSpecialCharacters: true }) === '$Cat-Dog' | ||
*/ | ||
function pathCase(string) { | ||
return getParts(string).join('/'); | ||
function trainCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '-' })) | ||
.map((word) => capitaliseWord(word)) | ||
.join(''); | ||
} | ||
/** | ||
* converts strings to space case (will add spaces but not change casing) | ||
* # 🕊 Ada_Case | ||
* converts a string to Ada_Case | ||
* - underscored & capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in path case | ||
* @example | ||
* adaCase('$catDog') === 'Cat_Dog' | ||
* @example | ||
* adaCase('$catDog', { keepSpecialCharacters: true }) === '$Cat_Dog' | ||
*/ | ||
function spaceCase(string) { | ||
return getParts(string).join(' '); | ||
function adaCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '_' })) | ||
.map((part) => capitaliseWord(part)) | ||
.join(''); | ||
} | ||
/** | ||
* converts strings to Capital Case (with spaces) | ||
* # 👔 COBOL-CASE | ||
* converts a string to COBOL-CASE | ||
* - hyphenated uppercase | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in Capital Case (with spaces) | ||
* @example | ||
* cobolCase('$catDog') === 'CAT-DOG' | ||
* @example | ||
* cobolCase('$catDog', { keepSpecialCharacters: true }) === '$CAT-DOG' | ||
*/ | ||
function capitalCase(string) { | ||
return getParts(string) | ||
.reduce(function (result, match) { | ||
return "".concat(result, " ").concat(capitaliseWord(match)); | ||
}, '') | ||
.trim(); | ||
function cobolCase(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '-' })) | ||
.join('') | ||
.toUpperCase(); | ||
} | ||
/** | ||
* converts strings to lower case (with spaces) | ||
* # 📍 Dot.notation | ||
* converts a string to dot.notation | ||
* - adds dots, does not change casing | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in lower case (with spaces) | ||
* @example | ||
* dotNotation('$catDog') === 'cat.Dog' | ||
* @example | ||
* dotNotation('$catDog', { keepSpecialCharacters: true }) === '$cat.Dog' | ||
*/ | ||
function lowerCase(string) { | ||
return getParts(string).join(' ').toLowerCase(); | ||
function dotNotation(string, options = { keepSpecialCharacters: false }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: '.' })).join(''); | ||
} | ||
/** | ||
* converts strings to UPPER CASE (with spaces) | ||
* # 📂 Path/case | ||
* converts a string to path/case | ||
* - adds slashes, does not change casing | ||
* - *keeps* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in UPPER CASE (with spaces) | ||
* @example | ||
* pathCase('$catDog') === '$cat/Dog' | ||
* @example | ||
* pathCase('$catDog', { keepSpecialCharacters: false }) === 'cat/Dog' | ||
*/ | ||
function upperCase(string) { | ||
return getParts(string).join(' ').toUpperCase(); | ||
function pathCase(string, options = { keepSpecialCharacters: true }) { | ||
return splitAndPrefix(string, options).reduce((result, word, i) => { | ||
const prefix = i === 0 || word[0] === '/' ? '' : '/'; | ||
return result + prefix + word; | ||
}, ''); | ||
} | ||
/** | ||
* # 🛰 Space case | ||
* converts a string to space case | ||
* - adds spaces, does not change casing | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* spaceCase('$catDog') === '$cat Dog' | ||
* @example | ||
* spaceCase('$catDog', { keepSpecialCharacters: false }) === 'cat Dog' | ||
*/ | ||
function spaceCase(string, options = { keepSpecialCharacters: true }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: ' ' })).join(''); | ||
} | ||
/** | ||
* # 🏛 Capital Case | ||
* converts a string to Capital Case | ||
* - capitalizes words and adds spaces | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* capitalCase('$catDog') === '$Cat Dog' | ||
* @example | ||
* capitalCase('$catDog', { keepSpecialCharacters: false }) === 'Cat Dog' | ||
* | ||
* ⟪ if you do not want to add spaces, use `pascalCase()` ⟫ | ||
*/ | ||
function capitalCase(string, options = { keepSpecialCharacters: true }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: ' ' })).reduce((result, word) => { | ||
return result + capitaliseWord(word); | ||
}, ''); | ||
} | ||
/** | ||
* # 🔡 lower case | ||
* converts a string to lower case | ||
* - makes words lowercase and adds spaces | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* lowerCase('$catDog') === '$cat dog' | ||
* @example | ||
* lowerCase('$catDog', { keepSpecialCharacters: false }) === 'cat dog' | ||
* | ||
* ⟪ if you do not want to add spaces, use the native JS `toLowerCase()` ⟫ | ||
*/ | ||
function lowerCase(string, options = { keepSpecialCharacters: true }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: ' ' })) | ||
.join('') | ||
.toLowerCase(); | ||
} | ||
/** | ||
* # 🔠 UPPER CASE | ||
* converts a string to UPPER CASE | ||
* - makes words upper case and adds spaces | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* upperCase('$catDog') === '$CAT DOG' | ||
* @example | ||
* upperCase('$catDog', { keepSpecialCharacters: false }) === 'CAT DOG' | ||
* | ||
* ⟪ if you do not want to add spaces, use the native JS `toUpperCase()` ⟫ | ||
*/ | ||
function upperCase(string, options = { keepSpecialCharacters: true }) { | ||
return splitAndPrefix(string, Object.assign(Object.assign({}, options), { prefix: ' ' })) | ||
.join('') | ||
.toUpperCase(); | ||
} | ||
exports.adaCase = adaCase; | ||
exports.camelCase = camelCase; | ||
exports.capitalCase = capitalCase; | ||
exports.cobolCase = cobolCase; | ||
exports.constantCase = constantCase; | ||
exports.dotNotation = dotNotation; | ||
exports.kebabCase = kebabCase; | ||
@@ -164,2 +332,4 @@ exports.lowerCase = lowerCase; | ||
exports.spaceCase = spaceCase; | ||
exports.trainCase = trainCase; | ||
exports.upperCamelCase = upperCamelCase; | ||
exports.upperCase = upperCase; | ||
@@ -166,0 +336,0 @@ |
/** | ||
* converts strings to camelCase | ||
* # 🐪 camelCase | ||
* converts a string to camelCase | ||
* - first lowercase then all capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in camelCase | ||
* @example | ||
* camelCase('$catDog') === 'catDog' | ||
* @example | ||
* camelCase('$catDog', { keepSpecialCharacters: true }) === '$catDog' | ||
*/ | ||
export declare function camelCase(string: string): string; | ||
export declare function camelCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* converts strings to PascalCase | ||
* # 🐫 PascalCase | ||
* converts a string to PascalCase (also called UpperCamelCase) | ||
* - all capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in PascalCase | ||
* @example | ||
* pascalCase('$catDog') === 'CatDog' | ||
* @example | ||
* pascalCase('$catDog', { keepSpecialCharacters: true }) === '$CatDog' | ||
*/ | ||
export declare function pascalCase(string: string): string; | ||
export declare function pascalCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* converts strings to kebab-case | ||
* # 🐫 UpperCamelCase | ||
* converts a string to UpperCamelCase (also called PascalCase) | ||
* - all capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in kebab-case | ||
* @example | ||
* upperCamelCase('$catDog') === 'CatDog' | ||
* @example | ||
* upperCamelCase('$catDog', { keepSpecialCharacters: true }) === '$CatDog' | ||
*/ | ||
export declare function kebabCase(string: string): string; | ||
export declare const upperCamelCase: typeof pascalCase; | ||
/** | ||
* converts strings to snake_case | ||
* # 🥙 kebab-case | ||
* converts a string to kebab-case | ||
* - hyphenated lowercase | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in snake_case | ||
* @example | ||
* kebabCase('$catDog') === 'cat-dog' | ||
* @example | ||
* kebabCase('$catDog', { keepSpecialCharacters: true }) === '$cat-dog' | ||
*/ | ||
export declare function snakeCase(string: string): string; | ||
export declare function kebabCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* converts strings to CONSTANT_CASE | ||
* # 🐍 snake_case | ||
* converts a string to snake_case | ||
* - underscored lowercase | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in CONSTANT_CASE | ||
* @example | ||
* snakeCase('$catDog') === 'cat_dog' | ||
* @example | ||
* snakeCase('$catDog', { keepSpecialCharacters: true }) === '$cat_dog' | ||
*/ | ||
export declare function constantCase(string: string): string; | ||
export declare function snakeCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* converts strings to path/case | ||
* # 📣 CONSTANT_CASE | ||
* converts a string to CONSTANT_CASE | ||
* - underscored uppercase | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in path/case | ||
* @example | ||
* constantCase('$catDog') === 'CAT_DOG' | ||
* @example | ||
* constantCase('$catDog', { keepSpecialCharacters: true }) === '$CAT_DOG' | ||
*/ | ||
export declare function pathCase(string: string): string; | ||
export declare function constantCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* converts strings to space case (will add spaces but not change casing) | ||
* # 🚂 Train-Case | ||
* converts strings to Train-Case | ||
* - hyphenated & capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in path case | ||
* @example | ||
* trainCase('$catDog') === 'Cat-Dog' | ||
* @example | ||
* trainCase('$catDog', { keepSpecialCharacters: true }) === '$Cat-Dog' | ||
*/ | ||
export declare function spaceCase(string: string): string; | ||
export declare function trainCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* converts strings to Capital Case (with spaces) | ||
* # 🕊 Ada_Case | ||
* converts a string to Ada_Case | ||
* - underscored & capitalised | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in Capital Case (with spaces) | ||
* @example | ||
* adaCase('$catDog') === 'Cat_Dog' | ||
* @example | ||
* adaCase('$catDog', { keepSpecialCharacters: true }) === '$Cat_Dog' | ||
*/ | ||
export declare function capitalCase(string: string): string; | ||
export declare function adaCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* converts strings to lower case (with spaces) | ||
* # 👔 COBOL-CASE | ||
* converts a string to COBOL-CASE | ||
* - hyphenated uppercase | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in lower case (with spaces) | ||
* @example | ||
* cobolCase('$catDog') === 'CAT-DOG' | ||
* @example | ||
* cobolCase('$catDog', { keepSpecialCharacters: true }) === '$CAT-DOG' | ||
*/ | ||
export declare function lowerCase(string: string): string; | ||
export declare function cobolCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* converts strings to UPPER CASE (with spaces) | ||
* # 📍 Dot.notation | ||
* converts a string to dot.notation | ||
* - adds dots, does not change casing | ||
* - *strips away* special characters by default | ||
* | ||
* @export | ||
* @param {string} string | ||
* @returns {string} in UPPER CASE (with spaces) | ||
* @example | ||
* dotNotation('$catDog') === 'cat.Dog' | ||
* @example | ||
* dotNotation('$catDog', { keepSpecialCharacters: true }) === '$cat.Dog' | ||
*/ | ||
export declare function upperCase(string: string): string; | ||
export declare function dotNotation(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* # 📂 Path/case | ||
* converts a string to path/case | ||
* - adds slashes, does not change casing | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* pathCase('$catDog') === '$cat/Dog' | ||
* @example | ||
* pathCase('$catDog', { keepSpecialCharacters: false }) === 'cat/Dog' | ||
*/ | ||
export declare function pathCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* # 🛰 Space case | ||
* converts a string to space case | ||
* - adds spaces, does not change casing | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* spaceCase('$catDog') === '$cat Dog' | ||
* @example | ||
* spaceCase('$catDog', { keepSpecialCharacters: false }) === 'cat Dog' | ||
*/ | ||
export declare function spaceCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* # 🏛 Capital Case | ||
* converts a string to Capital Case | ||
* - capitalizes words and adds spaces | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* capitalCase('$catDog') === '$Cat Dog' | ||
* @example | ||
* capitalCase('$catDog', { keepSpecialCharacters: false }) === 'Cat Dog' | ||
* | ||
* ⟪ if you do not want to add spaces, use `pascalCase()` ⟫ | ||
*/ | ||
export declare function capitalCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* # 🔡 lower case | ||
* converts a string to lower case | ||
* - makes words lowercase and adds spaces | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* lowerCase('$catDog') === '$cat dog' | ||
* @example | ||
* lowerCase('$catDog', { keepSpecialCharacters: false }) === 'cat dog' | ||
* | ||
* ⟪ if you do not want to add spaces, use the native JS `toLowerCase()` ⟫ | ||
*/ | ||
export declare function lowerCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; | ||
/** | ||
* # 🔠 UPPER CASE | ||
* converts a string to UPPER CASE | ||
* - makes words upper case and adds spaces | ||
* - *keeps* special characters by default | ||
* | ||
* @example | ||
* upperCase('$catDog') === '$CAT DOG' | ||
* @example | ||
* upperCase('$catDog', { keepSpecialCharacters: false }) === 'CAT DOG' | ||
* | ||
* ⟪ if you do not want to add spaces, use the native JS `toUpperCase()` ⟫ | ||
*/ | ||
export declare function upperCase(string: string, options?: { | ||
keepSpecialCharacters: boolean; | ||
}): string; |
@@ -1,1 +0,1 @@ | ||
export { camelCase, pascalCase, kebabCase, snakeCase, constantCase, pathCase, spaceCase, capitalCase, lowerCase, upperCase, } from './core'; | ||
export { camelCase, upperCamelCase, pascalCase, dotNotation, kebabCase, snakeCase, adaCase, constantCase, cobolCase, pathCase, spaceCase, capitalCase, lowerCase, upperCase, trainCase, } from './core'; |
@@ -0,22 +1,23 @@ | ||
export declare const magicSplit: RegExp; | ||
export declare const spaceSplit: RegExp; | ||
/** | ||
* A string.match function that will return an array of "string parts" | ||
* | ||
* @param {string} string | ||
* @returns {string[]} | ||
* A string.matchAll function that will return an array of "string parts" and the indexes at which it split each part | ||
*/ | ||
export declare function splitOnSpecialChars(string: string): any[]; | ||
export declare function getPartsAndIndexes(string: string, splitRegex: RegExp): { | ||
parts: string[]; | ||
prefixes: string[]; | ||
}; | ||
/** | ||
* A string.match function that will return an array of "string parts" | ||
* | ||
* @param {string} string | ||
* @returns {string[]} | ||
* A function that splits a string on words and returns an array of words. | ||
* - It can prefix each word with a given character | ||
* - It can strip or keep special characters, this affects the logic for adding a prefix as well | ||
*/ | ||
export declare function getParts(string: string, noSpecialChars?: boolean): any[]; | ||
export declare function splitAndPrefix(string: string, options?: { | ||
keepSpecialCharacters?: boolean; | ||
prefix?: string; | ||
}): string[]; | ||
/** | ||
* Capitalises a single word | ||
* | ||
* @export | ||
* @param {string} string the word | ||
* @returns {string} the word with the first character in uppercase and the rest in lowercase | ||
* @returns the word with the first character in uppercase and the rest in lowercase | ||
*/ | ||
export declare function capitaliseWord(string: string): string; |
{ | ||
"name": "case-anything", | ||
"version": "1.1.5", | ||
"version": "2.0.0", | ||
"sideEffects": false, | ||
@@ -19,5 +19,10 @@ "description": "camelCase, kebab-case, PascalCase... a simple integration with nano package size. (SMALL footprint!)", | ||
], | ||
"engines": { | ||
"node": ">=12.13", | ||
"npm": ">=7" | ||
}, | ||
"scripts": { | ||
"lint": "eslint . --ext .js,.jsx,.ts,.tsx", | ||
"test": "ava", | ||
"test--only": "ava --match='*only:*'", | ||
"rollup": "rollup -c build.js", | ||
@@ -55,4 +60,8 @@ "build": "rm -rf ./dist && npm run lint && npm run rollup && npm run test", | ||
"snake-case", | ||
"ada-case", | ||
"constant-case", | ||
"train-case", | ||
"cobol-case", | ||
"path-case", | ||
"dot-case", | ||
"camel", | ||
@@ -59,0 +68,0 @@ "pascal", |
182
README.md
# Case anything 🐫 | ||
<a href="https://www.npmjs.com/package/case-anything"><img src="https://img.shields.io/npm/v/case-anything.svg" alt="Total Downloads"></a> | ||
<a href="https://www.npmjs.com/package/case-anything"><img src="https://img.shields.io/npm/dw/case-anything.svg" alt="Latest Stable Version"></a> | ||
``` | ||
@@ -18,12 +21,2 @@ npm i case-anything | ||
## Meet the family | ||
- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything) | ||
- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything) | ||
- [find-and-replace-anything 🎣](https://github.com/mesqueeb/find-and-replace-anything) | ||
- [compare-anything 🛰](https://github.com/mesqueeb/compare-anything) | ||
- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything) | ||
- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything) | ||
- [is-what 🙉](https://github.com/mesqueeb/is-what) | ||
## Usage | ||
@@ -33,69 +26,97 @@ | ||
```js | ||
import { camelCase, pascalCase, kebabCase, snakeCase, constantCase } from 'case-anything' | ||
### Strings without spaces | ||
const str = 'PonytaVaporeon_poliwrath-BUTTERFREE' | ||
// or any variant on this | ||
| Name | Input example | Output example | | ||
| --------------------------------- | --------------------------------------------- | -------------- | | ||
| 🐪 camelCase | `camelCase('$catDog')` | `catDog` | | ||
| 🐫 PascalCase<br />UpperCamelCase | `pascalCase('$catDog')`<br />`upperCamelCase` | `CatDog` | | ||
| 🥙 kebab-case | `kebabCase('$catDog')` | `cat-dog` | | ||
| 🐍 snake_case | `snakeCase('$catDog')` | `cat_dog` | | ||
| 📣 CONSTANT_CASE | `constantCase('$catDog')` | `CAT_DOG` | | ||
| 🚂 Train-Case | `trainCase('$catDog')` | `Cat-Dog` | | ||
| 🕊 Ada_Case | `adaCase('$catDog')` | `Cat_Dog` | | ||
| 👔 COBOL-CASE | `cobolCase('$catDog')` | `CAT-DOG` | | ||
| 📍 Dot.notation | `dotNotation('$catDog')` | `cat.Dog` | | ||
| 📂 Path/case | `pathCase('$catDog')` | `$cat/Dog` | | ||
| 🛰 Space case | `spaceCase('$catDog')` | `$cat Dog` | | ||
| 🏛 Capital Case | `capitalCase('$catDog')` | `$Cat Dog` | | ||
| 🔡 lower case | `lowerCase('$catDog')` | `$cat dog` | | ||
| 🔠 UPPER CASE | `upperCase('$catDog')` | `$CAT DOG` | | ||
camelCase(str) === 'ponytaVaporeonPoliwrathButterfree' | ||
#### Special Characters | ||
pascalCase(str) === 'PonytaVaporeonPoliwrathButterfree' | ||
<table> | ||
<tr> | ||
<th>functions that <strong>strip away special characters*</strong></th> | ||
<th>functions that <strong>keep special characters*</strong></th> | ||
</tr> | ||
<tr> | ||
<td> | ||
<li>camelCase</li> | ||
<li>pascalCase</li> | ||
<li>kebabCase</li> | ||
<li>snakeCase</li> | ||
<li>constantCase</li> | ||
<li>trainCase</li> | ||
<li>adaCase</li> | ||
<li>cobolCase</li> | ||
<li>dotNotation</li> | ||
</td> | ||
<td> | ||
<li>pathCase</li> | ||
<li>spaceCase</li> | ||
<li>capitalCase</li> | ||
<li>lowerCase</li> | ||
<li>upperCase</li> | ||
</td> | ||
</tr> | ||
</table> | ||
kebabCase(str) === 'ponyta-vaporeon-poliwrath-butterfree' | ||
\*You can control wether or not to _keep or remove_ special characters like so: | ||
snakeCase(str) === 'ponyta_vaporeon_poliwrath_butterfree' | ||
```js | ||
camelCase('$catDog') === 'catDog' | ||
// force keeping special characters: | ||
camelCase('$catDog', { keepSpecialCharacters: true }) === '$catDog' | ||
constantCase(str) === 'PONYTA_VAPOREON_POLIWRATH_BUTTERFREE' | ||
pathCase('$catDog') === '$cat/Dog' | ||
// force removing special characters: | ||
pathCase('$catDog', { keepSpecialCharacters: false }) === 'cat/Dog' | ||
``` | ||
There is also `spaceCase` and `pathCase`, which does **not convert the casing**: | ||
#### Case Changing | ||
```js | ||
import { spaceCase, pathCase } from 'case-anything' | ||
These cases _**do not change the casing**_ of the words: | ||
const str = 'PonytaVaporeon_poliwrath-BUTTERFREE' | ||
- dotNotation | ||
- pathCase | ||
- spaceCase | ||
spaceCase(str) === 'Ponyta Vaporeon poliwrath BUTTERFREE' | ||
pathCase(str) === 'Ponyta/Vaporeon/poliwrath/BUTTERFREE' | ||
``` | ||
There is also upper, lower and capital case. These will all convert the casing & also add spaces in between: | ||
```js | ||
import { upperCase, lowerCase, capitalCase } from 'case-anything' | ||
const str = 'PonytaVaporeon_poliwrath-BUTTERFREE' | ||
upperCase(str) === 'PONYTA VAPOREON POLIWRATH BUTTERFREE' | ||
lowerCase(str) === 'ponyta vaporeon poliwrath butterfree' | ||
capitalCase(str) === 'Ponyta Vaporeon Poliwrath Butterfree' | ||
dotNotation('$catDog') === 'cat.Dog' | ||
// force lower case: | ||
dotNotation('$catDog').toLowerCase() === 'cat.dog' | ||
``` | ||
### When spaces are involved | ||
### Strings with spaces | ||
As soon as there is a space in the target string, it will regard the input as a _sentence_ and only split each part at the spaces. | ||
See this example to understand each case: | ||
| Name | Input example | Output example | | ||
| --------------------------------- | ----------------------------------------------- | -------------- | | ||
| 🐪 camelCase | `camelCase("I'm O.K.!")` | `imOk` | | ||
| 🐫 PascalCase<br />UpperCamelCase | `pascalCase("I'm O.K.!")`<br />`upperCamelCase` | `ImOk` | | ||
| 🥙 kebab-case | `kebabCase("I'm O.K.!")` | `im-ok` | | ||
| 🐍 snake_case | `snakeCase("I'm O.K.!")` | `im_ok` | | ||
| 📣 CONSTANT_CASE | `constantCase("I'm O.K.!")` | `IM_OK` | | ||
| 🚂 Train-Case | `trainCase("I'm O.K.!")` | `Im-Ok` | | ||
| 🕊 Ada_Case | `adaCase("I'm O.K.!")` | `Im_Ok` | | ||
| 👔 COBOL-CASE | `cobolCase("I'm O.K.!")` | `IM-OK` | | ||
| 📍 Dot.notation | `dotNotation("I'm O.K.!")` | `Im.OK` | | ||
| 📂 Path/case | `pathCase("I'm O.K.!")` | `I\'m/O.K.!` | | ||
| 🛰 Space case | `spaceCase("I'm O.K.!")` | `I\'m O.K.!` | | ||
| 🏛 Capital Case | `capitalCase("I'm O.K.!")` | `I\'m O.k.!` | | ||
| 🔡 lower case | `lowerCase("I'm O.K.!")` | `i\'m o.k.!` | | ||
| 🔠 UPPER CASE | `upperCase("I'm O.K.!")` | `I\'M O.K.!` | | ||
<!-- prettier-ignore-start --> | ||
```js | ||
const str = `listen I'm O.K.!` | ||
// splits on spaces & removes special characters | ||
camelCase(str) === 'listenImOk' | ||
pascalCase(str) === 'ListenImOk' | ||
kebabCase(str) === 'listen-im-ok' | ||
snakeCase(str) === 'listen_im_ok' | ||
constantCase(str) === 'LISTEN_IM_OK' | ||
// splits on spaces & keeps special characters | ||
spaceCase(str) === `listen I'm O.K.!` | ||
pathCase(str) === `listen/I'm/O.K.!` | ||
lowerCase(str) === `listen i'm o.k.!` | ||
upperCase(str) === `LISTEN I'M O.K.!` | ||
capitalCase(str) === `Listen I'm O.k.!` | ||
``` | ||
<!-- prettier-ignore-end --> | ||
Also note, that multiple sequential spaces are treated as one space. | ||
@@ -111,17 +132,19 @@ | ||
```js | ||
const str = 'Çâfé Ågård' | ||
// CONVERTS special characters: | ||
camelCase(str) === 'cafeAgard' | ||
pascalCase(str) === 'CafeAgard' | ||
kebabCase(str) === 'cafe-agard' | ||
snakeCase(str) === 'cafe_agard' | ||
constantCase(str) === 'CAFE_AGARD' | ||
camelCase('Çâfé Ågård') === 'cafeAgard' | ||
pascalCase('Çâfé Ågård') === 'CafeAgard' | ||
kebabCase('Çâfé Ågård') === 'cafe-agard' | ||
snakeCase('Çâfé Ågård') === 'cafe_agard' | ||
constantCase('Çâfé Ågård') === 'CAFE_AGARD' | ||
trainCase('Çâfé Ågård') === 'Cafe-Agard' | ||
adaCase('Çâfé Ågård') === 'Cafe_Agard' | ||
cobolCase('Çâfé Ågård') === 'CAFE-AGARD' | ||
dotNotation('Çâfé Ågård') === 'Cafe.Agard' | ||
// DOES NOT convert special characters: | ||
spaceCase(str) === 'Çâfé Ågård' | ||
pathCase(str) === 'Çâfé/Ågård' | ||
lowerCase(str) === 'çâfé ågård' | ||
upperCase(str) === 'ÇÂFÉ ÅGÅRD' | ||
capitalCase(str) === 'Çâfé Ågård' | ||
spaceCase('Çâfé Ågård') === 'Çâfé Ågård' | ||
pathCase('Çâfé Ågård') === 'Çâfé/Ågård' | ||
lowerCase('Çâfé Ågård') === 'çâfé ågård' | ||
upperCase('Çâfé Ågård') === 'ÇÂFÉ ÅGÅRD' | ||
capitalCase('Çâfé Ågård') === 'Çâfé Ågård' | ||
``` | ||
@@ -155,8 +178,21 @@ <!-- prettier-ignore-end --> | ||
What keeps my package small, is that it's literally just a regex: | ||
What keeps my package small, is that literally just uses a regex to separate "words". | ||
```js | ||
export function splitOnSpecialChars (string: string): any[] { | ||
// the source code is similar to: | ||
export function splitOnSpecialChars(string: string): any[] { | ||
return string.match(/^[a-z]+|[A-Z][a-z]+|[a-z]+|[0-9]+|[A-Z]+(?![a-z])/g) | ||
} | ||
``` | ||
The actual regex used is a little bit more comprehensive and can be found [here](https://github.com/mesqueeb/case-anything/blob/production/src/utils.ts#L7). | ||
## Meet the family (other utils) | ||
- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything) | ||
- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything) | ||
- [find-and-replace-anything 🎣](https://github.com/mesqueeb/find-and-replace-anything) | ||
- [compare-anything 🛰](https://github.com/mesqueeb/compare-anything) | ||
- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything) | ||
- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything) | ||
- [is-what 🙉](https://github.com/mesqueeb/is-what) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
44310
881
195