GetMatchString.js 901 B

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