From 6e541dcb05ccc721a52424bc67b40a3ec18f7eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A0=82=E7=B3=96=E6=A2=A8=E5=AD=90?= <18373361+satouriko@users.noreply.github.com> Date: Mon, 19 Jul 2021 16:55:01 +0800 Subject: [PATCH] fix(path): fix segments match (#1826) --- packages/path/src/__tests__/match.spec.ts | 5 +++++ packages/path/src/matcher.ts | 4 ++-- packages/path/src/shared.ts | 5 +++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/path/src/__tests__/match.spec.ts b/packages/path/src/__tests__/match.spec.ts index 2e77b090b06..6f01e322492 100644 --- a/packages/path/src/__tests__/match.spec.ts +++ b/packages/path/src/__tests__/match.spec.ts @@ -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': [ diff --git a/packages/path/src/matcher.ts b/packages/path/src/matcher.ts index 5cb79f846d3..20d3946093e 100644 --- a/packages/path/src/matcher.ts +++ b/packages/path/src/matcher.ts @@ -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 !== '' @@ -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++ } diff --git a/packages/path/src/shared.ts b/packages/path/src/shared.ts index e45905bbf7d..c28f6e0f3d9 100644 --- a/packages/path/src/shared.ts +++ b/packages/path/src/shared.ts @@ -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 +}