!function(e, t) { "object" == typeof exports && "undefined" != typeof module ? t(exports) : "function" == typeof define && define.amd ? define([ "exports" ], t) : t((e = "undefined" != typeof globalThis ? globalThis : e || self).SplitSmartly2 = {}); }(this, (function(e) { "use strict"; var t, r; e.EnumIncludeSeparatorMode = void 0, (t = e.EnumIncludeSeparatorMode || (e.EnumIncludeSeparatorMode = {})).INCLUDE_SEPARATOR_NONE = "NONE", t.INCLUDE_SEPARATOR_SEPARATELY = "SEPARATELY", t.INCLUDE_SEPARATOR_LEFT = "LEFT", t.INCLUDE_SEPARATOR_RIGHT = "RIGHT", t.INCLUDE_SEPARATOR_ONLY = "ONLY", function(e) { e[e.ACTION_CLOSE = 1] = "ACTION_CLOSE", e[e.ACTION_OPEN = 2] = "ACTION_OPEN", e[e.ACTION_ADD_FRAGMENT = 3] = "ACTION_ADD_FRAGMENT", e[e.ACTION_NULL = 4] = "ACTION_NULL"; }(r || (r = {})); const once = e => { let t, r; return function(...s) { return r || (t = e(...s), r = !0), t; }; }, isEmpty = e => { if (!e) return !0; if (Array.isArray(e)) { if (0 === e.length) return !0; } else if ("object" == typeof e && 0 === Object.keys(e).length) return !0; return !1; }, first = e => e[0]; function buildIndexesObject(e) { const t = [ e ].flat().filter(Boolean); return !isEmpty(t) && { values: new Set(t), max: Math.max(...t), count: 0, hasIndex() { return -Infinity === this.max || this.values.has(this.count++); }, isOverMax() { return -Infinity !== this.max && this.count > this.max; } }; } class SearchResults { constructor(e, t) { this.string = e, this.searchSettings = t, this.prepareSearch(); } prepareSearch() { for (const e of [ this.searchSettings.separatorSearch, this.searchSettings.bracketsSearch ]) e.lastIndex = 0; Object.assign(this, { brackets: [], pipe: [], currentMentions: [], position: 0, isDone: !1, freeArea: { start: 0, end: void 0 }, lastSeparator: void 0, searchString: this.searchSettings.ignoreCase && !this.searchSettings.separatorSearch.ignoreCase ? this.string.toUpperCase() : this.string, indexes: buildIndexesObject(this.searchSettings.indexes) }); } get pipeIsEmpty() { return isEmpty(this.pipe); } getMentions(e, t) { const r = [], s = []; for (const n of this.currentMentions) n.index >= e && n.index < t ? r.push(n.mention) : s.push(n); return [ r.length && r, s ]; } trimResultText(e) { return this.searchSettings.trimResult ? e.trim() : e; } trimSeparatorText(e) { return this.searchSettings.trimSeparators ? e.trim() : e; } checkSeparator(e) { const {string: t} = this, {check: r, includePositions: s, mentions: n} = this.searchSettings; let {0: i = "", index: a = t.length, searchWithinData: o} = null != e ? e : {}; const c = i.length, h = o ? o.openPosition : this.position; let l = t.substring(h, a); i || (this.isDone = !0), l = this.trimResultText(l), i = this.trimSeparatorText(i); let p, u = o ? [ o.open, o.close ] : i; if (s && (l = { text: l, position: h }, u = { text: u, position: a, isSeparator: !0 }), n) { l = "string" == typeof l ? { text: l } : l; const [e, t] = this.getMentions(h, a); e && (l.mentions = e, p = t); } if (r && i) { const e = isNaN(this.tempPosition) ? h : this.tempPosition; this.tempPosition = a + i.length; const s = this; if (!r({ getString: once((() => s.trimResultText(t.substring(e, a)))), getTextAfter: once((() => t.substring(a + i.length))), getMentions: once((() => s.getMentions(e, a)[0])), getSeparator: once((() => i)), get string() { return this.getString(); }, get textAfter() { return this.getTextAfter(); }, get mentions() { return this.getMentions(); }, get separator() { return this.getSeparator(); } })) return []; delete this.tempPosition; } return p && (this.currentMentions = p), this.position = a + c, [ l, u, !0 ]; } pushToPipe(e) { if (this.indexes) { if (!this.indexes.hasIndex()) return; this.indexes.isOverMax() && (this.isDone = !0); } this.pipe.push(e); } addToPipe(e) { let [t, r, s] = this.checkSeparator(e); if (!s) return !1; switch (this.searchSettings.includeSeparatorMode) { case "SEPARATELY": this.pushToPipe(t), r && this.pushToPipe(r); break; case "LEFT": this.pushToPipe([ t, r ]); break; case "RIGHT": !("object" == typeof t ? t.text : t) && !this.lastSeparator || this.pushToPipe([ this.lastSeparator, t ]), this.lastSeparator = r; break; case "ONLY": r && this.pushToPipe(r); break; default: this.pushToPipe(t); } return !this.pipeIsEmpty; } findBrackets() { const {searchString: e, brackets: t, freeArea: r, searchSettings: s} = this, {bracketsSearch: n, separatorSearch: i, searchWithin: a} = s, o = a ? () => this.pipeIsEmpty : () => ("number" != typeof r.start || r.start !== r.end) && !r.end; for (;o(); ) { var c; const o = n.exec(e); if (!o) { if (a || isNaN(r.start)) return !1; r.end = e.length - 1; continue; } const l = o[0], {close: p, ignoreMode: u, searchLevels: f} = (h = t)[h.length - 1] || {}; let g; switch ((l === p ? 1 : u && 4) || (g = s.bracketsMap[l]) && 2 || (null === (c = s.mentions) || void 0 === c ? void 0 : c[l]) && 3) { case 1: const e = t.pop(); a ? (!0 === f || f.includes(t.length + 1)) && this.addToPipe(Object.assign(o, { searchWithinData: e })) : isEmpty(t) && (r.start = o.index, i && i.lastIndex < r.start && (i.lastIndex = r.start)); break; case 2: t.push({ ...g, openPosition: o.index + l.length }), 1 !== t.length || a || (r.end = o.index); break; case 3: this.currentMentions.push({ mention: s.mentions[l], index: o.index }); } } var h; return !0; } findSeparator(e) { const {searchString: t, freeArea: r} = this, {separatorSearch: s} = this.searchSettings; let n; for (;!n; ) { if (e = e || s.exec(t)) if (e.index <= r.end) { const t = e.index >= r.start && this.addToPipe(e); if (e = null, !t) continue; } else r.start = r.end = void 0; else this.addToPipe(); n = !0; } return e; } getNext() { let e; for (;this.pipeIsEmpty && !this.isDone; ) this.findBrackets() ? this.searchSettings.searchWithin || (e = this.findSeparator(e)) : this.isDone = !0; return this.pipeIsEmpty ? null : this.pipe.shift(); } getAll() { return [ ...this ]; } getRest() { const e = []; let t; for (;null !== (t = this.getNext()); ) e.push(t); return e; } * [Symbol.iterator]() { this.prepareSearch(); const e = this; let t; do { t = e.getNext(), null !== t && (yield t); } while (null !== t); } } function split(e, t) { const r = this.merge(t); let s = new SearchResults(e, r); return "number" == typeof r.indexes ? s.getNext() : r.returnIterator ? s : s.getAll(); } function createSplitFunction(e) { const t = split.bind(e); return Object.assign(t, { getOne(e, r, s = {}) { if (isNaN(r)) throw new TypeError("second parameter of `getOne` function should be index"); return t(e, { ...s, indexes: r }); }, getFirst: (e, r = {}) => t(e, { ...r, indexes: 0 }), getIndexes(e, r, s = {}) { if (!Array.isArray(r)) throw new TypeError("second parameter of `getOne` function should be array of indexes"); return t(e, { ...s, indexes: r }); }, getIterator: (e, r = {}) => t(e, { ...r, returnIterator: !0 }) }); } function getSplitSmartlyArgs(e, t) { if (null == e || !e.length) throw new RangeError("empty arguments"); if (3 === e.length) { if (!t) return e; } else if (1 === e.length) { const t = first(e); "string" == typeof t ? e.push(",", {}) : Array.isArray(t) ? (e.unshift(null), e.push({})) : "object" == typeof t && e.unshift(null, ","); } else if (2 === e.length) "string" == typeof e[0] && e[1] instanceof RegExp || "string" == typeof e[1] || Array.isArray(e[1]) ? e.push({}) : e.unshift(null); else if (e.length > 3) throw new RangeError("Too many arguments passed to splitSmartly function!!!"); return t && (e[2] = { ...e[2], ...t }), e; } let s; function prepareSearch(e, t) { const r = { brackets: [], mentions: [], ignoreInsideQuotes: !0, includeSeparatorMode: "NONE", ignoreCase: !0, trimResult: !0, trimSeparators: !1, defaultBrackets: [ [ "(", ")" ], [ "[", "]" ], [ "{", "}" ] ], ...t, separators: e, init() { return function initSettings(e) { if (Array.isArray(e.mentions) || "string" == typeof e.mentions) { const t = [ e.mentions ].flat().filter(Boolean).reduce(((t, r) => (t[e.ignoreCase ? r.toUpperCase() : r] = r, t)), {}); e.mentions = !isEmpty(t) && t; } return e.createBracketsMap().createBracketsSearch().createSeparatorsSearch(); }(this); }, merge(e) { return function mergeSettings(e, t) { return t ? (t = { ...e, ...t }, [ "brackets", "mentions" ].some((e => e in t)) && t.init(), t) : e; }(this, e); }, arrayToPattern: e => function arrayToPattern(e) { var t; return null !== (t = s) && void 0 !== t || (s = new Set(".{}[]^()+*?\\/$|".split(""))), e.map((e => e instanceof RegExp ? e.source : e.split("").map((e => s.has(e) ? "\\" + e : e)).join(""))).join("|"); }(e), createRegExp: e => RegExp(e, "g"), createBracketsMap() { return function createBracketsMap(e) { let t = e.brackets = function normalizeBrackets(e, t) { var r; return !0 === e ? e = t.slice() : "object" != typeof e || Array.isArray(e) ? "string" == typeof e && (e = e.split(",").map((e => { let t = e.trim().split(" "); if (2 !== t.length) { if (2 !== first(t).length) throw new TypeError("open and close parts of brackets should be separated by space symbol"); t = first(t).split(""); } return t; }))) : e = Object.entries(e), null !== (r = e) && void 0 !== r ? r : []; }(e.brackets, e.defaultBrackets); return t = function handleBracketsMapOptions(e, t) { return t.ignoreInsideQuotes && e.unshift([ "'", , , !0 ], [ '"', , , !0 ]), e; }(t, e), e.bracketsMap = function buildBracketsMap(e, t) { return e.reduce(((e, [r, s, ...n]) => { 1 !== n.length || t || n.unshift(void 0); let [i = t && 1, a] = n; return "number" == typeof i && (i = [ i ]), e[r] = { open: r, ignoreMode: a, searchLevels: i, close: s || r }, e; }), {}); }(t, e.searchWithin), e; }(this); }, createBracketsSearch() { return function createBracketsSearch(e) { const t = Object.entries(e.bracketsMap).flatMap((([, {close: e, open: t}]) => e !== t ? [ t, e ] : t)).concat(Object.keys(e.mentions || {})).filter(Boolean), r = e.arrayToPattern(t); return e.bracketsSearch = e.createRegExp(r), e; }(this); }, createSeparatorsSearch() { return function createSeparatorsSearch(e) { const {separators: t} = e; if ("string" == typeof t || Array.isArray(t)) { const r = e.arrayToPattern([ t ].flat().filter(Boolean)); e.separatorSearch = e.createRegExp(r); } else t ? (e.separatorSearch = t, e.ignoreCase = t.ignoreCase) : e.separatorSearch = /empty/; return e; }(this); } }; return r.init(); } function _splitSmartlyCore(e, t) { const r = prepareSearch(e, t); return { splitSettings: r, splitFn: createSplitFunction(r) }; } function splitSmartly(...e) { let [t, r, s] = getSplitSmartlyArgs(e); const {splitFn: n} = _splitSmartlyCore(r, s); return null !== t ? n(t) : n; } e.SearchResults = SearchResults, e._splitSmartlyCore = _splitSmartlyCore, e.createSplitFunction = createSplitFunction, e.default = splitSmartly, e.getSplitSmartlyArgs = getSplitSmartlyArgs, e.newDefaultBrackets = function newDefaultBrackets() { return [ [ "(", ")" ], [ "[", "]" ], [ "{", "}" ] ]; }, e.newDefaultSettings = function newDefaultSettings() { return { brackets: [], mentions: [], ignoreInsideQuotes: !0, includeSeparatorMode: "NONE", ignoreCase: !0, trimResult: !0, trimSeparators: !1, defaultBrackets: [ [ "(", ")" ], [ "[", "]" ], [ "{", "}" ] ] }; }, e.prepareSearch = prepareSearch, e.search = function search(...e) { return splitSmartly(...getSplitSmartlyArgs(e, { includeSeparatorMode: "ONLY" })); }, e.searchWithin = function searchWithin(...e) { return 1 === e.length && ("string" == typeof e[0] ? e.push(null, {}) : e.unshift(null)), "object" == typeof e[1] && e[1].brackets || (e[1] = { brackets: e[1] }), e.splice(1, 0, null), splitSmartly(...getSplitSmartlyArgs(e, { searchWithin: !0 })); }, e.splitSmartly = splitSmartly, Object.defineProperty(e, "__esModule", { value: !0 }); })); //# sourceMappingURL=index.umd.production.min.cjs.map