shim.js 809 B

1234567891011121314151617181920212223242526
  1. // Based on: https://github.com/mathiasbynens/String.prototype.codePointAt
  2. // Thanks @mathiasbynens !
  3. 'use strict';
  4. var toInteger = require('../../../number/to-integer')
  5. , validValue = require('../../../object/valid-value');
  6. module.exports = function (pos) {
  7. var str = String(validValue(this)), l = str.length, first, second;
  8. pos = toInteger(pos);
  9. // Account for out-of-bounds indices:
  10. if (pos < 0 || pos >= l) return undefined;
  11. // Get the first code unit
  12. first = str.charCodeAt(pos);
  13. if ((first >= 0xD800) && (first <= 0xDBFF) && (l > pos + 1)) {
  14. second = str.charCodeAt(pos + 1);
  15. if (second >= 0xDC00 && second <= 0xDFFF) {
  16. // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  17. return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  18. }
  19. }
  20. return first;
  21. };