IsStringWellFormedUnicode.js 631 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. var CodePointAt = require('./CodePointAt');
  3. var $TypeError = require('es-errors/type');
  4. // https://262.ecma-international.org/14.0/#sec-isstringwellformedunicode
  5. module.exports = function IsStringWellFormedUnicode(string) {
  6. if (typeof string !== 'string') {
  7. throw new $TypeError('Assertion failed: `string` must be a String');
  8. }
  9. var len = string.length; // step 1
  10. var k = 0; // step 2
  11. while (k < len) { // step 3
  12. var cp = CodePointAt(string, k); // step 3.a
  13. if (cp['[[IsUnpairedSurrogate]]']) {
  14. return false; // step 3.b
  15. }
  16. k += cp['[[CodeUnitCount]]']; // step 3.c
  17. }
  18. return true; // step 4
  19. };