shim.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Based on:
  2. // http://norbertlindenberg.com/2012/05/ecmascript-supplementary-characters/
  3. // and:
  4. // https://github.com/mathiasbynens/String.fromCodePoint/blob/master
  5. // /fromcodepoint.js
  6. "use strict";
  7. var floor = Math.floor, fromCharCode = String.fromCharCode;
  8. // eslint-disable-next-line no-unused-vars
  9. module.exports = function (codePoint1 /*, …codePoints*/) {
  10. var chars = [], length = arguments.length, i, codePoint, result = "";
  11. for (i = 0; i < length; ++i) {
  12. codePoint = Number(arguments[i]);
  13. if (
  14. !isFinite(codePoint) ||
  15. codePoint < 0 ||
  16. codePoint > 0x10ffff ||
  17. floor(codePoint) !== codePoint
  18. ) {
  19. throw new RangeError("Invalid code point " + codePoint);
  20. }
  21. if (codePoint < 0x10000) {
  22. chars.push(codePoint);
  23. } else {
  24. codePoint -= 0x10000;
  25. // eslint-disable-next-line no-bitwise
  26. chars.push((codePoint >> 10) + 0xd800, codePoint % 0x400 + 0xdc00);
  27. }
  28. if (i + 1 !== length && chars.length <= 0x4000) continue;
  29. result += fromCharCode.apply(null, chars);
  30. chars.length = 0;
  31. }
  32. return result;
  33. };