!function(e, t) { "object" == typeof exports && "undefined" != typeof module ? t(exports, require("yaml"), require("array-hyper-unique"), require("lazy-aggregate-error"), require("picomatch")) : "function" == typeof define && define.amd ? define([ "exports", "yaml", "array-hyper-unique", "lazy-aggregate-error", "picomatch" ], t) : t((e = "undefined" != typeof globalThis ? globalThis : e || self).SdWildcardsUtils = {}, e.yaml, e.arrayHyperUnique, e.lazyAggregateError, e.picomatch); }(this, (function(e, t, r, n, i) { "use strict"; function getOptionsShared(e) { var t; return null !== (t = e) && void 0 !== t || (e = {}), { allowMultiRoot: e.allowMultiRoot, disableUniqueItemValues: e.disableUniqueItemValues, minifyPrompts: e.minifyPrompts, disableUnsafeQuote: e.disableUnsafeQuote }; } function defaultOptionsStringify(e) { return { blockQuote: !0, defaultKeyType: "PLAIN", defaultStringType: "PLAIN", collectionStyle: "block", uniqueKeys: !0, ...e }; } function defaultOptionsParseDocument(e) { var t; return null !== (t = e) && void 0 !== t || (e = {}), { prettyErrors: !0, ...e, toStringDefaults: defaultOptionsStringify({ ...getOptionsShared(e), ...e.toStringDefaults }) }; } function getOptionsFromDocument(e, t) { return { ...e.options, ...t }; } function stripZeroStr(e) { return e.replace(/[\x00\u200b]+/g, ""); } function trimPrompts(e) { return e.replace(/\xa0/g, " ").replace(/^\s+|\s+$/g, "").replace(/^\s+|\s+$/gm, "").replace(/\n\s*\n/g, "\n").replace(/\s+/gm, " ").replace(/[ ,.]+(?=,|$)/gm, "").replace(/,\s*(?=,|$)/g, ""); } function normalizeWildcardsYamlString(e) { return stripZeroStr(e).replace(/\xa0/g, " ").replace(/[,.]+(?=,)/gm, "").replace(/[ .]+$/gm, "").replace(/\{\s+(\d+(?:\.\d+)?(?:-(?:\d+(?:\.\d+)?)?\$\$|::))/gm, "{$1").replace(/\|\s(\d+(?:\.\d+)?::)/gm, "|$1").replace(/^[ \t]+-[ \t]*$/gm, "").replace(/^([ \t]+-)[ \t][ ,.]+/gm, "$1 ").replace(/^([ \t]+-[^\n]+),+$/gm, "$1"); } function formatPrompts(e, t) { var r; return null !== (r = t) && void 0 !== r || (t = {}), e = normalizeWildcardsYamlString(e = trimPrompts(e = stripZeroStr(e))), t.minifyPrompts && (e = e.replace(/(,)\s+/gm, "$1").replace(/\s+(,)/gm, "$1").replace(/(?<=,\|})\s+/gm, "").replace(/\s+(?=\{(?:\s*\d+(?:\.\d+)?::)?,)/gm, "")), e; } function isWildcardsYAMLDocument(e) { return t.isDocument(e); } function isWildcardsYAMLMap(e) { return t.isMap(e); } function _validMap(e, r, ...n) { const i = r.items.findIndex((e => !t.isPair(e) || null == (null == e ? void 0 : e.value))); if (-1 !== i) { const t = handleVisitPathsFull(e, r, ...n); throw new SyntaxError(`Invalid SYNTAX. paths: [${t}], key: ${e}, node: ${r}, elem: ${r.items[i]}`); } } function _validSeq(e, r, ...n) { const i = r.items.findIndex((e => !t.isScalar(e))); if (-1 !== i) { const t = handleVisitPathsFull(e, r, ...n); throw new SyntaxError(`Invalid SYNTAX. paths: [${t}], indexKey: ${e} key: ${e}, node: ${r}, index: ${i}, node: ${r.items[i]}`); } } function _validPair(e, t, ...r) { const n = t.key; if (!isSafeKey("string" == typeof n ? n : n.value)) { const i = handleVisitPathsFull(e, t, ...r); throw new SyntaxError(`Invalid Key. paths: [${i}], key: ${e}, keyNodeValue: "${null == n ? void 0 : n.value}", keyNode: ${n}`); } } function createDefaultVisitWildcardsYAMLOptions(e) { var t; let r = { Map: _validMap, Seq: _validSeq }; if (null !== (t = e) && void 0 !== t || (e = {}), e.allowUnsafeKey || (r.Pair = _validPair), !e.disableUniqueItemValues) { const e = r.Seq; r.Seq = (t, r, ...n) => { e(t, r, ...n), uniqueSeqItems(r.items); }; } return r; } function validWildcardsYamlData(e, r) { var n; if (null !== (n = r) && void 0 !== n || (r = {}), t.isDocument(e)) { if (t.isNode(e.contents) && !t.isMap(e.contents)) throw TypeError(`The 'contents' property of the provided YAML document must be a YAMLMap. Received: ${e.contents}`); visitWildcardsYAML(e, createDefaultVisitWildcardsYAMLOptions(r)), e = e.toJSON(); } if (null == e) { if (r.allowEmptyDocument) return; throw new TypeError(`The provided JSON contents should not be empty. ${e}`); } let i = Object.keys(e); if (!i.length) throw TypeError("The provided JSON contents must contain at least one key."); if (1 !== i.length && !r.allowMultiRoot) throw TypeError("The provided JSON object cannot have more than one root key. Only one root key is allowed unless explicitly allowed by the 'allowMultiRoot' option."); } function isSafeKey(e) { return "string" == typeof e && /^[._\w-]+$/.test(e) && !/^[\._-]|[\._-]$/.test(e); } function _checkValue(e) { let t = /(?:^|[\s{},])_(?=[^_]|$)|(? t.isPair(e))); } function convertPairsToPathsList(e) { return e.map((e => e.key.value)); } function handleVisitPaths(e) { return convertPairsToPathsList(_handleVisitPathsCore(e)); } function handleVisitPathsFull(e, t, r) { const n = handleVisitPaths(r); return "number" == typeof e && n.push(e), n; } const a = /['"]/, o = /^\s*-|[{$~!@}\n|:?#'"]/; function _visitNormalizeScalar(e, t, r) { let n = t.value; if ("string" == typeof n) { if (r.checkUnsafeQuote && a.test(n)) throw new SyntaxError(`Invalid SYNTAX [UNSAFE_QUOTE]. key: ${e}, node: ${t}`); if (("QUOTE_DOUBLE" === t.type || "QUOTE_SINGLE" === t.type && !n.includes("\\")) && (t.type = "PLAIN"), n = formatPrompts(n, r.options), !n.length) throw new SyntaxError(`Invalid SYNTAX [EMPTY_VALUE]. key: ${e}, node: ${t}`); o.test(n) && ("PLAIN" === t.type || "BLOCK_FOLDED" === t.type && /#/.test(n)) && (t.type = "BLOCK_LITERAL"); let i = _checkValue(n); if (null != i && i.error) throw new SyntaxError(`${i.error}. key: ${e}, node: ${t}`); t.value = n; } } function getTopRootContents(e) { if (isWildcardsYAMLDocument(e) && (e = e.contents), isWildcardsYAMLMap(e)) return e; throw new TypeError("Input document is not a YAML Document or a YAML Map. Please provide a valid YAML structure."); } const s = /(? 0; for (const c in e) { if (r.onlyFirstMatchAll && a.length) break; const d = n.slice().concat(c), u = n.slice().concat(s), m = i.isMatch(pathsToWildcardsPath(d), pathsToWildcardsPath(u), o.globOpts); if (m) { const n = e[c], i = !Array.isArray(n); if (l) { if (i && "string" != typeof n) { _findPathCore(n, t, r, d, a, o); continue; } } else { if (!i) { a.push({ key: d, value: n }); continue; } if (!l && o.findOpts.allowWildcardsAtEndMatchRecord && s.includes("*") && "object" == typeof n && n) { a.push({ key: d, value: n }); continue; } } if (!s.includes("*") || i && !l) throw new TypeError(`Invalid Type. paths: [${d}], isMatch: ${m}, deep: ${l}, deep paths: [${t}], notArray: ${i}, match: [${u}], value: ${n}, _cache : ${JSON.stringify(o)}`); } } if (0 === n.length && r.throwWhenNotFound && !a.length) throw new RangeError(`Invalid Paths. paths: [${[ s, ...t ]}], _cache : ${JSON.stringify(o)}`); return a; } function normalizeDocument(e, t) { let r = getOptionsFromDocument(e, t); const n = createDefaultVisitWildcardsYAMLOptions(r); let i = !r.disableUnsafeQuote; visitWildcardsYAML(e, { ...n, Scalar: (e, t) => _visitNormalizeScalar(e, t, { checkUnsafeQuote: i, options: r }) }); } function parseWildcardsYaml(e, r) { var n; (r = defaultOptionsParseDocument(r)).allowEmptyDocument && (null !== (n = e) && void 0 !== n || (e = "")); let i = t.parseDocument(e.toString(), r); return validWildcardsYamlData(i, r), i; } e.RE_DYNAMIC_PROMPTS_WILDCARDS = s, e.RE_DYNAMIC_PROMPTS_WILDCARDS_GLOBAL = l, e.RE_WILDCARDS_NAME = c, e._checkValue = _checkValue, e._findPathCore = _findPathCore, e._handleVisitPathsCore = _handleVisitPathsCore, e._matchDynamicPromptsWildcardsCore = _matchDynamicPromptsWildcardsCore, e._mergeSeqCore = _mergeSeqCore, e._mergeWildcardsYAMLDocumentRootsCore = _mergeWildcardsYAMLDocumentRootsCore, e._nearString = _nearString, e._toJSON = _toJSON, e._validKey = function _validKey(e) { if (!isSafeKey(e)) throw new SyntaxError(`Invalid Key. key: ${e}`); }, e._validMap = _validMap, e._validPair = _validPair, e._validSeq = _validSeq, e._visitNormalizeScalar = _visitNormalizeScalar, e.assertWildcardsName = function assertWildcardsName(e) { if (isWildcardsName(e)) throw new SyntaxError(`Invalid Wildcards Name Syntax: ${e}`); }, e.checkAllSelfLinkWildcardsExists = function checkAllSelfLinkWildcardsExists(e, r) { var n, a; null !== (n = r) && void 0 !== n || (r = {}); const o = r.maxErrors > 0 ? r.maxErrors : 10; t.isDocument(e) || t.isNode(e) || (e = parseWildcardsYaml(e)); const s = e.toString(), l = e.toJSON(); let c = matchDynamicPromptsWildcardsAll(s, !0), isMatchIgnore = () => !1; null !== (a = r.ignore) && void 0 !== a && a.length && (isMatchIgnore = i(r.ignore)); const d = [], u = []; for (const e of c) { if (isMatchIgnore(e.name)) { d.push(e.name); continue; } const t = convertWildcardsNameToPaths(e.name); let n = []; try { n = findPath(l, t, { onlyFirstMatchAll: !0, throwWhenNotFound: !0, allowWildcardsAtEndMatchRecord: r.allowWildcardsAtEndMatchRecord }); } catch (e) { if (u.push(e), u.length >= o) { let e = new RangeError(`Max Errors. errors.length ${u.length} >= ${o}`); u.unshift(e); break; } continue; } } return { obj: e, hasExists: [], ignoreList: d, errors: u }; }, e.convertPairsToPathsList = convertPairsToPathsList, e.convertWildcardsNameToPaths = convertWildcardsNameToPaths, e.createDefaultVisitWildcardsYAMLOptions = createDefaultVisitWildcardsYAMLOptions, e.deepFindSingleRootAt = deepFindSingleRootAt, e.default = parseWildcardsYaml, e.defaultCheckerIgnoreCase = defaultCheckerIgnoreCase, e.defaultOptionsParseDocument = defaultOptionsParseDocument, e.defaultOptionsStringify = defaultOptionsStringify, e.defaultOptionsStringifyMinify = function defaultOptionsStringifyMinify() { return { lineWidth: 0, minifyPrompts: !0 }; }, e.findPath = findPath, e.findPathOptionsToGlobOptions = findPathOptionsToGlobOptions, e.findWildcardsYAMLPathsAll = function findWildcardsYAMLPathsAll(e) { const t = []; return visitWildcardsYAML(e, { Seq(...e) { const r = handleVisitPathsFull(...e); t.push(r); } }), t; }, e.formatPrompts = formatPrompts, e.getOptionsFromDocument = getOptionsFromDocument, e.getOptionsShared = getOptionsShared, e.getTopRootContents = getTopRootContents, e.getTopRootNodes = function getTopRootNodes(e) { return getTopRootContents(e).items; }, e.handleVisitPaths = handleVisitPaths, e.handleVisitPathsFull = handleVisitPathsFull, e.isDynamicPromptsWildcards = function isDynamicPromptsWildcards(e) { return matchDynamicPromptsWildcards(e).isFullMatch; }, e.isSafeKey = isSafeKey, e.isWildcardsName = isWildcardsName, e.isWildcardsPathSyntx = isWildcardsPathSyntx, e.isWildcardsYAMLDocument = isWildcardsYAMLDocument, e.isWildcardsYAMLDocumentAndContentsIsMap = function isWildcardsYAMLDocumentAndContentsIsMap(e) { return t.isDocument(e) && t.isMap(e.contents); }, e.isWildcardsYAMLMap = isWildcardsYAMLMap, e.matchDynamicPromptsWildcards = matchDynamicPromptsWildcards, e.matchDynamicPromptsWildcardsAll = matchDynamicPromptsWildcardsAll, e.matchDynamicPromptsWildcardsAllGenerator = matchDynamicPromptsWildcardsAllGenerator, e.mergeFindSingleRoots = function mergeFindSingleRoots(e, r) { if (!t.isDocument(e) && !t.isMap(e)) throw TypeError(`The merge target should be a YAMLMap or Document. doc: ${e}`); r = [ r ].flat(); for (let i of r) { let r = deepFindSingleRootAt(i); if (!r) throw new TypeError(`Only YAMLMap can be merged. node: ${i}`); { let i = e.getIn(r.paths); if (i) { if (!t.isMap(i)) throw new TypeError(`Only YAMLMap can be merged. node: ${i}`); r.value.items.forEach((e => { const a = e.key.value, o = i.get(a); if (o) if (t.isSeq(o) && t.isSeq(e.value)) _mergeSeqCore(o, e.value); else { if (!t.isMap(o) || !t.isMap(e.value)) throw new TypeError(`Current does not support deep merge at paths: ${JSON.stringify(r.paths.concat(a))}, a: ${o}, b: ${e.value}`); { const t = [], i = []; for (const r of e.value.items) try { o.add(r, !1); } catch (e) { t.push(r.key.value), i.push(e); } if (i.length) throw new n.AggregateErrorExtra(i, `Failure when merging sub YAMLMap. Paths: ${JSON.stringify(r.paths.concat(a))}. Conflicting keys: ${JSON.stringify(t)}`); } } else i.items.push(e); })); } else e.setIn(r.paths, r.value); } } return e; }, e.mergeSeq = function mergeSeq(e, r) { if (t.isSeq(e) && t.isSeq(r)) return _mergeSeqCore(e, r); throw new TypeError("Only allow merge YAMLSeq"); }, e.mergeWildcardsYAMLDocumentJsonBy = function mergeWildcardsYAMLDocumentJsonBy(e, t) { return t.deepmerge(e.map(_toJSON)); }, e.mergeWildcardsYAMLDocumentRoots = function mergeWildcardsYAMLDocumentRoots(e) { return e.reduce(_mergeWildcardsYAMLDocumentRootsCore); }, e.normalizeDocument = normalizeDocument, e.normalizeWildcardsYamlString = normalizeWildcardsYamlString, e.parseWildcardsYaml = parseWildcardsYaml, e.pathsToDotPath = function pathsToDotPath(e) { return e.join("."); }, e.pathsToWildcardsPath = pathsToWildcardsPath, e.stringifyWildcardsYamlData = function stringifyWildcardsYamlData(e, r) { const n = t.isDocument(e); return n && (r = getOptionsFromDocument(e, r)), r = defaultOptionsStringify(r), n ? (normalizeDocument(e, r), e.toString(r)) : t.stringify(e, r); }, e.stripBlankLines = function stripBlankLines(e, t) { return e = e.replace(/(\r?\n)[\s\r\n\t\xa0]+(\r?\n)/g, "$1$2").replace(/(\r?\n)(?:\r?\n)(?=[\s\t\xa0])/g, "$1").replace(/[ \xa0\t]+$/gm, ""), t && (e = e.replace(/\s+$/, ""), e += "\n\n"), e; }, e.stripZeroStr = stripZeroStr, e.trimPrompts = trimPrompts, e.uniqueSeqItems = uniqueSeqItems, e.uniqueSeqItemsChecker = uniqueSeqItemsChecker, e.validWildcardsYamlData = validWildcardsYamlData, e.visitWildcardsYAML = visitWildcardsYAML, e.wildcardsPathToPaths = function wildcardsPathToPaths(e) { return isWildcardsPathSyntx(e) && (e = matchDynamicPromptsWildcards(e).name), convertWildcardsNameToPaths(e); }, Object.defineProperty(e, "__esModule", { value: !0 }); })); //# sourceMappingURL=index.umd.production.min.cjs.map