Skip to content
This repository has been archived by the owner on Feb 12, 2025. It is now read-only.

Commit

Permalink
fix: fix converting glob expression to regex error and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gweesin committed Feb 6, 2025
1 parent 7cd80d7 commit ea280fe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import pm from 'picomatch'
export function constructPatternFilter(patterns: string[]): (str: string) => boolean {
const matchers = patterns.map((glob) => {
if (glob.match(/[\^$*{}]/)) {
const re = pm.toRegex(glob)
const { output } = pm.parse(glob)
const re = pm.toRegex(output)
return (str: string) => re.test(str)
}
else {
Expand Down
36 changes: 36 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, it } from 'vitest'
import { constructPatternFilter } from '../src/utils'

describe('constructPatternFilter', () => {
it('should match exact strings', () => {
const filter = constructPatternFilter(['test', 'example'])
expect(filter('test')).toBe(true)
expect(filter('example')).toBe(true)
expect(filter('other')).toBe(false)
})

it('should match patterns with wildcards', () => {
const filter = constructPatternFilter(['*.js', '*.ts'])
expect(filter('file.js')).toBe(true)
expect(filter('file.ts')).toBe(true)
expect(filter('file.txt')).toBe(false)
})

it('should match patterns with regex special characters', () => {
const filter = constructPatternFilter(['example*'])
expect(filter('example123')).toBe(true)
expect(filter('test123')).toBe(false)
})

it('should handle empty patterns', () => {
const filter = constructPatternFilter([])
expect(filter('anything')).toBe(false)
})

it('should match mixed exact strings and patterns', () => {
const filter = constructPatternFilter(['test', '*.js'])
expect(filter('test')).toBe(true)
expect(filter('file.js')).toBe(true)
expect(filter('example')).toBe(false)
})
})

0 comments on commit ea280fe

Please sign in to comment.