68 rader
1.3 KiB
JavaScript

/**
* Export.
*/
module.exports = toNoCase
/**
* Test whether a string is camel-case.
*/
var hasSpace = /\s/
var hasSeparator = /(_|-|\.|:)/
var hasCamel = /([a-z][A-Z]|[A-Z][a-z])/
/**
* Remove any starting case from a `string`, like camel or snake, but keep
* spaces and punctuation that may be important otherwise.
*
* @param {String} string
* @return {String}
*/
function toNoCase(string) {
if (hasSpace.test(string)) return string.toLowerCase()
if (hasSeparator.test(string)) return (unseparate(string) || string).toLowerCase()
if (hasCamel.test(string)) return uncamelize(string).toLowerCase()
return string.toLowerCase()
}
/**
* Separator splitter.
*/
var separatorSplitter = /[\W_]+(.|$)/g
/**
* Un-separate a `string`.
*
* @param {String} string
* @return {String}
*/
function unseparate(string) {
return string.replace(separatorSplitter, function (m, next) {
return next ? ' ' + next : ''
})
}
/**
* Camelcase splitter.
*/
var camelSplitter = /(.)([A-Z]+)/g
/**
* Un-camelcase a `string`.
*
* @param {String} string
* @return {String}
*/
function uncamelize(string) {
return string.replace(camelSplitter, function (m, previous, uppers) {
return previous + ' ' + uppers.toLowerCase().split('').join(' ')
})
}