From f16f442e2342ae45825be79101334bf12a3dfa78 Mon Sep 17 00:00:00 2001 From: Hallz <736155049@qq.com> Date: Fri, 8 Sep 2023 13:58:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D`parseKey`=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E6=AD=A3=E7=A1=AE=E8=A7=A3=E6=9E=90=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I185f3ef98013c90d754b398d158b4813180fface --- src/test/utils/keyTree.test.ts | 40 +++++++++++++++++++++++++++++++++- src/utils/keyTree.ts | 2 +- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/test/utils/keyTree.test.ts b/src/test/utils/keyTree.test.ts index 7908962..f44548e 100644 --- a/src/test/utils/keyTree.test.ts +++ b/src/test/utils/keyTree.test.ts @@ -14,9 +14,47 @@ describe('keyTree 测试', () => { expect(tree.has('S.AAA')).toBeTruthy(); expect(tree.get('S.AAA')?.key).toBe('AAA'); - expect(() => tree.add('S.S1.S2.SS', KeyTreeNodeType.Leaf, false)).toThrow(); tree.add('Y.Y1.Y2.YS', KeyTreeNodeType.Leaf); expect(tree.has('Y.Y1.Y2.YS')).toBeTruthy(); + + const keys = ['p', 'p-p', '__pp--pp__', '$$o--c$', '--$/2331__=', '$$%%s_']; + tree.add(keys.join('.')); + + let prefix = ''; + for (var i = 0; i < keys.length; i++) { + const key = prefix ? `${prefix}.${keys[i]}` : keys[i]; + + expect(tree.has(key)).toBeTruthy(); + expect(tree.get(key)?.key).toBe(keys[i]); + + prefix = key; + } + }); + + test('新增直接子节点', () => { + expect(() => tree.add('S.SS.SSS.SSSS.SSSSS', KeyTreeNodeType.Leaf, false)).toThrowError(); + + tree.add('s', KeyTreeNodeType.Node); + + tree.add('S.AAA'); + + expect(tree.has('S.AAA')).toBeTruthy(); + + expect(tree.get('S.AAA')?.key).toBe('AAA'); + + const keys = ['p', 'p-p', '__pp--pp__', '$$o--c$', '--$/2331__=', '$$%%s_']; + + let prefix = ''; + for (var i = 0; i < keys.length; i++) { + const key = prefix ? `${prefix}.${keys[i]}` : keys[i]; + tree.add(key, KeyTreeNodeType.Node); + + expect(tree.has(key)).toBeTruthy(); + expect(tree.get(key)?.key).toBe(keys[i]); + + prefix = key; + } + }); test('key检测', () => { diff --git a/src/utils/keyTree.ts b/src/utils/keyTree.ts index 1b05c22..72f9fab 100644 --- a/src/utils/keyTree.ts +++ b/src/utils/keyTree.ts @@ -326,7 +326,7 @@ export class KeyTree { if (str.startsWith('.') || str.endsWith('.')) { throw new Error(`${key} 格式不正确,请确保是以'.'符号分割以及非空字符`); } - const [source, parentKey, childKey] = str.split(/^([\w-$_@.:?]+)\.([\w-$_@.:?]+)$/); + const [source, parentKey, childKey] = str.split(/^(.+)\.([^.]+)$/); if (source === str) { return {