es.string.from-code-point.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. var $ = require('../internals/export');
  2. var uncurryThis = require('../internals/function-uncurry-this');
  3. var toAbsoluteIndex = require('../internals/to-absolute-index');
  4. var $RangeError = RangeError;
  5. var fromCharCode = String.fromCharCode;
  6. // eslint-disable-next-line es/no-string-fromcodepoint -- required for testing
  7. var $fromCodePoint = String.fromCodePoint;
  8. var join = uncurryThis([].join);
  9. // length should be 1, old FF problem
  10. var INCORRECT_LENGTH = !!$fromCodePoint && $fromCodePoint.length != 1;
  11. // `String.fromCodePoint` method
  12. // https://tc39.es/ecma262/#sec-string.fromcodepoint
  13. $({ target: 'String', stat: true, arity: 1, forced: INCORRECT_LENGTH }, {
  14. // eslint-disable-next-line no-unused-vars -- required for `.length`
  15. fromCodePoint: function fromCodePoint(x) {
  16. var elements = [];
  17. var length = arguments.length;
  18. var i = 0;
  19. var code;
  20. while (length > i) {
  21. code = +arguments[i++];
  22. if (toAbsoluteIndex(code, 0x10FFFF) !== code) throw $RangeError(code + ' is not a valid code point');
  23. elements[i] = code < 0x10000
  24. ? fromCharCode(code)
  25. : fromCharCode(((code -= 0x10000) >> 10) + 0xD800, code % 0x400 + 0xDC00);
  26. } return join(elements, '');
  27. }
  28. });