utils.js
function splitIntoLines(str, max) {
  function _splitIntoLines(str, max) {
    return str.trim().split(' ').reduce(function(lines, word) {
      var line = lines[lines.length - 1]
      if (line && wcwidth(line.join(' ')) + wcwidth(word) < max) {
        lines[lines.length - 1].push(word) // add to line
      }
      else lines.push([word]) // new line
      return lines
    }, []).map(function(l) {
      return l.join(' ')
    })
  }
  return str.split('\n').map(function(str) {
    return _splitIntoLines(str, max)
  }).reduce(function(lines, line) {
    return lines.concat(line)
  }, [])
}
function splitIntoLines(str, max) {
  function _splitIntoLines(str, max) {
    return str.trim().split(' ').reduce(function(lines, word) {
      var line = lines[lines.length - 1]
      if (line && wcwidth(line.join(' ')) + wcwidth(word) < max) {
        lines[lines.length - 1].push(word) // add to line
      }
      else lines.push([word]) // new line
      return lines
    }, []).map(function(l) {
      return l.join(' ')
    })
  }
  return str.split('\n').map(function(str) {
    return _splitIntoLines(str, max)
  }).reduce(function(lines, line) {
    return lines.concat(line)
  }, [])
}
function splitIntoLines ( str , max ) { function _splitIntoLines ( str , max ) { return str . trim ( ) . split ( ' ' ) . reduce ( function ( lines , word ) { var line = lines [ lines . length - 1 ] if ( line && wcwidth ( line . join ( ' ' ) ) + wcwidth ( word ) < max ) { lines [ lines . length - 1 ] . push ( word ) // add to line } else lines . push ( [ word ] ) // new line return lines } , [ ] ) . map ( function ( l ) { return l . join ( ' ' ) } ) } return str . split ( '\n' ) . map ( function ( str ) { return _splitIntoLines ( str , max ) } ) . reduce ( function ( lines , line ) { return lines . concat ( line ) } , [ ] ) }
Split a String `str` into lines of maxiumum length `max`.
Splits on word boundaries. Preserves existing new lines.

@param String str string to split
@param Number max length of each line
@return Array Array containing lines.
Split a String str into lines of maxiumum length max . Splits on word boundaries . Preserves existing new lines .