GT2/GT2-iOS/node_modules/sentence-case/sentence-case.js

41 lines
1.0 KiB
JavaScript

var lowerCase = require('lower-case')
var NON_WORD_REGEXP = require('./vendor/non-word-regexp')
var CAMEL_CASE_REGEXP = require('./vendor/camel-case-regexp')
var TRAILING_DIGIT_REGEXP = require('./vendor/trailing-digit-regexp')
/**
* Sentence case a string.
*
* @param {String} str
* @param {String} locale
* @param {String} replacement
* @return {String}
*/
module.exports = function (str, locale, replacement) {
if (str == null) {
return ''
}
replacement = replacement || ' '
function replace (match, index, string) {
if (index === 0 || index === (string.length - match.length)) {
return ''
}
return replacement
}
str = String(str)
// Support camel case ("camelCase" -> "camel Case").
.replace(CAMEL_CASE_REGEXP, '$1 $2')
// Support digit groups ("test2012" -> "test 2012").
.replace(TRAILING_DIGIT_REGEXP, '$1 $2')
// Remove all non-word characters and replace with a single space.
.replace(NON_WORD_REGEXP, replace)
// Lower case the entire string.
return lowerCase(str, locale)
}