UTF16EncodeCodePoint.js 713 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var $fromCharCode = GetIntrinsic('%String.fromCharCode%');
  5. var floor = require('./floor');
  6. var modulo = require('./modulo');
  7. var isCodePoint = require('../helpers/isCodePoint');
  8. // https://ecma-international.org/ecma-262/12.0/#sec-utf16encoding
  9. module.exports = function UTF16EncodeCodePoint(cp) {
  10. if (!isCodePoint(cp)) {
  11. throw new $TypeError('Assertion failed: `cp` must be >= 0 and <= 0x10FFFF');
  12. }
  13. if (cp <= 65535) {
  14. return $fromCharCode(cp);
  15. }
  16. var cu1 = $fromCharCode(floor((cp - 65536) / 1024) + 0xD800);
  17. var cu2 = $fromCharCode(modulo(cp - 65536, 1024) + 0xDC00);
  18. return cu1 + cu2;
  19. };