es.string.at-alternative.js 919 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var requireObjectCoercible = require('../internals/require-object-coercible');
  5. var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
  6. var toString = require('../internals/to-string');
  7. var fails = require('../internals/fails');
  8. var charAt = uncurryThis(''.charAt);
  9. var FORCED = fails(function () {
  10. return '𠮷'.at(-2) !== '\uD842';
  11. });
  12. // `String.prototype.at` method
  13. // https://github.com/tc39/proposal-relative-indexing-method
  14. $({ target: 'String', proto: true, forced: FORCED }, {
  15. at: function at(index) {
  16. var S = toString(requireObjectCoercible(this));
  17. var len = S.length;
  18. var relativeIndex = toIntegerOrInfinity(index);
  19. var k = relativeIndex >= 0 ? relativeIndex : len + relativeIndex;
  20. return (k < 0 || k >= len) ? undefined : charAt(S, k);
  21. }
  22. });