Skip to content

Commit 59ac1d8

Browse files
committed
feat(utils): 新增 joinSlices
1 parent 16b997d commit 59ac1d8

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/utils/string.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ export function toTypePath(props: string[]): string {
4545
const path = props.map((p) => JSON.stringify(p)).join('][');
4646
return path ? '[' + path + ']' : '';
4747
}
48+
49+
export function joinSlices(slices: Array<string | undefined>, separator = '\n') {
50+
return slices.filter(Boolean).join(separator);
51+
}

test/utils/string.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { buildName, findOrigin, RefInfo, refToType, toTypePath, varString } from '../../src/utils/string';
1+
import { buildName, findOrigin, joinSlices, RefInfo, refToType, toTypePath, varString } from '../../src/utils/string';
22

33
test('buildName', () => {
44
expect(buildName('!')).toEqual('unnamed');
@@ -55,3 +55,8 @@ test('toTypePath', () => {
5555
expect(toTypePath(['a'])).toEqual('["a"]');
5656
expect(toTypePath(['a', 'b'])).toEqual('["a"]["b"]');
5757
});
58+
59+
test('joinSlices', () => {
60+
expect(joinSlices(['', undefined, ''])).toEqual('');
61+
expect(joinSlices(['1', undefined, '2', ''])).toEqual('1\n2');
62+
});

0 commit comments

Comments
 (0)