Skip to content

Commit

Permalink
Require Node.js 6
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 31, 2018
1 parent 14f3af7 commit 76d163e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 58 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
after_success: npm run coveralls
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
85 changes: 37 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,29 @@ const wrapAnsi = code => `${ESCAPES.values().next().value}[${code}m`;

// Calculate the length of words split on ' ', ignoring
// the extra characters added by ansi escape codes
const wordLengths = str => str.split(' ').map(s => stringWidth(s));
const wordLengths = string => string.split(' ').map(character => stringWidth(character));

// Wrap a long word across multiple rows
// Ansi escape codes do not count towards length
const wrapWord = (rows, word, cols) => {
const arr = Array.from(word);
const wrapWord = (rows, word, columns) => {
const characters = [...word];

let insideEscape = false;
let visible = stringWidth(stripAnsi(rows[rows.length - 1]));

for (const item of arr.entries()) {
const i = item[0];
const char = item[1];
const charLength = stringWidth(char);
for (const [index, character] of characters.entries()) {
const characterLength = stringWidth(character);

if (visible + charLength <= cols) {
rows[rows.length - 1] += char;
if (visible + characterLength <= columns) {
rows[rows.length - 1] += character;
} else {
rows.push(char);
rows.push(character);
visible = 0;
}

if (ESCAPES.has(char)) {
if (ESCAPES.has(character)) {
insideEscape = true;
} else if (insideEscape && char === 'm') {
} else if (insideEscape && character === 'm') {
insideEscape = false;
continue;
}
Expand All @@ -47,9 +45,9 @@ const wrapWord = (rows, word, cols) => {
continue;
}

visible += charLength;
visible += characterLength;

if (visible === cols && i < arr.length - 1) {
if (visible === columns && index < characters.length - 1) {
rows.push('');
visible = 0;
}
Expand All @@ -66,33 +64,27 @@ const wrapWord = (rows, word, cols) => {
// in either 'hard' or 'soft' wrap mode
//
// 'hard' will never allow a string to take up more
// than cols characters
// than columns characters
//
// 'soft' allows long words to expand past the column length
const exec = (str, cols, opts) => {
const options = opts || {};

if (str.trim() === '') {
return options.trim === false ? str : str.trim();
const exec = (string, columns, options = {}) => {
if (string.trim() === '') {
return options.trim === false ? string : string.trim();
}

let pre = '';
let ret = '';
let escapeCode;

const lengths = wordLengths(str);
const words = str.split(' ');
const lengths = wordLengths(string);
const rows = [''];

for (const item of Array.from(words).entries()) {
const i = item[0];
const word = item[1];

for (const [index, word] of string.split(' ').entries()) {
rows[rows.length - 1] = options.trim === false ? rows[rows.length - 1] : rows[rows.length - 1].trim();
let rowLength = stringWidth(rows[rows.length - 1]);

if (rowLength || word === '') {
if (rowLength === cols && options.wordWrap === false) {
if (rowLength === columns && options.wordWrap === false) {
// If we start with a new word but the current row length equals the length of the columns, add a new row
rows.push('');
rowLength = 0;
Expand All @@ -103,51 +95,48 @@ const exec = (str, cols, opts) => {
}

// In 'hard' wrap mode, the length of a line is
// never allowed to extend past 'cols'
if (lengths[i] > cols && options.hard) {
// never allowed to extend past 'columns'
if (lengths[index] > columns && options.hard) {
if (rowLength) {
rows.push('');
}
wrapWord(rows, word, cols);
wrapWord(rows, word, columns);
continue;
}

if (rowLength + lengths[i] > cols && rowLength > 0) {
if (options.wordWrap === false && rowLength < cols) {
wrapWord(rows, word, cols);
if (rowLength + lengths[index] > columns && rowLength > 0) {
if (options.wordWrap === false && rowLength < columns) {
wrapWord(rows, word, columns);
continue;
}

rows.push('');
}

if (rowLength + lengths[i] > cols && options.wordWrap === false) {
wrapWord(rows, word, cols);
if (rowLength + lengths[index] > columns && options.wordWrap === false) {
wrapWord(rows, word, columns);
continue;
}

rows[rows.length - 1] += word;
}

pre = rows.map(r => options.trim === false ? r : r.trim()).join('\n');

for (const item of Array.from(pre).entries()) {
const i = item[0];
const char = item[1];
pre = rows.map(row => options.trim === false ? row : row.trim()).join('\n');

ret += char;
for (const [index, character] of [...pre].entries()) {
ret += character;

if (ESCAPES.has(char)) {
const code = parseFloat(/\d[^m]*/.exec(pre.slice(i, i + 4)));
if (ESCAPES.has(character)) {
const code = parseFloat(/\d[^m]*/.exec(pre.slice(index, index + 4)));
escapeCode = code === END_CODE ? null : code;
}

const code = ansiStyles.codes.get(Number(escapeCode));

if (escapeCode && code) {
if (pre[i + 1] === '\n') {
if (pre[index + 1] === '\n') {
ret += wrapAnsi(code);
} else if (char === '\n') {
} else if (character === '\n') {
ret += wrapAnsi(escapeCode);
}
}
Expand All @@ -157,10 +146,10 @@ const exec = (str, cols, opts) => {
};

// For each newline, invoke the method separately
module.exports = (str, cols, opts) => {
return String(str)
module.exports = (string, columns, options) => {
return String(string)
.normalize()
.split('\n')
.map(line => exec(line, cols, opts))
.map(line => exec(line, columns, options))
.join('\n');
};
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=6"
},
"scripts": {
"test": "xo && nyc ava",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
"test": "xo && nyc ava"
},
"files": [
"index.js"
Expand Down Expand Up @@ -52,12 +51,12 @@
"strip-ansi": "^4.0.0"
},
"devDependencies": {
"ava": "^0.23.0",
"ava": "^0.25.0",
"chalk": "^2.0.1",
"coveralls": "^3.0.0",
"has-ansi": "^3.0.0",
"nyc": "^11.0.3",
"nyc": "^13.0.1",
"strip-ansi": "^4.0.0",
"xo": "^0.18.2"
"xo": "^0.22.0"
}
}

0 comments on commit 76d163e

Please sign in to comment.