Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 9, 2024
1 parent 02fa893 commit b630317
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 50 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 22
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
10 changes: 5 additions & 5 deletions fixtures/ansi-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const vt52Codes = new Map([
['2', ['Graphics processor off']],
['<', ['Enter ANSI mode']],
['s', ['Cursor save']],
['u', ['Cursor restore']]
['u', ['Cursor restore']],
]);

// From https://espterm.github.io/docs/VT100%20escape%20codes.html
Expand Down Expand Up @@ -76,7 +76,7 @@ export const ansiCompatible = new Map([

['c', ['Causes power-up reset routine to be executed']],
['#8', ['Fill screen with "E"']],
['[2;176y', ['Invoke Test(s), where Ps is a decimal computed by adding the numbers of the desired tests to be executed']]
['[2;176y', ['Invoke Test(s), where Ps is a decimal computed by adding the numbers of the desired tests to be executed']],
]);

// From http://ascii-table.com/ansi-escape-sequences-vt-100.php
Expand Down Expand Up @@ -181,7 +181,7 @@ export const commonCodes = new Map([
['[1q', ['Turn on LED #1', 'DECLL1']],
['[2q', ['Turn on LED #2', 'DECLL2']],
['[3q', ['Turn on LED #3', 'DECLL3']],
['[4q', ['Turn on LED #4', 'DECLL4']]
['[4q', ['Turn on LED #4', 'DECLL4']],
]);

// From http://ascii-table.com/ansi-escape-sequences-vt-100.php
Expand Down Expand Up @@ -212,7 +212,7 @@ export const otherCode = new Map([
['5n', ['Device status report', 'DSR']],
['0n', ['Response: terminal is OK', 'DSR']],
['3n', ['Response: terminal is not OK', 'DSR']],
['6n', ['Get cursor position', 'DSR']]
['6n', ['Get cursor position', 'DSR']],
]);

// `urxvt` escapes
Expand All @@ -229,5 +229,5 @@ export const urxvt = new Map([
['[3;5;606t', ['URxvt.keysym.C-M-y']],
['[3;1605;5t', ['URxvt.keysym.C-M-e']],
['[3;1605;606t', ['URxvt.keysym.C-M-c']],
[']710;9x15bold\u0007', ['URxvt.keysym.font']]
[']710;9x15bold\u0007', ['URxvt.keysym.font']],
]);
5 changes: 3 additions & 2 deletions fixtures/view-codes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ansiCodes from './ansi-codes.js';
import process from 'node:process';
import ansiRegex from '../index.js';
import ansiCodes from './ansi-codes.js';

const allCodes = {};
const supported = [];
Expand All @@ -18,7 +19,7 @@ function identifySupportedCodes() {
codeSupport = {
code,
matches: `\u001B${code}`.match(ansiRegex()),
description: value[0]
description: value[0],
};

if (codeSupport.matches !== null && codeSupport.matches[0] === `\u001B${code}`) {
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export interface Options {
export type Options = {
/**
Match only the first ANSI escape.
@default false
*/
readonly onlyFirst: boolean;
}
};

/**
Regular expression for matching ANSI escape codes.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default function ansiRegex({onlyFirst = false} = {}) {
const pattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))'
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))',
].join('|');

return new RegExp(pattern, onlyFirst ? undefined : 'g');
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
},
"type": "module",
"exports": "./index.js",
"types": "./index.d.ts",
"sideEffects": false,
"engines": {
"node": ">=12"
},
Expand Down Expand Up @@ -53,7 +55,7 @@
"devDependencies": {
"ansi-escapes": "^5.0.0",
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
"tsd": "^0.21.0",
"xo": "^0.54.2"
}
}
16 changes: 2 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
## Install

```
$ npm install ansi-regex
```sh
npm install ansi-regex
```

## Usage
Expand Down Expand Up @@ -58,15 +58,3 @@ On the historical side, those ECMA standards were established in the early 90's

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

---

<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-ansi-regex?utm_source=npm-ansi-regex&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>
40 changes: 19 additions & 21 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ test('match terminal link', t => {
t.regex('\u001B]8;;mailto:[email protected]\u0007mail\u001B]8;;\u0007', ansiRegex());
t.deepEqual('\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le\u0007click\u001B]8;;\u0007'.match(ansiRegex()), [
'\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le\u0007',
'\u001B]8;;\u0007'
'\u001B]8;;\u0007',
]);
t.deepEqual('\u001B]8;;mailto:[email protected]\u0007mail-me\u001B]8;;\u0007'.match(ansiRegex()), [
'\u001B]8;;mailto:[email protected]\u0007',
'\u001B]8;;\u0007'
'\u001B]8;;\u0007',
]);
});

Expand All @@ -60,14 +60,14 @@ test('match "change icon name and window title" in string', t => {
});

// Testing against extended codes (excluding codes ending in 0-9)
for (const codeSet of Object.keys(ansiCodes)) {
for (const [code, codeInfo] of ansiCodes[codeSet]) {
const skip = /\d$/.test(code);
const skipText = skip ? '[SKIP] ' : '';
for (const [codeSetKey, codeSetValue] of Object.entries(ansiCodes)) {
for (const [code, codeInfo] of codeSetValue) {
const shouldSkip = /\d$/.test(code);
const skipText = shouldSkip ? '[SKIP] ' : '';
const ecode = `\u001B${code}`;

test(`${codeSet} - ${skipText}${code}${codeInfo[0]}`, t => {
if (skip) {
test(`${codeSetKey} - ${skipText}${code}${codeInfo[0]}`, t => {
if (shouldSkip) {
t.pass();
return;
}
Expand All @@ -78,36 +78,34 @@ for (const codeSet of Object.keys(ansiCodes)) {
t.is(string.replace(ansiRegex(), ''), 'hello');
});

test(`${codeSet} - ${skipText}${code} should not overconsume`, t => {
if (skip) {
test(`${codeSetKey} - ${skipText}${code} should not overconsume`, t => {
if (shouldSkip) {
t.pass();
return;
}

for (const c of consumptionCharacters) {
const string = ecode + c;
for (const character of consumptionCharacters) {
const string = ecode + character;
t.regex(string, ansiRegex());
t.is(string.match(ansiRegex())[0], ecode);
t.is(string.replace(ansiRegex(), ''), c);
t.is(string.replace(ansiRegex(), ''), character);
}
});
}
}

const escapeCodeFunctionArgs = [1, 2];
const escapeCodeFunctionArguments = [1, 2];
const escapeCodeIgnoresList = new Set(['beep', 'image', 'iTerm']);
const escapeCodeResultMap = new Map([['link', escapeCodeFunctionArgs[0]]]);
const escapeCodeResultMap = new Map([['link', escapeCodeFunctionArguments[0]]]);

for (const key of Object.keys(ansiEscapes)) {
for (const [key, escapeCode] of Object.entries(ansiEscapes)) {
if (escapeCodeIgnoresList.has(key)) {
continue;
}

const escapeCode = ansiEscapes[key];

const escapeCodeValue = typeof escapeCode === 'function' ?
escapeCode(...escapeCodeFunctionArgs) :
escapeCode;
const escapeCodeValue = typeof escapeCode === 'function'
? escapeCode(...escapeCodeFunctionArguments)
: escapeCode;

test(`ansi-escapes ${key}`, t => {
for (const character of consumptionCharacters) {
Expand Down

0 comments on commit b630317

Please sign in to comment.