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 178363b commit 1b337ad
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 51 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/ansi-regex
4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
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
12 changes: 5 additions & 7 deletions fixtures/ansi-codes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

// From http://www.umich.edu/~archive/apple2/misc/programmers/vt100.codes.txt
exports.vt52Codes = new Map([
export const vt52Codes = new Map([
['A', ['Cursor up']],
['B', ['Cursor down']],
['C', ['Cursor right']],
Expand All @@ -21,7 +19,7 @@ exports.vt52Codes = new Map([
]);

// From http://www.umich.edu/~archive/apple2/misc/programmers/vt100.codes.txt
exports.ansiCompatible = new Map([
export const ansiCompatible = new Map([
['[176A', ['Cursor up Pn lines']],
['[176B', ['Cursor down Pn lines']],
['[176C', ['Cursor forward Pn characters (right)']],
Expand Down Expand Up @@ -80,7 +78,7 @@ exports.ansiCompatible = new Map([
]);

// From http://ascii-table.com/ansi-escape-sequences-vt-100.php
exports.commonCodes = new Map([
export const commonCodes = new Map([
['[176A', ['Move cursor up n lines', 'CUU']],
['[176B', ['Move cursor down n lines', 'CUD']],
['[176C', ['Move cursor right n lines', 'CUF']],
Expand Down Expand Up @@ -185,7 +183,7 @@ exports.commonCodes = new Map([
]);

// From http://ascii-table.com/ansi-escape-sequences-vt-100.php
exports.otherCode = new Map([
export const otherCode = new Map([
['7', ['Save cursor position and attributes', 'DECSC']],
['8', ['Restore cursor position and attributes', 'DECSC']],

Expand Down Expand Up @@ -216,7 +214,7 @@ exports.otherCode = new Map([
]);

// `urxvt` escapes
exports.urxvt = new Map([
export const urxvt = new Map([
['[5~', ['URxvt.keysym.Prior']],
['[6~', ['URxvt.keysym.Next']],
['[7~', ['URxvt.keysym.Home']],
Expand Down
5 changes: 2 additions & 3 deletions fixtures/view-codes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const ansiCodes = require('./ansi-codes');
const ansiRegex = require('..');
import ansiCodes from './ansi-codes.js';
import ansiRegex from '../index.js';

const allCodes = {};
const supported = [];
Expand Down
22 changes: 9 additions & 13 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
declare namespace ansiRegex {
interface Options {
/**
Match only the first ANSI escape.
@default false
*/
onlyFirst: boolean;
}
export interface Options {
/**
Match only the first ANSI escape.
@default false
*/
readonly onlyFirst: boolean;
}

/**
Regular expression for matching ANSI escape codes.
@example
```
import ansiRegex = require('ansi-regex');
import ansiRegex from 'ansi-regex';
ansiRegex().test('\u001B[4mcake\u001B[0m');
//=> true
Expand All @@ -32,6 +30,4 @@ ansiRegex().test('cake');
//=> ['\u001B]8;;https://github.com\u0007', '\u001B]8;;\u0007']
```
*/
declare function ansiRegex(options?: ansiRegex.Options): RegExp;

export = ansiRegex;
export default function ansiRegex(options?: Options): RegExp;
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

module.exports = ({onlyFirst = false} = {}) => {
export default function ansiRegex({onlyFirst = false} = {}) {
const pattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
].join('|');

return new RegExp(pattern, onlyFirst ? undefined : 'g');
};
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import ansiRegex = require('.');
import ansiRegex from './index.js';

expectType<RegExp>(ansiRegex());
expectType<RegExp>(ansiRegex({onlyFirst: true}));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (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
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Regular expression for matching ANSI escape codes",
"license": "MIT",
"repository": "chalk/ansi-regex",
"funding": "https://github.com/chalk/ansi-regex?sponsor=1",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd",
Expand Down Expand Up @@ -48,8 +51,8 @@
"pattern"
],
"devDependencies": {
"ava": "^2.4.0",
"tsd": "^0.9.0",
"xo": "^0.25.3"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
10 changes: 2 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

> Regular expression for matching [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code)

## Install

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


## Usage

```js
const ansiRegex = require('ansi-regex');
import ansiRegex from 'ansi-regex';

ansiRegex().test('\u001B[4mcake\u001B[0m');
//=> true
Expand All @@ -31,7 +29,6 @@ ansiRegex().test('cake');
//=> ['\u001B]8;;https://github.com\u0007', '\u001B]8;;\u0007']
```


## API

### ansiRegex(options?)
Expand All @@ -44,12 +41,11 @@ Type: `object`

##### onlyFirst

Type: `boolean`<br>
Type: `boolean`\
Default: `false` *(Matches any ANSI escape codes in a string)*

Match only the first ANSI escape.


## FAQ

### Why do you test for codes not in the ECMA 48 standard?
Expand All @@ -58,13 +54,11 @@ Some of the codes we run as a test are codes that we acquired finding various li

On the historical side, those ECMA standards were established in the early 90's whereas the VT100, for example, was designed in the mid/late 70's. At that point in time, control codes were still pretty ungoverned and engineers used them for a multitude of things, namely to activate hardware ports that may have been proprietary. Somewhere else you see a similar 'anarchy' of codes is in the x86 architecture for processors; there are a ton of "interrupts" that can mean different things on certain brands of processors, most of which have been phased out.


## Maintainers

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


---

<div align="center">
Expand Down
8 changes: 3 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import ansiCodes from './fixtures/ansi-codes';
import ansiRegex from '.';
import * as ansiCodes from './fixtures/ansi-codes.js';
import ansiRegex from './index.js';

const consumptionCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+1234567890-=[]{};\':"./>?,<\\|';

Expand Down Expand Up @@ -60,9 +60,7 @@ 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 el of ansiCodes[codeSet]) {
const code = el[0];
const codeInfo = el[1];
for (const [code, codeInfo] of ansiCodes[codeSet]) {
const skip = /\d$/.test(code);
const skipText = skip ? '[SKIP] ' : '';
const ecode = `\u001B${code}`;
Expand Down

0 comments on commit 1b337ad

Please sign in to comment.