-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.ts
73 lines (65 loc) · 2.16 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { accumulate } from '$/utils';
import type { ExportingMenuObject } from 'highcharts';
import { DialogPlugin } from 'tdesign-vue-next';
export function viewItemsInLib(itemIDs: number[]) {
addon.getGlobal('ZoteroPane').selectItems(itemIDs);
}
export const buttons = {
contextButton: {
menuItems: [
'viewFullscreen',
'downloadSVG',
'downloadJPEG',
'viewData',
'help',
],
},
};
export function helpMessageOption(msg: string) {
return {
help: {
onclick () {
DialogPlugin.alert({
header: addon.locale.help,
body: h =>
h(
'div',
{ style: { cursor: 'auto' } },
msg.split('\n').map(s => h('p', s))
),
footer: false,
draggable: true,
width: '80%',
mode: 'modeless',
theme: 'info',
});
},
} as ExportingMenuObject,
};
}
export function splitOtherData(data: Array<Highcharts.PointOptionsObject>) {
const sum = accumulate(data, 'y'),
isOther = (y: number) => y / Math.max(1, sum) < 0.02;
return [
data.filter(d => !isOther(d.y ?? 0)),
data.filter(d => isOther(d.y ?? 0))
]
}
function splitText(text: string): string[] {
// 假名、汉字、韩文
const characters = /[\u3040-\u30FF\u4E00-\u9FAF\uAC00-\uD7AF]/;
return text.split(/[\s,]+/).flatMap(word => {
if (characters.test(word)) return [...word];
return word;
}).filter(Boolean);
}
export function jaccardSimilarity(text1: string, text2: string) {
const set1 = new Set(splitText(text1)),
set2 = new Set(splitText(text2)),
intersection = new Set([...set1].filter(x => set2.has(x))),
union = new Set([...set1, ...set2]);
return intersection.size / union.size;
}
export function creator2str(creator: Zotero.Item.CreatorJSON): string {
return creator.name ?? `${creator.firstName} ${creator.lastName}`.trim();
}