SplitMatch.js 808 B

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