diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.gitignore b/.gitignore index 3c3629e..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index 7d69d74..2ae9d62 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: node_js node_js: + - '10' - '8' - '6' - - '4' diff --git a/fixtures/ansi-codes.js b/fixtures/ansi-codes.js index add2b14..90f04d3 100644 --- a/fixtures/ansi-codes.js +++ b/fixtures/ansi-codes.js @@ -215,7 +215,7 @@ exports.otherCode = new Map([ ['6n', ['Get cursor position', 'DSR']] ]); -// urxvt escapes +// `urxvt` escapes exports.urxvt = new Map([ ['[5~', ['URxvt.keysym.Prior']], ['[6~', ['URxvt.keysym.Next']], diff --git a/fixtures/view-codes.js b/fixtures/view-codes.js index a13105a..a921899 100644 --- a/fixtures/view-codes.js +++ b/fixtures/view-codes.js @@ -7,7 +7,7 @@ const supported = []; const unsupported = []; function addCodesToTest(codes) { - for (const code in codes) { + for (const code of Object.keys(codes)) { allCodes[code] = codes[code]; } } @@ -15,7 +15,7 @@ function addCodesToTest(codes) { function identifySupportedCodes() { let codeSupport = {}; - for (const code in allCodes) { + for (const code of Object.keys(allCodes)) { codeSupport = { code, matches: `\u001B${code}`.match(ansiRegex()), diff --git a/index.js b/index.js index e5d7d01..76d354a 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,9 @@ 'use strict'; -const defaultOptions = { - onlyFirst: false -}; - module.exports = options => { - options = Object.assign({}, defaultOptions, options); + options = Object.assign({ + onlyFirst: false + }, options); const pattern = [ '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)', diff --git a/package.json b/package.json index e94852f..8368ad8 100644 --- a/package.json +++ b/package.json @@ -1,53 +1,53 @@ { - "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": "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": ">=6" + }, + "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": "^0.25.0", + "xo": "^0.23.0" + } } diff --git a/readme.md b/readme.md index 353248e..1b9d5fa 100644 --- a/readme.md +++ b/readme.md @@ -42,7 +42,7 @@ Returns a regex for matching ANSI escape codes. Type: `boolean`
Default: `false` *(Matches any ANSI escape codes in a string)* -Match only the first one.
+Match only the first ANSI escape. ## FAQ diff --git a/test.js b/test.js index cd25205..acc59de 100644 --- a/test.js +++ b/test.js @@ -1,48 +1,48 @@ import test from 'ava'; import ansiCodes from './fixtures/ansi-codes'; -import m from '.'; +import ansiRegex from '.'; const consumptionChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+1234567890-=[]{};\':"./>?,<\\|'; // Testing against codes found at: http://ascii-table.com/ansi-escape-sequences-vt-100.php test('match ansi code in a string', t => { - t.regex('foo\u001B[4mcake\u001B[0m', m()); - t.regex('\u001B[4mcake\u001B[0m', m()); - t.regex('foo\u001B[4mcake\u001B[0m', m()); - t.regex('\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m', m()); - t.regex('foo\u001B[mfoo', m()); + t.regex('foo\u001B[4mcake\u001B[0m', ansiRegex()); + t.regex('\u001B[4mcake\u001B[0m', ansiRegex()); + t.regex('foo\u001B[4mcake\u001B[0m', ansiRegex()); + t.regex('\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m', ansiRegex()); + t.regex('foo\u001B[mfoo', ansiRegex()); }); test('match ansi code from ls command', t => { - t.regex('\u001B[00;38;5;244m\u001B[m\u001B[00;38;5;33mfoo\u001B[0m', m()); + t.regex('\u001B[00;38;5;244m\u001B[m\u001B[00;38;5;33mfoo\u001B[0m', ansiRegex()); }); test('match reset;setfg;setbg;italics;strike;underline sequence in a string', t => { - t.regex('\u001B[0;33;49;3;9;4mbar\u001B[0m', m()); - t.is('foo\u001B[0;33;49;3;9;4mbar'.match(m())[0], '\u001B[0;33;49;3;9;4m'); + t.regex('\u001B[0;33;49;3;9;4mbar\u001B[0m', ansiRegex()); + t.is('foo\u001B[0;33;49;3;9;4mbar'.match(ansiRegex())[0], '\u001B[0;33;49;3;9;4m'); }); test('match clear tabs sequence in a string', t => { - t.regex('foo\u001B[0gbar', m()); - t.is('foo\u001B[0gbar'.match(m())[0], '\u001B[0g'); + t.regex('foo\u001B[0gbar', ansiRegex()); + t.is('foo\u001B[0gbar'.match(ansiRegex())[0], '\u001B[0g'); }); test('match clear line from cursor right in a string', t => { - t.regex('foo\u001B[Kbar', m()); - t.is('foo\u001B[Kbar'.match(m())[0], '\u001B[K'); + t.regex('foo\u001B[Kbar', ansiRegex()); + t.is('foo\u001B[Kbar'.match(ansiRegex())[0], '\u001B[K'); }); test('match clear screen in a string', t => { - t.regex('foo\u001B[2Jbar', m()); - t.is('foo\u001B[2Jbar'.match(m())[0], '\u001B[2J'); + t.regex('foo\u001B[2Jbar', ansiRegex()); + t.is('foo\u001B[2Jbar'.match(ansiRegex())[0], '\u001B[2J'); }); test('match only first', t => { - t.is('foo\u001B[4mcake\u001B[0m'.match(m({onlyFirst: true})).length, 1); + t.is('foo\u001B[4mcake\u001B[0m'.match(ansiRegex({onlyFirst: true})).length, 1); }); test.failing('match "change icon name and window title" in string', t => { - t.is('\u001B]0;sg@tota:~/git/\u0007\u001B[01;32m[sg@tota\u001B[01;37m misc-tests\u001B[01;32m]$'.match(m())[0], '\u001B]0;sg@tota:~/git/\u0007'); + t.is('\u001B]0;sg@tota:~/git/\u0007\u001B[01;32m[sg@tota\u001B[01;37m misc-tests\u001B[01;32m]$'.match(ansiRegex())[0], '\u001B]0;sg@tota:~/git/\u0007'); }); // Testing against extended codes (excluding codes ending in 0-9) @@ -61,9 +61,9 @@ for (const codeSet of Object.keys(ansiCodes)) { } const string = `hel${ecode}lo`; - t.regex(string, m()); - t.is(string.match(m())[0], ecode); - t.is(string.replace(m(), ''), 'hello'); + t.regex(string, ansiRegex()); + t.is(string.match(ansiRegex())[0], ecode); + t.is(string.replace(ansiRegex(), ''), 'hello'); }); test(`${skipText}${code} should not overconsume`, t => { @@ -74,9 +74,9 @@ for (const codeSet of Object.keys(ansiCodes)) { for (const c of consumptionChars) { const string = ecode + c; - t.regex(string, m()); - t.is(string.match(m())[0], ecode); - t.is(string.replace(m(), ''), c); + t.regex(string, ansiRegex()); + t.is(string.match(ansiRegex())[0], ecode); + t.is(string.replace(ansiRegex(), ''), c); } }); }