string-kit
Advanced tools
Comparing version
@@ -33,3 +33,10 @@ /* | ||
underscoreToSpace: true , | ||
lowerCaseWords: new Set( [ 'a' , 'the' , 'of' , 'in' ] ) | ||
lowerCaseWords: new Set( [ | ||
// Articles | ||
'a' , 'an' , 'the' , | ||
// Conjunctions (only coordinating conjunctions, maybe we will have to add subordinating and correlative conjunctions) | ||
'for' , 'and' , 'nor' , 'but' , 'or' , 'yet' , 'so' , | ||
// Prepositions (there are more, but usually only preposition with 2 or 3 letters are lower-cased) | ||
'of' , 'on' , 'off' , 'in' , 'into' , 'by' , 'with' , 'to' , 'at' , 'up' , 'down' , 'as' | ||
] ) | ||
} ; | ||
@@ -42,30 +49,31 @@ | ||
if ( options.dashToSpace ) { | ||
str = str.replace( /-+/g , ' ' ) ; | ||
} | ||
// Manage options | ||
var dashToSpace = options.dashToSpace ?? DEFAULT_OPTIONS.dashToSpace , | ||
underscoreToSpace = options.underscoreToSpace ?? DEFAULT_OPTIONS.underscoreToSpace , | ||
zealous = options.zealous ?? DEFAULT_OPTIONS.zealous , | ||
preserveAllCaps = options.preserveAllCaps ?? DEFAULT_OPTIONS.preserveAllCaps , | ||
lowerCaseWords = options.lowerCaseWords ?? DEFAULT_OPTIONS.lowerCaseWords ; | ||
if ( options.underscoreToSpace ) { | ||
str = str.replace( /_+/g , ' ' ) ; | ||
} | ||
lowerCaseWords = | ||
lowerCaseWords instanceof Set ? lowerCaseWords : | ||
Array.isArray( lowerCaseWords ) ? new Set( lowerCaseWords ) : | ||
null ; | ||
if ( dashToSpace ) { str = str.replace( /-+/g , ' ' ) ; } | ||
if ( 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 ) { | ||
return str.replace( /[^\s_-]+/g , ( part , position ) => { | ||
// Check word that must be lower-cased (excluding the first and the last word) | ||
if ( lowerCaseWords && position && position + part.length < str.length ) { | ||
let lowerCased = part.toLowerCase() ; | ||
if ( options.lowerCaseWords.has( lowerCased ) ) { return lowerCased ; } | ||
if ( lowerCaseWords.has( lowerCased ) ) { return lowerCased ; } | ||
} | ||
if ( options.zealous ) { | ||
if ( options.preserveAllCaps && part === part.toUpperCase() ) { | ||
if ( zealous ) { | ||
if ( preserveAllCaps && part === part.toUpperCase() ) { | ||
// This is a ALLCAPS word | ||
@@ -72,0 +80,0 @@ return part ; |
{ | ||
"name": "string-kit", | ||
"version": "0.16.2", | ||
"version": "0.16.3", | ||
"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
314326
0.34%4866
0.21%