Skip to content

Commit 716b67d

Browse files
committed
Filter out empty icons
1 parent 493badb commit 716b67d

2 files changed

Lines changed: 9 additions & 41 deletions

File tree

src/ast.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export type ParseIconsOptions = {
181181

182182
export function parseIcons(src: string, offset: number, opts?: ParseIconsOptions): (RDTIcon | RDTText)[] {
183183
return split(src, '!~')
184+
.filter(({ part }) => !!part)
184185
.map(({ part, offset: partOffset }) =>
185186
part.includes('*')
186187
? (opts?.parseText ?? parseText)(part, offset + partOffset)

test/ast.parseIcons.test.ts

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -43,80 +43,47 @@ test('mixed', () => {
4343
});
4444

4545
test('empty', () => {
46-
const parseIcon = vi.fn().mockReturnValue({ kind: 'icon', offset: 0, length: 111, name: 'foo' });
47-
48-
expect(parseIcons('', 0, { parseIcon })).toStrictEqual([
49-
{ kind: 'icon', offset: 0, length: 111, name: 'foo' },
50-
]);
51-
52-
expect(parseIcon).toHaveBeenCalledOnce();
53-
expect(parseIcon).toHaveBeenCalledWith('', 0);
46+
expect(parseIcons('', 0)).toStrictEqual([]);
5447
});
5548

5649
test('only splitter', () => {
57-
const parseIcon = vi.fn()
58-
.mockReturnValueOnce({ kind: 'icon', offset: 0, length: 111, name: 'aaa' })
59-
.mockReturnValueOnce({ kind: 'icon', offset: 1, length: 222, name: 'bbb' })
60-
.mockReturnValueOnce({ kind: 'icon', offset: 2, length: 333, name: 'ccc' });
61-
62-
expect(parseIcons('!~!~', 0, { parseIcon })).toStrictEqual([
63-
{ kind: 'icon', offset: 0, length: 111, name: 'aaa' },
64-
{ kind: 'icon', offset: 1, length: 222, name: 'bbb' },
65-
{ kind: 'icon', offset: 2, length: 333, name: 'ccc' },
66-
]);
67-
68-
expect(parseIcon).toHaveBeenCalledTimes(3);
69-
expect(parseIcon).toHaveBeenNthCalledWith(1, '', 0);
70-
expect(parseIcon).toHaveBeenNthCalledWith(2, '', 2);
71-
expect(parseIcon).toHaveBeenNthCalledWith(3, '', 4);
50+
expect(parseIcons('!~!~', 0)).toStrictEqual([]);
7251
});
7352

7453
test('leading and trailing splitter', () => {
7554
const parseIcon = vi.fn()
76-
.mockReturnValueOnce({ kind: 'icon', offset: 100, length: 111, name: 'empty1' })
7755
.mockReturnValueOnce({ kind: 'icon', offset: 200, length: 222, name: 'mock-str' })
78-
.mockReturnValueOnce({ kind: 'icon', offset: 300, length: 333, name: 'mock-dex' })
79-
.mockReturnValueOnce({ kind: 'icon', offset: 400, length: 444, name: 'empty2' });
56+
.mockReturnValueOnce({ kind: 'icon', offset: 300, length: 333, name: 'mock-dex' });
8057
const parseText = vi.fn().mockReturnValue({ kind: 'text', offset: 500, length: 555, text: 'mock-text', prefix: 'mock-prefix' });
8158

8259
expect(parseIcons('!~STR!~*Hello World!~DEX!~', 0, { parseIcon, parseText })).toStrictEqual([
83-
{ kind: 'icon', offset: 100, length: 111, name: 'empty1' },
8460
{ kind: 'icon', offset: 200, length: 222, name: 'mock-str' },
8561
{ kind: 'text', offset: 500, length: 555, text: 'mock-text', prefix: 'mock-prefix' },
8662
{ kind: 'icon', offset: 300, length: 333, name: 'mock-dex' },
87-
{ kind: 'icon', offset: 400, length: 444, name: 'empty2' },
8863
]);
8964

90-
expect(parseIcon).toHaveBeenCalledTimes(4);
91-
expect(parseIcon).toHaveBeenNthCalledWith(1, '', 0);
92-
expect(parseIcon).toHaveBeenNthCalledWith(2, 'STR', 2);
93-
expect(parseIcon).toHaveBeenNthCalledWith(3, 'DEX', 21);
94-
expect(parseIcon).toHaveBeenNthCalledWith(4, '', 26);
65+
expect(parseIcon).toHaveBeenCalledTimes(2);
66+
expect(parseIcon).toHaveBeenNthCalledWith(1, 'STR', 2);
67+
expect(parseIcon).toHaveBeenNthCalledWith(2, 'DEX', 21);
9568
expect(parseText).toHaveBeenCalledOnce();
9669
expect(parseText).toHaveBeenCalledWith('*Hello World', 7);
9770
});
9871

9972
test('consecutive splitters', () => {
10073
const parseIcon = vi.fn()
10174
.mockReturnValueOnce({ kind: 'icon', offset: 1010, length: 1111, name: 'first-icon' })
102-
.mockReturnValueOnce({ kind: 'icon', offset: 2020, length: 2222, name: 'empty-middle1' })
103-
.mockReturnValueOnce({ kind: 'icon', offset: 3030, length: 3333, name: 'empty-middle2' })
10475
.mockReturnValueOnce({ kind: 'icon', offset: 4040, length: 4444, name: 'last-icon' });
10576
const parseText = vi.fn().mockReturnValue({ kind: 'text', offset: 5050, length: 5555, text: 'fake-text', prefix: 'fake-prefix' });
10677

10778
expect(parseIcons('STR!~!~*Hello World!~!~DEX', 0, { parseIcon, parseText })).toStrictEqual([
10879
{ kind: 'icon', offset: 1010, length: 1111, name: 'first-icon' },
109-
{ kind: 'icon', offset: 2020, length: 2222, name: 'empty-middle1' },
11080
{ kind: 'text', offset: 5050, length: 5555, text: 'fake-text', prefix: 'fake-prefix' },
111-
{ kind: 'icon', offset: 3030, length: 3333, name: 'empty-middle2' },
11281
{ kind: 'icon', offset: 4040, length: 4444, name: 'last-icon' },
11382
]);
11483

115-
expect(parseIcon).toHaveBeenCalledTimes(4);
84+
expect(parseIcon).toHaveBeenCalledTimes(2);
11685
expect(parseIcon).toHaveBeenNthCalledWith(1, 'STR', 0);
117-
expect(parseIcon).toHaveBeenNthCalledWith(2, '', 5);
118-
expect(parseIcon).toHaveBeenNthCalledWith(3, '', 21);
119-
expect(parseIcon).toHaveBeenNthCalledWith(4, 'DEX', 23);
86+
expect(parseIcon).toHaveBeenNthCalledWith(2, 'DEX', 23);
12087
expect(parseText).toHaveBeenCalledOnce();
12188
expect(parseText).toHaveBeenCalledWith('*Hello World', 7);
12289
});

0 commit comments

Comments
 (0)