GetMatchIndexPair.js 855 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var Type = require('./Type');
  5. var assertRecord = require('../helpers/assertRecord');
  6. // https://ecma-international.org/ecma-262/13.0/#sec-getmatchindexpair
  7. module.exports = function GetMatchIndexPair(S, match) {
  8. if (Type(S) !== 'String') {
  9. throw new $TypeError('Assertion failed: `S` must be a String');
  10. }
  11. assertRecord(Type, 'Match Record', 'match', match);
  12. if (!(match['[[StartIndex]]'] <= S.length)) {
  13. throw new $TypeError('`match` [[StartIndex]] must be a non-negative integer <= the length of S');
  14. }
  15. if (!(match['[[EndIndex]]'] <= S.length)) {
  16. throw new $TypeError('`match` [[EndIndex]] must be an integer between [[StartIndex]] and the length of S, inclusive');
  17. }
  18. return [match['[[StartIndex]]'], match['[[EndIndex]]']];
  19. };