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 {