1
0

shim.js 829 B

123456789101112131415161718192021222324252627282930
  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. module.exports = function (codePoint/*, …codePoints*/) {
  9. var chars = [], l = arguments.length, i, c, result = '';
  10. for (i = 0; i < l; ++i) {
  11. c = Number(arguments[i]);
  12. if (!isFinite(c) || c < 0 || c > 0x10FFFF || floor(c) !== c) {
  13. throw new RangeError("Invalid code point " + c);
  14. }
  15. if (c < 0x10000) {
  16. chars.push(c);
  17. } else {
  18. c -= 0x10000;
  19. chars.push((c >> 10) + 0xD800, (c % 0x400) + 0xDC00);
  20. }
  21. if (i + 1 !== l && chars.length <= 0x4000) continue;
  22. result += fromCharCode.apply(null, chars);
  23. chars.length = 0;
  24. }
  25. return result;
  26. };