UNPKG

voca

Version:

The ultimate JavaScript string library

122 lines (107 loc) 3.69 kB
'use strict'; require('./internal/is_nil.js'); var coerce_to_boolean = require('./internal/coerce_to_boolean.js'); require('./is_string.js'); var coerce_to_string = require('./internal/coerce_to_string.js'); var coerce_to_number = require('./internal/coerce_to_number.js'); var OPTION_WIDTH = 'width'; var OPTION_NEW_LINE = 'newLine'; var OPTION_INDENT = 'indent'; var OPTION_CUT = 'cut'; /** * Wraps `subject` to a given number of characters using a string break character. * * @function wordWrap * @static * @since 1.0.0 * @memberOf Manipulate * @param {string} [subject=''] The string to wrap. * @param {Object} [options={}] The wrap options. * @param {number} [options.width=75] The number of characters at which to wrap. * @param {string} [options.newLine='\n'] The string to add at the end of line. * @param {string} [options.indent=''] The string to intend the line. * @param {boolean} [options.cut=false] When `false` (default) does not split the word even if word length is bigger than `width`. <br/> * When `true` breaks the word that has length bigger than `width`. * * @return {string} Returns wrapped string. * @example * v.wordWrap('Hello world', { * width: 5 * }); * // => 'Hello\nworld' * * v.wordWrap('Hello world', { * width: 5, * newLine: '<br/>', * indent: '__' * }); * // => '__Hello<br/>__world' * * v.wordWrap('Wonderful world', { * width: 5, * cut: true * }); * // => 'Wonde\nrful\nworld' * */ function wordWrap(subject) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var subjectString = coerce_to_string.coerceToString(subject); var _determineOptions = determineOptions(options), width = _determineOptions.width, newLine = _determineOptions.newLine, indent = _determineOptions.indent, cut = _determineOptions.cut; if (subjectString === '' || width <= 0) { return indent; } var subjectLength = subjectString.length; var substring = subjectString.substring.bind(subjectString); var offset = 0; var wrappedLine = ''; while (subjectLength - offset > width) { if (subjectString[offset] === ' ') { offset++; continue; } var spaceToWrapAt = subjectString.lastIndexOf(' ', width + offset); if (spaceToWrapAt >= offset) { wrappedLine += indent + substring(offset, spaceToWrapAt) + newLine; offset = spaceToWrapAt + 1; } else { if (cut) { wrappedLine += indent + substring(offset, width + offset) + newLine; offset += width; } else { spaceToWrapAt = subjectString.indexOf(' ', width + offset); if (spaceToWrapAt >= 0) { wrappedLine += indent + substring(offset, spaceToWrapAt) + newLine; offset = spaceToWrapAt + 1; } else { wrappedLine += indent + substring(offset); offset = subjectLength; } } } } if (offset < subjectLength) { wrappedLine += indent + substring(offset); } return wrappedLine; } /** * Determine the word wrap options. The missing values are filled with defaults. * * @param {Object} options The options object. * @return {Object} The word wrap options, with default settings if necessary. * @ignore */ function determineOptions(options) { return { width: coerce_to_number.coerceToNumber(options[OPTION_WIDTH], 75), newLine: coerce_to_string.coerceToString(options[OPTION_NEW_LINE], '\n'), indent: coerce_to_string.coerceToString(options[OPTION_INDENT], ''), cut: coerce_to_boolean.coerceToBoolean(options[OPTION_CUT], false) }; } module.exports = wordWrap;