esnext.string.at.js 786 B

12345678910111213141516171819
  1. 'use strict';
  2. // TODO: Remove from `core-js@4`
  3. var $ = require('../internals/export');
  4. var charAt = require('../internals/string-multibyte').charAt;
  5. var requireObjectCoercible = require('../internals/require-object-coercible');
  6. var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
  7. var toString = require('../internals/to-string');
  8. // `String.prototype.at` method
  9. // https://github.com/mathiasbynens/String.prototype.at
  10. $({ target: 'String', proto: true, forced: true }, {
  11. at: function at(index) {
  12. var S = toString(requireObjectCoercible(this));
  13. var len = S.length;
  14. var relativeIndex = toIntegerOrInfinity(index);
  15. var k = relativeIndex >= 0 ? relativeIndex : len + relativeIndex;
  16. return (k < 0 || k >= len) ? undefined : charAt(S, k);
  17. }
  18. });