UNPKG

string-byte-slice

Version:
44 lines (25 loc) 712 B
import{LAST_ASCII_CODEPOINT,LAST_TWO_BYTES_CODEPOINT}from"./codepoints.js"; export const estimateCharWidth=(input)=>{ let asciiOnly=true; let longCharsCount=0; for(let index=0;index<SAMPLE_SIZE;index+=1){ const codepoint=getCodepoint(input,index); if(codepoint<=LAST_ASCII_CODEPOINT){ continue } if(asciiOnly){ asciiOnly=false } if(codepoint>LAST_TWO_BYTES_CODEPOINT){ longCharsCount+=1 } } return{asciiOnly,longCharsPercentage:longCharsCount/SAMPLE_SIZE} }; const getCodepoint=(input,index)=>{ const sampleSize=SAMPLE_SIZE-1; const percentage=1-(sampleSize-index)/sampleSize; const charIndex=Math.round(percentage*(input.length-1)); return input.charCodeAt(charIndex) }; const SAMPLE_SIZE=50;