@react-querybuilder/core 8.9.0 → 8.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/dist/arrayUtils-BF1P8iHS.mjs +122 -0
- package/dist/arrayUtils-BF1P8iHS.mjs.map +1 -0
- package/dist/basic-BfD-7CN3.d.mts +1235 -0
- package/dist/cjs/react-querybuilder_core.cjs.development.d.ts +21 -10
- package/dist/cjs/react-querybuilder_core.cjs.development.js +22 -19
- package/dist/cjs/react-querybuilder_core.cjs.development.js.map +1 -1
- package/dist/cjs/react-querybuilder_core.cjs.production.d.ts +21 -10
- package/dist/cjs/react-querybuilder_core.cjs.production.js +1 -1
- package/dist/cjs/react-querybuilder_core.cjs.production.js.map +1 -1
- package/dist/convertQuery-H7RhQiIc.mjs +75 -0
- package/dist/convertQuery-H7RhQiIc.mjs.map +1 -0
- package/dist/export-r-V7bU31.d.mts +452 -0
- package/dist/formatQuery.d.mts +667 -0
- package/dist/formatQuery.mjs +2366 -0
- package/dist/formatQuery.mjs.map +1 -0
- package/dist/import-BwbbP4oU.d.mts +28 -0
- package/dist/isRuleGroup-CnhYpLOM.mjs +40 -0
- package/dist/isRuleGroup-CnhYpLOM.mjs.map +1 -0
- package/dist/isRuleGroup-DqAs2x4E.js.map +1 -1
- package/dist/objectUtils-BtWdcZVG.mjs +11 -0
- package/dist/objectUtils-BtWdcZVG.mjs.map +1 -0
- package/dist/optGroupUtils-Duv-M8rf.mjs +102 -0
- package/dist/optGroupUtils-Duv-M8rf.mjs.map +1 -0
- package/dist/parseCEL.d.mts +34 -0
- package/dist/parseCEL.mjs +2593 -0
- package/dist/parseCEL.mjs.map +1 -0
- package/dist/parseJSONata.d.mts +36 -0
- package/dist/parseJSONata.mjs +268 -0
- package/dist/parseJSONata.mjs.map +1 -0
- package/dist/parseJsonLogic.d.mts +36 -0
- package/dist/parseJsonLogic.mjs +191 -0
- package/dist/parseJsonLogic.mjs.map +1 -0
- package/dist/parseMongoDB.d.mts +79 -0
- package/dist/parseMongoDB.mjs +267 -0
- package/dist/parseMongoDB.mjs.map +1 -0
- package/dist/parseNumber-BtGKa58z.mjs +24 -0
- package/dist/parseNumber-BtGKa58z.mjs.map +1 -0
- package/dist/parseSQL.d.mts +37 -0
- package/dist/parseSQL.mjs +6626 -0
- package/dist/parseSQL.mjs.map +1 -0
- package/dist/parseSpEL.d.mts +34 -0
- package/dist/parseSpEL.mjs +273 -0
- package/dist/parseSpEL.mjs.map +1 -0
- package/dist/prepareQueryObjects-CS6Wmhmf.mjs +154 -0
- package/dist/prepareQueryObjects-CS6Wmhmf.mjs.map +1 -0
- package/dist/react-querybuilder_core.d.mts +21 -10
- package/dist/react-querybuilder_core.legacy-esm.d.ts +21 -10
- package/dist/react-querybuilder_core.legacy-esm.js +19 -18
- package/dist/react-querybuilder_core.legacy-esm.js.map +1 -1
- package/dist/react-querybuilder_core.mjs +22 -20
- package/dist/react-querybuilder_core.mjs.map +1 -1
- package/dist/react-querybuilder_core.production.d.mts +21 -10
- package/dist/react-querybuilder_core.production.mjs +1 -1
- package/dist/react-querybuilder_core.production.mjs.map +1 -1
- package/dist/transformQuery-DdMvmrCh.mjs +41 -0
- package/dist/transformQuery-DdMvmrCh.mjs.map +1 -0
- package/dist/transformQuery.d.mts +118 -0
- package/dist/transformQuery.mjs +4 -0
- package/package.json +68 -19
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { DefaultRuleGroupType, DefaultRuleGroupTypeIC, Except, RuleGroupTypeAny, RuleType } from "./basic-BfD-7CN3.mjs";
|
|
2
|
+
import { RQBJsonLogic } from "./export-r-V7bU31.mjs";
|
|
3
|
+
import { ParserCommonOptions } from "./import-BwbbP4oU.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/utils/parseJsonLogic/parseJsonLogic.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Options object for {@link parseJsonLogic}.
|
|
8
|
+
*/
|
|
9
|
+
interface ParseJsonLogicOptions extends ParserCommonOptions {
|
|
10
|
+
jsonLogicOperations?: Record<string, (value: any) => RuleType | RuleGroupTypeAny | false>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Converts a JsonLogic object into a query suitable for the
|
|
14
|
+
* {@link index!QueryBuilder QueryBuilder} component's `query` or `defaultQuery` props
|
|
15
|
+
* ({@link index!DefaultRuleGroupType DefaultRuleGroupType}).
|
|
16
|
+
*/
|
|
17
|
+
declare function parseJsonLogic(rqbJsonLogic: string | RQBJsonLogic): DefaultRuleGroupType;
|
|
18
|
+
/**
|
|
19
|
+
* Converts a JsonLogic object into a query suitable for the
|
|
20
|
+
* {@link index!QueryBuilder QueryBuilder} component's `query` or `defaultQuery` props
|
|
21
|
+
* ({@link index!DefaultRuleGroupType DefaultRuleGroupType}).
|
|
22
|
+
*/
|
|
23
|
+
declare function parseJsonLogic(rqbJsonLogic: string | RQBJsonLogic, options: Except<ParseJsonLogicOptions, "independentCombinators"> & {
|
|
24
|
+
independentCombinators?: false;
|
|
25
|
+
}): DefaultRuleGroupType;
|
|
26
|
+
/**
|
|
27
|
+
* Converts a JsonLogic object into a query suitable for the
|
|
28
|
+
* {@link index!QueryBuilder QueryBuilder} component's `query` or `defaultQuery` props
|
|
29
|
+
* ({@link index!DefaultRuleGroupTypeIC DefaultRuleGroupTypeIC}).
|
|
30
|
+
*/
|
|
31
|
+
declare function parseJsonLogic(rqbJsonLogic: string | RQBJsonLogic, options: Except<ParseJsonLogicOptions, "independentCombinators"> & {
|
|
32
|
+
independentCombinators: true;
|
|
33
|
+
}): DefaultRuleGroupTypeIC;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { ParseJsonLogicOptions, parseJsonLogic };
|
|
36
|
+
//# sourceMappingURL=parseJsonLogic.d.mts.map
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { defaultOperatorNegationMap, joinWith } from "./arrayUtils-BF1P8iHS.mjs";
|
|
2
|
+
import { isPojo, isRuleGroup, isRuleGroupType } from "./isRuleGroup-CnhYpLOM.mjs";
|
|
3
|
+
import "./optGroupUtils-Duv-M8rf.mjs";
|
|
4
|
+
import { convertToIC } from "./convertQuery-H7RhQiIc.mjs";
|
|
5
|
+
import { fieldIsValidUtil, getFieldsArray, prepareRuleGroup } from "./prepareQueryObjects-CS6Wmhmf.mjs";
|
|
6
|
+
import { objectKeys } from "./objectUtils-BtWdcZVG.mjs";
|
|
7
|
+
|
|
8
|
+
//#region src/utils/parseJsonLogic/utils.ts
|
|
9
|
+
const isJsonLogicVar = (logic) => isPojo(logic) && "var" in logic;
|
|
10
|
+
const isRQBJsonLogicVar = (logic) => isJsonLogicVar(logic) && typeof logic.var === "string";
|
|
11
|
+
const isJsonLogicEqual = (logic) => isPojo(logic) && "==" in logic;
|
|
12
|
+
const isJsonLogicStrictEqual = (logic) => isPojo(logic) && "===" in logic;
|
|
13
|
+
const isJsonLogicNotEqual = (logic) => isPojo(logic) && "!=" in logic;
|
|
14
|
+
const isJsonLogicStrictNotEqual = (logic) => isPojo(logic) && "!==" in logic;
|
|
15
|
+
const isJsonLogicNegation = (logic) => isPojo(logic) && "!" in logic;
|
|
16
|
+
const isJsonLogicDoubleNegation = (logic) => isPojo(logic) && "!!" in logic;
|
|
17
|
+
const isJsonLogicOr = (logic) => isPojo(logic) && "or" in logic;
|
|
18
|
+
const isJsonLogicAnd = (logic) => isPojo(logic) && "and" in logic;
|
|
19
|
+
const isJsonLogicGreaterThan = (logic) => isPojo(logic) && ">" in logic;
|
|
20
|
+
const isJsonLogicGreaterThanOrEqual = (logic) => isPojo(logic) && ">=" in logic;
|
|
21
|
+
const isJsonLogicLessThan = (logic) => isPojo(logic) && "<" in logic && logic["<"].length === 2;
|
|
22
|
+
const isJsonLogicLessThanOrEqual = (logic) => isPojo(logic) && "<=" in logic && logic["<="].length === 2;
|
|
23
|
+
const isJsonLogicInArray = (logic) => isPojo(logic) && "in" in logic && Array.isArray(logic.in[1]);
|
|
24
|
+
const isJsonLogicInString = (logic) => isPojo(logic) && "in" in logic && !Array.isArray(logic.in[1]);
|
|
25
|
+
const isJsonLogicAll = (logic) => isPojo(logic) && "all" in logic;
|
|
26
|
+
const isJsonLogicNone = (logic) => isPojo(logic) && "none" in logic;
|
|
27
|
+
const isJsonLogicSome = (logic) => isPojo(logic) && "some" in logic;
|
|
28
|
+
const isJsonLogicBetweenExclusive = (logic) => isPojo(logic) && "<" in logic && Array.isArray(logic["<"]) && logic["<"].length === 3;
|
|
29
|
+
const isJsonLogicBetweenInclusive = (logic) => isPojo(logic) && "<=" in logic && Array.isArray(logic["<="]) && logic["<="].length === 3;
|
|
30
|
+
const isRQBJsonLogicStartsWith = (logic) => isPojo(logic) && "startsWith" in logic;
|
|
31
|
+
const isRQBJsonLogicEndsWith = (logic) => isPojo(logic) && "endsWith" in logic;
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/utils/parseJsonLogic/parseJsonLogic.ts
|
|
35
|
+
const emptyRuleGroup = {
|
|
36
|
+
combinator: "and",
|
|
37
|
+
rules: []
|
|
38
|
+
};
|
|
39
|
+
function parseJsonLogic(rqbJsonLogic, options = {}) {
|
|
40
|
+
const fieldsFlat = getFieldsArray(options.fields);
|
|
41
|
+
const { getValueSources, listsAsArrays, jsonLogicOperations } = options;
|
|
42
|
+
const fieldIsValid = (fieldName, operator, subordinateFieldName) => fieldIsValidUtil({
|
|
43
|
+
fieldName,
|
|
44
|
+
fieldsFlat,
|
|
45
|
+
operator,
|
|
46
|
+
subordinateFieldName,
|
|
47
|
+
getValueSources
|
|
48
|
+
});
|
|
49
|
+
function processLogic(logic, outermost) {
|
|
50
|
+
if (outermost && !isPojo(logic)) return false;
|
|
51
|
+
const [key, keyValue] = Object.entries(logic || {})?.[0] ?? [];
|
|
52
|
+
if (jsonLogicOperations && objectKeys(jsonLogicOperations).includes(key)) {
|
|
53
|
+
const rule$1 = jsonLogicOperations[key](keyValue);
|
|
54
|
+
return rule$1 ? outermost && !isRuleGroup(rule$1) ? {
|
|
55
|
+
combinator: "and",
|
|
56
|
+
rules: [rule$1]
|
|
57
|
+
} : rule$1 : false;
|
|
58
|
+
}
|
|
59
|
+
if (isJsonLogicAnd(logic)) return {
|
|
60
|
+
combinator: "and",
|
|
61
|
+
rules: logic.and.map((l) => processLogic(l)).filter(Boolean)
|
|
62
|
+
};
|
|
63
|
+
else if (isJsonLogicOr(logic)) return {
|
|
64
|
+
combinator: "or",
|
|
65
|
+
rules: logic.or.map((l) => processLogic(l)).filter(Boolean)
|
|
66
|
+
};
|
|
67
|
+
else if (isJsonLogicNegation(logic)) {
|
|
68
|
+
const rule$1 = processLogic(logic["!"]);
|
|
69
|
+
if (rule$1) {
|
|
70
|
+
if (!isRuleGroupType(rule$1) && (rule$1.operator === "between" || rule$1.operator === "in" || rule$1.operator === "contains" || rule$1.operator === "beginsWith" || rule$1.operator === "endsWith")) {
|
|
71
|
+
const newRule = {
|
|
72
|
+
...rule$1,
|
|
73
|
+
operator: defaultOperatorNegationMap[rule$1.operator]
|
|
74
|
+
};
|
|
75
|
+
if (outermost) return {
|
|
76
|
+
combinator: "and",
|
|
77
|
+
rules: [newRule]
|
|
78
|
+
};
|
|
79
|
+
return newRule;
|
|
80
|
+
} else if (isJsonLogicBetweenExclusive(logic["!"]) || isRuleGroupType(rule$1)) return {
|
|
81
|
+
...rule$1,
|
|
82
|
+
not: true
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
combinator: "and",
|
|
86
|
+
rules: [rule$1],
|
|
87
|
+
not: true
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
} else if (isJsonLogicDoubleNegation(logic)) return processLogic(logic["!!"]) || false;
|
|
92
|
+
let rule = false;
|
|
93
|
+
let field = "";
|
|
94
|
+
let operator = "=";
|
|
95
|
+
let value = "";
|
|
96
|
+
let valueSource = void 0;
|
|
97
|
+
if (isJsonLogicEqual(logic) || isJsonLogicStrictEqual(logic) || isJsonLogicNotEqual(logic) || isJsonLogicStrictNotEqual(logic) || isJsonLogicGreaterThan(logic) || isJsonLogicGreaterThanOrEqual(logic) || isJsonLogicLessThan(logic) || isJsonLogicLessThanOrEqual(logic) || isJsonLogicInString(logic) || isRQBJsonLogicStartsWith(logic) || isRQBJsonLogicEndsWith(logic)) {
|
|
98
|
+
const [first, second] = keyValue;
|
|
99
|
+
if (isRQBJsonLogicVar(first) && !isPojo(second)) {
|
|
100
|
+
field = first.var;
|
|
101
|
+
value = second;
|
|
102
|
+
} else if (!isPojo(first) && isRQBJsonLogicVar(second)) {
|
|
103
|
+
field = second.var;
|
|
104
|
+
value = first;
|
|
105
|
+
} else if (isRQBJsonLogicVar(first) && isRQBJsonLogicVar(second)) {
|
|
106
|
+
field = first.var;
|
|
107
|
+
value = second.var;
|
|
108
|
+
valueSource = "field";
|
|
109
|
+
} else return false;
|
|
110
|
+
if (isJsonLogicEqual(logic) || isJsonLogicStrictEqual(logic)) operator = value === null ? "null" : "=";
|
|
111
|
+
else if (isJsonLogicNotEqual(logic) || isJsonLogicStrictNotEqual(logic)) operator = value === null ? "notNull" : "!=";
|
|
112
|
+
else if (isJsonLogicInString(logic)) operator = "contains";
|
|
113
|
+
else if (isRQBJsonLogicStartsWith(logic)) operator = "beginsWith";
|
|
114
|
+
else if (isRQBJsonLogicEndsWith(logic)) operator = "endsWith";
|
|
115
|
+
else operator = key;
|
|
116
|
+
if (fieldIsValid(field, operator, valueSource === "field" ? value : void 0)) rule = {
|
|
117
|
+
field,
|
|
118
|
+
operator,
|
|
119
|
+
value,
|
|
120
|
+
valueSource
|
|
121
|
+
};
|
|
122
|
+
} else if (isJsonLogicAll(logic) && isRQBJsonLogicVar(logic["all"][0]) || isJsonLogicNone(logic) && isRQBJsonLogicVar(logic["none"][0]) || isJsonLogicSome(logic) && isRQBJsonLogicVar(logic["some"][0])) {
|
|
123
|
+
const match = { mode: isJsonLogicNone(logic) ? "none" : isJsonLogicSome(logic) ? "some" : "all" };
|
|
124
|
+
const [{ var: field$1 }, operation] = logic[match.mode];
|
|
125
|
+
const matcher = processLogic(operation);
|
|
126
|
+
if (!matcher) return false;
|
|
127
|
+
rule = {
|
|
128
|
+
field: field$1,
|
|
129
|
+
operator: "=",
|
|
130
|
+
match,
|
|
131
|
+
value: isRuleGroup(matcher) ? matcher : {
|
|
132
|
+
combinator: "and",
|
|
133
|
+
rules: [matcher]
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
} else if (isJsonLogicBetweenExclusive(logic) && isRQBJsonLogicVar(logic["<"][1])) {
|
|
137
|
+
field = logic["<"][1].var;
|
|
138
|
+
const values = [logic["<"][0], logic["<"][2]];
|
|
139
|
+
// istanbul ignore else
|
|
140
|
+
if (values.every((v) => isRQBJsonLogicVar(v)) || values.every((el) => typeof el === "string") || values.every((el) => typeof el === "number") || values.every((el) => typeof el === "boolean")) return processLogic({ and: [{ ">": [{ var: field }, values[0]] }, { "<": [{ var: field }, values[1]] }] }) || false;
|
|
141
|
+
} else if (isJsonLogicBetweenInclusive(logic) && isRQBJsonLogicVar(logic["<="][1])) {
|
|
142
|
+
field = logic["<="][1].var;
|
|
143
|
+
operator = "between";
|
|
144
|
+
const values = [logic["<="][0], logic["<="][2]];
|
|
145
|
+
if (logic["<="].every((v) => isRQBJsonLogicVar(v))) {
|
|
146
|
+
const vars = values;
|
|
147
|
+
valueSource = "field";
|
|
148
|
+
const fieldList = vars.map((el) => el.var).filter((sf) => fieldIsValid(field, operator, sf));
|
|
149
|
+
value = listsAsArrays ? fieldList : joinWith(fieldList, ",");
|
|
150
|
+
} else if (values.every((el) => typeof el === "string") || values.every((el) => typeof el === "number") || values.every((el) => typeof el === "boolean")) value = listsAsArrays ? values : joinWith(values.map((el) => `${el}`), ",");
|
|
151
|
+
if (fieldIsValid(field, operator) && value.length >= 2) rule = {
|
|
152
|
+
field,
|
|
153
|
+
operator,
|
|
154
|
+
value,
|
|
155
|
+
valueSource
|
|
156
|
+
};
|
|
157
|
+
} else if (isJsonLogicInArray(logic) && isRQBJsonLogicVar(keyValue[0])) {
|
|
158
|
+
field = keyValue[0].var;
|
|
159
|
+
operator = "in";
|
|
160
|
+
if (logic.in[1].every((v) => isRQBJsonLogicVar(v))) {
|
|
161
|
+
valueSource = "field";
|
|
162
|
+
const fieldList = logic.in[1].map((el) => el.var).filter((sf) => fieldIsValid(field, operator, sf));
|
|
163
|
+
value = listsAsArrays ? fieldList : joinWith(fieldList, ",");
|
|
164
|
+
} else if (logic.in[1].every((el) => typeof el === "string") || logic.in[1].every((el) => typeof el === "number") || logic.in[1].every((el) => typeof el === "boolean")) value = listsAsArrays ? logic.in[1] : joinWith(logic.in[1].map((el) => `${el}`), ",");
|
|
165
|
+
// istanbul ignore else
|
|
166
|
+
if (value.length > 0) rule = {
|
|
167
|
+
field,
|
|
168
|
+
operator,
|
|
169
|
+
value,
|
|
170
|
+
valueSource
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return rule ? outermost ? {
|
|
174
|
+
combinator: "and",
|
|
175
|
+
rules: [rule]
|
|
176
|
+
} : rule : false;
|
|
177
|
+
}
|
|
178
|
+
const prepare = options.generateIDs ? prepareRuleGroup : (g) => g;
|
|
179
|
+
let logicRoot = rqbJsonLogic;
|
|
180
|
+
if (typeof rqbJsonLogic === "string") try {
|
|
181
|
+
logicRoot = JSON.parse(rqbJsonLogic);
|
|
182
|
+
} catch {
|
|
183
|
+
return prepare(emptyRuleGroup);
|
|
184
|
+
}
|
|
185
|
+
const finalQuery = processLogic(logicRoot, true) || emptyRuleGroup;
|
|
186
|
+
return prepare(options.independentCombinators ? convertToIC(finalQuery) : finalQuery);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
//#endregion
|
|
190
|
+
export { parseJsonLogic };
|
|
191
|
+
//# sourceMappingURL=parseJsonLogic.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseJsonLogic.mjs","names":["emptyRuleGroup: DefaultRuleGroupType","rule","rule: DefaultRuleType | false","operator: DefaultOperatorName","value: any","valueSource: ValueSource | undefined","match: MatchConfig","field","finalQuery: DefaultRuleGroupType"],"sources":["../src/utils/parseJsonLogic/utils.ts","../src/utils/parseJsonLogic/parseJsonLogic.ts"],"sourcesContent":["import type {\n JsonLogicAll,\n JsonLogicAnd,\n JsonLogicDoubleNegation,\n JsonLogicEqual,\n JsonLogicGreaterThan,\n JsonLogicGreaterThanOrEqual,\n JsonLogicInArray,\n JsonLogicInString,\n JsonLogicLessThan,\n JsonLogicLessThanOrEqual,\n JsonLogicNegation,\n JsonLogicNone,\n JsonLogicNotEqual,\n JsonLogicOr,\n JsonLogicSome,\n JsonLogicStrictEqual,\n JsonLogicStrictNotEqual,\n JsonLogicVar,\n RQBJsonLogicEndsWith,\n RQBJsonLogicStartsWith,\n RQBJsonLogicVar,\n} from '../../types';\nimport { isPojo } from '../misc';\nimport type { JsonLogicBetweenExclusive, JsonLogicBetweenInclusive } from './types';\n\n// Standard JsonLogic operations\nexport const isJsonLogicVar = (\n logic: unknown\n): logic is JsonLogicVar<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n isPojo(logic) && 'var' in logic;\nexport const isRQBJsonLogicVar = (logic: unknown): logic is RQBJsonLogicVar =>\n isJsonLogicVar(logic) && typeof logic.var === 'string';\nexport const isJsonLogicEqual = (logic: unknown): logic is JsonLogicEqual =>\n isPojo(logic) && '==' in logic;\nexport const isJsonLogicStrictEqual = (logic: unknown): logic is JsonLogicStrictEqual =>\n isPojo(logic) && '===' in logic;\nexport const isJsonLogicNotEqual = (logic: unknown): logic is JsonLogicNotEqual =>\n isPojo(logic) && '!=' in logic;\nexport const isJsonLogicStrictNotEqual = (logic: unknown): logic is JsonLogicStrictNotEqual =>\n isPojo(logic) && '!==' in logic;\nexport const isJsonLogicNegation = (logic: unknown): logic is JsonLogicNegation =>\n isPojo(logic) && '!' in logic;\nexport const isJsonLogicDoubleNegation = (logic: unknown): logic is JsonLogicDoubleNegation =>\n isPojo(logic) && '!!' in logic;\nexport const isJsonLogicOr = (\n logic: unknown\n): logic is JsonLogicOr<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n isPojo(logic) && 'or' in logic;\nexport const isJsonLogicAnd = (\n logic: unknown\n): logic is JsonLogicAnd<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n isPojo(logic) && 'and' in logic;\nexport const isJsonLogicGreaterThan = (\n logic: unknown\n): logic is JsonLogicGreaterThan<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n isPojo(logic) && '>' in logic;\nexport const isJsonLogicGreaterThanOrEqual = (\n logic: unknown\n): logic is JsonLogicGreaterThanOrEqual<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n isPojo(logic) && '>=' in logic;\nexport const isJsonLogicLessThan = (\n logic: unknown\n): logic is JsonLogicLessThan<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n isPojo(logic) && '<' in logic && logic['<'].length === 2;\nexport const isJsonLogicLessThanOrEqual = (\n logic: unknown\n): logic is JsonLogicLessThanOrEqual<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n isPojo(logic) && '<=' in logic && logic['<='].length === 2;\nexport const isJsonLogicInArray = (\n logic: unknown\n): logic is JsonLogicInArray<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n isPojo(logic) && 'in' in logic && Array.isArray(logic.in[1]);\nexport const isJsonLogicInString = (\n logic: unknown\n): logic is JsonLogicInString<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n isPojo(logic) && 'in' in logic && !Array.isArray(logic.in[1]);\nexport const isJsonLogicAll = (\n logic: unknown\n): logic is JsonLogicAll<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n isPojo(logic) && 'all' in logic;\nexport const isJsonLogicNone = (\n logic: unknown\n): logic is JsonLogicNone<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n isPojo(logic) && 'none' in logic;\nexport const isJsonLogicSome = (\n logic: unknown\n): logic is JsonLogicSome<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n isPojo(logic) && 'some' in logic;\n\n// \"Between\" operations are special cases of '<' and '<='\nexport const isJsonLogicBetweenExclusive = (logic: unknown): logic is JsonLogicBetweenExclusive =>\n isPojo(logic) && '<' in logic && Array.isArray(logic['<']) && logic['<'].length === 3;\nexport const isJsonLogicBetweenInclusive = (logic: unknown): logic is JsonLogicBetweenInclusive =>\n isPojo(logic) && '<=' in logic && Array.isArray(logic['<=']) && logic['<='].length === 3;\n\n// RQB extensions\nexport const isRQBJsonLogicStartsWith = (logic: unknown): logic is RQBJsonLogicStartsWith =>\n isPojo(logic) && 'startsWith' in logic;\nexport const isRQBJsonLogicEndsWith = (logic: unknown): logic is RQBJsonLogicEndsWith =>\n isPojo(logic) && 'endsWith' in logic;\n\n// Type guards for unused JsonLogic operations\n\n// import type {\n// JsonLogicCat,\n// JsonLogicDifference,\n// JsonLogicFilter,\n// JsonLogicIf,\n// JsonLogicLog,\n// JsonLogicMap,\n// JsonLogicMax,\n// JsonLogicMerge,\n// JsonLogicMin,\n// JsonLogicMissing,\n// JsonLogicMissingSome,\n// JsonLogicProduct,\n// JsonLogicQuotient,\n// JsonLogicReduce,\n// JsonLogicRemainder,\n// JsonLogicSubstr,\n// JsonLogicSum,\n// } from '../../types';\n//\n// export const isJsonLogicMissing = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicMissing<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && 'missing' in logic;\n// export const isJsonLogicMissingSome = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicMissingSome<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && 'missing_some' in logic;\n// export const isJsonLogicIf = (logic: RQBJsonLogic): logic is JsonLogicIf =>\n// isPojo(logic) && 'if' in logic;\n// export const isJsonLogicMax = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicMax<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && 'max' in logic;\n// export const isJsonLogicMin = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicMin<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && 'min' in logic;\n// export const isJsonLogicSum = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicSum<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && '+' in logic;\n// export const isJsonLogicDifference = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicDifference<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && '-' in logic;\n// export const isJsonLogicProduct = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicProduct<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && '*' in logic;\n// export const isJsonLogicQuotient = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicQuotient<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && '/' in logic;\n// export const isJsonLogicRemainder = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicRemainder<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && '%' in logic;\n// export const isJsonLogicMap = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicMap<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && 'map' in logic;\n// export const isJsonLogicFilter = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicFilter<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && 'filter' in logic;\n// export const isJsonLogicReduce = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicReduce<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && 'reduce' in logic;\n// export const isJsonLogicMerge = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicMerge<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && 'merge' in logic;\n// export const isJsonLogicCat = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicCat<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && 'cat' in logic;\n// export const isJsonLogicSubstr = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicSubstr<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && 'substr' in logic;\n// export const isJsonLogicLog = (\n// logic: RQBJsonLogic\n// ): logic is JsonLogicLog<RQBJsonLogicStartsWith | RQBJsonLogicEndsWith> =>\n// isPojo(logic) && 'log' in logic;\n","import { defaultOperatorNegationMap } from '../../defaults';\nimport type {\n DefaultOperatorName,\n DefaultRuleGroupType,\n DefaultRuleGroupTypeAny,\n DefaultRuleGroupTypeIC,\n DefaultRuleType,\n Except,\n MatchConfig,\n RQBJsonLogic,\n RQBJsonLogicVar,\n RuleGroupTypeAny,\n RuleType,\n ValueSource,\n} from '../../types';\nimport type { ParserCommonOptions } from '../../types/import';\nimport { joinWith } from '../arrayUtils';\nimport { convertToIC } from '../convertQuery';\nimport { isRuleGroup, isRuleGroupType } from '../isRuleGroup';\nimport { isPojo } from '../misc';\nimport { objectKeys } from '../objectUtils';\nimport { fieldIsValidUtil, getFieldsArray } from '../parserUtils';\nimport { prepareRuleGroup } from '../prepareQueryObjects';\nimport {\n isJsonLogicAll,\n isJsonLogicAnd,\n isJsonLogicBetweenExclusive,\n isJsonLogicBetweenInclusive,\n isJsonLogicDoubleNegation,\n isJsonLogicEqual,\n isJsonLogicGreaterThan,\n isJsonLogicGreaterThanOrEqual,\n isJsonLogicInArray,\n isJsonLogicInString,\n isJsonLogicLessThan,\n isJsonLogicLessThanOrEqual,\n isJsonLogicNegation,\n isJsonLogicNone,\n isJsonLogicNotEqual,\n isJsonLogicOr,\n isJsonLogicSome,\n isJsonLogicStrictEqual,\n isJsonLogicStrictNotEqual,\n isRQBJsonLogicEndsWith,\n isRQBJsonLogicStartsWith,\n isRQBJsonLogicVar,\n} from './utils';\n\n/**\n * Options object for {@link parseJsonLogic}.\n */\nexport interface ParseJsonLogicOptions extends ParserCommonOptions {\n // oxlint-disable-next-line typescript/no-explicit-any\n jsonLogicOperations?: Record<string, (value: any) => RuleType | RuleGroupTypeAny | false>;\n}\n\nconst emptyRuleGroup: DefaultRuleGroupType = { combinator: 'and', rules: [] };\n\n/**\n * Converts a JsonLogic object into a query suitable for the\n * {@link index!QueryBuilder QueryBuilder} component's `query` or `defaultQuery` props\n * ({@link index!DefaultRuleGroupType DefaultRuleGroupType}).\n */\nfunction parseJsonLogic(rqbJsonLogic: string | RQBJsonLogic): DefaultRuleGroupType;\n/**\n * Converts a JsonLogic object into a query suitable for the\n * {@link index!QueryBuilder QueryBuilder} component's `query` or `defaultQuery` props\n * ({@link index!DefaultRuleGroupType DefaultRuleGroupType}).\n */\nfunction parseJsonLogic(\n rqbJsonLogic: string | RQBJsonLogic,\n options: Except<ParseJsonLogicOptions, 'independentCombinators'> & {\n independentCombinators?: false;\n }\n): DefaultRuleGroupType;\n/**\n * Converts a JsonLogic object into a query suitable for the\n * {@link index!QueryBuilder QueryBuilder} component's `query` or `defaultQuery` props\n * ({@link index!DefaultRuleGroupTypeIC DefaultRuleGroupTypeIC}).\n */\nfunction parseJsonLogic(\n rqbJsonLogic: string | RQBJsonLogic,\n options: Except<ParseJsonLogicOptions, 'independentCombinators'> & {\n independentCombinators: true;\n }\n): DefaultRuleGroupTypeIC;\nfunction parseJsonLogic(\n rqbJsonLogic: string | RQBJsonLogic,\n options: ParseJsonLogicOptions = {}\n): DefaultRuleGroupTypeAny {\n const fieldsFlat = getFieldsArray(options.fields);\n const { getValueSources, listsAsArrays, jsonLogicOperations } = options;\n\n const fieldIsValid = (\n fieldName: string,\n operator: DefaultOperatorName,\n subordinateFieldName?: string\n ) =>\n fieldIsValidUtil({\n fieldName,\n fieldsFlat,\n operator,\n subordinateFieldName,\n getValueSources,\n });\n\n // Overload 1: Always return a rule group or false for the outermost logic object\n function processLogic(logic: RQBJsonLogic, outermost: true): DefaultRuleGroupType | false;\n // Overload 2: If not the outermost object, return value could also be a rule\n function processLogic(\n logic: RQBJsonLogic,\n outermost?: false\n ): DefaultRuleGroupType | DefaultRuleType | false;\n // Implementation\n function processLogic(\n logic: RQBJsonLogic,\n outermost?: boolean\n ): DefaultRuleGroupType | DefaultRuleType | false {\n // Bail if the outermost logic is not a plain object\n if (outermost && !isPojo(logic)) {\n return false;\n }\n const [key, keyValue] = Object.entries(logic || {})?.[0] ?? [];\n\n // Custom operations process logic\n if (jsonLogicOperations && objectKeys(jsonLogicOperations).includes(key)) {\n const rule = jsonLogicOperations[key](keyValue) as DefaultRuleType;\n return rule\n ? outermost && !isRuleGroup(rule)\n ? { combinator: 'and', rules: [rule] }\n : rule\n : false;\n }\n\n // Rule groups\n if (isJsonLogicAnd(logic)) {\n return {\n combinator: 'and',\n rules: logic.and.map(l => processLogic(l)).filter(Boolean) as (\n | DefaultRuleType\n | DefaultRuleGroupType\n )[],\n };\n } else if (isJsonLogicOr(logic)) {\n return {\n combinator: 'or',\n rules: logic.or.map(l => processLogic(l)).filter(Boolean) as (\n | DefaultRuleType\n | DefaultRuleGroupType\n )[],\n };\n } else if (isJsonLogicNegation(logic)) {\n const rule = processLogic(logic['!']);\n if (rule) {\n if (\n !isRuleGroupType(rule) &&\n (rule.operator === 'between' ||\n rule.operator === 'in' ||\n rule.operator === 'contains' ||\n rule.operator === 'beginsWith' ||\n rule.operator === 'endsWith')\n ) {\n const newRule = { ...rule, operator: defaultOperatorNegationMap[rule.operator] };\n if (outermost) {\n return { combinator: 'and', rules: [newRule] };\n }\n return newRule;\n } else if (isJsonLogicBetweenExclusive(logic['!']) || isRuleGroupType(rule)) {\n return { ...rule, not: true };\n }\n return { combinator: 'and', rules: [rule], not: true };\n }\n return false;\n } else if (isJsonLogicDoubleNegation(logic)) {\n const rule = processLogic(logic['!!']);\n return rule || false;\n }\n\n // All other keys represent rules\n let rule: DefaultRuleType | false = false;\n let field = '';\n let operator: DefaultOperatorName = '=';\n // oxlint-disable-next-line typescript/no-explicit-any\n let value: any = '';\n let valueSource: ValueSource | undefined = undefined;\n\n if (\n // Basic boolean operations\n isJsonLogicEqual(logic) ||\n isJsonLogicStrictEqual(logic) ||\n isJsonLogicNotEqual(logic) ||\n isJsonLogicStrictNotEqual(logic) ||\n isJsonLogicGreaterThan(logic) ||\n isJsonLogicGreaterThanOrEqual(logic) ||\n isJsonLogicLessThan(logic) ||\n isJsonLogicLessThanOrEqual(logic) ||\n isJsonLogicInString(logic) ||\n isRQBJsonLogicStartsWith(logic) ||\n isRQBJsonLogicEndsWith(logic)\n ) {\n const [first, second] = keyValue;\n if (isRQBJsonLogicVar(first) && !isPojo(second)) {\n field = first.var;\n value = second;\n } else if (!isPojo(first) && isRQBJsonLogicVar(second)) {\n field = second.var;\n value = first;\n } else if (isRQBJsonLogicVar(first) && isRQBJsonLogicVar(second)) {\n field = first.var;\n value = second.var;\n valueSource = 'field';\n } else {\n return false;\n }\n\n // Translate operator if necessary\n if (isJsonLogicEqual(logic) || isJsonLogicStrictEqual(logic)) {\n operator = value === null ? 'null' : '=';\n } else if (isJsonLogicNotEqual(logic) || isJsonLogicStrictNotEqual(logic)) {\n operator = value === null ? 'notNull' : '!=';\n } else if (isJsonLogicInString(logic)) {\n operator = 'contains';\n } else if (isRQBJsonLogicStartsWith(logic)) {\n operator = 'beginsWith';\n } else if (isRQBJsonLogicEndsWith(logic)) {\n operator = 'endsWith';\n } else {\n operator = key as DefaultOperatorName;\n }\n\n if (fieldIsValid(field, operator, valueSource === 'field' ? value : undefined)) {\n rule = { field, operator, value, valueSource };\n }\n } else if (\n (isJsonLogicAll(logic) && isRQBJsonLogicVar(logic['all'][0])) ||\n (isJsonLogicNone(logic) && isRQBJsonLogicVar(logic['none'][0])) ||\n (isJsonLogicSome(logic) && isRQBJsonLogicVar(logic['some'][0]))\n ) {\n // The array coverage functions must have a field as their first element.\n // Otherwise we'd be comparing values to values, which is not supported.\n const match: MatchConfig = {\n mode: isJsonLogicNone(logic) ? 'none' : isJsonLogicSome(logic) ? 'some' : 'all',\n };\n\n // oxlint-disable-next-line typescript/no-explicit-any\n const [{ var: field }, operation] = (logic as any)[match.mode];\n const matcher = processLogic(operation);\n\n // TODO: Support operations that evaluate array member properties\n if (!matcher) return false;\n\n rule = {\n field,\n operator: '=',\n match,\n value: isRuleGroup(matcher) ? matcher : { combinator: 'and', rules: [matcher] },\n };\n } else if (isJsonLogicBetweenExclusive(logic) && isRQBJsonLogicVar(logic['<'][1])) {\n field = logic['<'][1].var;\n const values = [logic['<'][0], logic['<'][2]];\n // istanbul ignore else\n if (\n values.every(v => isRQBJsonLogicVar(v)) ||\n values.every(el => typeof el === 'string') ||\n values.every(el => typeof el === 'number') ||\n values.every(el => typeof el === 'boolean')\n ) {\n return (\n processLogic({\n and: [{ '>': [{ var: field }, values[0]] }, { '<': [{ var: field }, values[1]] }],\n }) || /* istanbul ignore next */ false\n );\n }\n } else if (isJsonLogicBetweenInclusive(logic) && isRQBJsonLogicVar(logic['<='][1])) {\n field = logic['<='][1].var;\n operator = 'between';\n const values = [logic['<='][0], logic['<='][2]];\n if (logic['<='].every(v => isRQBJsonLogicVar(v))) {\n const vars = values as RQBJsonLogicVar[];\n valueSource = 'field';\n const fieldList = vars.map(el => el.var).filter(sf => fieldIsValid(field, operator, sf));\n value = listsAsArrays ? fieldList : joinWith(fieldList, ',');\n } else {\n // istanbul ignore else\n if (\n values.every(el => typeof el === 'string') ||\n values.every(el => typeof el === 'number') ||\n values.every(el => typeof el === 'boolean')\n ) {\n value = listsAsArrays\n ? values\n : joinWith(\n values.map(el => `${el}`),\n ','\n );\n }\n }\n\n if (fieldIsValid(field, operator) && value.length >= 2) {\n rule = { field, operator, value, valueSource };\n }\n } else if (isJsonLogicInArray(logic) && isRQBJsonLogicVar(keyValue[0])) {\n field = keyValue[0].var;\n operator = 'in';\n if (logic.in[1].every(v => isRQBJsonLogicVar(v))) {\n valueSource = 'field';\n const fieldList = logic.in[1]\n .map(el => el.var)\n .filter(sf => fieldIsValid(field, operator, sf));\n value = listsAsArrays ? fieldList : joinWith(fieldList, ',');\n } else {\n // istanbul ignore else\n if (\n logic.in[1].every(el => typeof el === 'string') ||\n logic.in[1].every(el => typeof el === 'number') ||\n logic.in[1].every(el => typeof el === 'boolean')\n ) {\n value = listsAsArrays\n ? logic.in[1]\n : joinWith(\n logic.in[1].map(el => `${el}`),\n ','\n );\n }\n }\n\n // istanbul ignore else\n if (value.length > 0) {\n rule = { field, operator, value, valueSource };\n }\n }\n\n return rule ? (outermost ? { combinator: 'and', rules: [rule] } : rule) : false;\n }\n\n const prepare = options.generateIDs ? prepareRuleGroup : <T>(g: T) => g;\n\n let logicRoot = rqbJsonLogic;\n if (typeof rqbJsonLogic === 'string') {\n try {\n logicRoot = JSON.parse(rqbJsonLogic);\n } catch {\n return prepare(emptyRuleGroup);\n }\n }\n\n const result = processLogic(logicRoot, true);\n const finalQuery: DefaultRuleGroupType = result || emptyRuleGroup;\n return prepare(\n options.independentCombinators ? convertToIC<DefaultRuleGroupTypeIC>(finalQuery) : finalQuery\n );\n}\n\nexport { parseJsonLogic };\n"],"mappings":";;;;;;;;AA2BA,MAAa,kBACX,UAEA,OAAO,MAAM,IAAI,SAAS;AAC5B,MAAa,qBAAqB,UAChC,eAAe,MAAM,IAAI,OAAO,MAAM,QAAQ;AAChD,MAAa,oBAAoB,UAC/B,OAAO,MAAM,IAAI,QAAQ;AAC3B,MAAa,0BAA0B,UACrC,OAAO,MAAM,IAAI,SAAS;AAC5B,MAAa,uBAAuB,UAClC,OAAO,MAAM,IAAI,QAAQ;AAC3B,MAAa,6BAA6B,UACxC,OAAO,MAAM,IAAI,SAAS;AAC5B,MAAa,uBAAuB,UAClC,OAAO,MAAM,IAAI,OAAO;AAC1B,MAAa,6BAA6B,UACxC,OAAO,MAAM,IAAI,QAAQ;AAC3B,MAAa,iBACX,UAEA,OAAO,MAAM,IA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-0,0 +1,79 @@
|
|
|
1
|
+
import { DefaultRuleGroupType, DefaultRuleGroupTypeIC, Except, RuleGroupType, RuleType } from "./basic-BfD-7CN3.mjs";
|
|
2
|
+
import { ParserCommonOptions } from "./import-BwbbP4oU.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/parseMongoDB/parseMongoDB.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Options object for {@link parseMongoDB}.
|
|
8
|
+
*/
|
|
9
|
+
interface ParseMongoDbOptions extends ParserCommonOptions {
|
|
10
|
+
/**
|
|
11
|
+
* When `true`, MongoDB rules in the form of `{ fieldName: { $not: { <...rule> } } }`
|
|
12
|
+
* will be parsed into a rule group with the `not` attribute set to `true`. By default
|
|
13
|
+
* (i.e., when this attribute is `false`), such "`$not`" rules will be parsed into a
|
|
14
|
+
* rule with a negated operator.
|
|
15
|
+
*
|
|
16
|
+
* For example, with `preventOperatorNegation` set to `true`, a MongoDB rule like this...
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* { fieldName: { $not: { $eq: 1 } } }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* ...would yield a rule group like this:
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* {
|
|
26
|
+
* combinator: 'and',
|
|
27
|
+
* not: true,
|
|
28
|
+
* rules: [{ field: 'fieldName', operator: '=', value: 1 }]
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* By default, the same MongoDB rule would yield a rule like this:
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* { field: 'fieldName', operator: '!=', value: 1 }
|
|
36
|
+
* // negated operator ^
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
41
|
+
preventOperatorNegation?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Map of additional operators to their respective processing functions. Operators
|
|
44
|
+
* must begin with `"$"`. Processing functions should return either a {@link index!RuleType RuleType}
|
|
45
|
+
* or {@link index!RuleGroupType RuleGroupType}.
|
|
46
|
+
*
|
|
47
|
+
* (The functions should _not_ return {@link index!RuleGroupTypeIC RuleGroupTypeIC}, even if using independent
|
|
48
|
+
* combinators. If the `independentCombinators` option is `true`, `parseMongoDB`
|
|
49
|
+
* will convert the final query to {@link index!RuleGroupTypeIC RuleGroupTypeIC} before returning it.)
|
|
50
|
+
*
|
|
51
|
+
* @default {}
|
|
52
|
+
*/
|
|
53
|
+
additionalOperators?: Record<`$${string}`, (field: string, operator: string, value: any, options: ParserCommonOptions) => RuleType | RuleGroupType>;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Converts a MongoDB query object or parseable string into a query suitable
|
|
57
|
+
* for the {@link index!QueryBuilder QueryBuilder} component's `query` or `defaultQuery` props
|
|
58
|
+
* ({@link index!DefaultRuleGroupType DefaultRuleGroupType}).
|
|
59
|
+
*/
|
|
60
|
+
declare function parseMongoDB(mongoDbRules: string | Record<string, any>): DefaultRuleGroupType;
|
|
61
|
+
/**
|
|
62
|
+
* Converts a MongoDB query object or parseable string into a query suitable
|
|
63
|
+
* for the {@link index!QueryBuilder QueryBuilder} component's `query` or `defaultQuery` props
|
|
64
|
+
* ({@link index!DefaultRuleGroupType DefaultRuleGroupType}).
|
|
65
|
+
*/
|
|
66
|
+
declare function parseMongoDB(mongoDbRules: string | Record<string, any>, options: Except<ParseMongoDbOptions, "independentCombinators"> & {
|
|
67
|
+
independentCombinators?: false;
|
|
68
|
+
}): DefaultRuleGroupType;
|
|
69
|
+
/**
|
|
70
|
+
* Converts a MongoDB query object or parseable string into a query suitable
|
|
71
|
+
* for the {@link index!QueryBuilder QueryBuilder} component's `query` or `defaultQuery` props
|
|
72
|
+
* ({@link index!DefaultRuleGroupTypeIC DefaultRuleGroupTypeIC}).
|
|
73
|
+
*/
|
|
74
|
+
declare function parseMongoDB(mongoDbRules: string | Record<string, any>, options: Except<ParseMongoDbOptions, "independentCombinators"> & {
|
|
75
|
+
independentCombinators: true;
|
|
76
|
+
}): DefaultRuleGroupTypeIC;
|
|
77
|
+
//#endregion
|
|
78
|
+
export { ParseMongoDbOptions, parseMongoDB };
|
|
79
|
+
//# sourceMappingURL=parseMongoDB.d.mts.map
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { defaultOperatorNegationMap, joinWith } from "./arrayUtils-BF1P8iHS.mjs";
|
|
2
|
+
import { isPojo, isRuleGroupType } from "./isRuleGroup-CnhYpLOM.mjs";
|
|
3
|
+
import "./optGroupUtils-Duv-M8rf.mjs";
|
|
4
|
+
import { convertToIC } from "./convertQuery-H7RhQiIc.mjs";
|
|
5
|
+
import { fieldIsValidUtil, getFieldsArray, prepareRuleGroup } from "./prepareQueryObjects-CS6Wmhmf.mjs";
|
|
6
|
+
import { objectKeys } from "./objectUtils-BtWdcZVG.mjs";
|
|
7
|
+
|
|
8
|
+
//#region src/utils/parseMongoDB/utils.ts
|
|
9
|
+
const getRegExStr = (re) => typeof re === "string" ? re : re.source;
|
|
10
|
+
const isPrimitive = (v) => typeof v === "string" || typeof v === "number" || typeof v === "boolean";
|
|
11
|
+
const mongoDbToRqbOperatorMap = {
|
|
12
|
+
$eq: "=",
|
|
13
|
+
$ne: "!=",
|
|
14
|
+
$gt: ">",
|
|
15
|
+
$gte: ">=",
|
|
16
|
+
$lt: "<",
|
|
17
|
+
$lte: "<="
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/utils/parseMongoDB/parseMongoDB.ts
|
|
22
|
+
const emptyRuleGroup = {
|
|
23
|
+
combinator: "and",
|
|
24
|
+
rules: []
|
|
25
|
+
};
|
|
26
|
+
function parseMongoDB(mongoDbRules, options = {}) {
|
|
27
|
+
const listsAsArrays = !!options.listsAsArrays;
|
|
28
|
+
const fieldsFlat = getFieldsArray(options.fields);
|
|
29
|
+
const getValueSources = options.getValueSources;
|
|
30
|
+
const additionalOperators = options.additionalOperators ?? {};
|
|
31
|
+
const preventOperatorNegation = !!options.preventOperatorNegation;
|
|
32
|
+
const { additionalOperators: _ao,...otherOptions } = options;
|
|
33
|
+
const fieldIsValid = (fieldName, operator, subordinateFieldName) => fieldIsValidUtil({
|
|
34
|
+
fieldName,
|
|
35
|
+
fieldsFlat,
|
|
36
|
+
operator,
|
|
37
|
+
subordinateFieldName,
|
|
38
|
+
getValueSources
|
|
39
|
+
});
|
|
40
|
+
function processMongoDbQueryBooleanOperator(field, mdbOperator, keyValue) {
|
|
41
|
+
let operator = "=";
|
|
42
|
+
let value = "";
|
|
43
|
+
// istanbul ignore else
|
|
44
|
+
if (mdbOperator === "$eq" || mdbOperator === "$ne" || mdbOperator === "$gt" || mdbOperator === "$gte" || mdbOperator === "$lt" || mdbOperator === "$lte") if (mdbOperator === "$ne" && keyValue === null) {
|
|
45
|
+
if (fieldIsValid(field, "notNull")) return {
|
|
46
|
+
field,
|
|
47
|
+
operator: "notNull",
|
|
48
|
+
value: null
|
|
49
|
+
};
|
|
50
|
+
} else {
|
|
51
|
+
operator = mongoDbToRqbOperatorMap[mdbOperator];
|
|
52
|
+
if (fieldIsValid(field, operator)) return {
|
|
53
|
+
field,
|
|
54
|
+
operator,
|
|
55
|
+
value: keyValue
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
else if (mdbOperator === "$regex" && /^[^$^]$|^[^^].*[^$]$/.test(getRegExStr(keyValue))) {
|
|
59
|
+
if (fieldIsValid(field, "contains")) return {
|
|
60
|
+
field,
|
|
61
|
+
operator: "contains",
|
|
62
|
+
value: getRegExStr(keyValue)
|
|
63
|
+
};
|
|
64
|
+
} else if (mdbOperator === "$regex" && /^\^.*[^$]/.test(getRegExStr(keyValue))) {
|
|
65
|
+
if (fieldIsValid(field, "beginsWith")) return {
|
|
66
|
+
field,
|
|
67
|
+
operator: "beginsWith",
|
|
68
|
+
value: getRegExStr(keyValue).replace(/^\^/, "")
|
|
69
|
+
};
|
|
70
|
+
} else if (mdbOperator === "$regex" && /[^^].*\$/.test(getRegExStr(keyValue))) {
|
|
71
|
+
if (fieldIsValid(field, "endsWith")) return {
|
|
72
|
+
field,
|
|
73
|
+
operator: "endsWith",
|
|
74
|
+
value: getRegExStr(keyValue).replace(/\$$/, "")
|
|
75
|
+
};
|
|
76
|
+
} else if (mdbOperator === "$in" && Array.isArray(keyValue)) {
|
|
77
|
+
if (fieldIsValid(field, "in")) {
|
|
78
|
+
value = listsAsArrays ? keyValue : joinWith(keyValue.map((v) => `${v}`), ",");
|
|
79
|
+
return {
|
|
80
|
+
field,
|
|
81
|
+
operator: "in",
|
|
82
|
+
value
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
} else if (mdbOperator === "$nin" && Array.isArray(keyValue) && fieldIsValid(field, "notIn")) {
|
|
86
|
+
value = listsAsArrays ? keyValue : joinWith(keyValue.map((v) => `${v}`), ",");
|
|
87
|
+
return {
|
|
88
|
+
field,
|
|
89
|
+
operator: "notIn",
|
|
90
|
+
value
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
function processMongoDbQueryObjectKey(key, keyValue) {
|
|
96
|
+
let field = "";
|
|
97
|
+
// istanbul ignore else
|
|
98
|
+
if (key === "$and") {
|
|
99
|
+
if (!Array.isArray(keyValue) || keyValue.length === 0 || !keyValue.every((v) => isPojo(v))) return false;
|
|
100
|
+
if (keyValue.length === 2 && keyValue.every((kv) => objectKeys(kv).length === 1)) {
|
|
101
|
+
const [rule1, rule2] = keyValue;
|
|
102
|
+
const [ruleKey1, ruleKey2] = keyValue.map((kv) => objectKeys(kv)[0]);
|
|
103
|
+
if (ruleKey1 === ruleKey2 && isPojo(rule1[ruleKey1]) && objectKeys(rule1[ruleKey1]).length === 1 && isPojo(rule2[ruleKey2]) && objectKeys(rule2[ruleKey2]).length === 1 && ("$gte" in rule1[ruleKey1] && "$lte" in rule2[ruleKey2] && rule2[ruleKey2].$lte >= rule1[ruleKey1].$gte || "$lte" in rule1[ruleKey1] && "$gte" in rule2[ruleKey2] && rule1[ruleKey1].$lte >= rule2[ruleKey2].$gte)) {
|
|
104
|
+
const [val1, val2] = [rule1[ruleKey1].$gte ?? rule1[ruleKey1].$lte, rule2[ruleKey2].$lte ?? rule2[ruleKey2].$gte];
|
|
105
|
+
let value = listsAsArrays ? [val1, val2] : joinWith([val1, val2], ",");
|
|
106
|
+
if (val1 > val2) value = listsAsArrays ? [val2, val1] : joinWith([val2, val1], ",");
|
|
107
|
+
return {
|
|
108
|
+
field: ruleKey1,
|
|
109
|
+
operator: "between",
|
|
110
|
+
value
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const rules = keyValue.map((l) => processMongoDbQueryObject(l)).filter(Boolean);
|
|
115
|
+
return rules.length > 0 ? {
|
|
116
|
+
combinator: "and",
|
|
117
|
+
rules
|
|
118
|
+
} : false;
|
|
119
|
+
} else if (key === "$or") {
|
|
120
|
+
if (!Array.isArray(keyValue) || keyValue.length === 0 || !keyValue.every((v) => isPojo(v))) return false;
|
|
121
|
+
if (keyValue.length === 2 && keyValue.every((kv) => objectKeys(kv).length === 1)) {
|
|
122
|
+
const [rule1, rule2] = keyValue;
|
|
123
|
+
const [ruleKey1, ruleKey2] = keyValue.map((kv) => objectKeys(kv)[0]);
|
|
124
|
+
if (ruleKey1 === ruleKey2 && isPojo(rule1[ruleKey1]) && objectKeys(rule1[ruleKey1]).length === 1 && isPojo(rule2[ruleKey2]) && objectKeys(rule2[ruleKey2]).length === 1 && ("$gt" in rule1[ruleKey1] && "$lt" in rule2[ruleKey2] && rule1[ruleKey1].$gt >= rule2[ruleKey2].$lt || "$lt" in rule1[ruleKey1] && "$gt" in rule2[ruleKey2] && rule2[ruleKey2].$gt >= rule1[ruleKey1].$lt)) {
|
|
125
|
+
const [val1, val2] = [rule1[ruleKey1].$gt ?? rule1[ruleKey1].$lt, rule2[ruleKey2].$lt ?? rule2[ruleKey2].$gt];
|
|
126
|
+
let value = listsAsArrays ? [val1, val2] : `${val1},${val2}`;
|
|
127
|
+
if (val1 > val2) value = listsAsArrays ? [val2, val1] : `${val2},${val1}`;
|
|
128
|
+
return {
|
|
129
|
+
field: ruleKey1,
|
|
130
|
+
operator: "notBetween",
|
|
131
|
+
value
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const rules = keyValue.map((l) => processMongoDbQueryObject(l)).filter(Boolean);
|
|
136
|
+
return rules.length > 0 ? {
|
|
137
|
+
combinator: "or",
|
|
138
|
+
rules
|
|
139
|
+
} : false;
|
|
140
|
+
} else if (key === "$not" && isPojo(keyValue)) {
|
|
141
|
+
const ruleOrGroup = processMongoDbQueryObject(keyValue);
|
|
142
|
+
if (ruleOrGroup) {
|
|
143
|
+
if (isRuleGroupType(ruleOrGroup)) return ruleOrGroup.not ? {
|
|
144
|
+
combinator: "and",
|
|
145
|
+
rules: [ruleOrGroup],
|
|
146
|
+
not: true
|
|
147
|
+
} : {
|
|
148
|
+
...ruleOrGroup,
|
|
149
|
+
not: true
|
|
150
|
+
};
|
|
151
|
+
return preventOperatorNegation ? {
|
|
152
|
+
combinator: "and",
|
|
153
|
+
rules: [ruleOrGroup],
|
|
154
|
+
not: true
|
|
155
|
+
} : {
|
|
156
|
+
...ruleOrGroup,
|
|
157
|
+
operator: defaultOperatorNegationMap[ruleOrGroup.operator]
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
return false;
|
|
161
|
+
} else if (key === "$expr") {
|
|
162
|
+
const op = objectKeys(keyValue)[0];
|
|
163
|
+
if (/^\$(eq|gte?|lte?|n?in)$/.test(op) && Array.isArray(keyValue[op]) && keyValue[op].length === 2 && typeof keyValue[op][0] === "string" && keyValue[op][0].startsWith("$")) {
|
|
164
|
+
field = keyValue[op][0].replace(/^\$/, "");
|
|
165
|
+
const val = keyValue[op][1];
|
|
166
|
+
if (typeof val === "string" && val.startsWith("$") || Array.isArray(val) && val.every((v) => typeof v === "string") && val.every((v) => v.startsWith("$"))) {
|
|
167
|
+
const valForProcessing = Array.isArray(val) ? val.map((v) => v.replace(/^\$/, "")) : val.replace(/^\$/, "");
|
|
168
|
+
const tempRule = processMongoDbQueryBooleanOperator(field, op, valForProcessing);
|
|
169
|
+
if (tempRule) {
|
|
170
|
+
if (typeof tempRule.value === "string" && !fieldIsValid(field, tempRule.operator, tempRule.value)) return false;
|
|
171
|
+
return {
|
|
172
|
+
...tempRule,
|
|
173
|
+
valueSource: "field"
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return processMongoDbQueryBooleanOperator(field, op, keyValue[op][1]);
|
|
178
|
+
}
|
|
179
|
+
} else if (/^[^$]/.test(key)) {
|
|
180
|
+
field = key;
|
|
181
|
+
if (isPrimitive(keyValue)) {
|
|
182
|
+
if (fieldIsValid(field, "=")) return {
|
|
183
|
+
field,
|
|
184
|
+
operator: "=",
|
|
185
|
+
value: keyValue
|
|
186
|
+
};
|
|
187
|
+
} else if (keyValue === null) {
|
|
188
|
+
if (fieldIsValid(field, "null")) return {
|
|
189
|
+
field,
|
|
190
|
+
operator: "null",
|
|
191
|
+
value: keyValue
|
|
192
|
+
};
|
|
193
|
+
} else if (isPojo(keyValue)) {
|
|
194
|
+
let betweenRule = false;
|
|
195
|
+
let notRule = false;
|
|
196
|
+
const allOps = [
|
|
197
|
+
"eq",
|
|
198
|
+
"ne",
|
|
199
|
+
"gte?",
|
|
200
|
+
"lte?",
|
|
201
|
+
"n?in",
|
|
202
|
+
"regex",
|
|
203
|
+
"not",
|
|
204
|
+
...objectKeys(additionalOperators).map((o) => o.replace(/^\$/, ""))
|
|
205
|
+
];
|
|
206
|
+
const acceptedOpsRegExp = /* @__PURE__ */ new RegExp(`^\\$(${allOps.join("|")})$`);
|
|
207
|
+
const operators = objectKeys(keyValue).filter((o) => acceptedOpsRegExp.test(o)).sort();
|
|
208
|
+
if (operators.length === 0) return false;
|
|
209
|
+
if ("$not" in keyValue && isPojo(keyValue.$not)) {
|
|
210
|
+
const invertedNotRule = processMongoDbQueryObject({ [field]: keyValue.$not });
|
|
211
|
+
if (invertedNotRule) if (isRuleGroupType(invertedNotRule)) notRule = {
|
|
212
|
+
...invertedNotRule,
|
|
213
|
+
not: true
|
|
214
|
+
};
|
|
215
|
+
else notRule = preventOperatorNegation ? {
|
|
216
|
+
combinator: "and",
|
|
217
|
+
rules: [invertedNotRule],
|
|
218
|
+
not: true
|
|
219
|
+
} : {
|
|
220
|
+
...invertedNotRule,
|
|
221
|
+
operator: defaultOperatorNegationMap[invertedNotRule.operator]
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
if ("$gte" in keyValue && "$lte" in keyValue) betweenRule = {
|
|
225
|
+
field,
|
|
226
|
+
operator: "between",
|
|
227
|
+
value: listsAsArrays ? [keyValue.$gte, keyValue.$lte] : `${keyValue.$gte},${keyValue.$lte}`
|
|
228
|
+
};
|
|
229
|
+
const rules = operators.filter((op) => !(notRule && op === "$not")).filter((op) => !(betweenRule && (op === "$gte" || op === "$lte"))).map((op) => op in additionalOperators && typeof additionalOperators[op] === "function" ? additionalOperators[op](field, op, keyValue[op], otherOptions) : processMongoDbQueryBooleanOperator(field, op, keyValue[op])).filter(Boolean);
|
|
230
|
+
if (notRule) rules.unshift(notRule);
|
|
231
|
+
if (betweenRule) rules.unshift(betweenRule);
|
|
232
|
+
if (rules.length === 0) return false;
|
|
233
|
+
if (rules.length === 1) return rules[0];
|
|
234
|
+
return {
|
|
235
|
+
combinator: "and",
|
|
236
|
+
rules
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
function processMongoDbQueryObject(mongoDbQueryObject) {
|
|
243
|
+
const rules = objectKeys(mongoDbQueryObject).map((k) => processMongoDbQueryObjectKey(k, mongoDbQueryObject[k])).filter(Boolean);
|
|
244
|
+
return rules.length === 1 ? rules[0] : rules.length > 1 ? {
|
|
245
|
+
combinator: "and",
|
|
246
|
+
rules
|
|
247
|
+
} : false;
|
|
248
|
+
}
|
|
249
|
+
const prepare = options.generateIDs ? prepareRuleGroup : (g) => g;
|
|
250
|
+
let mongoDbPOJO = mongoDbRules;
|
|
251
|
+
if (typeof mongoDbRules === "string") try {
|
|
252
|
+
mongoDbPOJO = JSON.parse(mongoDbRules);
|
|
253
|
+
} catch {
|
|
254
|
+
return prepare(emptyRuleGroup);
|
|
255
|
+
}
|
|
256
|
+
if (!isPojo(mongoDbPOJO)) return prepare(emptyRuleGroup);
|
|
257
|
+
const result = processMongoDbQueryObject(mongoDbPOJO);
|
|
258
|
+
const finalQuery = result ? isRuleGroupType(result) ? result : {
|
|
259
|
+
combinator: "and",
|
|
260
|
+
rules: [result]
|
|
261
|
+
} : emptyRuleGroup;
|
|
262
|
+
return prepare(options.independentCombinators ? convertToIC(finalQuery) : finalQuery);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
//#endregion
|
|
266
|
+
export { parseMongoDB };
|
|
267
|
+
//# sourceMappingURL=parseMongoDB.mjs.map
|