string-kit
Advanced tools
Comparing version
@@ -31,8 +31,38 @@ /* | ||
module.exports = function toTitleCase( str , options ) { | ||
const DEFAULT_OPTIONS = { | ||
underscoreToSpace: true , | ||
lowerCaseWords: new Set( [ 'a' , 'the' , 'of' , 'in' ] ) | ||
} ; | ||
module.exports = ( str , options = DEFAULT_OPTIONS ) => { | ||
if ( ! str || typeof str !== 'string' ) { return '' ; } | ||
options = options || {} ; | ||
if ( options.dashToSpace ) { | ||
str = str.replace( /-+/g , ' ' ) ; | ||
} | ||
if ( options.underscoreToSpace ) { | ||
str = str.replace( /_+/g , ' ' ) ; | ||
} | ||
// Squash multiple spaces into only one, and trim | ||
str = str.replace( / +/g , ' ' ).trim() ; | ||
var lowerCaseWords = | ||
options.lowerCaseWords instanceof Set ? options.lowerCaseWords : | ||
Array.isArray( options.lowerCaseWords ) ? new Set( options.lowerCaseWords ) : | ||
null ; | ||
var wordCount = 0 ; | ||
return str.replace( /[^\s_-]+/g , ( part ) => { | ||
wordCount ++ ; | ||
if ( wordCount > 1 && options.lowerCaseWords ) { | ||
let lowerCased = part.toLowerCase() ; | ||
if ( options.lowerCaseWords.has( lowerCased ) ) { return lowerCased ; } | ||
} | ||
if ( options.zealous ) { | ||
@@ -45,10 +75,7 @@ if ( options.preserveAllCaps && part === part.toUpperCase() ) { | ||
return part[ 0 ].toUpperCase() + part.slice( 1 ).toLowerCase() ; | ||
} | ||
return part[ 0 ].toUpperCase() + part.slice( 1 ) ; | ||
} ) ; | ||
} ; | ||
{ | ||
"name": "string-kit", | ||
"version": "0.16.1", | ||
"version": "0.16.2", | ||
"engines": { | ||
@@ -5,0 +5,0 @@ "node": ">=14.15.0" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
313276
0.28%4856
0.48%