lib/libs.js
function wordWrapToLength (s, width, padleft, padright, omitFirst) {
                    if(padright === undefined && padleft) padright = padleft;

                    padleft  = !isNaN(parseInt(padleft,  10)) ? parseInt(padleft, 10)  : 0;
                    padright = !isNaN(parseInt(padright, 10)) ? parseInt(padright, 10) : 0;

                    var paddingLeft = '';
                    for(var n = 0; n < padleft;  n++) paddingLeft  += ' ';

                    var cols   = !isNaN(parseInt(width, 10)) ? length : 120,
                        arr    = s.split(' '),
                        item   = null,
                        len    = !omitFirst ? cols - padright - padleft : cols - padright,
                        str    = !omitFirst ? paddingLeft : '',
                        olen   = cols - padright - padleft;

                    while((item = arr.shift()) !== undefined) {
                        if(item.length < len) {
                            str += item + ' ';
                            len -= item.length + 1;
                        }
                        else if(item.length > olen) {
                            str += item.substring(0, len - 1) + '-\n' + paddingLeft;
                            arr.unshift(item.substring(len, item.length - 1));
                            len = cols - padright - padleft;
                        }
                        else {
                            str += '\n' + paddingLeft + item + ' ';
                            len = cols - padright - 1 - padleft - item.length;
                        }
                    }
                    return str;
                }
function wordWrapToLength (s, width, padleft, padright, omitFirst) {
                    if(padright === undefined && padleft) padright = padleft;

                    padleft  = !isNaN(parseInt(padleft,  10)) ? parseInt(padleft, 10)  : 0;
                    padright = !isNaN(parseInt(padright, 10)) ? parseInt(padright, 10) : 0;

                    var paddingLeft = '';
                    for(var n = 0; n < padleft;  n++) paddingLeft  += ' ';

                    var cols   = !isNaN(parseInt(width, 10)) ? length : 120,
                        arr    = s.split(' '),
                        item   = null,
                        len    = !omitFirst ? cols - padright - padleft : cols - padright,
                        str    = !omitFirst ? paddingLeft : '',
                        olen   = cols - padright - padleft;

                    while((item = arr.shift()) !== undefined) {
                        if(item.length < len) {
                            str += item + ' ';
                            len -= item.length + 1;
                        }
                        else if(item.length > olen) {
                            str += item.substring(0, len - 1) + '-\n' + paddingLeft;
                            arr.unshift(item.substring(len, item.length - 1));
                            len = cols - padright - padleft;
                        }
                        else {
                            str += '\n' + paddingLeft + item + ' ';
                            len = cols - padright - 1 - padleft - item.length;
                        }
                    }
                    return str;
                }
function wordWrapToLength ( s , width , padleft , padright , omitFirst ) { if ( padright === undefined && padleft ) padright = padleft ; padleft = ! isNaN ( parseInt ( padleft , 10 ) ) ? parseInt ( padleft , 10 ) : 0 ; padright = ! isNaN ( parseInt ( padright , 10 ) ) ? parseInt ( padright , 10 ) : 0 ; var paddingLeft = '' ; for ( var n = 0 ; n < padleft ; n ++ ) paddingLeft += ' ' ; var cols = ! isNaN ( parseInt ( width , 10 ) ) ? length : 120 , arr = s . split ( ' ' ) , item = null , len = ! omitFirst ? cols - padright - padleft : cols - padright , str = ! omitFirst ? paddingLeft : '' , olen = cols - padright - padleft ; while ( ( item = arr . shift ( ) ) !== undefined ) { if ( item . length < len ) { str += item + ' ' ; len -= item . length + 1 ; } else if ( item . length > olen ) { str += item . substring ( 0 , len - 1 ) + '-\n' + paddingLeft ; arr . unshift ( item . substring ( len , item . length - 1 ) ) ; len = cols - padright - padleft ; } else { str += '\n' + paddingLeft + item + ' ' ; len = cols - padright - 1 - padleft - item . length ; } } return str ; }
Adjusts a string to fit within the confines of 'width', without breaking words.
@param {String} s The string to operate on.
@param {Number=} [length=120] The length to word wrap the string to.
@param {Number=} [padleft=0] The number of columns to pad the string on the left
@param {Number=} [padright=0] The number of columns to pad the string on the right
@param {Boolean=} omitFirst If true, the first line will not be padded left
@return {String} The string adjusted and padded for the stdout.
@function
Adjusts a string to fit within the confines of width without breaking words .