-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdeduplicateText.js
More file actions
40 lines (36 loc) · 930 Bytes
/
deduplicateText.js
File metadata and controls
40 lines (36 loc) · 930 Bytes
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
/**
* 统计文本中每行内容的出现次数
* @author github.com/qyuja
* @param {string} input - 输入的字符串
* @returns {string} - 返回重复出现次数 eg: hello world (2) foo bar (3)
* @returns {object} - 错误信息
*/
if (item.ContentType !== "Text") {
return {
error: "只支持文本类型的剪贴板内容",
};
}
const text = item.Content || "";
if (!text) {
return {
error: "剪贴板内容为空",
};
}
try{
const counter = new Map();
// 过滤空行
const lines = text.split(/\r?\n/).map(i => i.trim()).filter(Boolean)
// 去重并计数
for (const line of lines) {
counter.set(line, (counter.get(line) ?? 0) + 1);
}
// 输出示例:xxx (8)
const result = [...counter.entries()]
.map(([value, count]) => `${value} (${count})`)
.join("\n");
return result;
}catch(error){
return {
error:"解析异常"
}
}