string-kit
Version:
A string manipulation toolbox, featuring a string formatter (inspired by sprintf), a variable inspector (output featuring ANSI colors and HTML) and various escape functions (shell argument, regexp, html, etc).
203 lines (155 loc) • 6.01 kB
JavaScript
/*
String Kit
Copyright (c) 2014 - 2021 Cédric Ronvel
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
;
const unicode = require( './unicode.js' ) ;
// French typography rules with '!', '?', ':' and ';'
const FRENCH_DOUBLE_GRAPH_TYPO = {
'!': true ,
'?': true ,
':': true ,
';': true
} ;
/*
.wordwrap( str , width )
.wordwrap( str , options )
str: the string to process
width: the max width (default to 80)
options: object, where:
width: the max width (default to 80)
glue: (optional) the char used to join lines, by default: lines are joined with '\n',
noJoin: (optional) if set: don't join, instead return an array of lines
offset: (optional) offset of the first-line
updateOffset: (optional) if set, options.offset is updated with the last line width
noTrim: (optional) if set, don't right-trim lines, if not set, right-trim all lines except the last
fill: (optional) if set, fill the remaining width with space (it forces noTrim)
skipFn: (optional) a function used to skip a whole sequence, useful for special sequences
like ANSI escape sequence, and so on...
charWidthFn: (optional) a function used to compute the width of one char/group of chars
regroupFn: (optional) a function used to regroup chars together
*/
module.exports = function wordwrap( str , options ) {
var start = 0 , end , skipEnd , lineWidth , currentLine , currentWidth , length ,
lastEnd , lastWidth , lastWasSpace , charWidthFn ,
explicitNewLine = true ,
strArray = unicode.toArray( str ) ,
trimNewLine = false ,
line , lines = [] ;
if ( typeof options !== 'object' ) {
options = { width: options } ;
}
// Catch NaN, zero or negative and non-number
if ( ! options.width || typeof options.width !== 'number' || options.width <= 0 ) { options.width = 80 ; }
lineWidth = options.offset ? options.width - options.offset : options.width ;
if ( typeof options.glue !== 'string' ) { options.glue = '\n' ; }
if ( options.regroupFn ) {
strArray = options.regroupFn( strArray ) ;
// If char are grouped, use unicode.width() as a default
charWidthFn = options.charWidthFn || unicode.width ;
}
else {
// If char are not grouped, use unicode.charWidth() as a default
charWidthFn = options.charWidthFn || unicode.charWidth ;
}
length = strArray.length ;
var getNextLine = () => {
//originStart = start ;
if ( ! explicitNewLine || trimNewLine ) {
// Find the first non-space char
while ( strArray[ start ] === ' ' ) { start ++ ; }
if ( trimNewLine && strArray[ start ] === '\n' ) {
explicitNewLine = true ;
start ++ ;
/*
originStart = start ;
while ( strArray[ start ] === ' ' ) { start ++ ; }
*/
}
}
if ( start >= length ) { return null ; }
explicitNewLine = false ;
trimNewLine = false ;
lastWasSpace = false ;
end = lastEnd = start ;
currentWidth = lastWidth = 0 ;
for ( ;; ) {
if ( end >= length ) {
return strArray.slice( start , end ).join( '' ) ;
}
if ( strArray[ end ] === '\n' ) {
explicitNewLine = true ;
currentLine = strArray.slice( start , end ++ ).join( '' ) ;
if ( options.fill ) {
currentLine += ' '.repeat( lineWidth - currentWidth ) ;
}
return currentLine ;
}
if ( options.skipFn ) {
skipEnd = options.skipFn( strArray , end ) ;
if ( skipEnd !== end ) {
end = skipEnd ;
continue ;
}
}
if ( strArray[ end ] === ' ' && ! lastWasSpace && ! FRENCH_DOUBLE_GRAPH_TYPO[ strArray[ end + 1 ] ] ) {
// This is the first space of a group of space
lastEnd = end ;
lastWidth = currentWidth ;
}
else {
lastWasSpace = false ;
}
currentWidth += charWidthFn( strArray[ end ] ) ;
if ( currentWidth > lineWidth ) {
// If lastEnd === start, this is a word that takes the whole line: cut it
// If not, use the lastEnd
trimNewLine = true ;
if ( lastEnd !== start ) {
end = lastEnd ;
}
else if ( lineWidth < options.width ) {
// This is the first line with an offset, so just start over in line two
end = start ;
return '' ;
}
currentLine = strArray.slice( start , end ).join( '' ) ;
if ( options.fill ) {
currentLine += ' '.repeat( lineWidth - lastWidth ) ;
}
return currentLine ;
}
// Do not move that inside the for(;;), some part are using a continue statement and manage the end value by themself
end ++ ;
}
} ;
while ( start < length && ( line = getNextLine() ) !== null ) {
lines.push( line ) ;
start = end ;
lineWidth = options.width ;
}
// If it ends with an explicit newline, we have to reproduce it now!
if ( explicitNewLine ) { lines.push( '' ) ; }
if ( ! options.noTrim && ! options.fill ) {
lines = lines.map( ( line_ , index ) => index === lines.length - 1 ? line_ : line_.trimRight() ) ;
}
if ( ! options.noJoin ) { lines = lines.join( options.glue ) ; }
if ( options.updateOffset ) { options.offset = currentWidth ; }
return lines ;
} ;