最近在写一个 vscode 插件,需要参考一下 CODELF 的功能,看了源码之后提取出其中核心部分代码,nodejs 下,网页端都可以跑
代码
const axios = require('axios');
let variableRepoMapping = {};
axios
.get('https://searchcode.com/api/codesearch_I/?q=soup&p=1&per_page=100')
.then((res) => {
console.log(`res`, res);
let { data } = res;
let { results } = data;
console.log(`parseVariableList`, parseVariableList(results, 'soup'));
});
function getKeyWordReg(keyword) {
return new RegExp(
'([\\-_\\w\\d\\/\\$]{0,}){0,1}' + keyword + '([\\-_\\w\\d\\$]{0,}){0,1}',
'gi'
);
}
function getKeyWroddRegs(keywords) {
return keywords.split(' ').reduce((accumulator, curr) => {
if (curr.length && curr.length > 1) {
return accumulator.concat(getKeyWordReg(curr));
}
return accumulator;
}, []);
}
function 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);
}
return accu;
}, [])
.join('')
.replace(/\r\n/g, ' '); // remove \r\n
//match variables
getKeyWroddRegs(keywords).forEach((reg) => {
(lineStr.match(reg) || []).forEach((val) => {
//remove "-" and "/" from the start and the end
val = val.replace(/^(\-|\/)*/, '').replace(/(\-|\/)*$/, '');
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,
});
}
});
});
});
return variables.map((val) => {
val.repoList = getVariableRepoMapping(val.keyword);
return val;
});
}
function updateVariableRepoMapping(val, repo) {
if (!/\//g.test(val) /*exclude links*/ && val.length < 64 /*too long*/) {
val = `__${val.toLowerCase()}`;
variableRepoMapping[val] = variableRepoMapping[val] || [];
if (!variableRepoMapping[val].find((key) => key.id == repo.id)) {
repo.lines = null;
delete repo.lines;
variableRepoMapping[val].push(repo);
}
}
}
function getVariableRepoMapping(val) {
val = `__${val.toLowerCase()}`;
return variableRepoMapping[val];
}
function isZH(val) {
let isZH = false;
val
.replace(/\s+/gi, '+')
.split('+')
.forEach((key) => {
if (/[^\x00-\xff]/gi.test(key)) {
isZH = true;
}
});
return isZH;
}