shim.js 818 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)), length = str.length, first, second;
  8. pos = toInteger(pos);
  9. // Account for out-of-bounds indices:
  10. if (pos < 0 || pos >= length) return undefined;
  11. // Get the first code unit
  12. first = str.charCodeAt(pos);
  13. if (first >= 0xd800 && first <= 0xdbff && length > 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. };