SplitMatch.js 884 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var callBound = require('call-bind/callBound');
  4. var $TypeError = GetIntrinsic('%TypeError%');
  5. var Type = require('./Type');
  6. var isInteger = require('../helpers/isInteger');
  7. var $charAt = callBound('String.prototype.charAt');
  8. // https://262.ecma-international.org/6.0/#sec-splitmatch
  9. module.exports = function SplitMatch(S, q, R) {
  10. if (Type(S) !== 'String') {
  11. throw new $TypeError('Assertion failed: `S` must be a String');
  12. }
  13. if (!isInteger(q)) {
  14. throw new $TypeError('Assertion failed: `q` must be an integer');
  15. }
  16. if (Type(R) !== 'String') {
  17. throw new $TypeError('Assertion failed: `R` must be a String');
  18. }
  19. var r = R.length;
  20. var s = S.length;
  21. if (q + r > s) {
  22. return false;
  23. }
  24. for (var i = 0; i < r; i += 1) {
  25. if ($charAt(S, q + i) !== $charAt(R, i)) {
  26. return false;
  27. }
  28. }
  29. return q + r;
  30. };