balanced-line-split
Advanced tools
Comparing version 1.0.0 to 1.1.0
53
index.js
const stringWidth = require('string-width'); | ||
const stripAnsi = require('strip-ansi'); | ||
const isBreakCharacter = (str, index) => { | ||
const regex = /(\s|-|\u2014|\u2013|\u00ad)/g; | ||
const breakMatches = []; | ||
let match; | ||
while ((match = regex.exec(str))) { | ||
breakMatches.push(match.index); | ||
} | ||
return breakMatches.includes(index); | ||
/* | ||
* Break characters are: | ||
* - whitespace (except non-breaking space: u00a0) | ||
* - hyphen | ||
* - em dash (u2014) | ||
* - en dash (u2013) | ||
* - soft hyphen (u00ad) | ||
*/ | ||
const isBreakCharacter = (char) => { | ||
const regex = /([^\S\u00a0]|-|\u2014|\u2013|\u00ad)/; | ||
return regex.test(char); | ||
}; | ||
@@ -21,3 +22,6 @@ | ||
return isBreakCharacter(str, index - 1) && !isBreakCharacter(str, index); | ||
const previousChar = str.substring(index - 1, index); | ||
const nextChar = str.substring(index, index + 1); | ||
return isBreakCharacter(previousChar) && !isBreakCharacter(nextChar); | ||
}; | ||
@@ -34,3 +38,4 @@ | ||
} | ||
else if (direction > 0 && (index >= desiredWidth || index >= str.length)) { | ||
if (direction > 0 && (index >= desiredWidth || index >= str.length)) { | ||
return index; | ||
@@ -43,5 +48,9 @@ } | ||
const exec = (str, lines) => { | ||
const idealLineWidth = Math.ceil(stringWidth(str) / lines); | ||
const exec = (str, lines, maxLineLength) => { | ||
if (lines === 1) { | ||
return stripAnsi(str); | ||
} | ||
let idealLineWidth = Math.ceil(stringWidth(str) / lines); | ||
let remainingLines = lines; | ||
@@ -51,2 +60,14 @@ let remainingText = stripAnsi(str); | ||
// If there are no break opportunities, return the full string as the first | ||
// line and pad the rest. | ||
if (![...remainingText].some((char) => isBreakCharacter(char))) { | ||
return `${remainingText}${'\n'.repeat(lines - 1)}`; | ||
} | ||
if (idealLineWidth > maxLineLength) { | ||
const additionalLines = Math.ceil(idealLineWidth / maxLineLength); | ||
remainingLines += additionalLines; | ||
idealLineWidth = maxLineLength; | ||
} | ||
while (remainingLines > 1) { | ||
@@ -70,8 +91,8 @@ const guessedIndex = Math.round((remainingText.length + 1) / remainingLines) - 1; | ||
module.exports = (str, lines) => { | ||
module.exports = (str, lines = 2, maxLineLength = Infinity) => { | ||
return String(str) | ||
.normalize() | ||
.split('\n') | ||
.map((line) => exec(line, lines)) | ||
.map((line) => exec(line, lines, maxLineLength)) | ||
.join('\n'); | ||
}; |
{ | ||
"name": "balanced-line-split", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Split a string into multiple lines of similar lengths.", | ||
@@ -17,6 +17,6 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">= 4" | ||
"node": ">= 8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && nyc ava" | ||
}, | ||
@@ -32,4 +32,5 @@ "files": [ | ||
"ava": "^0.25.0", | ||
"eslint-config-bdougherty": "^4.1.1", | ||
"xo": "^0.20.3" | ||
"eslint-config-bdougherty": "^5.0.1", | ||
"nyc": "^11.7.3", | ||
"xo": "^0.21.0" | ||
}, | ||
@@ -36,0 +37,0 @@ "xo": { |
# balanced-line-split | ||
[![Build Status](https://travis-ci.org/bdougherty/balanced-line-split.svg?branch=master)](https://travis-ci.org/bdougherty/balanced-line-split) | ||
@@ -26,3 +27,3 @@ > Split a string into multiple lines of similar lengths. | ||
### balancedLineSplit(str, lines) | ||
### balancedLineSplit(str, lines = 1, maxLineLength = Infinity) | ||
@@ -37,6 +38,12 @@ #### str | ||
Type: `number` | ||
Type: `Number` | ||
The number of lines to split the string across. Note that the resulting string will include enough line breaks to match this number of lines, even if there is no text to fill them. | ||
#### maxLineLength | ||
Type: `Number` | ||
Attempt to stay within a specified maximum line length. | ||
## Related | ||
@@ -43,0 +50,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6351
74
54
4