IsStringWellFormedUnicode.js 716 B

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