123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- (function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.RegExtras = {}));
- }(this, (function (exports) { 'use strict';
-
- function _classCallCheck(instance, Constructor) {
- if (!(instance instanceof Constructor)) {
- throw new TypeError("Cannot call a class as a function");
- }
- }
-
- function _defineProperties(target, props) {
- for (var i = 0; i < props.length; i++) {
- var descriptor = props[i];
- descriptor.enumerable = descriptor.enumerable || false;
- descriptor.configurable = true;
- if ("value" in descriptor) descriptor.writable = true;
- Object.defineProperty(target, descriptor.key, descriptor);
- }
- }
-
- function _createClass(Constructor, protoProps, staticProps) {
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
- if (staticProps) _defineProperties(Constructor, staticProps);
- return Constructor;
- }
-
- function _setPrototypeOf(o, p) {
- _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
- o.__proto__ = p;
- return o;
- };
-
- return _setPrototypeOf(o, p);
- }
-
- function _isNativeReflectConstruct() {
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
- if (Reflect.construct.sham) return false;
- if (typeof Proxy === "function") return true;
-
- try {
- Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
- return true;
- } catch (e) {
- return false;
- }
- }
-
- function _construct(Parent, args, Class) {
- if (_isNativeReflectConstruct()) {
- _construct = Reflect.construct;
- } else {
- _construct = function _construct(Parent, args, Class) {
- var a = [null];
- a.push.apply(a, args);
- var Constructor = Function.bind.apply(Parent, a);
- var instance = new Constructor();
- if (Class) _setPrototypeOf(instance, Class.prototype);
- return instance;
- };
- }
-
- return _construct.apply(null, arguments);
- }
-
- /* 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;
- }
-
- exports.RegExtras = /*#__PURE__*/function () {
- function RegExtras(regex, flags, newLastIndex) {
- _classCallCheck(this, RegExtras);
-
- this.regex = mixinRegex(typeof regex === 'string' ? new RegExp(regex) : mixinRegex(regex), flags || '', newLastIndex);
- }
-
- _createClass(RegExtras, [{
- key: "forEach",
- value: function forEach(str, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var regex = mixinRegex(this.regex, 'g');
- var matches,
- n0,
- i = 0;
-
- while ((matches = regex.exec(str)) !== null) {
- n0 = matches.splice(0, 1);
- cb.apply(thisObj, matches.concat(i++, n0));
- }
-
- return this;
- }
- }, {
- key: "some",
- value: function some(str, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var regex = mixinRegex(this.regex, 'g');
- var matches,
- ret,
- n0,
- i = 0;
-
- while ((matches = regex.exec(str)) !== null) {
- n0 = matches.splice(0, 1);
- ret = cb.apply(thisObj, matches.concat(i++, n0));
-
- if (ret) {
- return true;
- }
- }
-
- return false;
- }
- }, {
- key: "every",
- value: function every(str, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var regex = mixinRegex(this.regex, 'g');
- var matches,
- ret,
- n0,
- i = 0;
-
- while ((matches = regex.exec(str)) !== null) {
- n0 = matches.splice(0, 1);
- ret = cb.apply(thisObj, matches.concat(i++, n0));
-
- if (!ret) {
- return false;
- }
- }
-
- return true;
- }
- }, {
- key: "map",
- value: function map(str, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var ret = [];
- var regex = mixinRegex(this.regex, 'g');
- var matches,
- n0,
- i = 0;
-
- while ((matches = regex.exec(str)) !== null) {
- n0 = matches.splice(0, 1);
- ret.push(cb.apply(thisObj, matches.concat(i++, n0)));
- }
-
- return ret;
- }
- }, {
- key: "filter",
- value: function filter(str, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var matches,
- n0,
- i = 0;
- var ret = [],
- regex = mixinRegex(this.regex, 'g');
-
- while ((matches = regex.exec(str)) !== null) {
- n0 = matches.splice(0, 1);
- matches = matches.concat(i++, n0);
-
- if (cb.apply(thisObj, matches)) {
- ret.push(n0[0]);
- }
- }
-
- return ret;
- }
- }, {
- key: "reduce",
- value: function reduce(str, cb, prev) {
- var thisObj = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
- var matches,
- n0,
- i = 0;
- var regex = mixinRegex(this.regex, 'g');
-
- if (!prev) {
- if ((matches = regex.exec(str)) !== null) {
- n0 = matches.splice(0, 1);
- prev = cb.apply(thisObj, [''].concat(matches.concat(i++, n0)));
- }
- }
-
- while ((matches = regex.exec(str)) !== null) {
- n0 = matches.splice(0, 1);
- prev = cb.apply(thisObj, [prev].concat(matches.concat(i++, n0)));
- }
-
- return prev;
- }
- }, {
- key: "reduceRight",
- value: function reduceRight(str, cb, prevOrig, thisObjOrig) {
- var matches,
- n0,
- i,
- thisObj = thisObjOrig,
- prev = prevOrig;
- var matchesContainer = [],
- regex = mixinRegex(this.regex, 'g');
- thisObj = thisObj || null;
-
- while ((matches = regex.exec(str)) !== 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;
- }
- }, {
- key: "find",
- value: function find(str, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var matches,
- ret,
- n0,
- i = 0;
- var regex = mixinRegex(this.regex, 'g');
-
- while ((matches = regex.exec(str)) !== null) {
- n0 = matches.splice(0, 1);
- ret = cb.apply(thisObj, matches.concat(i++, n0));
-
- if (ret) {
- return n0[0];
- }
- }
-
- return false;
- }
- }, {
- key: "findIndex",
- value: function findIndex(str, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var regex = mixinRegex(this.regex, 'g');
- var matches,
- i = 0;
-
- while ((matches = regex.exec(str)) !== null) {
- var n0 = matches.splice(0, 1);
- var ret = cb.apply(thisObj, matches.concat(i++, n0));
-
- if (ret) {
- return i - 1;
- }
- }
-
- return -1;
- }
- }, {
- key: "findExec",
- value: function findExec(str, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var regex = mixinRegex(this.regex, 'g');
- var matches,
- i = 0;
-
- while ((matches = regex.exec(str)) !== null) {
- var n0 = matches.splice(0, 1);
- var ret = cb.apply(thisObj, matches.concat(i++, n0));
-
- if (ret) {
- return matches;
- }
- }
-
- return false;
- }
- }, {
- key: "filterExec",
- value: function filterExec(str, cb) {
- var thisObj = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
- var matches,
- n0,
- i = 0;
- var ret = [],
- regex = mixinRegex(this.regex, 'g');
-
- while ((matches = regex.exec(str)) !== null) {
- n0 = matches.splice(0, 1);
- matches.push(i++, n0[0]);
-
- if (cb.apply(thisObj, matches)) {
- ret.push(matches);
- }
- }
-
- return ret;
- }
- }]);
-
- return RegExtras;
- }();
-
- var _RegExtras = exports.RegExtras;
-
- exports.RegExtras = function RegExtras() {
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
-
- // eslint-disable-line no-class-assign
- return _construct(_RegExtras, args);
- };
-
- exports.RegExtras.prototype = _RegExtras.prototype;
- exports.RegExtras.mixinRegex = mixinRegex;
-
- exports.mixinRegex = mixinRegex;
-
- Object.defineProperty(exports, '__esModule', { value: true });
-
- })));
|