Skip to content

Commit

Permalink
Require Node.js 14
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 24, 2023
1 parent 29f76a4 commit 424b42a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 38 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 18
- 16
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
25 changes: 14 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import isFullwidthCodePoint from 'is-fullwidth-code-point';
// \x1b and \x9b
const ESCAPES = new Set([27, 155]);

const CHAR_CODE_0 = '0'.charCodeAt(0);
const CHAR_CODE_9 = '9'.charCodeAt(0);
const CODE_POINT_0 = '0'.codePointAt(0);
const CODE_POINT_9 = '9'.codePointAt(0);

const endCodesSet = new Set();
const endCodesMap = new Map();
Expand Down Expand Up @@ -38,8 +38,8 @@ function getEndCode(code) {

function findNumberIndex(string) {
for (let index = 0; index < string.length; index++) {
const charCode = string.charCodeAt(index);
if (charCode >= CHAR_CODE_0 && charCode <= CHAR_CODE_9) {
const codePoint = string.codePointAt(index);
if (codePoint >= CODE_POINT_0 && codePoint <= CODE_POINT_9) {
return index;
}
}
Expand All @@ -60,7 +60,7 @@ function parseAnsiCode(string, offset) {
}
}

function tokenize(string, endChar = Number.POSITIVE_INFINITY) {
function tokenize(string, endCharacter = Number.POSITIVE_INFINITY) {
const returnValue = [];

let index = 0;
Expand All @@ -74,7 +74,7 @@ function tokenize(string, endChar = Number.POSITIVE_INFINITY) {
returnValue.push({
type: 'ansi',
code,
endCode: getEndCode(code)
endCode: getEndCode(code),
});
index += code.length;
continue;
Expand All @@ -87,11 +87,13 @@ function tokenize(string, endChar = Number.POSITIVE_INFINITY) {
returnValue.push({
type: 'character',
value: character,
isFullWidth
isFullWidth,
});

index += character.length;
visibleCount += isFullWidth ? 2 : character.length;
if (visibleCount >= endChar) {

if (visibleCount >= endCharacter) {
break;
}
}
Expand All @@ -101,6 +103,7 @@ function tokenize(string, endChar = Number.POSITIVE_INFINITY) {

function reduceAnsiCodes(codes) {
let returnValue = [];

for (const code of codes) {
if (code.code === ansiStyles.reset.open) {
// Reset code, disable all codes
Expand All @@ -124,7 +127,7 @@ function undoAnsiCodes(codes) {
return endCodes.reverse().join('');
}

export default function sliceAnsi(string, begin, end) {
export default function sliceAnsi(string, start, end) {
const tokens = tokenize(string, end);
let activeCodes = [];
let position = 0;
Expand All @@ -142,8 +145,8 @@ export default function sliceAnsi(string, begin, end) {
returnValue += token.code;
}
} else {
// Char
if (!include && position >= begin) {
// Character
if (!include && position >= start) {
include = true;
// Simplify active codes
activeCodes = reduceAnsiCodes(activeCodes);
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
"node": ">=14.16"
},
"scripts": {
"test": "xo && ava"
Expand Down Expand Up @@ -40,14 +40,14 @@
"text"
],
"dependencies": {
"ansi-styles": "^6.0.0",
"ansi-styles": "^6.2.1",
"is-fullwidth-code-point": "^4.0.0"
},
"devDependencies": {
"ava": "^3.15.0",
"chalk": "^4.1.0",
"random-item": "^4.0.0",
"strip-ansi": "^7.0.0",
"xo": "^0.38.2"
"ava": "^5.2.0",
"chalk": "^5.2.0",
"random-item": "^4.0.1",
"strip-ansi": "^7.0.1",
"xo": "^0.53.1"
}
}
22 changes: 5 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
## Install

```
$ npm install slice-ansi
```sh
npm install slice-ansi
```

## Usage
Expand All @@ -22,19 +22,19 @@ console.log(sliceAnsi(string, 20, 30));

## API

### sliceAnsi(string, beginSlice, endSlice?)
### sliceAnsi(string, startSlice, endSlice?)

#### string

Type: `string`

String with ANSI escape codes. Like one styled by [`chalk`](https://github.com/chalk/chalk).

#### beginSlice
#### startSlice

Type: `number`

Zero-based index at which to begin the slice.
Zero-based index at which to start the slice.

#### endSlice

Expand All @@ -52,15 +52,3 @@ Zero-based index at which to end the slice.

- [Sindre Sorhus](https://github.com/sindresorhus)
- [Josh Junon](https://github.com/qix-)

---

<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>

0 comments on commit 424b42a

Please # to comment.