GetStringIndex.js 766 B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. var callBound = require('call-bind/callBound');
  3. var $TypeError = require('es-errors/type');
  4. var StringToCodePoints = require('./StringToCodePoints');
  5. var isInteger = require('../helpers/isInteger');
  6. var $indexOf = callBound('String.prototype.indexOf');
  7. // https://262.ecma-international.org/13.0/#sec-getstringindex
  8. module.exports = function GetStringIndex(S, e) {
  9. if (typeof S !== 'string') {
  10. throw new $TypeError('Assertion failed: `S` must be a String');
  11. }
  12. if (!isInteger(e) || e < 0) {
  13. throw new $TypeError('Assertion failed: `e` must be a non-negative integer');
  14. }
  15. if (S === '') {
  16. return 0;
  17. }
  18. var codepoints = StringToCodePoints(S);
  19. var eUTF = e >= codepoints.length ? S.length : $indexOf(S, codepoints[e]);
  20. return eUTF;
  21. };