Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 16, 2021
1 parent 1d1eb94 commit 8dee3ba
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/funding.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github: [sindresorhus,Qix-]
github: [sindresorhus, Qix-]
tidelift: npm/slice-ansi
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const isFullwidthCodePoint = require('is-fullwidth-code-point');
const astralRegex = require('astral-regex');
const ansiStyles = require('ansi-styles');
import isFullwidthCodePoint from 'is-fullwidth-code-point';
import ansiStyles from 'ansi-styles';

const astralRegex = /^[\uD800-\uDBFF][\uDC00-\uDFFF]$/;

const ESCAPES = [
'\u001B',
Expand Down Expand Up @@ -41,14 +41,16 @@ const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => {

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

return output.join('');
};

module.exports = (string, begin, end) => {
export default function sliceAnsi(string, begin, end) {
const characters = [...string];
const ansiCodes = [];

Expand Down Expand Up @@ -81,7 +83,7 @@ module.exports = (string, begin, end) => {
visible++;
}

if (!astralRegex({exact: true}).test(character) && isFullwidthCodePoint(character.codePointAt())) {
if (!astralRegex.test(character) && isFullwidthCodePoint(character.codePointAt())) {
visible++;

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

return output;
};
}
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"license": "MIT",
"repository": "chalk/slice-ansi",
"funding": "https://github.com/chalk/slice-ansi?sponsor=1",
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"test": "xo && ava"
Expand Down Expand Up @@ -38,15 +40,14 @@
"text"
],
"dependencies": {
"ansi-styles": "^4.0.0",
"astral-regex": "^2.0.0",
"is-fullwidth-code-point": "^3.0.0"
"ansi-styles": "^6.0.0",
"is-fullwidth-code-point": "^4.0.0"
},
"devDependencies": {
"ava": "^2.1.0",
"chalk": "^3.0.0",
"random-item": "^3.0.0",
"strip-ansi": "^6.0.0",
"xo": "^0.26.1"
"ava": "^3.15.0",
"chalk": "^4.1.0",
"random-item": "^4.0.0",
"strip-ansi": "^7.0.0",
"xo": "^0.38.2"
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ $ npm install slice-ansi
## Usage

```js
const chalk = require('chalk');
const sliceAnsi = require('slice-ansi');
import chalk from 'chalk';
import sliceAnsi from 'slice-ansi';

const string = 'The quick brown ' + chalk.red('fox jumped over ') +
'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
Expand Down
39 changes: 20 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import util from 'util';
import test from 'ava';
import chalk from 'chalk';
import stripAnsi from 'strip-ansi';
import randomItem from 'random-item';
import sliceAnsi from '.';
import sliceAnsi from './index.js';

chalk.level = 1;

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

function generate(string) {
const rand1 = randomItem(['rock', 'paper', 'scissors']);
const rand2 = randomItem(['blue', 'green', 'yellow', 'red']);
return `${string}:${chalk[rand2](rand1)} `;
const random1 = randomItem(['rock', 'paper', 'scissors']);
const random2 = randomItem(['blue', 'green', 'yellow', 'red']);
return `${string}:${chalk[random2](random1)} `;
}

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

const a = util.inspect('\u001B[31mthe \u001B[39m\u001B[32mquick \u001B[39m');
const b = util.inspect('\u001B[34mbrown \u001B[39m\u001B[36mfox \u001B[39m');
const c = util.inspect('\u001B[31m \u001B[39m\u001B[32mquick \u001B[39m\u001B[34mbrown \u001B[39m\u001B[36mfox \u001B[39m');
const a = JSON.stringify('\u001B[31mthe \u001B[39m\u001B[32mquick \u001B[39m');
const b = JSON.stringify('\u001B[34mbrown \u001B[39m\u001B[36mfox \u001B[39m');
const c = JSON.stringify('\u001B[31m \u001B[39m\u001B[32mquick \u001B[39m\u001B[34mbrown \u001B[39m\u001B[36mfox \u001B[39m');

t.is(util.inspect(sliceAnsi(fixture, 0, 10)), a);
t.is(util.inspect(sliceAnsi(fixture, 10, 20)), b);
t.is(util.inspect(sliceAnsi(fixture, 3, 20)), c);
t.is(JSON.stringify(sliceAnsi(fixture, 0, 10)), a);
t.is(JSON.stringify(sliceAnsi(fixture, 10, 20)), b);
t.is(JSON.stringify(sliceAnsi(fixture, 3, 20)), c);

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);
const native = stripAnsi(str).slice(0, 55);
const ansi = stripAnsi(sliceAnsi(str, 0, 55));
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);
const native = stripAnsi(string).slice(0, 55);
const ansi = stripAnsi(sliceAnsi(string, 0, 55));
t.is(native, ansi);
});

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

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

0 comments on commit 8dee3ba

Please # to comment.