Skip to content

Commit

Permalink
Allowing regex to match only first (chalk#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZSkycat committed Jul 6, 2018
1 parent 14839a4 commit f1bf82f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

module.exports = () => {
module.exports = onlyFirst => {
const pattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))'
].join('|');

return new RegExp(pattern, 'g');
return new RegExp(pattern, onlyFirst ? undefined : 'g');
};
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ test('match clear screen in a string', t => {
t.is('foo\u001B[2Jbar'.match(m())[0], '\u001B[2J');
});

test('match only first', t => {
t.is('foo\u001B[4mcake\u001B[0m'.match(m(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');
});
Expand Down

0 comments on commit f1bf82f

Please sign in to comment.