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
| 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'); const lineStr = Object.keys(res.lines) .reduce((accu, line) => { let lstr = res.lines[line]; if (!(/;base64,/g.test(lstr) && lstr.length > 256)) { return accu.concat(lstr); } return accu; }, []) .join('') .replace(/\r\n/g, ' '); getKeyWroddRegs(keywords).forEach((reg) => { (lineStr.match(reg) || []).forEach((val) => { val = val.replace(/^(\-|\/)*/, '').replace(/(\-|\/)*$/, ''); updateVariableRepoMapping(val, res); if ( !/\//g.test(val) && vals.indexOf(val) === -1 && vals.indexOf(val.toLowerCase()) === -1 && vals.indexOf(val.toUpperCase()) === -1 && val.length < 64 ) { 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) && val.length < 64 ) { 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; }
|