@fibery/expression-utils 9.7.2 → 10.0.0

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 (53) hide show
  1. package/lib/_virtual/_rolldown/runtime.js +13 -0
  2. package/lib/index.d.ts +191 -585
  3. package/lib/index.js +15 -50
  4. package/lib/src/context-variables.d.ts +27 -22
  5. package/lib/src/context-variables.js +106 -138
  6. package/lib/src/errors.d.ts +5 -2
  7. package/lib/src/errors.js +14 -14
  8. package/lib/src/field-expression-visitor-type-aware.d.ts +18 -16
  9. package/lib/src/field-expression-visitor-type-aware.js +116 -131
  10. package/lib/src/filter-expression/index.d.ts +12 -9
  11. package/lib/src/filter-expression/index.js +34 -47
  12. package/lib/src/filter-expression/operator-utils.d.ts +14 -11
  13. package/lib/src/filter-expression/operator-utils.js +191 -185
  14. package/lib/src/filter-expression/operators.d.ts +111 -106
  15. package/lib/src/filter-expression/operators.js +1130 -846
  16. package/lib/src/filter-expression/pattern-match-placeholders.d.ts +63 -61
  17. package/lib/src/filter-expression/pattern-match-placeholders.js +107 -138
  18. package/lib/src/filter-expression/pattern-match.d.ts +17 -15
  19. package/lib/src/filter-expression/pattern-match.js +118 -130
  20. package/lib/src/filter-expression/pattern-to-expression.d.ts +13 -11
  21. package/lib/src/filter-expression/pattern-to-expression.js +42 -45
  22. package/lib/src/filter-expression/types.d.ts +29 -26
  23. package/lib/src/filter-expression/types.js +0 -3
  24. package/lib/src/params-placeholders.d.ts +55 -51
  25. package/lib/src/params-placeholders.js +199 -192
  26. package/lib/src/tsfixme.d.ts +4 -1
  27. package/lib/src/tsfixme.js +0 -3
  28. package/lib/src/types.d.ts +47 -45
  29. package/lib/src/types.js +0 -3
  30. package/lib/src/utils.d.ts +53 -48
  31. package/lib/src/utils.js +276 -338
  32. package/lib/src/visitors.d.ts +43 -37
  33. package/lib/src/visitors.js +306 -365
  34. package/lib/test-data/schema-mock.d.ts +68 -751
  35. package/lib/test-data/schema-mock.js +175 -139
  36. package/package.json +17 -10
  37. package/lib/index.js.map +0 -1
  38. package/lib/src/context-variables.js.map +0 -1
  39. package/lib/src/errors.js.map +0 -1
  40. package/lib/src/field-expression-visitor-type-aware.js.map +0 -1
  41. package/lib/src/filter-expression/index.js.map +0 -1
  42. package/lib/src/filter-expression/operator-utils.js.map +0 -1
  43. package/lib/src/filter-expression/operators.js.map +0 -1
  44. package/lib/src/filter-expression/pattern-match-placeholders.js.map +0 -1
  45. package/lib/src/filter-expression/pattern-match.js.map +0 -1
  46. package/lib/src/filter-expression/pattern-to-expression.js.map +0 -1
  47. package/lib/src/filter-expression/types.js.map +0 -1
  48. package/lib/src/params-placeholders.js.map +0 -1
  49. package/lib/src/tsfixme.js.map +0 -1
  50. package/lib/src/types.js.map +0 -1
  51. package/lib/src/utils.js.map +0 -1
  52. package/lib/src/visitors.js.map +0 -1
  53. package/lib/test-data/schema-mock.js.map +0 -1
@@ -1,135 +1,120 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.fieldAccessVisitorTypeAware = void 0;
7
- const _1 = require("@fibery/helpers/src/_");
8
- const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
9
- const utils_1 = require("./utils");
10
- const visitFieldExpression = ({ expression: fieldExpression, typeObject: initialTypeObject, onField, onFieldNotFound, }) => fieldExpression.reduce(({ typeObject, expression }, fieldAccess) => {
11
- if (typeObject && (0, utils_1.isMultiFieldAccess)(fieldAccess) && Object.hasOwn(typeObject.fieldObjectsById, fieldAccess[0])) {
12
- const typeId = fieldAccess[1];
13
- const fieldObject = typeObject.fieldObjectsById[fieldAccess[0]];
14
- const nextTypeObject = fieldObject.multiRelatedFieldObjects.find((f) => f.holderTypeObject.id === typeId)?.holderTypeObject;
15
- if (!nextTypeObject) {
16
- const notFoundResult = onFieldNotFound({
17
- currentTypeObject: typeObject,
18
- expression: fieldExpression,
19
- fieldAccess,
20
- });
21
- return notFoundResult
22
- ? { typeObject: notFoundResult.typeObject, expression: [...expression, notFoundResult.fieldAccess] }
23
- : {
24
- typeObject,
25
- expression,
26
- };
27
- }
28
- const onFieldResult = onField({
29
- fieldAccess,
30
- fieldObject,
31
- typeObject: nextTypeObject,
32
- });
33
- return onFieldResult
34
- ? {
35
- typeObject: onFieldResult.typeObject,
36
- expression: [...expression, onFieldResult.fieldAccess],
37
- }
38
- : {
39
- typeObject,
40
- expression,
41
- };
42
- }
43
- else if (typeObject && Object.hasOwn(typeObject.fieldObjectsById, fieldAccess)) {
44
- const fieldObject = typeObject.fieldObjectsById[fieldAccess];
45
- const onFieldResult = onField({
46
- fieldAccess,
47
- fieldObject,
48
- typeObject: fieldObject.typeObject,
49
- });
50
- return onFieldResult
51
- ? {
52
- typeObject: onFieldResult.typeObject,
53
- expression: [...expression, onFieldResult.fieldAccess],
54
- }
55
- : {
56
- typeObject,
57
- expression,
58
- };
59
- }
60
- else {
61
- const notFoundResult = onFieldNotFound({
62
- currentTypeObject: typeObject,
63
- expression: fieldExpression,
64
- fieldAccess,
65
- });
66
- return notFoundResult
67
- ? { typeObject: notFoundResult.typeObject, expression: [...expression, notFoundResult.fieldAccess] }
68
- : {
69
- typeObject,
70
- expression,
71
- };
72
- }
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
+ }
73
64
  }, {
74
- typeObject: initialTypeObject,
75
- expression: [],
65
+ typeObject: initialTypeObject,
66
+ expression: []
76
67
  });
77
- const defaultOnFieldNotFound = () => undefined;
78
- const defaultOnField = () => undefined;
79
- const fieldAccessVisitorTypeAwareInternal = ({ typeObject, rootTypeObject, onField = defaultOnField, onFieldNotFound = defaultOnFieldNotFound, }) => {
80
- return (0, utils_1.createExpressionVisitor)({
81
- visitFieldExpression: (expression) => visitFieldExpression({
82
- typeObject,
83
- expression,
84
- onField,
85
- onFieldNotFound,
86
- }).expression,
87
- visitFromRootFieldExpression: ([fromRootKeyword, ...rest]) => [
88
- fromRootKeyword,
89
- ...rest.map((x) => fieldAccessVisitorTypeAwareInternal({
90
- typeObject: rootTypeObject,
91
- rootTypeObject,
92
- onField,
93
- onFieldNotFound,
94
- }).visitExpression(x)),
95
- ],
96
- visitQueryExpression: (subQueryExpression) => {
97
- const { "q/from": fromExpression, "q/select": selectExpression, "q/where": whereExpression, "q/order-by": orderByExpression, } = subQueryExpression;
98
- const fromVisitResult = visitFieldExpression({
99
- typeObject,
100
- expression: fromExpression,
101
- onField,
102
- onFieldNotFound,
103
- });
104
- if (fromVisitResult?.typeObject) {
105
- const subQueryVisitor = fieldAccessVisitorTypeAwareInternal({
106
- typeObject: fromVisitResult?.typeObject,
107
- rootTypeObject,
108
- onField,
109
- onFieldNotFound,
110
- });
111
- return {
112
- ...subQueryExpression,
113
- ...{
114
- "q/from": fromVisitResult.expression,
115
- "q/select": (0, isPlainObject_1.default)(selectExpression)
116
- ? (0, _1.mapValues)(selectExpression, (val) => subQueryVisitor.visitExpression(val))
117
- : subQueryVisitor.visitExpression(selectExpression),
118
- },
119
- ...(whereExpression ? { "q/where": subQueryVisitor.visitExpression(whereExpression) } : null),
120
- ...(orderByExpression
121
- ? {
122
- "q/order-by": subQueryVisitor.visitOrderByExpression(orderByExpression),
123
- }
124
- : null),
125
- };
126
- }
127
- return subQueryExpression;
128
- },
129
- });
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
+ });
130
110
  };
131
- const fieldAccessVisitorTypeAware = ({ typeObject, onField = defaultOnField, onFieldNotFound = defaultOnFieldNotFound, }) => {
132
- return fieldAccessVisitorTypeAwareInternal({ typeObject, rootTypeObject: typeObject, onField, onFieldNotFound });
111
+ const fieldAccessVisitorTypeAware = ({ typeObject, onField = defaultOnField, onFieldNotFound = defaultOnFieldNotFound }) => {
112
+ return fieldAccessVisitorTypeAwareInternal({
113
+ typeObject,
114
+ rootTypeObject: typeObject,
115
+ onField,
116
+ onFieldNotFound
117
+ });
133
118
  };
134
- exports.fieldAccessVisitorTypeAware = fieldAccessVisitorTypeAware;
135
- //# sourceMappingURL=field-expression-visitor-type-aware.js.map
119
+ //#endregion
120
+ export { fieldAccessVisitorTypeAware };
@@ -1,10 +1,13 @@
1
- import type { Schema } from "@fibery/schema";
2
- import type { Expression } from "../types";
3
- import type { Operator } from "./types";
4
- export declare const findMatchedFilter: (filterExpression: Expression, params: Record<string, unknown>, operators: Array<Operator>, schema: Schema | null, type: string) => {
5
- operator: Operator;
6
- value: any;
7
- expression: Expression;
8
- type: string;
9
- id: string;
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;
10
11
  } | null;
12
+ //#endregion
13
+ export { findMatchedFilter };
@@ -1,49 +1,36 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findMatchedFilter = void 0;
4
- const uuid_1 = require("uuid");
5
- const errors_1 = require("../errors");
6
- const pattern_match_1 = require("./pattern-match");
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
7
5
  const findMatchedFilter = (filterExpression, params, operators, schema, type) => {
8
- if (!schema || !Object.hasOwn(schema.typeObjectsByName, type)) {
9
- return null;
10
- }
11
- const typeObject = schema.typeObjectsByName[type];
12
- for (const operator of operators) {
13
- if (operator.customMatch) {
14
- const matchResult = operator.customMatch(filterExpression, params, typeObject);
15
- if (matchResult.matchSuccess) {
16
- return {
17
- operator,
18
- value: matchResult.value,
19
- expression: matchResult.expression,
20
- type,
21
- id: (0, uuid_1.v4)(),
22
- };
23
- }
24
- }
25
- else {
26
- const { matchSuccess, matches } = (0, pattern_match_1.doPatternMatch)(filterExpression, params, operator.pattern, typeObject);
27
- if (matchSuccess) {
28
- const paramMatches = matches.filter((x) => x.matchType === ":expression-match-type/param");
29
- const expressionMatches = matches.filter((x) => x.matchType === ":expression-match-type/expression");
30
- if (paramMatches.length > 1) {
31
- throw new errors_1.NotImplementedError(`Operator pattern with several param placeholders`);
32
- }
33
- if (expressionMatches.length > 1) {
34
- throw new errors_1.NotImplementedError(`Operator pattern with several expression placeholders`);
35
- }
36
- return {
37
- operator,
38
- value: paramMatches[0] && paramMatches[0].value,
39
- expression: expressionMatches[0].value,
40
- type,
41
- id: (0, uuid_1.v4)(),
42
- };
43
- }
44
- }
45
- }
46
- return null;
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;
47
34
  };
48
- exports.findMatchedFilter = findMatchedFilter;
49
- //# sourceMappingURL=index.js.map
35
+ //#endregion
36
+ export { findMatchedFilter };
@@ -1,12 +1,15 @@
1
- import type { $TSFixMe } from "../tsfixme";
2
- import type { Expression } from "../types";
3
- import type { CustomMatchFn, Operator } from "./types";
4
- export declare const isDateTime: (dateString: string) => boolean;
5
- export declare const NO_VALUE: undefined;
6
- export declare const isNoValue: (value: unknown) => boolean;
7
- export declare const getCompareOperatorForDateTime: (id: string, title: string, operator: string, compareByStartInterval: boolean, isNegative: boolean) => Operator;
8
- export declare const getWithinOperatorCustomConvertFn: (leftPartExcluded: boolean) => (expression: Expression, value: {
9
- withinRangeStart: unknown;
10
- withinRangeEnd: unknown;
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;
11
12
  } | null, getNextParamName: () => string) => $TSFixMe;
12
- export declare const getWithinOperatorCustomMatchFn: (fieldPlaceholder: $TSFixMe, leftPartExcluded: boolean) => CustomMatchFn;
13
+ declare const getWithinOperatorCustomMatchFn: (fieldPlaceholder: $TSFixMe, leftPartExcluded: boolean) => CustomMatchFn;
14
+ //#endregion
15
+ export { NO_VALUE, getCompareOperatorForDateTime, getWithinOperatorCustomConvertFn, getWithinOperatorCustomMatchFn, isDateTime, isNoValue };