esnext.string.is-well-formed.js 880 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var requireObjectCoercible = require('../internals/require-object-coercible');
  5. var toString = require('../internals/to-string');
  6. var charCodeAt = uncurryThis(''.charCodeAt);
  7. // `String.prototype.isWellFormed` method
  8. // https://github.com/tc39/proposal-is-usv-string
  9. $({ target: 'String', proto: true, forced: true }, {
  10. isWellFormed: function isWellFormed() {
  11. var S = toString(requireObjectCoercible(this));
  12. var length = S.length;
  13. for (var i = 0; i < length; i++) {
  14. var charCode = charCodeAt(S, i);
  15. // single UTF-16 code unit
  16. if ((charCode & 0xF800) != 0xD800) continue;
  17. // unpaired surrogate
  18. if (charCode >= 0xDC00 || ++i >= length || (charCodeAt(S, i) & 0xFC00) != 0xDC00) return false;
  19. } return true;
  20. }
  21. });