@mercury25/aggregator 1.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 (85) hide show
  1. package/dist/aggregate.d.ts +7 -0
  2. package/dist/aggregate.d.ts.map +1 -0
  3. package/dist/aggregate.js +46 -0
  4. package/dist/aggregate.js.map +1 -0
  5. package/dist/buildPipeline.d.ts +4 -0
  6. package/dist/buildPipeline.d.ts.map +1 -0
  7. package/dist/buildPipeline.js +42 -0
  8. package/dist/buildPipeline.js.map +1 -0
  9. package/dist/docs.d.ts +126 -0
  10. package/dist/docs.d.ts.map +1 -0
  11. package/dist/docs.js +123 -0
  12. package/dist/docs.js.map +1 -0
  13. package/dist/errors.d.ts +5 -0
  14. package/dist/errors.d.ts.map +1 -0
  15. package/dist/errors.js +16 -0
  16. package/dist/errors.js.map +1 -0
  17. package/dist/index.d.ts +6 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +5 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/inputSchema.d.ts +118 -0
  22. package/dist/inputSchema.d.ts.map +1 -0
  23. package/dist/inputSchema.js +44 -0
  24. package/dist/inputSchema.js.map +1 -0
  25. package/dist/pipeline/createGroups.d.ts +4 -0
  26. package/dist/pipeline/createGroups.d.ts.map +1 -0
  27. package/dist/pipeline/createGroups.js +61 -0
  28. package/dist/pipeline/createGroups.js.map +1 -0
  29. package/dist/pipeline/createProjects.d.ts +3 -0
  30. package/dist/pipeline/createProjects.d.ts.map +1 -0
  31. package/dist/pipeline/createProjects.js +84 -0
  32. package/dist/pipeline/createProjects.js.map +1 -0
  33. package/dist/pipeline/createSorts.d.ts +4 -0
  34. package/dist/pipeline/createSorts.d.ts.map +1 -0
  35. package/dist/pipeline/createSorts.js +141 -0
  36. package/dist/pipeline/createSorts.js.map +1 -0
  37. package/dist/pipeline/filters/conditionSchemas.d.ts +286 -0
  38. package/dist/pipeline/filters/conditionSchemas.d.ts.map +1 -0
  39. package/dist/pipeline/filters/conditionSchemas.js +57 -0
  40. package/dist/pipeline/filters/conditionSchemas.js.map +1 -0
  41. package/dist/pipeline/filters/conditions.d.ts +8 -0
  42. package/dist/pipeline/filters/conditions.d.ts.map +1 -0
  43. package/dist/pipeline/filters/conditions.js +673 -0
  44. package/dist/pipeline/filters/conditions.js.map +1 -0
  45. package/dist/pipeline/filters/createFilters.d.ts +4 -0
  46. package/dist/pipeline/filters/createFilters.d.ts.map +1 -0
  47. package/dist/pipeline/filters/createFilters.js +107 -0
  48. package/dist/pipeline/filters/createFilters.js.map +1 -0
  49. package/dist/pipeline/verifyCreateAndSort.d.ts +3 -0
  50. package/dist/pipeline/verifyCreateAndSort.d.ts.map +1 -0
  51. package/dist/pipeline/verifyCreateAndSort.js +70 -0
  52. package/dist/pipeline/verifyCreateAndSort.js.map +1 -0
  53. package/dist/pipeline/verifyGroup.d.ts +3 -0
  54. package/dist/pipeline/verifyGroup.d.ts.map +1 -0
  55. package/dist/pipeline/verifyGroup.js +40 -0
  56. package/dist/pipeline/verifyGroup.js.map +1 -0
  57. package/dist/pipeline/verifyProject.d.ts +2 -0
  58. package/dist/pipeline/verifyProject.d.ts.map +1 -0
  59. package/dist/pipeline/verifyProject.js +19 -0
  60. package/dist/pipeline/verifyProject.js.map +1 -0
  61. package/dist/types.d.ts +51 -0
  62. package/dist/types.d.ts.map +1 -0
  63. package/dist/types.js +2 -0
  64. package/dist/types.js.map +1 -0
  65. package/dist/util/dayOfWeekMap.d.ts +11 -0
  66. package/dist/util/dayOfWeekMap.d.ts.map +1 -0
  67. package/dist/util/dayOfWeekMap.js +10 -0
  68. package/dist/util/dayOfWeekMap.js.map +1 -0
  69. package/dist/util/isIsoDateString.d.ts +2 -0
  70. package/dist/util/isIsoDateString.d.ts.map +1 -0
  71. package/dist/util/isIsoDateString.js +9 -0
  72. package/dist/util/isIsoDateString.js.map +1 -0
  73. package/dist/util/isObject.d.ts +2 -0
  74. package/dist/util/isObject.d.ts.map +1 -0
  75. package/dist/util/isObject.js +7 -0
  76. package/dist/util/isObject.js.map +1 -0
  77. package/dist/util/monthMap.d.ts +16 -0
  78. package/dist/util/monthMap.d.ts.map +1 -0
  79. package/dist/util/monthMap.js +15 -0
  80. package/dist/util/monthMap.js.map +1 -0
  81. package/dist/util/numberValidators.d.ts +4 -0
  82. package/dist/util/numberValidators.d.ts.map +1 -0
  83. package/dist/util/numberValidators.js +10 -0
  84. package/dist/util/numberValidators.js.map +1 -0
  85. package/package.json +49 -0
@@ -0,0 +1,673 @@
1
+ import mongoose from "mongoose";
2
+ import { dayOfWeekMap } from "../../util/dayOfWeekMap.js";
3
+ import { monthMap } from "../../util/monthMap.js";
4
+ import { AggregatorError } from "../../errors.js";
5
+ import { isObject } from "../../util/isObject.js";
6
+ import { isIsoDateString } from "../../util/isIsoDateString.js";
7
+ import { isValidHour, isValidMinuteOrSecond, isValidYear, } from "../../util/numberValidators.js";
8
+ const mappedValue = (fieldType, val, message) => {
9
+ if (fieldType === "date") {
10
+ if (!isIsoDateString(val))
11
+ throw new AggregatorError(message);
12
+ return new Date(val);
13
+ }
14
+ else if (fieldType === "number") {
15
+ if (typeof val !== "number")
16
+ throw new AggregatorError(message);
17
+ return val;
18
+ }
19
+ else if (fieldType === "boolean") {
20
+ if (typeof val !== "boolean")
21
+ throw new AggregatorError(message);
22
+ return val;
23
+ }
24
+ else if (fieldType === "string") {
25
+ if (typeof val !== "string")
26
+ throw new AggregatorError(message);
27
+ return val;
28
+ }
29
+ else if (fieldType === "oid") {
30
+ if (typeof val !== "string")
31
+ throw new AggregatorError(message);
32
+ return new mongoose.Types.ObjectId(val);
33
+ }
34
+ else
35
+ return null;
36
+ };
37
+ const fieldDisplay = (field) => {
38
+ if (typeof field === "string") {
39
+ return field;
40
+ }
41
+ else if ("$strLenCP" in field) {
42
+ return `Length of ${field.$strLenCP}`;
43
+ }
44
+ else if ("$size" in field) {
45
+ return `Size of ${field.$size}`;
46
+ }
47
+ return "Unknown Field";
48
+ };
49
+ const fieldTypeDisplay = (fieldType) => {
50
+ switch (fieldType) {
51
+ case "date":
52
+ return "ISO Date String";
53
+ case "number":
54
+ return "Number";
55
+ case "boolean":
56
+ return "Boolean";
57
+ case "string":
58
+ return "String";
59
+ case "oid":
60
+ return "ObjectId String";
61
+ case "array":
62
+ return "Array";
63
+ default:
64
+ return "Unknown";
65
+ }
66
+ };
67
+ const isValidCondition = (args) => {
68
+ if (args.validTypes.map((type) => fieldTypeDisplay(type)).includes(args.fieldType)) {
69
+ return;
70
+ }
71
+ throw new AggregatorError(`Cannot use the ${args.conditionName} condition on the field: ${args.field}. This field has a field type of: ${args.fieldType}.`);
72
+ };
73
+ const condition_exists = (args) => {
74
+ const { field, fieldType, condition, value } = args;
75
+ const conditionName = "exists";
76
+ if (condition !== conditionName)
77
+ return;
78
+ const fieldDisplayText = fieldDisplay(field);
79
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
80
+ const validTypes = ["date", "number", "boolean", "string", "array", "oid"];
81
+ isValidCondition({
82
+ conditionName,
83
+ field: fieldDisplayText,
84
+ fieldType: fieldTypeDisplayText,
85
+ validTypes,
86
+ });
87
+ if (typeof value !== "boolean") {
88
+ throw new AggregatorError(`Invalid value for 'exists' in ${fieldDisplayText} filter. Expected boolean, received ${typeof value}.`);
89
+ }
90
+ return { $expr: { $ifNull: [field, value] } };
91
+ };
92
+ const condition_equals = (args) => {
93
+ let { field, fieldType, condition, value } = args;
94
+ const conditionName = "equals";
95
+ if (condition !== conditionName)
96
+ return;
97
+ const fieldDisplayText = fieldDisplay(field);
98
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
99
+ const validTypes = ["date", "number", "boolean", "string", "oid"];
100
+ isValidCondition({
101
+ conditionName,
102
+ field: fieldDisplayText,
103
+ fieldType: fieldTypeDisplayText,
104
+ validTypes,
105
+ });
106
+ value = mappedValue(fieldType, value, `Invalid value for 'equals' in ${fieldDisplayText} filter. Expected ${fieldTypeDisplayText}, received ${typeof value}.`);
107
+ return { $expr: { $eq: [field, value] } };
108
+ };
109
+ const condition_notEquals = (args) => {
110
+ let { field, fieldType, condition, value } = args;
111
+ const conditionName = "notEquals";
112
+ if (condition !== conditionName)
113
+ return;
114
+ const fieldDisplayText = fieldDisplay(field);
115
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
116
+ const validTypes = ["date", "number", "boolean", "string", "oid"];
117
+ isValidCondition({
118
+ conditionName,
119
+ field: fieldDisplayText,
120
+ fieldType: fieldTypeDisplayText,
121
+ validTypes,
122
+ });
123
+ value = mappedValue(fieldType, value, `Invalid value for 'notEquals' in ${fieldDisplayText} filter. Expected ${fieldTypeDisplayText}, received ${typeof value}.`);
124
+ return { $expr: { $ne: [field, value] } };
125
+ };
126
+ const condition_anyOf = (args) => {
127
+ let { field, fieldType, condition, value } = args;
128
+ const conditionName = "anyOf";
129
+ if (condition !== conditionName)
130
+ return;
131
+ const fieldDisplayText = fieldDisplay(field);
132
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
133
+ const validTypes = ["date", "number", "string", "boolean", "oid"];
134
+ isValidCondition({
135
+ conditionName,
136
+ field: fieldDisplayText,
137
+ fieldType: fieldTypeDisplayText,
138
+ validTypes,
139
+ });
140
+ if (!Array.isArray(value)) {
141
+ throw new AggregatorError(`Invalid value for 'anyOf' in ${fieldDisplayText} filter. Expected array, received ${typeof value}.`);
142
+ }
143
+ value = value.map((val, index) => {
144
+ return mappedValue(fieldType, val, `Invalid value in array for 'anyOf' in ${fieldDisplayText} filter. Expected ${fieldTypeDisplayText}, received ${typeof val} at index ${index}.`);
145
+ });
146
+ return { $expr: { $in: [field, value] } };
147
+ };
148
+ const condition_day = (args) => {
149
+ const { field, fieldType, condition, value } = args;
150
+ const conditionName = "day";
151
+ if (condition !== conditionName)
152
+ return;
153
+ const fieldDisplayText = fieldDisplay(field);
154
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
155
+ const validTypes = ["date"];
156
+ isValidCondition({
157
+ conditionName,
158
+ field: fieldDisplayText,
159
+ fieldType: fieldTypeDisplayText,
160
+ validTypes,
161
+ });
162
+ if (typeof value !== "string" || !(value in dayOfWeekMap)) {
163
+ throw new AggregatorError(`Invalid value for 'day' in ${fieldDisplayText} filter. Expected one of ${Object.keys(dayOfWeekMap).join(", ")}, received ${value}.`);
164
+ }
165
+ return {
166
+ $expr: {
167
+ $eq: [{ $dayOfWeek: field }, dayOfWeekMap[value]],
168
+ },
169
+ };
170
+ };
171
+ const condition_month = (args) => {
172
+ const { field, fieldType, condition, value } = args;
173
+ const conditionName = "month";
174
+ if (condition !== conditionName)
175
+ return;
176
+ const fieldDisplayText = fieldDisplay(field);
177
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
178
+ const validTypes = ["date"];
179
+ isValidCondition({
180
+ conditionName,
181
+ field: fieldDisplayText,
182
+ fieldType: fieldTypeDisplayText,
183
+ validTypes,
184
+ });
185
+ if (typeof value !== "string" || !(value in monthMap)) {
186
+ throw new AggregatorError(`Invalid value for 'month' in ${fieldDisplayText} filter. Expected one of ${Object.keys(monthMap).join(", ")}, received ${value}.`);
187
+ }
188
+ return {
189
+ $expr: {
190
+ $eq: [{ $month: field }, monthMap[value]],
191
+ },
192
+ };
193
+ };
194
+ const condition_year = (args) => {
195
+ const { field, fieldType, condition, value } = args;
196
+ const conditionName = "year";
197
+ if (condition !== conditionName)
198
+ return;
199
+ const fieldDisplayText = fieldDisplay(field);
200
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
201
+ const validTypes = ["date"];
202
+ isValidCondition({
203
+ conditionName,
204
+ field: fieldDisplayText,
205
+ fieldType: fieldTypeDisplayText,
206
+ validTypes,
207
+ });
208
+ if (typeof value !== "number" || !isValidYear(value)) {
209
+ throw new AggregatorError(`Invalid value for 'year' in ${fieldDisplayText} filter. Expected a number value between 1800 and ${new Date().getFullYear()}, received ${value}.`);
210
+ }
211
+ return {
212
+ $expr: { $eq: [{ $year: field }, value] },
213
+ };
214
+ };
215
+ const condition_hour = (args) => {
216
+ const { field, fieldType, condition, value } = args;
217
+ const conditionName = "hour";
218
+ if (condition !== conditionName)
219
+ return;
220
+ const fieldDisplayText = fieldDisplay(field);
221
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
222
+ const validTypes = ["date"];
223
+ isValidCondition({
224
+ conditionName,
225
+ field: fieldDisplayText,
226
+ fieldType: fieldTypeDisplayText,
227
+ validTypes,
228
+ });
229
+ if (typeof value !== "number" || !isValidHour(value)) {
230
+ throw new AggregatorError(`Invalid value for 'hour' in ${fieldDisplayText} filter. Expected a number value between 0 and 23, received ${value}.`);
231
+ }
232
+ return {
233
+ $expr: { $eq: [{ $hour: field }, value] },
234
+ };
235
+ };
236
+ const condition_minute = (args) => {
237
+ const { field, fieldType, condition, value } = args;
238
+ const conditionName = "minute";
239
+ if (condition !== conditionName)
240
+ return;
241
+ const fieldDisplayText = fieldDisplay(field);
242
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
243
+ const validTypes = ["date"];
244
+ isValidCondition({
245
+ conditionName,
246
+ field: fieldDisplayText,
247
+ fieldType: fieldTypeDisplayText,
248
+ validTypes,
249
+ });
250
+ if (typeof value !== "number" || !isValidMinuteOrSecond(value)) {
251
+ throw new AggregatorError(`Invalid value for 'minute' in ${fieldDisplayText} filter. Expected a number value between 0 and 59, received ${value}.`);
252
+ }
253
+ return {
254
+ $expr: { $eq: [{ $minute: field }, value] },
255
+ };
256
+ };
257
+ const condition_second = (args) => {
258
+ const { field, fieldType, condition, value } = args;
259
+ const conditionName = "second";
260
+ if (condition !== conditionName)
261
+ return;
262
+ const fieldDisplayText = fieldDisplay(field);
263
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
264
+ const validTypes = ["date"];
265
+ isValidCondition({
266
+ conditionName,
267
+ field: fieldDisplayText,
268
+ fieldType: fieldTypeDisplayText,
269
+ validTypes,
270
+ });
271
+ if (typeof value !== "number" || !isValidMinuteOrSecond(value)) {
272
+ throw new AggregatorError(`Invalid value for 'second' in ${fieldDisplayText} filter. Expected a number value between 0 and 59, received ${value}.`);
273
+ }
274
+ return {
275
+ $expr: { $eq: [{ $second: field }, value] },
276
+ };
277
+ };
278
+ const condition_isWeekend = (args) => {
279
+ const { field, fieldType, condition, value } = args;
280
+ const conditionName = "isWeekend";
281
+ if (condition !== conditionName)
282
+ return;
283
+ const fieldDisplayText = fieldDisplay(field);
284
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
285
+ const validTypes = ["date"];
286
+ isValidCondition({
287
+ conditionName,
288
+ field: fieldDisplayText,
289
+ fieldType: fieldTypeDisplayText,
290
+ validTypes,
291
+ });
292
+ if (typeof value !== "boolean") {
293
+ throw new AggregatorError(`Invalid value for 'isWeekend' in ${fieldDisplayText} filter. Expected boolean, received ${typeof value}.`);
294
+ }
295
+ return {
296
+ $or: [
297
+ { $eq: [{ $dayOfWeek: field }, 0] },
298
+ { $eq: [{ $dayOfWeek: field }, 6] },
299
+ ],
300
+ };
301
+ };
302
+ const condition_isWeekday = (args) => {
303
+ const { field, fieldType, condition, value } = args;
304
+ const conditionName = "isWeekday";
305
+ if (condition !== conditionName)
306
+ return;
307
+ const fieldDisplayText = fieldDisplay(field);
308
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
309
+ const validTypes = ["date"];
310
+ isValidCondition({
311
+ conditionName,
312
+ field: fieldDisplayText,
313
+ fieldType: fieldTypeDisplayText,
314
+ validTypes,
315
+ });
316
+ if (typeof value !== "boolean") {
317
+ throw new AggregatorError(`Invalid value for 'isWeekday' in ${fieldDisplayText} filter. Expected boolean, received ${typeof value}.`);
318
+ }
319
+ return {
320
+ $and: [
321
+ { $ne: [{ $dayOfWeek: field }, 0] },
322
+ { $ne: [{ $dayOfWeek: field }, 6] },
323
+ ],
324
+ };
325
+ };
326
+ const condition_isLeapYear = (args) => {
327
+ const { field, fieldType, condition, value } = args;
328
+ const conditionName = "isLeapYear";
329
+ if (condition !== conditionName)
330
+ return;
331
+ const fieldDisplayText = fieldDisplay(field);
332
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
333
+ const validTypes = ["date"];
334
+ isValidCondition({
335
+ conditionName,
336
+ field: fieldDisplayText,
337
+ fieldType: fieldTypeDisplayText,
338
+ validTypes,
339
+ });
340
+ if (typeof value !== "boolean") {
341
+ throw new AggregatorError(`Invalid value for 'isLeapYear' in ${fieldDisplayText} filter. Expected boolean, received ${typeof value}.`);
342
+ }
343
+ return {
344
+ $expr: {
345
+ $eq: [
346
+ { $year: field },
347
+ { $cond: [{ $eq: [{ $mod: [{ $year: field }, 4] }, 0] }, 1, 0] },
348
+ ],
349
+ },
350
+ };
351
+ };
352
+ const condition_greaterThan = (args) => {
353
+ let { field, fieldType, condition, value } = args;
354
+ const conditionName = "greaterThan";
355
+ if (condition !== conditionName)
356
+ return;
357
+ const fieldDisplayText = fieldDisplay(field);
358
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
359
+ const validTypes = ["date", "number"];
360
+ isValidCondition({
361
+ conditionName,
362
+ field: fieldDisplayText,
363
+ fieldType: fieldTypeDisplayText,
364
+ validTypes,
365
+ });
366
+ value = mappedValue(fieldType, value, `Invalid value for 'greaterThan' in ${fieldDisplayText} filter. Expected ${fieldTypeDisplayText}, received ${typeof value}.`);
367
+ return { $expr: { $gt: [field, value] } };
368
+ };
369
+ const condition_greaterThanOrEquals = (args) => {
370
+ let { field, fieldType, condition, value } = args;
371
+ const conditionName = "greaterThanOrEquals";
372
+ if (condition !== conditionName)
373
+ return;
374
+ const fieldDisplayText = fieldDisplay(field);
375
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
376
+ const validTypes = ["date", "number"];
377
+ isValidCondition({
378
+ conditionName,
379
+ field: fieldDisplayText,
380
+ fieldType: fieldTypeDisplayText,
381
+ validTypes,
382
+ });
383
+ value = mappedValue(fieldType, value, `Invalid value for 'greaterThanOrEquals' in ${fieldDisplayText} filter. Expected ${fieldTypeDisplayText}, received ${typeof value}.`);
384
+ return { $expr: { $gte: [field, value] } };
385
+ };
386
+ const condition_lessThan = (args) => {
387
+ let { field, fieldType, condition, value } = args;
388
+ const conditionName = "lessThan";
389
+ if (condition !== conditionName)
390
+ return;
391
+ const fieldDisplayText = fieldDisplay(field);
392
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
393
+ const validTypes = ["date", "number"];
394
+ isValidCondition({
395
+ conditionName,
396
+ field: fieldDisplayText,
397
+ fieldType: fieldTypeDisplayText,
398
+ validTypes,
399
+ });
400
+ value = mappedValue(fieldType, value, `Invalid value for 'lessThan' in ${fieldDisplayText} filter. Expected ${fieldTypeDisplayText}, received ${typeof value}.`);
401
+ return { $expr: { $lt: [field, value] } };
402
+ };
403
+ const condition_lessThanOrEquals = (args) => {
404
+ let { field, fieldType, condition, value } = args;
405
+ const conditionName = "lessThanOrEquals";
406
+ if (condition !== conditionName)
407
+ return;
408
+ const fieldDisplayText = fieldDisplay(field);
409
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
410
+ const validTypes = ["date", "number"];
411
+ isValidCondition({
412
+ conditionName,
413
+ field: fieldDisplayText,
414
+ fieldType: fieldTypeDisplayText,
415
+ validTypes,
416
+ });
417
+ value = mappedValue(fieldType, value, `Invalid value for 'lessThanOrEquals' in ${fieldDisplayText} filter. Expected ${fieldTypeDisplayText}, received ${typeof value}.`);
418
+ return { $expr: { $lte: [field, value] } };
419
+ };
420
+ const condition_betweenExclusive = (args) => {
421
+ let { field, fieldType, condition, value } = args;
422
+ const conditionName = "betweenExclusive";
423
+ if (condition !== conditionName)
424
+ return;
425
+ const fieldDisplayText = fieldDisplay(field);
426
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
427
+ const validTypes = ["date", "number"];
428
+ isValidCondition({
429
+ conditionName,
430
+ field: fieldDisplayText,
431
+ fieldType: fieldTypeDisplayText,
432
+ validTypes,
433
+ });
434
+ if (!Array.isArray(value) || value.length !== 2) {
435
+ throw new AggregatorError(`Invalid value for 'betweenExclusive' in ${fieldDisplayText} filter. Expected an array of two values, received ${JSON.stringify(value)}.`);
436
+ }
437
+ value = value.map((val, index) => {
438
+ return mappedValue(fieldType, val, `Invalid value in array for 'betweenExclusive' in ${fieldDisplayText} filter. Expected ${fieldTypeDisplayText}, received ${typeof val} at index ${index}.`);
439
+ });
440
+ const tuple = value;
441
+ return {
442
+ $expr: {
443
+ $and: [{ $gt: [field, tuple[0]] }, { $lt: [field, tuple[1]] }],
444
+ },
445
+ };
446
+ };
447
+ const condition_betweenInclusive = (args) => {
448
+ let { field, fieldType, condition, value } = args;
449
+ const conditionName = "betweenInclusive";
450
+ if (condition !== conditionName)
451
+ return;
452
+ const fieldDisplayText = fieldDisplay(field);
453
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
454
+ const validTypes = ["date", "number"];
455
+ isValidCondition({
456
+ conditionName,
457
+ field: fieldDisplayText,
458
+ fieldType: fieldTypeDisplayText,
459
+ validTypes,
460
+ });
461
+ if (!Array.isArray(value) || value.length !== 2) {
462
+ throw new AggregatorError(`Invalid value for 'betweenInclusive' in ${fieldDisplayText} filter. Expected an array of two values, received ${JSON.stringify(value)}.`);
463
+ }
464
+ value = value.map((val, index) => {
465
+ return mappedValue(fieldType, val, `Invalid value in array for 'betweenInclusive' in ${fieldDisplayText} filter. Expected ${fieldTypeDisplayText}, received ${typeof val} at index ${index}.`);
466
+ });
467
+ const tuple = value;
468
+ return {
469
+ $expr: {
470
+ $and: [{ $gte: [field, tuple[0]] }, { $lte: [field, tuple[1]] }],
471
+ },
472
+ };
473
+ };
474
+ const condition_divisibleBy = (args) => {
475
+ const { field, fieldType, condition, value } = args;
476
+ const conditionName = "divisibleBy";
477
+ if (condition !== conditionName)
478
+ return;
479
+ const fieldDisplayText = fieldDisplay(field);
480
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
481
+ const validTypes = ["number"];
482
+ isValidCondition({
483
+ conditionName,
484
+ field: fieldDisplayText,
485
+ fieldType: fieldTypeDisplayText,
486
+ validTypes,
487
+ });
488
+ if (typeof value !== "number" || value <= 0) {
489
+ throw new AggregatorError(`Invalid value for 'divisibleBy' in ${fieldDisplayText} filter. Expected a positive number, received ${value}.`);
490
+ }
491
+ return {
492
+ $expr: { $eq: [{ $mod: [field, value] }, 0] },
493
+ };
494
+ };
495
+ const condition_parity = (args) => {
496
+ const { field, fieldType, condition, value } = args;
497
+ const conditionName = "parity";
498
+ if (condition !== conditionName)
499
+ return;
500
+ const fieldDisplayText = fieldDisplay(field);
501
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
502
+ const validTypes = ["number"];
503
+ isValidCondition({
504
+ conditionName,
505
+ field: fieldDisplayText,
506
+ fieldType: fieldTypeDisplayText,
507
+ validTypes,
508
+ });
509
+ if (typeof value !== "string" || (value !== "even" && value !== "odd")) {
510
+ throw new AggregatorError(`Invalid value for 'parity' in ${fieldDisplayText} filter. Expected 'even' or 'odd', received ${value}.`);
511
+ }
512
+ const parityValue = value === "even" ? 0 : 1;
513
+ return {
514
+ $expr: { $eq: [{ $mod: [field, 2] }, parityValue] },
515
+ };
516
+ };
517
+ const condition_contains = (args) => {
518
+ const { field, fieldType, condition, value } = args;
519
+ const conditionName = "contains";
520
+ if (condition !== conditionName)
521
+ return;
522
+ const fieldDisplayText = fieldDisplay(field);
523
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
524
+ const validTypes = ["string"];
525
+ isValidCondition({
526
+ conditionName,
527
+ field: fieldDisplayText,
528
+ fieldType: fieldTypeDisplayText,
529
+ validTypes,
530
+ });
531
+ if (typeof value !== "string") {
532
+ throw new AggregatorError(`Invalid value for 'contains' in ${fieldDisplayText} filter. Expected a string, received ${typeof value}.`);
533
+ }
534
+ return {
535
+ $expr: {
536
+ $regexMatch: { input: field, regex: value, options: "i" },
537
+ },
538
+ };
539
+ };
540
+ const condition_startsWith = (args) => {
541
+ const { field, fieldType, condition, value } = args;
542
+ const conditionName = "startsWith";
543
+ if (condition !== conditionName)
544
+ return;
545
+ const fieldDisplayText = fieldDisplay(field);
546
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
547
+ const validTypes = ["string"];
548
+ isValidCondition({
549
+ conditionName,
550
+ field: fieldDisplayText,
551
+ fieldType: fieldTypeDisplayText,
552
+ validTypes,
553
+ });
554
+ if (typeof value !== "string") {
555
+ throw new AggregatorError(`Invalid value for 'startsWith' in ${fieldDisplayText} filter. Expected a string, received ${typeof value}.`);
556
+ }
557
+ return {
558
+ $expr: {
559
+ $regexMatch: { input: field, regex: `^${value}`, options: "i" },
560
+ },
561
+ };
562
+ };
563
+ const condition_endsWith = (args) => {
564
+ const { field, fieldType, condition, value } = args;
565
+ const conditionName = "endsWith";
566
+ if (condition !== conditionName)
567
+ return;
568
+ const fieldDisplayText = fieldDisplay(field);
569
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
570
+ const validTypes = ["string"];
571
+ isValidCondition({
572
+ conditionName,
573
+ field: fieldDisplayText,
574
+ fieldType: fieldTypeDisplayText,
575
+ validTypes,
576
+ });
577
+ if (typeof value !== "string") {
578
+ throw new AggregatorError(`Invalid value for 'endsWith' in ${fieldDisplayText} filter. Expected a string, received ${typeof value}.`);
579
+ }
580
+ return {
581
+ $expr: {
582
+ $regexMatch: { input: field, regex: `${value}$`, options: "i" },
583
+ },
584
+ };
585
+ };
586
+ const condition_regex = (args) => {
587
+ const { field, fieldType, condition, value } = args;
588
+ const conditionName = "regex";
589
+ if (condition !== conditionName)
590
+ return;
591
+ const fieldDisplayText = fieldDisplay(field);
592
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
593
+ const validTypes = ["string"];
594
+ isValidCondition({
595
+ conditionName,
596
+ field: fieldDisplayText,
597
+ fieldType: fieldTypeDisplayText,
598
+ validTypes,
599
+ });
600
+ if (typeof value !== "string") {
601
+ throw new AggregatorError(`Invalid value for 'regex' in ${fieldDisplayText} filter. Expected a string, received ${typeof value}.`);
602
+ }
603
+ return {
604
+ $expr: {
605
+ $regexMatch: { input: field, regex: value, options: "i" },
606
+ },
607
+ };
608
+ };
609
+ const condition_length = (args) => {
610
+ const { field, fieldType, condition, value } = args;
611
+ const conditionName = "length";
612
+ if (condition !== conditionName)
613
+ return [];
614
+ const fieldDisplayText = fieldDisplay(field);
615
+ const fieldTypeDisplayText = fieldTypeDisplay(fieldType);
616
+ const validTypes = ["string", "array"];
617
+ isValidCondition({
618
+ conditionName,
619
+ field: fieldDisplayText,
620
+ fieldType: fieldTypeDisplayText,
621
+ validTypes,
622
+ });
623
+ if (fieldType === "string") {
624
+ return [
625
+ { $eq: [{ $type: field }, "string"] },
626
+ ...condition_controller({ $strLenCP: field }, "number", value),
627
+ ];
628
+ }
629
+ else if (fieldType === "array") {
630
+ return [
631
+ { $expr: { $eq: [{ $type: field }, "array"] } },
632
+ ...condition_controller({ $size: field }, "number", value),
633
+ ];
634
+ }
635
+ else
636
+ return [];
637
+ };
638
+ export const condition_controller = (field, fieldType, conditions, exprs = []) => {
639
+ if (!isObject(conditions)) {
640
+ throw new AggregatorError("For the field: " + String(field) + ", the conditions must be an object.");
641
+ }
642
+ for (const [condition, value] of Object.entries(conditions)) {
643
+ const args = { field, fieldType, condition, value };
644
+ exprs.push(condition_exists(args));
645
+ exprs.push(condition_equals(args));
646
+ exprs.push(condition_notEquals(args));
647
+ exprs.push(condition_anyOf(args));
648
+ exprs.push(condition_day(args));
649
+ exprs.push(condition_month(args));
650
+ exprs.push(condition_year(args));
651
+ exprs.push(condition_hour(args));
652
+ exprs.push(condition_minute(args));
653
+ exprs.push(condition_second(args));
654
+ exprs.push(condition_isWeekend(args));
655
+ exprs.push(condition_isWeekday(args));
656
+ exprs.push(condition_isLeapYear(args));
657
+ exprs.push(condition_greaterThan(args));
658
+ exprs.push(condition_greaterThanOrEquals(args));
659
+ exprs.push(condition_lessThan(args));
660
+ exprs.push(condition_lessThanOrEquals(args));
661
+ exprs.push(condition_betweenExclusive(args));
662
+ exprs.push(condition_betweenInclusive(args));
663
+ exprs.push(condition_divisibleBy(args));
664
+ exprs.push(condition_parity(args));
665
+ exprs.push(condition_contains(args));
666
+ exprs.push(condition_startsWith(args));
667
+ exprs.push(condition_endsWith(args));
668
+ exprs.push(condition_regex(args));
669
+ exprs.push(...condition_length(args));
670
+ }
671
+ return exprs.filter(Boolean);
672
+ };
673
+ //# sourceMappingURL=conditions.js.map