slice-ansi
Advanced tools
Comparing version 2.1.0 to 3.0.0
84
index.js
@@ -11,33 +11,74 @@ 'use strict'; | ||
const END_CODE = 39; | ||
const wrapAnsi = code => `${ESCAPES[0]}[${code}m`; | ||
module.exports = (str, begin, end) => { | ||
const arr = [...str.normalize()]; | ||
const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => { | ||
let output = []; | ||
ansiCodes = [...ansiCodes]; | ||
end = typeof end === 'number' ? end : arr.length; | ||
for (let ansiCode of ansiCodes) { | ||
const ansiCodeOrigin = ansiCode; | ||
if (ansiCode.match(';')) { | ||
ansiCode = ansiCode.split(';')[0][0] + '0'; | ||
} | ||
let insideEscape = false; | ||
let escapeCode = null; | ||
const item = ansiStyles.codes.get(parseInt(ansiCode, 10)); | ||
if (item) { | ||
const indexEscape = ansiCodes.indexOf(item.toString()); | ||
if (indexEscape >= 0) { | ||
ansiCodes.splice(indexEscape, 1); | ||
} else { | ||
output.push(wrapAnsi(isEscapes ? item : ansiCodeOrigin)); | ||
} | ||
} else if (isEscapes) { | ||
output.push(wrapAnsi(0)); | ||
break; | ||
} else { | ||
output.push(wrapAnsi(ansiCodeOrigin)); | ||
} | ||
} | ||
if (isEscapes) { | ||
output = output.filter((element, index) => output.indexOf(element) === index); | ||
if (endAnsiCode !== undefined) { | ||
const fistEscapeCode = wrapAnsi(ansiStyles.codes.get(parseInt(endAnsiCode, 10))); | ||
output = output.reduce((current, next) => next === fistEscapeCode ? [next, ...current] : [...current, next], []); | ||
} | ||
} | ||
return output.join(''); | ||
}; | ||
module.exports = (string, begin, end) => { | ||
const characters = [...string.normalize()]; | ||
const ansiCodes = []; | ||
end = typeof end === 'number' ? end : characters.length; | ||
let isInsideEscape = false; | ||
let ansiCode; | ||
let visible = 0; | ||
let output = ''; | ||
for (const [i, x] of arr.entries()) { | ||
for (const [index, character] of characters.entries()) { | ||
let leftEscape = false; | ||
if (ESCAPES.includes(x)) { | ||
insideEscape = true; | ||
const code = /\d[^m]*/.exec(str.slice(i, i + 18)); | ||
escapeCode = code === END_CODE ? null : code; | ||
} else if (insideEscape && x === 'm') { | ||
insideEscape = false; | ||
if (ESCAPES.includes(character)) { | ||
const code = /\d[^m]*/.exec(string.slice(index, index + 18)); | ||
ansiCode = code && code.length > 0 ? code[0] : undefined; | ||
if (visible < end) { | ||
isInsideEscape = true; | ||
if (ansiCode !== undefined) { | ||
ansiCodes.push(ansiCode); | ||
} | ||
} | ||
} else if (isInsideEscape && character === 'm') { | ||
isInsideEscape = false; | ||
leftEscape = true; | ||
} | ||
if (!insideEscape && !leftEscape) { | ||
if (!isInsideEscape && !leftEscape) { | ||
++visible; | ||
} | ||
if (!astralRegex({exact: true}).test(x) && isFullwidthCodePoint(x.codePointAt())) { | ||
if (!astralRegex({exact: true}).test(character) && isFullwidthCodePoint(character.codePointAt())) { | ||
++visible; | ||
@@ -47,10 +88,7 @@ } | ||
if (visible > begin && visible <= end) { | ||
output += x; | ||
} else if (visible === begin && !insideEscape && escapeCode !== null && escapeCode !== END_CODE) { | ||
output += wrapAnsi(escapeCode); | ||
output += character; | ||
} else if (visible === begin && !isInsideEscape && ansiCode !== undefined) { | ||
output = checkAnsi(ansiCodes); | ||
} else if (visible >= end) { | ||
if (escapeCode !== null) { | ||
output += wrapAnsi(ansiStyles.codes.get(parseInt(escapeCode, 10)) || END_CODE); | ||
} | ||
output += checkAnsi(ansiCodes, true, ansiCode); | ||
break; | ||
@@ -57,0 +95,0 @@ } |
{ | ||
"name": "slice-ansi", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Slice a string with ANSI escape codes", | ||
@@ -8,3 +8,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
@@ -41,10 +41,10 @@ "scripts": { | ||
"dependencies": { | ||
"ansi-styles": "^3.2.0", | ||
"astral-regex": "^1.0.0", | ||
"is-fullwidth-code-point": "^2.0.0" | ||
"ansi-styles": "^4.0.0", | ||
"astral-regex": "^2.0.0", | ||
"is-fullwidth-code-point": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.1.0", | ||
"ava": "^2.1.0", | ||
"chalk": "^2.4.2", | ||
"random-item": "^1.0.0", | ||
"random-item": "^3.0.0", | ||
"strip-ansi": "^5.0.0", | ||
@@ -51,0 +51,0 @@ "xo": "^0.24.0" |
@@ -19,6 +19,6 @@ # slice-ansi [![Build Status](https://travis-ci.org/chalk/slice-ansi.svg?branch=master)](https://travis-ci.org/chalk/slice-ansi) [![XO: Linted](https://img.shields.io/badge/xo-linted-blue.svg)](https://github.com/xojs/xo) | ||
const input = 'The quick brown ' + chalk.red('fox jumped over ') + | ||
const string = 'The quick brown ' + chalk.red('fox jumped over ') + | ||
'the lazy ' + chalk.green('dog and then ran away with the unicorn.'); | ||
console.log(sliceAnsi(input, 20, 30)); | ||
console.log(sliceAnsi(string, 20, 30)); | ||
``` | ||
@@ -29,5 +29,5 @@ | ||
### sliceAnsi(input, beginSlice, [endSlice]) | ||
### sliceAnsi(string, beginSlice, endSlice?) | ||
#### input | ||
#### string | ||
@@ -64,4 +64,12 @@ Type: `string` | ||
## License | ||
--- | ||
MIT | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm-slice_ansi?utm_source=npm-slice-ansi&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> |
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
6204
81
73
+ Addedansi-styles@4.3.0(transitive)
+ Addedastral-regex@2.0.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedastral-regex@1.0.0(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedis-fullwidth-code-point@2.0.0(transitive)
Updatedansi-styles@^4.0.0
Updatedastral-regex@^2.0.0