Skip to content

Commit

Permalink
fix(path): fix segments match (#1826)
Browse files Browse the repository at this point in the history
  • Loading branch information
satouriko authored Jul 19, 2021
1 parent 0b936b1 commit 6e541dc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/path/src/__tests__/match.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ test('test group', () => {
expect(node.match('phases.0.steps.1.type')).toBeTruthy()
})

test('test segments', () => {
const node = Path.parse('a.0.b')
expect(node.match(['a', 0, 'b'])).toEqual(true)
})

match({
'*': [[], ['aa'], ['aa', 'bb', 'cc'], ['aa', 'dd', 'gg']],
'*.a.b': [
Expand Down
4 changes: 2 additions & 2 deletions packages/path/src/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
RangeExpressionNode,
DotOperatorNode,
} from './types'
import { isEqual, toArr } from './shared'
import { isEqual, toArr, isSegmentEqual } from './shared'

const isValid = (val) => val !== undefined && val !== null && val !== ''

Expand Down Expand Up @@ -231,7 +231,7 @@ export class Matcher {
if (source.length !== target.length) return false
const match = (pos: number) => {
const current = () => {
const res = isEqual(source[pos], target[pos])
const res = isSegmentEqual(source[pos], target[pos])
if (record && record.score !== undefined) {
record.score++
}
Expand Down
5 changes: 5 additions & 0 deletions packages/path/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ export const isEqual = (a: any, b: any) => {
}
return a !== a && b !== b
}
export const isSegmentEqual = (a: any, b: any) => {
a = typeof a === 'symbol' ? a : `${a}`
b = typeof b === 'symbol' ? b : `${b}`
return a === b
}

0 comments on commit 6e541dc

Please sign in to comment.