-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
215 lines (194 loc) · 3.98 KB
/
index.js
File metadata and controls
215 lines (194 loc) · 3.98 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
'use strict';
const pinyin = require('pinyin');
const _ = require('lodash');
class Retrieve {
constructor(option) {
const tmpOption = {
fields: {},
};
option = _.merge({}, tmpOption, option);
this.fields = {
id: option.fields.id || 'id',
name: option.fields.name || 'name',
};
}
convertArray(data, result = []) {
const that = this;
let tmpArr = [];
data.forEach(v1 => {
if (_.isString(v1)) {
if (result.length === 0) {
tmpArr.push([v1]);
} else {
result.forEach(v2 => {
tmpArr.push(v2.concat([v1]));
});
}
} else {
tmpArr = that.convertArray(v1, tmpArr);
}
});
return tmpArr;
}
convertPinyin(data) {
const cns = data.split('');
const out = pinyin(data, {
heteronym: true, // 启用多音字模式
// segment: true, // 启用分词,以解决多音字问题。
style: pinyin.STYLE_NORMAL, // 设置拼音风格
heteronym: true,
});
out.forEach((value, key) => {
out[key].unshift(cns[key]);
});
return out;
}
convertDictionary(array) {
const that = this;
const data = array.map(tmpObj =>
_.mapKeys(tmpObj, (item, key) => {
const fields = that.fields;
if (key === fields.id) {
return 'id';
}
if (key === fields.name) {
return 'name';
}
})
);
const result = [];
data.forEach(item => {
const pys = that.convertPinyin(item.name);
const tmpArr = that.convertArray(pys);
tmpArr.forEach(v => {
const reg = new RegExp('[\\u4E00-\\u9FFF]+', 'g');
// if()
let v3 = '';
v.forEach(str => {
if (reg.test(str)) {
v3 += str;
} else {
v3 += str.slice(0, 1);
}
});
const v2 = v.join('');
result.push({
id: item.id,
name: item.name,
v1: v,
v2,
v3,
index: 0,
});
});
});
return result;
}
getResult(source, str) {
str = this.convertStr(str);
const result = [];
let idFlag;
let tmp = {};
source.forEach((item, k) => {
if (item.id !== idFlag) {
if (idFlag) {
result.push(tmp);
}
idFlag = item.id;
tmp = {
id: item.id,
name: item.name,
index: 0,
};
}
const v2Re = item.v2.indexOf(str);
if (v2Re === 0) {
tmp.index += 1000;
}
if (v2Re > 0) {
tmp.index += 10;
}
const v3Re = item.v3.indexOf(str);
if (v3Re === 0) {
tmp.index += 500;
}
if (v3Re > 0) {
tmp.index += 10;
}
if (k === source.length - 1) {
result.push(tmp);
}
});
return _.sortBy(result, v => -v.index);
}
convertStr(str) {
return str.toLowerCase();
}
/**
*
* @param {*} source
*[
{
name: '水电费',
id: 1,
},
{
name: '发过火',
id: 2,
},
{
name: '体温表',
id: 3,
},
{
name: '鲫鱼汤',
id: 4,
},
* @param {*} str
* shui
*/
searchSource(source, str) {
return this.getResult(this.convertDictionary(source), str);
}
search(source, str) {
const that = this;
const array = that.getResult(source, str);
return array.map(tmpObj =>
_.mapKeys(tmpObj, (item, key) => {
const fields = that.fields;
if (key === 'id') {
return that.fields.id;
}
if (key === 'name') {
return that.fields.name;
}
return key;
})
);
}
}
const retrieve = new Retrieve({
fields: {
id: 'token',
name: 'userName',
},
});
const test2 = [
{
userName: '水电费',
token: 1,
},
{
userName: '发过火',
token: 2,
},
{
userName: '体温表',
token: 3,
},
{
userName: '大洪水',
token: 4,
},
];
module.exports = Retrieve;