-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into greenkeeper/husky-2.0.0
- Loading branch information
Showing
21 changed files
with
529 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { | ||
optimize | ||
} from './optimize'; | ||
|
||
describe('UserAgentRegExp', () => { | ||
|
||
describe('optimize', () => { | ||
|
||
it('should remove braces', () => { | ||
|
||
expect( | ||
optimize('(Family)foo(NotFamily)(bar)') | ||
).toBe( | ||
'FamilyfooNotFamilybar' | ||
); | ||
|
||
expect( | ||
optimize('(Family)foo(NotFamily|bar)') | ||
).toBe( | ||
'Familyfoo(NotFamily|bar)' | ||
); | ||
|
||
expect( | ||
optimize('(Family)(foo)?(?=NotFamily)') | ||
).toBe( | ||
'Family(foo)?(?=NotFamily)' | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
BRACED_NUMBER_PATTERN, | ||
ESCAPE_SYMBOL, | ||
skipSquareBraces, | ||
capturePostfix | ||
} from './util'; | ||
|
||
export const OPTIMIZABLE_GROUP = /^\([\s\w\d_\-/!]+\)$/; | ||
|
||
/** | ||
* Optimize RegExp string: remove useless braces. | ||
* @param regExpStr - RegExp string to optimize. | ||
* @return Optimized RegExp string. | ||
*/ | ||
export function optimize(regExpStr: string) { | ||
|
||
const regExpStrLength = regExpStr.length; | ||
let inGroup = false; | ||
let skip = false; | ||
let char = ''; | ||
let prevChar = ''; | ||
let postfix = ''; | ||
let groupAccum = ''; | ||
let optimizedRegExpStr = ''; | ||
|
||
for (let i = 0; i < regExpStrLength; i++) { | ||
|
||
char = regExpStr[i]; | ||
prevChar = regExpStr[i - 1]; | ||
skip = skipSquareBraces(skip, prevChar, char); | ||
|
||
if (!skip | ||
&& prevChar !== ESCAPE_SYMBOL | ||
&& char === '(' | ||
) { | ||
|
||
if (inGroup) { | ||
optimizedRegExpStr += groupAccum; | ||
} | ||
|
||
inGroup = true; | ||
groupAccum = ''; | ||
} | ||
|
||
if (inGroup) { | ||
groupAccum += char; | ||
} else { | ||
optimizedRegExpStr += char; | ||
} | ||
|
||
if (!skip | ||
&& prevChar !== ESCAPE_SYMBOL | ||
&& char === ')' | ||
&& inGroup | ||
) { | ||
|
||
inGroup = false; | ||
postfix = capturePostfix(regExpStr, i + 1); | ||
groupAccum += postfix; | ||
|
||
if (groupAccum === BRACED_NUMBER_PATTERN | ||
|| OPTIMIZABLE_GROUP.test(groupAccum) | ||
) { | ||
groupAccum = groupAccum.substr(1, groupAccum.length - 2); | ||
} | ||
|
||
optimizedRegExpStr += groupAccum; | ||
i += postfix.length; | ||
} | ||
} | ||
|
||
return optimizedRegExpStr; | ||
} |
Oops, something went wrong.