index.js 610 B

1234567891011121314151617181920212223242526272829303132
  1. /* eslint-disable babel/new-cap, xo/throw-new-error */
  2. 'use strict';
  3. module.exports = function (str, pos) {
  4. if (str === null || str === undefined) {
  5. throw TypeError();
  6. }
  7. str = String(str);
  8. var size = str.length;
  9. var i = pos ? Number(pos) : 0;
  10. if (Number.isNaN(i)) {
  11. i = 0;
  12. }
  13. if (i < 0 || i >= size) {
  14. return undefined;
  15. }
  16. var first = str.charCodeAt(i);
  17. if (first >= 0xD800 && first <= 0xDBFF && size > i + 1) {
  18. var second = str.charCodeAt(i + 1);
  19. if (second >= 0xDC00 && second <= 0xDFFF) {
  20. return ((first - 0xD800) * 0x400) + second - 0xDC00 + 0x10000;
  21. }
  22. }
  23. return first;
  24. };