GetMatchString.js 884 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var substring = require('./substring');
  4. var isMatchRecord = require('../helpers/records/match-record');
  5. // https://262.ecma-international.org/13.0/#sec-getmatchstring
  6. module.exports = function GetMatchString(S, match) {
  7. if (typeof S !== 'string') {
  8. throw new $TypeError('Assertion failed: `S` must be a String');
  9. }
  10. if (!isMatchRecord(match)) {
  11. throw new $TypeError('Assertion failed: `match` must be a Match Record');
  12. }
  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. };