仓库指标
- 星标
- (14,327 个星标)
- PR 合并指标
- (30 天内没有已合并 PR)
描述
Description
Currently, the Code.findAllRanges method does not match an exact substring in a Code object. Instead, it unnecessarily creates a RegExp object. If the findAllCodeRanges function can take a RegExp object as an argument, why should it have to convert a string to RegExp?
Solution
I could not find any existing function that matched a substring in the docs, so I am suggesting a quick change to remove some code from the findAllCodeRanges function in packages/2d/src/lib/code/CodeRange.ts.
// remove this block of code
if (typeof pattern === 'string') {
pattern = new RegExp(pattern, 'g');
}
This means we can use code().findFirstRange('n * (n + 1) / 2') instead of typing out so many backslashes like this: code().findFirstRange('n \\* \\(n \\+ 1\\) \\/ 2'). It is probably better practice to use a RegExp constructor instead of implicitly converting a string into a RegExp.