StringToCodePoints.js 760 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://ecma-international.org/ecma-262/12.0/#sec-stringtocodepoints
  9. module.exports = function StringToCodePoints(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. };