123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- /* eslint-disable node/no-unsupported-features/es-syntax */
-
- /**
- * @param {RegExp} regex
- * @param {string} newFlags
- * @param {Integer} [newLastIndex=regex.lastIndex]
- * @returns {RegExp}
- */
- function mixinRegex(regex, newFlags) {
- var newLastIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : regex.lastIndex;
- newFlags = newFlags || '';
- regex = new RegExp(regex.source, (newFlags.includes('g') ? 'g' : regex.global ? 'g' : '') + (newFlags.includes('i') ? 'i' : regex.ignoreCase ? 'i' : '') + (newFlags.includes('m') ? 'm' : regex.multiline ? 'm' : '') + (newFlags.includes('u') ? 'u' : regex.unicode ? 'u' : '') + (newFlags.includes('y') ? 'y' : regex.sticky ? 'y' : '') + (newFlags.includes('s') ? 's' : regex.dotAll ? 's' : ''));
- regex.lastIndex = newLastIndex;
- return regex;
- }
-
- // We copy the regular expression so as to be able to always ensure the exec
-
- String.prototype.forEach = function (regex, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var matches,
- n0,
- i = 0;
- regex = mixinRegex(regex, 'g');
-
- while ((matches = regex.exec(this)) !== null) {
- n0 = matches.splice(0, 1);
- cb.apply(thisObj, matches.concat(i++, n0));
- }
-
- return this;
- };
-
- String.prototype.some = function (regex, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var matches,
- ret,
- n0,
- i = 0;
- regex = mixinRegex(regex, 'g');
-
- while ((matches = regex.exec(this)) !== null) {
- n0 = matches.splice(0, 1);
- ret = cb.apply(thisObj, matches.concat(i++, n0));
-
- if (ret) {
- return true;
- }
- }
-
- return false;
- };
-
- String.prototype.every = function (regex, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var matches,
- ret,
- n0,
- i = 0;
- regex = mixinRegex(regex, 'g');
-
- while ((matches = regex.exec(this)) !== null) {
- n0 = matches.splice(0, 1);
- ret = cb.apply(thisObj, matches.concat(i++, n0));
-
- if (!ret) {
- return false;
- }
- }
-
- return true;
- };
-
- String.prototype.map = function (regex, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var matches,
- n0,
- i = 0;
- var ret = [];
- regex = mixinRegex(regex, 'g');
-
- while ((matches = regex.exec(this)) !== null) {
- n0 = matches.splice(0, 1);
- ret.push(cb.apply(thisObj, matches.concat(i++, n0)));
- }
-
- return ret;
- };
-
- String.prototype.filter = function (regex, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var matches,
- n0,
- i = 0;
- var ret = [];
- regex = mixinRegex(regex, 'g');
-
- while ((matches = regex.exec(this)) !== null) {
- n0 = matches.splice(0, 1);
- matches = matches.concat(i++, n0);
-
- if (cb.apply(thisObj, matches)) {
- ret.push(n0[0]);
- }
- }
-
- return ret;
- };
-
- String.prototype.reduce = function (regex, cb, prev) {
- var thisObj = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
- var matches,
- n0,
- i = 0;
- regex = mixinRegex(regex, 'g');
-
- if (!prev) {
- if ((matches = regex.exec(this)) !== null) {
- n0 = matches.splice(0, 1);
- prev = cb.apply(thisObj, [''].concat(matches.concat(i++, n0)));
- }
- }
-
- while ((matches = regex.exec(this)) !== null) {
- n0 = matches.splice(0, 1);
- prev = cb.apply(thisObj, [prev].concat(matches.concat(i++, n0)));
- }
-
- return prev;
- };
-
- String.prototype.reduceRight = function (regex, cb, prevOrig, thisObjOrig) {
- var matches,
- n0,
- i,
- prev = prevOrig,
- thisObj = thisObjOrig;
- var matchesContainer = [];
- regex = mixinRegex(regex, 'g');
- thisObj = thisObj || null;
-
- while ((matches = regex.exec(this)) !== null) {
- matchesContainer.push(matches);
- }
-
- i = matchesContainer.length;
-
- if (!i) {
- if (arguments.length < 3) {
- throw new TypeError('reduce of empty matches array with no initial value');
- }
-
- return prev;
- }
-
- if (!prev) {
- matches = matchesContainer.splice(-1)[0];
- n0 = matches.splice(0, 1);
- prev = cb.apply(thisObj, [''].concat(matches.concat(i--, n0)));
- }
-
- matchesContainer.reduceRight(function (container, mtches) {
- n0 = mtches.splice(0, 1);
- prev = cb.apply(thisObj, [prev].concat(mtches.concat(i--, n0)));
- return container;
- }, matchesContainer);
- return prev;
- };
-
- String.prototype.find = function (regex, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var matches,
- ret,
- n0,
- i = 0;
- regex = mixinRegex(regex, 'g');
-
- while ((matches = regex.exec(this)) !== null) {
- n0 = matches.splice(0, 1);
- ret = cb.apply(thisObj, matches.concat(i++, n0));
-
- if (ret) {
- return n0[0];
- }
- }
-
- return false;
- };
-
- String.prototype.findIndex = function (regex, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var matches,
- ret,
- n0,
- i = 0;
- regex = mixinRegex(regex, 'g');
-
- while ((matches = regex.exec(this)) !== null) {
- n0 = matches.splice(0, 1);
- ret = cb.apply(thisObj, matches.concat(i++, n0));
-
- if (ret) {
- return i - 1;
- }
- }
-
- return -1;
- };
-
- String.prototype.findExec = function (regex, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var matches,
- ret,
- n0,
- i = 0;
- regex = mixinRegex(regex, 'g');
-
- while ((matches = regex.exec(this)) !== null) {
- n0 = matches.splice(0, 1);
- ret = cb.apply(thisObj, matches.concat(i++, n0));
-
- if (ret) {
- return matches;
- }
- }
-
- return false;
- };
-
- String.prototype.filterExec = function (regex, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var matches,
- n0,
- i = 0;
- var ret = [];
- regex = mixinRegex(regex, 'g');
-
- while ((matches = regex.exec(this)) !== null) {
- n0 = matches.splice(0, 1);
- matches.push(i++, n0[0]);
-
- if (cb.apply(thisObj, matches)) {
- ret.push(matches);
- }
- }
-
- return ret;
- };
|