Skip to content

Commit 8dee3ba

Browse files
committed
Require Node.js 12 and move to ESM
1 parent 1d1eb94 commit 8dee3ba

File tree

6 files changed

+43
-40
lines changed

6 files changed

+43
-40
lines changed

.github/funding.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github: [sindresorhus,Qix-]
1+
github: [sindresorhus, Qix-]
22
tidelift: npm/slice-ansi

.github/workflows/main.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ jobs:
1212
node-version:
1313
- 14
1414
- 12
15-
- 10
1615
steps:
1716
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v1
17+
- uses: actions/setup-node@v2
1918
with:
2019
node-version: ${{ matrix.node-version }}
2120
- run: npm install

index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
'use strict';
2-
const isFullwidthCodePoint = require('is-fullwidth-code-point');
3-
const astralRegex = require('astral-regex');
4-
const ansiStyles = require('ansi-styles');
1+
import isFullwidthCodePoint from 'is-fullwidth-code-point';
2+
import ansiStyles from 'ansi-styles';
3+
4+
const astralRegex = /^[\uD800-\uDBFF][\uDC00-\uDFFF]$/;
55

66
const ESCAPES = [
77
'\u001B',
@@ -41,14 +41,16 @@ const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => {
4141

4242
if (endAnsiCode !== undefined) {
4343
const fistEscapeCode = wrapAnsi(ansiStyles.codes.get(Number.parseInt(endAnsiCode, 10)));
44+
// TODO: Remove the use of `.reduce` here.
45+
// eslint-disable-next-line unicorn/no-array-reduce
4446
output = output.reduce((current, next) => next === fistEscapeCode ? [next, ...current] : [...current, next], []);
4547
}
4648
}
4749

4850
return output.join('');
4951
};
5052

51-
module.exports = (string, begin, end) => {
53+
export default function sliceAnsi(string, begin, end) {
5254
const characters = [...string];
5355
const ansiCodes = [];
5456

@@ -81,7 +83,7 @@ module.exports = (string, begin, end) => {
8183
visible++;
8284
}
8385

84-
if (!astralRegex({exact: true}).test(character) && isFullwidthCodePoint(character.codePointAt())) {
86+
if (!astralRegex.test(character) && isFullwidthCodePoint(character.codePointAt())) {
8587
visible++;
8688

8789
if (typeof end !== 'number') {
@@ -100,4 +102,4 @@ module.exports = (string, begin, end) => {
100102
}
101103

102104
return output;
103-
};
105+
}

package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
"license": "MIT",
66
"repository": "chalk/slice-ansi",
77
"funding": "https://github.com/chalk/slice-ansi?sponsor=1",
8+
"type": "module",
9+
"exports": "./index.js",
810
"engines": {
9-
"node": ">=10"
11+
"node": ">=12"
1012
},
1113
"scripts": {
1214
"test": "xo && ava"
@@ -38,15 +40,14 @@
3840
"text"
3941
],
4042
"dependencies": {
41-
"ansi-styles": "^4.0.0",
42-
"astral-regex": "^2.0.0",
43-
"is-fullwidth-code-point": "^3.0.0"
43+
"ansi-styles": "^6.0.0",
44+
"is-fullwidth-code-point": "^4.0.0"
4445
},
4546
"devDependencies": {
46-
"ava": "^2.1.0",
47-
"chalk": "^3.0.0",
48-
"random-item": "^3.0.0",
49-
"strip-ansi": "^6.0.0",
50-
"xo": "^0.26.1"
47+
"ava": "^3.15.0",
48+
"chalk": "^4.1.0",
49+
"random-item": "^4.0.0",
50+
"strip-ansi": "^7.0.0",
51+
"xo": "^0.38.2"
5152
}
5253
}

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ $ npm install slice-ansi
1111
## Usage
1212

1313
```js
14-
const chalk = require('chalk');
15-
const sliceAnsi = require('slice-ansi');
14+
import chalk from 'chalk';
15+
import sliceAnsi from 'slice-ansi';
1616

1717
const string = 'The quick brown ' + chalk.red('fox jumped over ') +
1818
'the lazy ' + chalk.green('dog and then ran away with the unicorn.');

test.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
1-
import util from 'util';
21
import test from 'ava';
32
import chalk from 'chalk';
43
import stripAnsi from 'strip-ansi';
54
import randomItem from 'random-item';
6-
import sliceAnsi from '.';
5+
import sliceAnsi from './index.js';
6+
7+
chalk.level = 1;
78

89
const fixture = chalk.red('the ') + chalk.green('quick ') + chalk.blue('brown ') + chalk.cyan('fox ') + chalk.yellow('jumped ');
910
const stripped = stripAnsi(fixture);
1011

1112
function generate(string) {
12-
const rand1 = randomItem(['rock', 'paper', 'scissors']);
13-
const rand2 = randomItem(['blue', 'green', 'yellow', 'red']);
14-
return `${string}:${chalk[rand2](rand1)} `;
13+
const random1 = randomItem(['rock', 'paper', 'scissors']);
14+
const random2 = randomItem(['blue', 'green', 'yellow', 'red']);
15+
return `${string}:${chalk[random2](random1)} `;
1516
}
1617

1718
test('main', t => {
1819
// The slice should behave exactly as a regular JS slice behaves
19-
for (let i = 0; i < 20; i++) {
20-
for (let j = 19; j > i; j--) {
21-
const nativeSlice = stripped.slice(i, j);
22-
const ansiSlice = sliceAnsi(fixture, i, j);
20+
for (let index = 0; index < 20; index++) {
21+
for (let index2 = 19; index2 > index; index2--) {
22+
const nativeSlice = stripped.slice(index, index2);
23+
const ansiSlice = sliceAnsi(fixture, index, index2);
2324
t.is(nativeSlice, stripAnsi(ansiSlice));
2425
}
2526
}
2627

27-
const a = util.inspect('\u001B[31mthe \u001B[39m\u001B[32mquick \u001B[39m');
28-
const b = util.inspect('\u001B[34mbrown \u001B[39m\u001B[36mfox \u001B[39m');
29-
const c = util.inspect('\u001B[31m \u001B[39m\u001B[32mquick \u001B[39m\u001B[34mbrown \u001B[39m\u001B[36mfox \u001B[39m');
28+
const a = JSON.stringify('\u001B[31mthe \u001B[39m\u001B[32mquick \u001B[39m');
29+
const b = JSON.stringify('\u001B[34mbrown \u001B[39m\u001B[36mfox \u001B[39m');
30+
const c = JSON.stringify('\u001B[31m \u001B[39m\u001B[32mquick \u001B[39m\u001B[34mbrown \u001B[39m\u001B[36mfox \u001B[39m');
3031

31-
t.is(util.inspect(sliceAnsi(fixture, 0, 10)), a);
32-
t.is(util.inspect(sliceAnsi(fixture, 10, 20)), b);
33-
t.is(util.inspect(sliceAnsi(fixture, 3, 20)), c);
32+
t.is(JSON.stringify(sliceAnsi(fixture, 0, 10)), a);
33+
t.is(JSON.stringify(sliceAnsi(fixture, 10, 20)), b);
34+
t.is(JSON.stringify(sliceAnsi(fixture, 3, 20)), c);
3435

35-
const str = generate(1) + generate(2) + generate(3) + generate(4) + generate(5) + generate(6) + generate(7) + generate(8) + generate(9) + generate(10) + generate(11) + generate(12) + generate(13) + generate(14) + generate(15) + generate(1) + generate(2) + generate(3) + generate(4) + generate(5) + generate(6) + generate(7) + generate(8) + generate(9) + generate(10) + generate(11) + generate(12) + generate(13) + generate(14) + generate(15);
36-
const native = stripAnsi(str).slice(0, 55);
37-
const ansi = stripAnsi(sliceAnsi(str, 0, 55));
36+
const string = generate(1) + generate(2) + generate(3) + generate(4) + generate(5) + generate(6) + generate(7) + generate(8) + generate(9) + generate(10) + generate(11) + generate(12) + generate(13) + generate(14) + generate(15) + generate(1) + generate(2) + generate(3) + generate(4) + generate(5) + generate(6) + generate(7) + generate(8) + generate(9) + generate(10) + generate(11) + generate(12) + generate(13) + generate(14) + generate(15);
37+
const native = stripAnsi(string).slice(0, 55);
38+
const ansi = stripAnsi(sliceAnsi(string, 0, 55));
3839
t.is(native, ansi);
3940
});
4041

@@ -90,7 +91,7 @@ test('doesn\'t add extra escapes', t => {
9091
const output = `${chalk.black.bgYellow(' RUNS ')} ${chalk.green('test')}`;
9192
t.is(sliceAnsi(output, 0, 7), `${chalk.black.bgYellow(' RUNS ')} `);
9293
t.is(sliceAnsi(output, 0, 8), `${chalk.black.bgYellow(' RUNS ')} `);
93-
t.is(sliceAnsi('\u001B[31m' + output, 0, 4), `\u001B[31m${chalk.black.bgYellow(' RUN')}`);
94+
t.is(JSON.stringify(sliceAnsi('\u001B[31m' + output, 0, 4)), JSON.stringify(`\u001B[31m${chalk.black.bgYellow(' RUN')}`));
9495
});
9596

9697
// See https://github.com/chalk/slice-ansi/issues/26

0 commit comments

Comments
 (0)