Skip to content

Commit

Permalink
Require Node.js 10
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 16, 2020
1 parent 013d918 commit 071c558
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- '12'
- '10'
- '8'
21 changes: 12 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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], []);
}
}
Expand All @@ -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);
}
Expand All @@ -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++;
}
}

Expand Down
1 change: 1 addition & 0 deletions license
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (c) DC <threedeecee@gmail.com>
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:

Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
}
}
6 changes: 0 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?)
Expand All @@ -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-)


---

<div align="center">
Expand Down

0 comments on commit 071c558

Please sign in to comment.