forked from unbug/codelf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchCodeModel.js
More file actions
225 lines (207 loc) · 6.46 KB
/
SearchCodeModel.js
File metadata and controls
225 lines (207 loc) · 6.46 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
216
217
218
219
220
221
222
223
224
225
import BaseModel from './BaseModel';
import * as Tools from '../utils/Tools';
import YoudaoTranslateData from './metadata/YoudaoTranslateData';
import JSONP from '../utils/JSONP';
import Store from './Store';
import AppModel from './AppModel';
import md5 from 'md5';
class SearchCodeModel extends BaseModel {
constructor() {
super();
this._data = {
isZH: false,
searchValue: null,
searchLang: [],
page: 0,
variableList: [],
suggestion: [],
sourceCode: null
};
this._variableRepoMapping = {};
this._sourceCodeStore = new Store(Infinity, {
persistence: 'session',
persistenceKey: AppModel.genPersistenceKey('source_code_key')
});
this._variableListStore = new Store(Infinity, {
persistence: 'session',
persistenceKey: AppModel.genPersistenceKey('variable_list_key')
});
}
//search code by query
async requestVariable(val, page, lang) {
if (val !== undefined && val !== null) {
val = val.trim().replace(/\s+/ig, ' '); // filter spaces
}
if (val.length < 1) {
return;
}
let q = val;
let suggestion = this._parseSuggestion(val.split(' '));
let isZH = this._isZH(val);
if (isZH) {
// translate by youdao
const translate = await YoudaoTranslateData.request(val);
if (translate) {
q = translate.translation;
suggestion = this._parseSuggestion(translate.suggestion, suggestion);
suggestion = this._parseSuggestion(q.split(' '), suggestion);
} else {
this.update({
searchValue: val,
page: page,
variableList: [...this.variableList, []],
searchLang: lang,
suggestion: suggestion,
isZH: isZH || this.isZH
});
}
}
const cacheId = md5(q + page + (lang ? lang.sort().join(',') : ''));
const cache = this._variableListStore.get(cacheId);
if (cache) {
this.update(cache);
return;
}
// multiple val separate with '+'
// const url = `//searchcode.com/api/codesearch_I/?q=${q.replace(' ', '+')}&p=${page}&per_page=42${lang.length ? ('&lan=' + lang.join(',')) : ''}`;
const url = `//searchcode.com/api/jsonp_codesearch_I/?callback=?&q=${q.replace(' ', '+')}&p=${page}&per_page=42${lang.length ? ('&lan=' + lang.join(',')) : ''}`;
val && JSONP(url)
.then(data => {
const cdata = {
searchValue: val,
page: page,
variableList: [...this._data.variableList, this._parseVariableList(data.results, q)],
searchLang: lang,
suggestion: suggestion,
isZH: isZH || this.isZH
};
this.update(cdata);
this._variableListStore.save(cacheId, cdata);
}).catch(err => {
this.update({
searchValue: val,
page: page,
variableList: [...this.variableList, []],
searchLang: lang,
suggestion: suggestion,
isZH: isZH || this.isZH
});
});
}
//get source code by id
requestSourceCode(id) {
const cache = this._sourceCodeStore.get(id);
if (cache) {
this.update({sourceCode: cache});
return;
}
id && fetch('https://searchcode.com/api/result/' + id + '/')
.then(res => res.json())
.then(data => {
this._sourceCodeStore.save(id, data.code);
this.update({sourceCode: data.code});
});
}
getKeyWordReg(keyword) {
return new RegExp('([\\-_\\w\\d\\/\\$]{0,}){0,1}' + keyword + '([\\-_\\w\\d\\$]{0,}){0,1}', 'gi');
}
getKeyWroddRegs(keywords) {
return keywords.split(' ').reduce((accumulator, curr) => {
if (curr.length && curr.length > 1) {
return accumulator.concat(this.getKeyWordReg(curr));
}
}, []);
}
_parseVariableList(results, keywords) {
let vals = [], variables = [];
results.forEach(res => {
res.repo = res.repo.replace('git://github.com', 'https://github.com');
//filter codes
const lineStr = Object.keys(res.lines).reduce((accu, line) => {
let lstr = res.lines[line];
//no base64
if (!(/;base64,/g.test(lstr) && lstr.length > 256)) {
return accu.concat(lstr);
}
}, []).join('').replace(/\r\n/g, ' '); // remove \r\n
//match variables
this.getKeyWroddRegs(keywords).forEach(reg => {
(lineStr.match(reg) || []).forEach(val => {
//remove "-" and "/" from the start and the end
val = val.replace(/^(\-|\/)*/, '').replace(/(\-|\/)*$/, '');
this._updateVariableRepoMapping(val, res);
if (
!/\//g.test(val) /*exclude links*/
&& vals.indexOf(val) === -1
&& vals.indexOf(val.toLowerCase()) === -1
&& vals.indexOf(val.toUpperCase()) === -1
&& val.length < 64 /*too long*/
) {
vals.push(val);
variables.push({
keyword: val,
repoLink: res.repo,
repoLang: res.language,
color: Tools.randomLabelColor()
});
}
});
});
});
return variables.map(val => {
val.repoList = this._getVariableRepoMapping(val.keyword);
return val;
});
}
_parseSuggestion(keywords, curr) {
let suggestion = curr || this.suggestion;
if (keywords) {
suggestion = keywords.concat(suggestion);
}
return [...new Set(suggestion)].filter((item, i) => !this._isZH(item));
}
_updateVariableRepoMapping(val, repo) {
if (!/\//g.test(val) /*exclude links*/ && val.length < 64 /*too long*/) {
val = `__${val.toLowerCase()}`;
this._variableRepoMapping[val] = this._variableRepoMapping[val] || [];
if (!this._variableRepoMapping[val].find(key => key.id == repo.id)) {
this._variableRepoMapping[val].push(repo);
}
}
}
_getVariableRepoMapping(val) {
val = `__${val.toLowerCase()}`;
return this._variableRepoMapping[val];
}
_isZH(val) {
let isZH = false;
val.replace(/\s+/ig, '+').split('+').forEach((key) => {
if (/[^\x00-\xff]/gi.test(key)) {
isZH = true;
}
});
return isZH;
}
get searchValue() {
return this._data.searchValue;
}
get searchLang() {
return this._data.searchLang;
}
get page() {
return this._data.page;
}
get variableList() {
return this._data.variableList;
}
get suggestion() {
return this._data.suggestion;
}
get isZH() {
return this._data.isZH;
}
get sourceCode() {
return this._data.sourceCode;
}
}
export default new SearchCodeModel();