getStrBytesCount.ts 259 B

123456789101112
  1. export function getStrBytesCount(str) {
  2. let bytesCount = 0;
  3. for (let i = 0; i < str.length; i++) {
  4. const c = str.charAt(i);
  5. if (/^[\u00-\uff]$/.test(c)) {
  6. bytesCount += 1;
  7. } else {
  8. bytesCount += 2;
  9. }
  10. }
  11. return bytesCount;
  12. }