@fibery/expression-utils 1.1.18 → 1.1.20

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.
@@ -90,11 +90,26 @@ const mapDynamicParams = (params, onDynamicParam) => {
90
90
  }
91
91
  }));
92
92
  };
93
+ const relativeDatePlaceholderRegex = /(\$start|\$end)-(\d+)-(second|minute|hour|day|week|month|quarter|year)-(before-now|after-now)-(date-time|date)/;
94
+ const getRelativeDateValue = relativeDatePlaceholderMatch => {
95
+ const startEnd = relativeDatePlaceholderMatch[1];
96
+ const amount = Number.parseInt(relativeDatePlaceholderMatch[2]);
97
+ const unit = relativeDatePlaceholderMatch[3];
98
+ const beforeAfter = relativeDatePlaceholderMatch[4];
99
+ const type = relativeDatePlaceholderMatch[5];
100
+ const result = beforeAfter === "before-now" ? moment__default["default"]().subtract(amount, unit) : moment__default["default"]().add(amount, unit);
101
+ const resultByStartEnd = startEnd === "$start" ? result.startOf(unit) : result.endOf(unit);
102
+ return type === "date" ? serializeDate(resultByStartEnd) : serializeDateTime(resultByStartEnd);
103
+ };
93
104
  const replacePlaceholdersInParams = params => params && ___default["default"].mapValues(params, (value, key) => {
94
105
  const replaceFn = paramsPlaceholdersLookup[key];
95
106
  if (replaceFn) {
96
107
  return replaceFn();
97
108
  }
109
+ const relativeDatePlaceholderMatch = key.match(relativeDatePlaceholderRegex);
110
+ if (relativeDatePlaceholderMatch) {
111
+ return getRelativeDateValue(relativeDatePlaceholderMatch);
112
+ }
98
113
  return value;
99
114
  });
100
115
  const dateToDateTimeIntervalLookup = {
@@ -639,12 +654,15 @@ const getExpressionTypeInternal = ({
639
654
  typeObject,
640
655
  functionsMeta,
641
656
  onFieldNotFound,
642
- returnRefTypeInsteadOfId: firstLastFunctions.has(fnName) ? returnRefTypeInsteadOfId : false
657
+ returnRefTypeInsteadOfId: firstLastFunctions.has(fnName) || fnName === "q/if" ? returnRefTypeInsteadOfId : false
643
658
  }));
644
659
  if (firstLastFunctions.has(fnName)) {
645
660
  //assuming q/first has one argument and result type equals arg type.
646
661
  //we need this trick to support 'returnRefTypeInsteadOfId' behavior for q/first and q/last.
647
662
  result = argTypes[0];
663
+ } else if (fnName === "q/if") {
664
+ //we need this trick to support 'returnRefTypeInsteadOfId' behavior for q/if.
665
+ result = argTypes[1];
648
666
  } else {
649
667
  const overload = fnMeta.overloads.find(o => o["arg-types"].every((argType, index) => argTypes[index] === UNKNOWN_EXPRESSION_TYPE || argTypes[index] === argType));
650
668
  if (!overload) {
@@ -89,11 +89,26 @@ const mapDynamicParams = (params, onDynamicParam) => {
89
89
  }
90
90
  }));
91
91
  };
92
+ const relativeDatePlaceholderRegex = /(\$start|\$end)-(\d+)-(second|minute|hour|day|week|month|quarter|year)-(before-now|after-now)-(date-time|date)/;
93
+ const getRelativeDateValue = relativeDatePlaceholderMatch => {
94
+ const startEnd = relativeDatePlaceholderMatch[1];
95
+ const amount = Number.parseInt(relativeDatePlaceholderMatch[2]);
96
+ const unit = relativeDatePlaceholderMatch[3];
97
+ const beforeAfter = relativeDatePlaceholderMatch[4];
98
+ const type = relativeDatePlaceholderMatch[5];
99
+ const result = beforeAfter === "before-now" ? moment__default["default"]().subtract(amount, unit) : moment__default["default"]().add(amount, unit);
100
+ const resultByStartEnd = startEnd === "$start" ? result.startOf(unit) : result.endOf(unit);
101
+ return type === "date" ? serializeDate(resultByStartEnd) : serializeDateTime(resultByStartEnd);
102
+ };
92
103
  const replacePlaceholdersInParams = params => params && ___default["default"].mapValues(params, (value, key) => {
93
104
  const replaceFn = paramsPlaceholdersLookup[key];
94
105
  if (replaceFn) {
95
106
  return replaceFn();
96
107
  }
108
+ const relativeDatePlaceholderMatch = key.match(relativeDatePlaceholderRegex);
109
+ if (relativeDatePlaceholderMatch) {
110
+ return getRelativeDateValue(relativeDatePlaceholderMatch);
111
+ }
97
112
  return value;
98
113
  });
99
114
  const dateToDateTimeIntervalLookup = {
package/lib/visitors.js CHANGED
@@ -383,12 +383,15 @@ const getExpressionTypeInternal = ({
383
383
  typeObject,
384
384
  functionsMeta,
385
385
  onFieldNotFound,
386
- returnRefTypeInsteadOfId: firstLastFunctions.has(fnName) ? returnRefTypeInsteadOfId : false
386
+ returnRefTypeInsteadOfId: firstLastFunctions.has(fnName) || fnName === "q/if" ? returnRefTypeInsteadOfId : false
387
387
  }));
388
388
  if (firstLastFunctions.has(fnName)) {
389
389
  //assuming q/first has one argument and result type equals arg type.
390
390
  //we need this trick to support 'returnRefTypeInsteadOfId' behavior for q/first and q/last.
391
391
  result = argTypes[0];
392
+ } else if (fnName === "q/if") {
393
+ //we need this trick to support 'returnRefTypeInsteadOfId' behavior for q/if.
394
+ result = argTypes[1];
392
395
  } else {
393
396
  const overload = fnMeta.overloads.find(o => o["arg-types"].every((argType, index) => argTypes[index] === UNKNOWN_EXPRESSION_TYPE || argTypes[index] === argType));
394
397
  if (!overload) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fibery/expression-utils",
3
- "version": "1.1.18",
3
+ "version": "1.1.20",
4
4
  "description": "utils for working with fibery api expressions",
5
5
  "exports": {
6
6
  ".": "./lib/expression-utils.js",
@@ -26,18 +26,18 @@
26
26
  "dependencies": {
27
27
  "lodash": "4.17.21",
28
28
  "moment": "2.29.4",
29
- "@fibery/helpers": "1.0.4"
29
+ "@fibery/helpers": "1.1.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@babel/core": "7.20.5",
33
33
  "jest": "27.5.1",
34
34
  "jest-junit": "13.0.0",
35
35
  "microbundle": "0.15.0",
36
- "@fibery/babel-preset": "7.2.0",
37
- "@fibery/eslint-config": "8.3.0"
36
+ "@fibery/eslint-config": "8.3.0",
37
+ "@fibery/babel-preset": "7.2.0"
38
38
  },
39
39
  "peerDependencies": {
40
- "@fibery/schema": "^8.1.11"
40
+ "@fibery/schema": "^8.1.13"
41
41
  },
42
42
  "jest": {
43
43
  "testEnvironment": "node",