UTF16DecodeString.js 753 B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var callBound = require('call-bind/callBound');
  5. var $push = callBound('Array.prototype.push');
  6. var CodePointAt = require('./CodePointAt');
  7. var Type = require('./Type');
  8. // https://262.ecma-international.org/11.0/#sec-utf16decodestring
  9. module.exports = function UTF16DecodeString(string) {
  10. if (Type(string) !== 'String') {
  11. throw new $TypeError('Assertion failed: `string` must be a String');
  12. }
  13. var codePoints = [];
  14. var size = string.length;
  15. var position = 0;
  16. while (position < size) {
  17. var cp = CodePointAt(string, position);
  18. $push(codePoints, cp['[[CodePoint]]']);
  19. position += cp['[[CodeUnitCount]]'];
  20. }
  21. return codePoints;
  22. };