UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 1.67 kB
export class FilePathScoreFunction{query;queryUpperCase;score;sequence;dataUpperCase;fileNameIndex;constructor(e){this.query=e,this.queryUpperCase=e.toUpperCase(),this.score=new Int32Array(2e3),this.sequence=new Int32Array(2e3),this.dataUpperCase="",this.fileNameIndex=0}calculateScore(e,t){if(!e||!this.query)return 0;const r=this.query.length,s=e.length;(!this.score||this.score.length<r*s)&&(this.score=new Int32Array(r*s*2),this.sequence=new Int32Array(r*s*2));const a=this.score,h=this.sequence;this.dataUpperCase=e.toUpperCase(),this.fileNameIndex=e.lastIndexOf("/");for(let t=0;t<r;++t)for(let r=0;r<s;++r){const n=0===r?0:a[t*s+r-1],i=0===t||0===r?0:a[(t-1)*s+r-1],c=0===t||0===r?0:h[(t-1)*s+r-1],o=this.match(this.query,e,t,r,c);o&&i+o>=n?(h[t*s+r]=c+1,a[t*s+r]=i+o):(h[t*s+r]=0,a[t*s+r]=n)}t&&this.restoreMatchIndexes(h,r,s,t);return 256*a[r*s-1]+(256-e.length)}testWordStart(e,t){if(0===t)return!0;const r=e.charAt(t-1);return"_"===r||"-"===r||"/"===r||"."===r||" "===r||e[t-1]!==this.dataUpperCase[t-1]&&e[t]===this.dataUpperCase[t]}restoreMatchIndexes(e,t,r,s){let a=t-1,h=r-1;for(;a>=0&&h>=0;)if(0===e[a*r+h])--h;else s.push(h),--a,--h;s.reverse()}singleCharScore(e,t,r,s){const a=this.testWordStart(t,s),h=s>this.fileNameIndex;let n=10;return(0===s||"/"===t[s-1])&&(n+=4),a&&(n+=2),e[r]===t[s]&&e[r]===this.queryUpperCase[r]&&(n+=6),h&&(n+=4),s===this.fileNameIndex+1&&0===r&&(n+=5),h&&a&&(n+=3),n}sequenceCharScore(e,t,r,s,a){let h=10;return s>this.fileNameIndex&&(h+=4),(0===s||"/"===t[s-1])&&(h+=5),h+=4*a,h}match(e,t,r,s,a){return this.queryUpperCase[r]!==this.dataUpperCase[s]?0:a?this.sequenceCharScore(e,t,r,s-a,a):this.singleCharScore(e,t,r,s)}}