$.string-at.js 613 B

1234567891011121314151617
  1. var toInteger = require('./$.to-integer')
  2. , defined = require('./$.defined');
  3. // true -> String#at
  4. // false -> String#codePointAt
  5. module.exports = function(TO_STRING){
  6. return function(that, pos){
  7. var s = String(defined(that))
  8. , i = toInteger(pos)
  9. , l = s.length
  10. , a, b;
  11. if(i < 0 || i >= l)return TO_STRING ? '' : undefined;
  12. a = s.charCodeAt(i);
  13. return a < 0xd800 || a > 0xdbff || i + 1 === l || (b = s.charCodeAt(i + 1)) < 0xdc00 || b > 0xdfff
  14. ? TO_STRING ? s.charAt(i) : a
  15. : TO_STRING ? s.slice(i, i + 2) : (a - 0xd800 << 10) + (b - 0xdc00) + 0x10000;
  16. };
  17. };