Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: chalk/ansi-regex
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0a8cc19946c03c38520fe8c086b8adb66f9cce0b
Choose a base ref
...
head repository: chalk/ansi-regex
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d908492e0070f26552fad1b25e339aff9011ae8b
Choose a head ref
  • 19 commits
  • 16 files changed
  • 9 contributors

Commits on Apr 21, 2018

  1. Add failing test for #21 (#22)

    cancerberoSgx authored and sindresorhus committed Apr 21, 2018

    Verified

    This commit was signed with the committer’s verified signature.
    bagder Daniel Stenberg
    Copy the full SHA
    14839a4 View commit details

Commits on Sep 18, 2018

  1. Add scroll escapes (#20)

    Fixes #19
    Qix- authored and sindresorhus committed Sep 18, 2018
    Copy the full SHA
    385eca9 View commit details
  2. Copy the full SHA
    eac826a View commit details
  3. Require Node.js 6

    sindresorhus committed Sep 18, 2018
    Copy the full SHA
    ced7421 View commit details
  4. 4.0.0

    sindresorhus committed Sep 18, 2018
    Copy the full SHA
    a1d9246 View commit details

Commits on Dec 25, 2018

  1. Copy the full SHA
    e076cd1 View commit details

Commits on Mar 8, 2019

  1. Support more escape types like links (#29)

    Fixes #21
    Fixes #28
    LitoMore authored and sindresorhus committed Mar 8, 2019
    Copy the full SHA
    96200bb View commit details
  2. 4.1.0

    sindresorhus committed Mar 8, 2019
    Copy the full SHA
    a079ab2 View commit details

Commits on May 31, 2019

  1. Tidelift tasks

    sindresorhus committed May 31, 2019
    Copy the full SHA
    f115fca View commit details
  2. Require Node.js 8

    sindresorhus committed May 31, 2019
    Copy the full SHA
    166a0d5 View commit details

Commits on Oct 4, 2019

  1. Add TypeScript definition (#32)

    Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
    Akim95 and sindresorhus committed Oct 4, 2019
    Copy the full SHA
    e77ea17 View commit details
  2. Meta tweaks

    sindresorhus committed Oct 4, 2019
    Copy the full SHA
    f26f7fe View commit details
  3. 5.0.0

    sindresorhus committed Oct 4, 2019
    Copy the full SHA
    2b56fb0 View commit details

Commits on Dec 16, 2020

  1. Add @Qix- to funding.yml

    sindresorhus authored Dec 16, 2020
    Copy the full SHA
    0755e66 View commit details

Commits on Jan 2, 2021

  1. Copy the full SHA
    178363b View commit details

Commits on Apr 16, 2021

  1. Copy the full SHA
    1b337ad View commit details
  2. 6.0.0

    sindresorhus committed Apr 16, 2021
    1
    Copy the full SHA
    c1b5e45 View commit details

Commits on Sep 10, 2021

  1. Fix potential ReDoS (#37)

    yetingli authored Sep 10, 2021
    14
    Copy the full SHA
    8d1d7cd View commit details
  2. 6.0.1

    Qix- committed Sep 10, 2021
    Copy the full SHA
    d908492 View commit details
Showing with 230 additions and 123 deletions.
  1. +1 −2 .gitattributes
  2. +2 −0 .github/funding.yml
  3. +3 −0 .github/security.md
  4. +21 −0 .github/workflows/main.yml
  5. +1 −0 .gitignore
  6. +1 −0 .npmrc
  7. +0 −5 .travis.yml
  8. +8 −8 fixtures/ansi-codes.js
  9. +10 −11 fixtures/view-codes.js
  10. +33 −0 index.d.ts
  11. +5 −7 index.js
  12. +5 −0 index.test-d.ts
  13. +1 −1 license
  14. +56 −51 package.json
  15. +35 −9 readme.md
  16. +48 −29 test.js
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
2 changes: 2 additions & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: [sindresorhus, Qix-]
tidelift: npm/ansi-regex
3 changes: 3 additions & 0 deletions .github/security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

16 changes: 8 additions & 8 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']],
@@ -10,6 +8,8 @@ exports.vt52Codes = new Map([
['I', ['Reverse line feed']],
['J', ['Erase to end of screen']],
['K', ['Erase to end of line']],
['S', ['Scroll up']],
['T', ['Scroll down']],
['Z', ['Identify']],
['=', ['Enter alternate keypad mode']],
['>', ['Exit alternate keypad mode']],
@@ -19,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)']],
@@ -78,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']],
@@ -183,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']],

@@ -213,8 +213,8 @@ exports.otherCode = new Map([
['6n', ['Get cursor position', 'DSR']]
]);

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

const allCodes = {};
const supported = [];
const unsupported = [];

function addCodesToTest(codes) {
for (const code in codes) {
allCodes[code] = codes[code];
for (const [key, value] of Object.entries(codes)) {
allCodes[key] = value;
}
}

function identifySupportedCodes() {
let codeSupport = {};

for (const code in allCodes) {
for (const [code, value] of Object.keys(allCodes)) {
codeSupport = {
code,
matches: `\u001B${code}`.match(ansiRegex()),
description: allCodes[code][0]
description: value[0]
};

if (codeSupport.matches !== null && codeSupport.matches[0] === `\u001B${code}`) {
@@ -34,15 +33,15 @@ function displaySupport() {
process.stdout.write('\u001B[32m');

console.log('SUPPORTED');
for (const el of supported) {
console.log(el);
for (const element of supported) {
console.log(element);
}

process.stdout.write('\u001B[31m');
console.log('UNSUPPORTED');

for (const el of unsupported) {
console.log(el);
for (const element of unsupported) {
console.log(element);
}

process.stdout.write('\u001B[0m');
33 changes: 33 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export interface Options {
/**
Match only the first ANSI escape.
@default false
*/
readonly onlyFirst: boolean;
}

/**
Regular expression for matching ANSI escape codes.
@example
```
import ansiRegex from 'ansi-regex';
ansiRegex().test('\u001B[4mcake\u001B[0m');
//=> true
ansiRegex().test('cake');
//=> false
'\u001B[4mcake\u001B[0m'.match(ansiRegex());
//=> ['\u001B[4m', '\u001B[0m']
'\u001B[4mcake\u001B[0m'.match(ansiRegex({onlyFirst: true}));
//=> ['\u001B[4m']
'\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007'.match(ansiRegex());
//=> ['\u001B]8;;https://github.com\u0007', '\u001B]8;;\u0007']
```
*/
export default function ansiRegex(options?: Options): RegExp;
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

module.exports = () => {
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-PRZcf-ntqry=><~]))'
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
].join('|');

return new RegExp(pattern, 'g');
};
return new RegExp(pattern, onlyFirst ? undefined : 'g');
}
5 changes: 5 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {expectType} from 'tsd';
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 <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:

107 changes: 56 additions & 51 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
{
"name": "ansi-regex",
"version": "3.0.0",
"description": "Regular expression for matching ANSI escape codes",
"license": "MIT",
"repository": "chalk/ansi-regex",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava",
"view-supported": "node fixtures/view-codes.js"
},
"files": [
"index.js"
],
"keywords": [
"ansi",
"styles",
"color",
"colour",
"colors",
"terminal",
"console",
"cli",
"string",
"tty",
"escape",
"formatting",
"rgb",
"256",
"shell",
"xterm",
"command-line",
"text",
"regex",
"regexp",
"re",
"match",
"test",
"find",
"pattern"
],
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "ansi-regex",
"version": "6.0.1",
"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": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd",
"view-supported": "node fixtures/view-codes.js"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"ansi",
"styles",
"color",
"colour",
"colors",
"terminal",
"console",
"cli",
"string",
"tty",
"escape",
"formatting",
"rgb",
"256",
"shell",
"xterm",
"command-line",
"text",
"regex",
"regexp",
"re",
"match",
"test",
"find",
"pattern"
],
"devDependencies": {
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
Loading