ucs2-length.js 448 B

1234567891011121314151617
  1. export function ucs2length(s) {
  2. let result = 0;
  3. let length = s.length;
  4. let index = 0;
  5. let charCode;
  6. while (index < length) {
  7. result++;
  8. charCode = s.charCodeAt(index++);
  9. if (charCode >= 0xd800 && charCode <= 0xdbff && index < length) {
  10. charCode = s.charCodeAt(index);
  11. if ((charCode & 0xfc00) == 0xdc00) {
  12. index++;
  13. }
  14. }
  15. }
  16. return result;
  17. }