From 071c5583e03650c655ff92a330008559c5a35036 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 16 Feb 2020 23:47:28 +0700 Subject: [PATCH] Require Node.js 10 --- .travis.yml | 1 - index.js | 21 ++++++++++++--------- license | 1 + package.json | 9 +++++---- readme.md | 6 ------ 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/.travis.yml b/.travis.yml index f98fed0..94ab01f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,4 +2,3 @@ language: node_js node_js: - '12' - '10' - - '8' diff --git a/index.js b/index.js index a2f41b8..72d37d9 100755 --- a/index.js +++ b/index.js @@ -16,17 +16,17 @@ const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => { for (let ansiCode of ansiCodes) { const ansiCodeOrigin = ansiCode; - if (ansiCode.match(';')) { + if (ansiCode.includes(';')) { ansiCode = ansiCode.split(';')[0][0] + '0'; } - const item = ansiStyles.codes.get(parseInt(ansiCode, 10)); + const item = ansiStyles.codes.get(Number.parseInt(ansiCode, 10)); if (item) { const indexEscape = ansiCodes.indexOf(item.toString()); - if (indexEscape >= 0) { - ansiCodes.splice(indexEscape, 1); - } else { + if (indexEscape === -1) { output.push(wrapAnsi(isEscapes ? item : ansiCodeOrigin)); + } else { + ansiCodes.splice(indexEscape, 1); } } else if (isEscapes) { output.push(wrapAnsi(0)); @@ -38,8 +38,9 @@ const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => { if (isEscapes) { output = output.filter((element, index) => output.indexOf(element) === index); + if (endAnsiCode !== undefined) { - const fistEscapeCode = wrapAnsi(ansiStyles.codes.get(parseInt(endAnsiCode, 10))); + const fistEscapeCode = wrapAnsi(ansiStyles.codes.get(Number.parseInt(endAnsiCode, 10))); output = output.reduce((current, next) => next === fistEscapeCode ? [next, ...current] : [...current, next], []); } } @@ -63,8 +64,10 @@ module.exports = (string, begin, end) => { if (ESCAPES.includes(character)) { const code = /\d[^m]*/.exec(string.slice(index, index + 18)); ansiCode = code && code.length > 0 ? code[0] : undefined; + if (visible < stringEnd) { isInsideEscape = true; + if (ansiCode !== undefined) { ansiCodes.push(ansiCode); } @@ -75,14 +78,14 @@ module.exports = (string, begin, end) => { } if (!isInsideEscape && !leftEscape) { - ++visible; + visible++; } if (!astralRegex({exact: true}).test(character) && isFullwidthCodePoint(character.codePointAt())) { - ++visible; + visible++; if (typeof end !== 'number') { - ++stringEnd; + stringEnd++; } } diff --git a/license b/license index 4d72633..e05b33c 100644 --- a/license +++ b/license @@ -1,6 +1,7 @@ MIT License Copyright (c) DC +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index 4112746..bdd0267 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,9 @@ "description": "Slice a string with ANSI escape codes", "license": "MIT", "repository": "chalk/slice-ansi", + "funding": "https://github.com/chalk/slice-ansi?sponsor=1", "engines": { - "node": ">=8" + "node": ">=10" }, "scripts": { "test": "xo && ava" @@ -43,9 +44,9 @@ }, "devDependencies": { "ava": "^2.1.0", - "chalk": "^2.4.2", + "chalk": "^3.0.0", "random-item": "^3.0.0", - "strip-ansi": "^5.0.0", - "xo": "^0.24.0" + "strip-ansi": "^6.0.0", + "xo": "^0.26.1" } } diff --git a/readme.md b/readme.md index 2c9c5ed..88d3677 100644 --- a/readme.md +++ b/readme.md @@ -2,14 +2,12 @@ > Slice a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) - ## Install ``` $ npm install slice-ansi ``` - ## Usage ```js @@ -22,7 +20,6 @@ const string = 'The quick brown ' + chalk.red('fox jumped over ') + console.log(sliceAnsi(string, 20, 30)); ``` - ## API ### sliceAnsi(string, beginSlice, endSlice?) @@ -45,20 +42,17 @@ Type: `number` Zero-based index at which to end the slice. - ## Related - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes - [cli-truncate](https://github.com/sindresorhus/cli-truncate) - Truncate a string to a specific width in the terminal - [chalk](https://github.com/chalk/chalk) - Terminal string styling done right - ## Maintainers - [Sindre Sorhus](https://github.com/sindresorhus) - [Josh Junon](https://github.com/qix-) - ---