File tree 3 files changed +31
-2
lines changed
3 files changed +31
-2
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
- module . exports = ( ) => {
3
+ const defaultOptions = {
4
+ onlyFirst : false
5
+ } ;
6
+
7
+ module . exports = options => {
8
+ options = Object . assign ( { } , defaultOptions , options ) ;
9
+
4
10
const pattern = [
5
11
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)' ,
6
12
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
7
13
] . join ( '|' ) ;
8
14
9
- return new RegExp ( pattern , 'g' ) ;
15
+ return new RegExp ( pattern , options . onlyFirst ? undefined : 'g' ) ;
10
16
} ;
Original file line number Diff line number Diff line change @@ -23,9 +23,28 @@ ansiRegex().test('cake');
23
23
24
24
' \u001B [4mcake\u001B [0m' .match (ansiRegex ());
25
25
// => ['\u001B[4m', '\u001B[0m']
26
+
27
+ ' \u001B [4mcake\u001B [0m' .match (ansiRegex ({onlyFirst: true }));
28
+ // => ['\u001B[4m']
26
29
```
27
30
28
31
32
+ ## API
33
+
34
+ ### ansiRegex([ options] )
35
+
36
+ Returns a regex for matching ANSI escape codes.
37
+
38
+ #### options
39
+
40
+ ##### onlyFirst
41
+
42
+ Type: ` boolean ` <br >
43
+ Default: ` false ` * (Matches any ANSI escape codes in a string)*
44
+
45
+ Match only the first one.<br >
46
+
47
+
29
48
## FAQ
30
49
31
50
### Why do you test for codes not in the ECMA 48 standard?
Original file line number Diff line number Diff line change @@ -37,6 +37,10 @@ test('match clear screen in a string', t => {
37
37
t . is ( 'foo\u001B[2Jbar' . match ( m ( ) ) [ 0 ] , '\u001B[2J' ) ;
38
38
} ) ;
39
39
40
+ test ( 'match only first' , t => {
41
+ t . is ( 'foo\u001B[4mcake\u001B[0m' . match ( m ( { onlyFirst : true } ) ) . length , 1 ) ;
42
+ } ) ;
43
+
40
44
test . failing ( 'match "change icon name and window title" in string' , t => {
41
45
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' ) ;
42
46
} ) ;
You can’t perform that action at this time.
0 commit comments