Skip to content

Commit

Permalink
fix: 修复parseKey没有正确解析的问题
Browse files Browse the repository at this point in the history
Change-Id: I185f3ef98013c90d754b398d158b4813180fface
  • Loading branch information
njikm2010 committed Sep 8, 2023
1 parent 62c8c1a commit f16f442
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
40 changes: 39 additions & 1 deletion src/test/utils/keyTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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检测', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/keyTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export class KeyTree<T extends {}> {
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 {
Expand Down

0 comments on commit f16f442

Please sign in to comment.