@fibery/expression-utils 9.7.3 → 10.0.1

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.
Files changed (83) hide show
  1. package/lib/_virtual/_rolldown/runtime.js +13 -0
  2. package/lib/context-variables.d.ts +30 -0
  3. package/lib/context-variables.js +116 -0
  4. package/lib/errors.d.ts +6 -0
  5. package/lib/errors.js +16 -0
  6. package/lib/field-expression-visitor-type-aware.d.ts +25 -0
  7. package/lib/field-expression-visitor-type-aware.js +120 -0
  8. package/lib/filter-expression/index.d.ts +13 -0
  9. package/lib/filter-expression/index.js +36 -0
  10. package/lib/filter-expression/operator-utils.d.ts +15 -0
  11. package/lib/filter-expression/operator-utils.js +206 -0
  12. package/lib/filter-expression/operators.d.ts +113 -0
  13. package/lib/filter-expression/operators.js +1151 -0
  14. package/lib/filter-expression/pattern-match-placeholders.d.ts +83 -0
  15. package/lib/filter-expression/pattern-match-placeholders.js +126 -0
  16. package/lib/filter-expression/pattern-match.d.ts +21 -0
  17. package/lib/filter-expression/pattern-match.js +126 -0
  18. package/lib/filter-expression/pattern-to-expression.d.ts +17 -0
  19. package/lib/filter-expression/pattern-to-expression.js +44 -0
  20. package/lib/filter-expression/types.d.ts +33 -0
  21. package/lib/filter-expression/types.js +0 -0
  22. package/lib/index.d.ts +191 -585
  23. package/lib/index.js +15 -50
  24. package/lib/params-placeholders.d.ts +58 -0
  25. package/lib/params-placeholders.js +215 -0
  26. package/lib/tsfixme.d.ts +4 -0
  27. package/lib/tsfixme.js +0 -0
  28. package/lib/types.d.ts +65 -0
  29. package/lib/types.js +0 -0
  30. package/lib/utils.d.ts +63 -0
  31. package/lib/utils.js +302 -0
  32. package/lib/visitors.d.ts +50 -0
  33. package/lib/visitors.js +327 -0
  34. package/package.json +13 -32
  35. package/lib/index.js.map +0 -1
  36. package/lib/src/context-variables.d.ts +0 -25
  37. package/lib/src/context-variables.js +0 -148
  38. package/lib/src/context-variables.js.map +0 -1
  39. package/lib/src/errors.d.ts +0 -3
  40. package/lib/src/errors.js +0 -16
  41. package/lib/src/errors.js.map +0 -1
  42. package/lib/src/field-expression-visitor-type-aware.d.ts +0 -23
  43. package/lib/src/field-expression-visitor-type-aware.js +0 -135
  44. package/lib/src/field-expression-visitor-type-aware.js.map +0 -1
  45. package/lib/src/filter-expression/index.d.ts +0 -10
  46. package/lib/src/filter-expression/index.js +0 -49
  47. package/lib/src/filter-expression/index.js.map +0 -1
  48. package/lib/src/filter-expression/operator-utils.d.ts +0 -12
  49. package/lib/src/filter-expression/operator-utils.js +0 -200
  50. package/lib/src/filter-expression/operator-utils.js.map +0 -1
  51. package/lib/src/filter-expression/operators.d.ts +0 -108
  52. package/lib/src/filter-expression/operators.js +0 -867
  53. package/lib/src/filter-expression/operators.js.map +0 -1
  54. package/lib/src/filter-expression/pattern-match-placeholders.d.ts +0 -81
  55. package/lib/src/filter-expression/pattern-match-placeholders.js +0 -157
  56. package/lib/src/filter-expression/pattern-match-placeholders.js.map +0 -1
  57. package/lib/src/filter-expression/pattern-match.d.ts +0 -19
  58. package/lib/src/filter-expression/pattern-match.js +0 -138
  59. package/lib/src/filter-expression/pattern-match.js.map +0 -1
  60. package/lib/src/filter-expression/pattern-to-expression.d.ts +0 -15
  61. package/lib/src/filter-expression/pattern-to-expression.js +0 -47
  62. package/lib/src/filter-expression/pattern-to-expression.js.map +0 -1
  63. package/lib/src/filter-expression/types.d.ts +0 -30
  64. package/lib/src/filter-expression/types.js +0 -3
  65. package/lib/src/filter-expression/types.js.map +0 -1
  66. package/lib/src/params-placeholders.d.ts +0 -54
  67. package/lib/src/params-placeholders.js +0 -208
  68. package/lib/src/params-placeholders.js.map +0 -1
  69. package/lib/src/tsfixme.d.ts +0 -1
  70. package/lib/src/tsfixme.js +0 -3
  71. package/lib/src/tsfixme.js.map +0 -1
  72. package/lib/src/types.d.ts +0 -63
  73. package/lib/src/types.js +0 -3
  74. package/lib/src/types.js.map +0 -1
  75. package/lib/src/utils.d.ts +0 -58
  76. package/lib/src/utils.js +0 -363
  77. package/lib/src/utils.js.map +0 -1
  78. package/lib/src/visitors.d.ts +0 -44
  79. package/lib/src/visitors.js +0 -386
  80. package/lib/src/visitors.js.map +0 -1
  81. package/lib/test-data/schema-mock.d.ts +0 -756
  82. package/lib/test-data/schema-mock.js +0 -139
  83. package/lib/test-data/schema-mock.js.map +0 -1
@@ -0,0 +1,13 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __defProp = Object.defineProperty;
3
+ var __exportAll = (all, no_symbols) => {
4
+ let target = {};
5
+ for (var name in all) __defProp(target, name, {
6
+ get: all[name],
7
+ enumerable: true
8
+ });
9
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
10
+ return target;
11
+ };
12
+ //#endregion
13
+ export { __exportAll };
@@ -0,0 +1,30 @@
1
+ import { Schema, TypeObject } from "@fibery/schema";
2
+ declare namespace context_variables_d_exports {
3
+ export { ContextVariable, currentUserContextVariableId, getAutomationButtonContextVariables, getAutomationTriggersContextVariables, getEntityQueryVariables, getTriggerCollectionFieldObject, previousTriggeredEntityContextVariableId, triggeredCollectionItemContextVariableId, triggeredEntityContextVariableId };
4
+ }
5
+ type ContextVariable = {
6
+ description: string;
7
+ id: string;
8
+ title: string;
9
+ typeObject: TypeObject;
10
+ isCollection: boolean;
11
+ };
12
+ declare const triggeredEntityContextVariableId = "triggeredEntity";
13
+ declare const previousTriggeredEntityContextVariableId = "previousTriggeredEntity";
14
+ declare const currentUserContextVariableId = "currentUser";
15
+ declare const triggeredCollectionItemContextVariableId = "triggered-collection-item";
16
+ declare const getAutomationButtonContextVariables: (typeId: string, fiberySchema: Schema) => Array<ContextVariable>;
17
+ declare const getTriggerCollectionFieldObject: (typeId: string, collectionFieldId: string, fiberySchema: Schema) => import("@fibery/schema").FieldObject | undefined;
18
+ declare const getAutomationTriggersContextVariables: (triggers: Array<{
19
+ trigger: string;
20
+ args: Record<string, unknown>;
21
+ }>, typeId: string, fiberySchema: Schema) => Array<ContextVariable>;
22
+ declare const getEntityQueryVariables: (schema: Schema) => {
23
+ typeObject: TypeObject;
24
+ id: string;
25
+ title: string;
26
+ isCollection: boolean;
27
+ description: string;
28
+ }[];
29
+ //#endregion
30
+ export { ContextVariable, context_variables_d_exports, currentUserContextVariableId, getAutomationButtonContextVariables, getAutomationTriggersContextVariables, getEntityQueryVariables, getTriggerCollectionFieldObject, previousTriggeredEntityContextVariableId, triggeredCollectionItemContextVariableId, triggeredEntityContextVariableId };
@@ -0,0 +1,116 @@
1
+ import { __exportAll } from "./_virtual/_rolldown/runtime.js";
2
+ import flatten from "lodash/flatten.js";
3
+ import groupBy from "lodash/groupBy.js";
4
+ import uniqBy from "lodash/uniqBy.js";
5
+ //#region src/context-variables.ts
6
+ var context_variables_exports = /* @__PURE__ */ __exportAll({
7
+ currentUserContextVariableId: () => currentUserContextVariableId,
8
+ getAutomationButtonContextVariables: () => getAutomationButtonContextVariables,
9
+ getAutomationTriggersContextVariables: () => getAutomationTriggersContextVariables,
10
+ getEntityQueryVariables: () => getEntityQueryVariables,
11
+ getTriggerCollectionFieldObject: () => getTriggerCollectionFieldObject,
12
+ previousTriggeredEntityContextVariableId: () => previousTriggeredEntityContextVariableId,
13
+ triggeredCollectionItemContextVariableId: () => triggeredCollectionItemContextVariableId,
14
+ triggeredEntityContextVariableId: () => triggeredEntityContextVariableId
15
+ });
16
+ const triggeredEntityContextVariableId = "triggeredEntity";
17
+ const previousTriggeredEntityContextVariableId = "previousTriggeredEntity";
18
+ const currentUserContextVariableId = "currentUser";
19
+ const triggeredCollectionItemContextVariableId = "triggered-collection-item";
20
+ const getAutomationButtonContextVariables = (typeId, fiberySchema) => {
21
+ if (!fiberySchema) return [];
22
+ const userTypeObject = fiberySchema.typeObjectsByName["fibery/user"];
23
+ const typeObject = fiberySchema.typeObjectsById[typeId];
24
+ return [{
25
+ id: triggeredEntityContextVariableId,
26
+ title: `Step 1 ${typeObject.singularTitle}`,
27
+ typeObject,
28
+ isCollection: false,
29
+ description: `The ${typeObject.singularTitle} for which the Button was clicked`
30
+ }, {
31
+ id: currentUserContextVariableId,
32
+ title: `User who clicked Button`,
33
+ typeObject: userTypeObject,
34
+ isCollection: false,
35
+ description: `The User who triggered this automation by clicking a Button.`
36
+ }];
37
+ };
38
+ const getTriggerCollectionFieldObject = (typeId, collectionFieldId, fiberySchema) => {
39
+ if (!fiberySchema) return;
40
+ if (!Object.hasOwn(fiberySchema.typeObjectsById, typeId)) return;
41
+ if (!Object.hasOwn(fiberySchema.typeObjectsById[typeId].fieldObjectsById, collectionFieldId)) return;
42
+ return fiberySchema.typeObjectsById[typeId].fieldObjectsById[collectionFieldId];
43
+ };
44
+ const getTriggerContextVariables = (trigger, typeId, fiberySchema) => {
45
+ if (!fiberySchema) return [];
46
+ const typeObject = fiberySchema.typeObjectsById[typeId];
47
+ const userTypeObject = fiberySchema.typeObjectsByName["fibery/user"];
48
+ const triggeredEntityVar = {
49
+ id: triggeredEntityContextVariableId,
50
+ title: `Step 1 ${typeObject.singularTitle}`,
51
+ typeObject,
52
+ isCollection: false,
53
+ description: `The ${typeObject.singularTitle} from step 1 of this Automation Rule`
54
+ };
55
+ const currentUserVar = {
56
+ id: currentUserContextVariableId,
57
+ title: `User who triggered Rule`,
58
+ typeObject: userTypeObject,
59
+ isCollection: false,
60
+ description: `The User who made the change which triggered this Automation Rule. Empty in case the change was made by a formula or an integration.`
61
+ };
62
+ if (trigger.trigger === "time-based") return [triggeredEntityVar];
63
+ else if (trigger.trigger === "created") return typeObject.syncSource !== null ? [triggeredEntityVar] : [triggeredEntityVar, currentUserVar];
64
+ else if (trigger.trigger === "updated") {
65
+ const result = [triggeredEntityVar];
66
+ if (!(trigger.args.updatedField && Array.isArray(trigger.args.updatedField) && trigger.args.updatedField.some((fieldId) => {
67
+ if (!Object.hasOwn(typeObject.fieldObjectsById, fieldId)) return false;
68
+ const fieldObject = typeObject.fieldObjectsById[fieldId];
69
+ return fieldObject?.isReadOnly || fieldObject?.type === "Collaboration~Documents/Document";
70
+ }))) result.push(currentUserVar);
71
+ if (trigger.args.updatedField && Array.isArray(trigger.args.updatedField) && trigger.args.updatedField.length === 1) {
72
+ const previousTriggeredEntityContextVariable = {
73
+ id: previousTriggeredEntityContextVariableId,
74
+ title: `Step 1 ${typeObject.singularTitle} (Previous)`,
75
+ typeObject,
76
+ isCollection: false,
77
+ description: `The previous value of ${typeObject.singularTitle} from step 1 of this Automation Rule`
78
+ };
79
+ result.push(previousTriggeredEntityContextVariable);
80
+ }
81
+ return result;
82
+ } else if (trigger.trigger === "collection-item-added" || trigger.trigger === "collection-item-removed") {
83
+ const collectionItemFieldObject = getTriggerCollectionFieldObject(typeId, trigger.args.changedCollectionField, fiberySchema);
84
+ const collectionItemTypeObject = collectionItemFieldObject?.typeObject;
85
+ const variables = [triggeredEntityVar];
86
+ collectionItemTypeObject && variables.push({
87
+ id: "triggered-collection-item",
88
+ title: `Step 1 ${trigger.trigger === "collection-item-added" ? "linked" : "unlinked"} ${collectionItemTypeObject.singularTitle}`,
89
+ typeObject: collectionItemTypeObject,
90
+ isCollection: false,
91
+ description: `The ${collectionItemTypeObject.singularTitle} from step 1 of this Automation Rule`
92
+ });
93
+ !collectionItemFieldObject?.isReadOnly && variables.push(currentUserVar);
94
+ return variables;
95
+ } else return [];
96
+ };
97
+ const getAutomationTriggersContextVariables = (triggers, typeId, fiberySchema) => {
98
+ const triggerContextVars = triggers.flatMap((trigger) => {
99
+ return getTriggerContextVariables(trigger, typeId, fiberySchema);
100
+ });
101
+ return uniqBy(triggers.length > 1 ? triggerContextVars.filter((variable) => variable.id !== previousTriggeredEntityContextVariableId) : triggerContextVars, (variable) => `${variable.id}_${variable.typeObject.id}`);
102
+ };
103
+ const getEntityQueryVariables = (schema) => {
104
+ const grouped = groupBy(schema.typeObjects.filter((x) => x.isDomain), (typeObject) => typeObject.pluralTitle);
105
+ return flatten(Object.values(grouped).map((group) => {
106
+ return group.map((typeObject) => ({
107
+ typeObject,
108
+ id: `entityQuery_${typeObject.id}`,
109
+ title: group.length > 1 ? `${typeObject.pluralTitle} (${typeObject.nameParts.namespace})` : typeObject.pluralTitle,
110
+ isCollection: true,
111
+ description: `All Entities from "${typeObject.title}" Database`
112
+ }));
113
+ }));
114
+ };
115
+ //#endregion
116
+ export { context_variables_exports, currentUserContextVariableId, getAutomationButtonContextVariables, getAutomationTriggersContextVariables, getEntityQueryVariables, getTriggerCollectionFieldObject, previousTriggeredEntityContextVariableId, triggeredCollectionItemContextVariableId, triggeredEntityContextVariableId };
@@ -0,0 +1,6 @@
1
+ //#region src/errors.d.ts
2
+ declare class NotImplementedError extends Error {
3
+ constructor(value: unknown, itemType?: string);
4
+ }
5
+ //#endregion
6
+ export { NotImplementedError };
package/lib/errors.js ADDED
@@ -0,0 +1,16 @@
1
+ //#region src/errors.ts
2
+ const stringify = (x) => {
3
+ if (x === void 0) return "undefined";
4
+ return JSON.stringify(x);
5
+ };
6
+ var NotImplementedError = class extends Error {
7
+ constructor(value, itemType) {
8
+ super([
9
+ `"${stringify(value)}"`,
10
+ itemType,
11
+ "is not implemented"
12
+ ].filter((x) => x !== void 0).join(" "));
13
+ }
14
+ };
15
+ //#endregion
16
+ export { NotImplementedError };
@@ -0,0 +1,25 @@
1
+ import { ExpressionVisitor, FieldExpression, MultiFieldAccess } from "./types.js";
2
+ import { FieldObject, TypeObject } from "@fibery/schema";
3
+ //#region src/field-expression-visitor-type-aware.d.ts
4
+ type FieldAccess = string | MultiFieldAccess;
5
+ type OnFieldResult = {
6
+ typeObject: TypeObject;
7
+ fieldAccess: FieldAccess;
8
+ } | undefined;
9
+ type OnField = (params: {
10
+ fieldAccess: FieldAccess;
11
+ fieldObject: FieldObject;
12
+ typeObject: TypeObject;
13
+ }) => OnFieldResult;
14
+ type OnFieldNotFound = (params: {
15
+ currentTypeObject: TypeObject;
16
+ expression: FieldExpression;
17
+ fieldAccess: FieldAccess;
18
+ }) => OnFieldResult;
19
+ declare const fieldAccessVisitorTypeAware: ({ typeObject, onField, onFieldNotFound }: {
20
+ typeObject: TypeObject;
21
+ onField?: OnField;
22
+ onFieldNotFound?: OnFieldNotFound;
23
+ }) => Required<ExpressionVisitor>;
24
+ //#endregion
25
+ export { fieldAccessVisitorTypeAware };
@@ -0,0 +1,120 @@
1
+ import { createExpressionVisitor, isMultiFieldAccess } from "./utils.js";
2
+ import { mapValues } from "@fibery/helpers/src/_";
3
+ import isPlainObject from "lodash/isPlainObject.js";
4
+ //#region src/field-expression-visitor-type-aware.ts
5
+ const visitFieldExpression = ({ expression: fieldExpression, typeObject: initialTypeObject, onField, onFieldNotFound }) => fieldExpression.reduce(({ typeObject, expression }, fieldAccess) => {
6
+ if (typeObject && isMultiFieldAccess(fieldAccess) && Object.hasOwn(typeObject.fieldObjectsById, fieldAccess[0])) {
7
+ const typeId = fieldAccess[1];
8
+ const fieldObject = typeObject.fieldObjectsById[fieldAccess[0]];
9
+ const nextTypeObject = fieldObject.multiRelatedFieldObjects.find((f) => f.holderTypeObject.id === typeId)?.holderTypeObject;
10
+ if (!nextTypeObject) {
11
+ const notFoundResult = onFieldNotFound({
12
+ currentTypeObject: typeObject,
13
+ expression: fieldExpression,
14
+ fieldAccess
15
+ });
16
+ return notFoundResult ? {
17
+ typeObject: notFoundResult.typeObject,
18
+ expression: [...expression, notFoundResult.fieldAccess]
19
+ } : {
20
+ typeObject,
21
+ expression
22
+ };
23
+ }
24
+ const onFieldResult = onField({
25
+ fieldAccess,
26
+ fieldObject,
27
+ typeObject: nextTypeObject
28
+ });
29
+ return onFieldResult ? {
30
+ typeObject: onFieldResult.typeObject,
31
+ expression: [...expression, onFieldResult.fieldAccess]
32
+ } : {
33
+ typeObject,
34
+ expression
35
+ };
36
+ } else if (typeObject && Object.hasOwn(typeObject.fieldObjectsById, fieldAccess)) {
37
+ const fieldObject = typeObject.fieldObjectsById[fieldAccess];
38
+ const onFieldResult = onField({
39
+ fieldAccess,
40
+ fieldObject,
41
+ typeObject: fieldObject.typeObject
42
+ });
43
+ return onFieldResult ? {
44
+ typeObject: onFieldResult.typeObject,
45
+ expression: [...expression, onFieldResult.fieldAccess]
46
+ } : {
47
+ typeObject,
48
+ expression
49
+ };
50
+ } else {
51
+ const notFoundResult = onFieldNotFound({
52
+ currentTypeObject: typeObject,
53
+ expression: fieldExpression,
54
+ fieldAccess
55
+ });
56
+ return notFoundResult ? {
57
+ typeObject: notFoundResult.typeObject,
58
+ expression: [...expression, notFoundResult.fieldAccess]
59
+ } : {
60
+ typeObject,
61
+ expression
62
+ };
63
+ }
64
+ }, {
65
+ typeObject: initialTypeObject,
66
+ expression: []
67
+ });
68
+ const defaultOnFieldNotFound = () => void 0;
69
+ const defaultOnField = () => void 0;
70
+ const fieldAccessVisitorTypeAwareInternal = ({ typeObject, rootTypeObject, onField = defaultOnField, onFieldNotFound = defaultOnFieldNotFound }) => {
71
+ return createExpressionVisitor({
72
+ visitFieldExpression: (expression) => visitFieldExpression({
73
+ typeObject,
74
+ expression,
75
+ onField,
76
+ onFieldNotFound
77
+ }).expression,
78
+ visitFromRootFieldExpression: ([fromRootKeyword, ...rest]) => [fromRootKeyword, ...rest.map((x) => fieldAccessVisitorTypeAwareInternal({
79
+ typeObject: rootTypeObject,
80
+ rootTypeObject,
81
+ onField,
82
+ onFieldNotFound
83
+ }).visitExpression(x))],
84
+ visitQueryExpression: (subQueryExpression) => {
85
+ const { "q/from": fromExpression, "q/select": selectExpression, "q/where": whereExpression, "q/order-by": orderByExpression } = subQueryExpression;
86
+ const fromVisitResult = visitFieldExpression({
87
+ typeObject,
88
+ expression: fromExpression,
89
+ onField,
90
+ onFieldNotFound
91
+ });
92
+ if (fromVisitResult?.typeObject) {
93
+ const subQueryVisitor = fieldAccessVisitorTypeAwareInternal({
94
+ typeObject: fromVisitResult?.typeObject,
95
+ rootTypeObject,
96
+ onField,
97
+ onFieldNotFound
98
+ });
99
+ return {
100
+ ...subQueryExpression,
101
+ "q/from": fromVisitResult.expression,
102
+ "q/select": isPlainObject(selectExpression) ? mapValues(selectExpression, (val) => subQueryVisitor.visitExpression(val)) : subQueryVisitor.visitExpression(selectExpression),
103
+ ...whereExpression ? { "q/where": subQueryVisitor.visitExpression(whereExpression) } : null,
104
+ ...orderByExpression ? { "q/order-by": subQueryVisitor.visitOrderByExpression(orderByExpression) } : null
105
+ };
106
+ }
107
+ return subQueryExpression;
108
+ }
109
+ });
110
+ };
111
+ const fieldAccessVisitorTypeAware = ({ typeObject, onField = defaultOnField, onFieldNotFound = defaultOnFieldNotFound }) => {
112
+ return fieldAccessVisitorTypeAwareInternal({
113
+ typeObject,
114
+ rootTypeObject: typeObject,
115
+ onField,
116
+ onFieldNotFound
117
+ });
118
+ };
119
+ //#endregion
120
+ export { fieldAccessVisitorTypeAware };
@@ -0,0 +1,13 @@
1
+ import { Expression } from "../types.js";
2
+ import { Operator } from "./types.js";
3
+ import { Schema } from "@fibery/schema";
4
+ //#region src/filter-expression/index.d.ts
5
+ declare const findMatchedFilter: (filterExpression: Expression, params: Record<string, unknown>, operators: Array<Operator>, schema: Schema | null, type: string) => {
6
+ operator: Operator;
7
+ value: any;
8
+ expression: Expression;
9
+ type: string;
10
+ id: string;
11
+ } | null;
12
+ //#endregion
13
+ export { findMatchedFilter };
@@ -0,0 +1,36 @@
1
+ import { NotImplementedError } from "../errors.js";
2
+ import { doPatternMatch } from "./pattern-match.js";
3
+ import { v4 } from "uuid";
4
+ //#region src/filter-expression/index.ts
5
+ const findMatchedFilter = (filterExpression, params, operators, schema, type) => {
6
+ if (!schema || !Object.hasOwn(schema.typeObjectsByName, type)) return null;
7
+ const typeObject = schema.typeObjectsByName[type];
8
+ for (const operator of operators) if (operator.customMatch) {
9
+ const matchResult = operator.customMatch(filterExpression, params, typeObject);
10
+ if (matchResult.matchSuccess) return {
11
+ operator,
12
+ value: matchResult.value,
13
+ expression: matchResult.expression,
14
+ type,
15
+ id: v4()
16
+ };
17
+ } else {
18
+ const { matchSuccess, matches } = doPatternMatch(filterExpression, params, operator.pattern, typeObject);
19
+ if (matchSuccess) {
20
+ const paramMatches = matches.filter((x) => x.matchType === ":expression-match-type/param");
21
+ const expressionMatches = matches.filter((x) => x.matchType === ":expression-match-type/expression");
22
+ if (paramMatches.length > 1) throw new NotImplementedError(`Operator pattern with several param placeholders`);
23
+ if (expressionMatches.length > 1) throw new NotImplementedError(`Operator pattern with several expression placeholders`);
24
+ return {
25
+ operator,
26
+ value: paramMatches[0] && paramMatches[0].value,
27
+ expression: expressionMatches[0].value,
28
+ type,
29
+ id: v4()
30
+ };
31
+ }
32
+ }
33
+ return null;
34
+ };
35
+ //#endregion
36
+ export { findMatchedFilter };
@@ -0,0 +1,15 @@
1
+ import { Expression } from "../types.js";
2
+ import { $TSFixMe } from "../tsfixme.js";
3
+ import { CustomMatchFn, Operator } from "./types.js";
4
+ //#region src/filter-expression/operator-utils.d.ts
5
+ declare const isDateTime: (dateString: string) => boolean;
6
+ declare const NO_VALUE: undefined;
7
+ declare const isNoValue: (value: unknown) => boolean;
8
+ declare const getCompareOperatorForDateTime: (id: string, title: string, operator: string, compareByStartInterval: boolean, isNegative: boolean) => Operator;
9
+ declare const getWithinOperatorCustomConvertFn: (leftPartExcluded: boolean) => (expression: Expression, value: {
10
+ withinRangeStart: unknown;
11
+ withinRangeEnd: unknown;
12
+ } | null, getNextParamName: () => string) => $TSFixMe;
13
+ declare const getWithinOperatorCustomMatchFn: (fieldPlaceholder: $TSFixMe, leftPartExcluded: boolean) => CustomMatchFn;
14
+ //#endregion
15
+ export { NO_VALUE, getCompareOperatorForDateTime, getWithinOperatorCustomConvertFn, getWithinOperatorCustomMatchFn, isDateTime, isNoValue };
@@ -0,0 +1,206 @@
1
+ import { doPatternMatch } from "./pattern-match.js";
2
+ import { dateToDateTimeIntervalLookup, monthAgoDateParamPlaceholder, monthAgoStartDateTimeParamPlaceholder, monthFromNowDateParamPlaceholder, monthFromNowEndDateTimeParamPlaceholder, paramsPlaceholdersLookup, parseRelativeDatePlaceholder, toRelativeDatePlaceholder, todayDateParamPlaceholder, todayEndDateTimeParamPlaceholder, weekAgoDateParamPlaceholder, weekAgoStartDateTimeParamPlaceholder, weekFromNowDateParamPlaceholder, weekFromNowEndDateTimeParamPlaceholder, yearAgoDateParamPlaceholder, yearAgoStartDateTimeParamPlaceholder, yearFromNowDateParamPlaceholder, yearFromNowEndDateTimeParamPlaceholder } from "../params-placeholders.js";
3
+ import { dateTimeOrDateTimeRangeExpressionPlaceholder, getParamPlaceholder } from "./pattern-match-placeholders.js";
4
+ import { isRecord } from "@fibery/helpers/src/_";
5
+ import isString from "lodash/isString.js";
6
+ import findKey from "lodash/findKey.js";
7
+ import isEqual from "lodash/isEqual.js";
8
+ //#region src/filter-expression/operator-utils.ts
9
+ const isDateTime = (dateString) => dateString.length === 24 || dateString.length === 10;
10
+ const NO_VALUE = void 0;
11
+ const isNoValue = (value) => {
12
+ if (isRecord(value) && "withinRangeStart" in value && "withinRangeEnd" in value) return value.withinRangeStart === void 0 && value.withinRangeEnd === void 0;
13
+ return value === void 0;
14
+ };
15
+ const getCompareOperatorForDateTime = (id, title, operator, compareByStartInterval, isNegative) => ({
16
+ id,
17
+ title,
18
+ customMatch: (where, params, typeObject) => {
19
+ const { matchSuccess, matches: [fieldMatch, paramMatch] } = doPatternMatch(where, params, [
20
+ operator,
21
+ dateTimeOrDateTimeRangeExpressionPlaceholder,
22
+ getParamPlaceholder((x) => {
23
+ return isString(x) && Boolean(paramsPlaceholdersLookup[x]) || isString(x) && isDateTime(x) || Boolean(parseRelativeDatePlaceholder(x)) || isNoValue(x);
24
+ })
25
+ ], typeObject);
26
+ if (matchSuccess) {
27
+ const paramValue = paramMatch.value;
28
+ const relativeDateInfo = parseRelativeDatePlaceholder(paramValue);
29
+ if (relativeDateInfo && relativeDateInfo.isStartOfInterval === compareByStartInterval) {
30
+ const value = toRelativeDatePlaceholder({
31
+ ...relativeDateInfo,
32
+ isStartOfInterval: void 0
33
+ });
34
+ return {
35
+ matchSuccess,
36
+ expression: fieldMatch.value,
37
+ value
38
+ };
39
+ } else if (paramsPlaceholdersLookup[paramValue]) {
40
+ const value = findKey(dateToDateTimeIntervalLookup, (val) => compareByStartInterval ? val.start === paramValue : val.end === paramValue);
41
+ return {
42
+ matchSuccess,
43
+ expression: fieldMatch.value,
44
+ value
45
+ };
46
+ } else return {
47
+ matchSuccess,
48
+ expression: fieldMatch.value,
49
+ value: paramValue
50
+ };
51
+ }
52
+ return { matchSuccess: false };
53
+ },
54
+ customConvertToWhereExpression: (expression, value, getNextParamName) => {
55
+ const dateTimePlaceholders = dateToDateTimeIntervalLookup[value];
56
+ let relativeDateInfo = null;
57
+ if (dateTimePlaceholders) {
58
+ const { start, end } = dateTimePlaceholders;
59
+ const intervalParamName = compareByStartInterval ? start : end;
60
+ return {
61
+ expression: [
62
+ operator,
63
+ expression,
64
+ intervalParamName
65
+ ],
66
+ params: { [intervalParamName]: intervalParamName }
67
+ };
68
+ } else if (relativeDateInfo = parseRelativeDatePlaceholder(value)) {
69
+ const paramName = toRelativeDatePlaceholder({
70
+ ...relativeDateInfo,
71
+ isStartOfInterval: compareByStartInterval,
72
+ unitForStart: "day"
73
+ });
74
+ return {
75
+ expression: [
76
+ operator,
77
+ expression,
78
+ paramName
79
+ ],
80
+ params: { [paramName]: paramName }
81
+ };
82
+ } else {
83
+ const dateParamName = getNextParamName();
84
+ return {
85
+ expression: [
86
+ operator,
87
+ expression,
88
+ dateParamName
89
+ ],
90
+ params: { [dateParamName]: value }
91
+ };
92
+ }
93
+ },
94
+ type: ":operator-type/binary",
95
+ isNegative
96
+ });
97
+ const getWithinRangeParam = ({ withinRangeValue, getNextParamName }) => {
98
+ if (parseRelativeDatePlaceholder(withinRangeValue)) return {
99
+ key: withinRangeValue,
100
+ value: withinRangeValue
101
+ };
102
+ else return {
103
+ key: getNextParamName(),
104
+ value: withinRangeValue
105
+ };
106
+ };
107
+ const getWithinOperatorCustomConvertFn = (leftPartExcluded) => {
108
+ return (expression, value, getNextParamName) => {
109
+ const { key: startParamKey, value: startParamValue } = getWithinRangeParam({
110
+ withinRangeValue: value?.withinRangeStart,
111
+ getNextParamName
112
+ });
113
+ const { key: endParamKey, value: endParamValue } = getWithinRangeParam({
114
+ withinRangeValue: value?.withinRangeEnd,
115
+ getNextParamName
116
+ });
117
+ return {
118
+ expression: [
119
+ "and",
120
+ [
121
+ leftPartExcluded ? ">" : ">=",
122
+ expression,
123
+ startParamKey
124
+ ],
125
+ [
126
+ "<=",
127
+ expression,
128
+ endParamKey
129
+ ]
130
+ ],
131
+ params: {
132
+ [startParamKey]: startParamValue,
133
+ [endParamKey]: endParamValue
134
+ }
135
+ };
136
+ };
137
+ };
138
+ const oldWithinPlaceholderToRelativeDateLookup = {
139
+ [weekAgoDateParamPlaceholder]: "$start-of-day-1-week-before-now-date",
140
+ [monthAgoDateParamPlaceholder]: "$start-of-day-1-month-before-now-date",
141
+ [yearAgoDateParamPlaceholder]: "$start-of-day-1-year-before-now-date",
142
+ [weekFromNowDateParamPlaceholder]: "$start-of-day-1-week-after-now-date",
143
+ [monthFromNowDateParamPlaceholder]: "$start-of-day-1-month-after-now-date",
144
+ [yearFromNowDateParamPlaceholder]: "$start-of-day-1-year-after-now-date",
145
+ [todayDateParamPlaceholder]: "$end-of-day-0-day-before-now-date",
146
+ [weekAgoStartDateTimeParamPlaceholder]: "$start-of-day-1-week-before-now-date-time",
147
+ [monthAgoStartDateTimeParamPlaceholder]: "$start-of-day-1-month-before-now-date-time",
148
+ [yearAgoStartDateTimeParamPlaceholder]: "$start-of-day-1-year-before-now-date-time",
149
+ [weekFromNowEndDateTimeParamPlaceholder]: "$start-of-day-1-week-after-now-date-time",
150
+ [monthFromNowEndDateTimeParamPlaceholder]: "$start-of-day-1-month-after-now-date-time",
151
+ [yearFromNowEndDateTimeParamPlaceholder]: "$start-of-day-1-year-after-now-date-time",
152
+ [todayEndDateTimeParamPlaceholder]: "$end-of-day-0-day-before-now-date-time"
153
+ };
154
+ const getWithinRangeValue = (paramMatchValue) => {
155
+ if (Object.hasOwn(oldWithinPlaceholderToRelativeDateLookup, paramMatchValue)) return oldWithinPlaceholderToRelativeDateLookup[paramMatchValue];
156
+ else return paramMatchValue;
157
+ };
158
+ const matchWithinOperator = (pattern, where, params, typeObject) => {
159
+ const { matchSuccess, matches: [firstFieldMatch, firstParamMatch, secondFieldMatch, secondParamMatch] } = doPatternMatch(where, params, pattern, typeObject);
160
+ if (matchSuccess) {
161
+ const firstFieldValue = firstFieldMatch.value;
162
+ const secondFieldValue = secondFieldMatch.value;
163
+ if (isEqual(firstFieldValue, secondFieldValue)) return {
164
+ matchSuccess,
165
+ expression: firstFieldValue,
166
+ value: {
167
+ withinRangeStart: getWithinRangeValue(firstParamMatch.value),
168
+ withinRangeEnd: getWithinRangeValue(secondParamMatch.value)
169
+ }
170
+ };
171
+ }
172
+ return { matchSuccess: false };
173
+ };
174
+ const getWithinOperatorCustomMatchFn = (fieldPlaceholder, leftPartExcluded) => {
175
+ return (where, params, typeObject) => {
176
+ const match = matchWithinOperator([
177
+ "and",
178
+ [
179
+ ">",
180
+ fieldPlaceholder,
181
+ getParamPlaceholder((x) => x !== null)
182
+ ],
183
+ [
184
+ "<=",
185
+ fieldPlaceholder,
186
+ getParamPlaceholder((x) => x !== null)
187
+ ]
188
+ ], where, params, typeObject);
189
+ if (!match.matchSuccess && !leftPartExcluded) return matchWithinOperator([
190
+ "and",
191
+ [
192
+ ">=",
193
+ fieldPlaceholder,
194
+ getParamPlaceholder((x) => x !== null)
195
+ ],
196
+ [
197
+ "<=",
198
+ fieldPlaceholder,
199
+ getParamPlaceholder((x) => x !== null)
200
+ ]
201
+ ], where, params, typeObject);
202
+ return match;
203
+ };
204
+ };
205
+ //#endregion
206
+ export { NO_VALUE, getCompareOperatorForDateTime, getWithinOperatorCustomConvertFn, getWithinOperatorCustomMatchFn, isDateTime, isNoValue };