SplitMatch.js 895 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var callBound = require('call-bind/callBound');
  4. var $TypeError = GetIntrinsic('%TypeError%');
  5. var IsIntegralNumber = require('./IsIntegralNumber');
  6. var Type = require('./Type');
  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 (!IsIntegralNumber(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. };