Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (143 loc) · 3.5 KB
/
Copy pathindex.js
File metadata and controls
154 lines (143 loc) · 3.5 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import pinyin from 'pinyin';
import inquirer from 'inquirer';
import { readFileSync, writeFileSync } from 'fs';
import { exec } from 'child_process';
import iconv from 'iconv-lite';
import low from 'lowdb';
import FileSync from 'lowdb/adapters/FileSync.js';
import msg from './message.js'
const adapter = new FileSync('db.json');
const db = low(adapter);
const print = console.log;
// 程序主入口
(async function () {
// 欢迎和初始化数据库
print(msg.title);
print(msg.greet);
generateDb();
// 循环体
while (true) {
let input = await inquirer.prompt([
{
type: 'input',
message: 'correct-name',
name: 'name',
prefix: '',
suffix: ' >',
filter: a => a.toLocaleLowerCase(),
},
]);
if (input.name == ':e') {
db.set('names', []).write();
print(msg.bye);
break;
}
if (input.name == ':g') {
generateDb();
continue;
}
if (input.name == ':h') {
print(msg.help)
continue;
}
let resultList = queryName(input.name);
switch (resultList.length) {
case 0:
print(msg.noNameMatched);
break;
case 1:
copy(resultList[0]);
print(resultList[0]);
print('Copied!'.green);
break;
default:
let selected = await inquirer.prompt([
{
type: 'list',
message: msg.chooseName,
name: 'name',
choices: resultList,
},
]);
copy(selected.name);
print(msg.copied);
}
}
})();
// 逐行读取文件到数组
function byLine(string) {
let i = 0;
let arr = [];
while (i > -1) {
var j = i;
i = string.indexOf('\n', j + 1);
var raw = string.substring(j, i == -1 ? string.length : i);
var noReturn = raw.substring(
raw.indexOf('\n') == -1 ? 0 : raw.indexOf('\n') + 1,
raw.indexOf('\r') == -1 ? raw.length : raw.length - 1
);
if (noReturn) arr.push(noReturn);
}
return arr;
}
// 生成首字母数据库
function generateDb() {
let namelist = [];
try {
namelist = byLine(readFileSync('namelist.txt', { encoding: 'utf8' }));
} catch (error) {
writeFileSync('namelist.txt', '\n');
}
if (namelist.length != 0) {
var namepy = [];
for (let i of namelist) namepy.push({ Name: i, Pinyin: generatePy(i) });
db.set('names', namepy).write();
print(msg.dbSuccess);
} else
print(msg.dbFaild);
}
// 针对不同平台的复制函数
function copy(text) {
exec('clip').stdin.end(iconv.encode(text, 'gbk')); // Windows 复制
}
// 生成一个拼音首字母
function generatePy(i) {
let py = pinyin(i, {
heteronym: true,
style: pinyin.STYLE_FIRST_LETTER,
});
return py;
}
// 查询一个拼音,返回匹配的列表
function queryName(n) {
let a = n.split('');
let query = db
.read()
.get('names')
.filter(item => {
if (item.Pinyin[0].indexOf(a[0]) >= 0) return true;
else return false;
})
.value();
if (a.length >= 2) {
for (const i in a) {
let tempArry = [];
query.forEach(element => {
if (element.Pinyin.length < a.length) return false;
if (element.Pinyin[i].indexOf(a[i]) >= 0) tempArry.push(element);
});
query = tempArry;
}
}
let names = [];
query.forEach(element => {
names.push(element.Name);
});
let rtn = names.sort((a, b) => {
if (a.length < b.length) return -1;
if (a.length > b.length) return 1;
else return pinyin.compare(a, b);
});
// print(rtn)
return rtn;
}