@fibery/expression-utils 1.1.19 → 2.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.
- package/index.js +6 -0
- package/lib/expression-utils.js +17 -0
- package/lib/paramsPlaceholders.js +17 -0
- package/package.json +7 -7
package/index.js
ADDED
package/lib/expression-utils.js
CHANGED
|
@@ -76,6 +76,7 @@ const paramsPlaceholdersLookup = {
|
|
|
76
76
|
const dynamicFilterParamPrefix = `$dynamic_`;
|
|
77
77
|
const isDynamicFilterParam = paramValue => paramValue.startsWith(dynamicFilterParamPrefix);
|
|
78
78
|
const getFieldIdFromDynamicParam = paramValue => paramValue.substring(dynamicFilterParamPrefix.length);
|
|
79
|
+
const whereParamPrefix = "$where";
|
|
79
80
|
const mapDynamicParams = (params, onDynamicParam) => {
|
|
80
81
|
if (!params) {
|
|
81
82
|
return params;
|
|
@@ -90,11 +91,26 @@ const mapDynamicParams = (params, onDynamicParam) => {
|
|
|
90
91
|
}
|
|
91
92
|
}));
|
|
92
93
|
};
|
|
94
|
+
const relativeDatePlaceholderRegex = /(\$start|\$end)-(\d+)-(second|minute|hour|day|week|month|quarter|year)-(before-now|after-now)-(date-time|date)/;
|
|
95
|
+
const getRelativeDateValue = relativeDatePlaceholderMatch => {
|
|
96
|
+
const startEnd = relativeDatePlaceholderMatch[1];
|
|
97
|
+
const amount = Number.parseInt(relativeDatePlaceholderMatch[2]);
|
|
98
|
+
const unit = relativeDatePlaceholderMatch[3];
|
|
99
|
+
const beforeAfter = relativeDatePlaceholderMatch[4];
|
|
100
|
+
const type = relativeDatePlaceholderMatch[5];
|
|
101
|
+
const result = beforeAfter === "before-now" ? moment__default["default"]().subtract(amount, unit) : moment__default["default"]().add(amount, unit);
|
|
102
|
+
const resultByStartEnd = startEnd === "$start" ? result.startOf(unit) : result.endOf(unit);
|
|
103
|
+
return type === "date" ? serializeDate(resultByStartEnd) : serializeDateTime(resultByStartEnd);
|
|
104
|
+
};
|
|
93
105
|
const replacePlaceholdersInParams = params => params && ___default["default"].mapValues(params, (value, key) => {
|
|
94
106
|
const replaceFn = paramsPlaceholdersLookup[key];
|
|
95
107
|
if (replaceFn) {
|
|
96
108
|
return replaceFn();
|
|
97
109
|
}
|
|
110
|
+
const relativeDatePlaceholderMatch = key.match(relativeDatePlaceholderRegex);
|
|
111
|
+
if (relativeDatePlaceholderMatch) {
|
|
112
|
+
return getRelativeDateValue(relativeDatePlaceholderMatch);
|
|
113
|
+
}
|
|
98
114
|
return value;
|
|
99
115
|
});
|
|
100
116
|
const dateToDateTimeIntervalLookup = {
|
|
@@ -171,6 +187,7 @@ var paramsPlaceholders = {
|
|
|
171
187
|
dynamicFilterParamPrefix: dynamicFilterParamPrefix,
|
|
172
188
|
isDynamicFilterParam: isDynamicFilterParam,
|
|
173
189
|
getFieldIdFromDynamicParam: getFieldIdFromDynamicParam,
|
|
190
|
+
whereParamPrefix: whereParamPrefix,
|
|
174
191
|
mapDynamicParams: mapDynamicParams,
|
|
175
192
|
replacePlaceholdersInParams: replacePlaceholdersInParams,
|
|
176
193
|
dateToDateTimeIntervalLookup: dateToDateTimeIntervalLookup
|
|
@@ -75,6 +75,7 @@ const paramsPlaceholdersLookup = {
|
|
|
75
75
|
const dynamicFilterParamPrefix = `$dynamic_`;
|
|
76
76
|
const isDynamicFilterParam = paramValue => paramValue.startsWith(dynamicFilterParamPrefix);
|
|
77
77
|
const getFieldIdFromDynamicParam = paramValue => paramValue.substring(dynamicFilterParamPrefix.length);
|
|
78
|
+
const whereParamPrefix = "$where";
|
|
78
79
|
const mapDynamicParams = (params, onDynamicParam) => {
|
|
79
80
|
if (!params) {
|
|
80
81
|
return params;
|
|
@@ -89,11 +90,26 @@ const mapDynamicParams = (params, onDynamicParam) => {
|
|
|
89
90
|
}
|
|
90
91
|
}));
|
|
91
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
|
+
};
|
|
92
104
|
const replacePlaceholdersInParams = params => params && ___default["default"].mapValues(params, (value, key) => {
|
|
93
105
|
const replaceFn = paramsPlaceholdersLookup[key];
|
|
94
106
|
if (replaceFn) {
|
|
95
107
|
return replaceFn();
|
|
96
108
|
}
|
|
109
|
+
const relativeDatePlaceholderMatch = key.match(relativeDatePlaceholderRegex);
|
|
110
|
+
if (relativeDatePlaceholderMatch) {
|
|
111
|
+
return getRelativeDateValue(relativeDatePlaceholderMatch);
|
|
112
|
+
}
|
|
97
113
|
return value;
|
|
98
114
|
});
|
|
99
115
|
const dateToDateTimeIntervalLookup = {
|
|
@@ -162,6 +178,7 @@ exports.weekAgoStartDateTimeParamPlaceholder = weekAgoStartDateTimeParamPlacehol
|
|
|
162
178
|
exports.weekFromNowDateParamPlaceholder = weekFromNowDateParamPlaceholder;
|
|
163
179
|
exports.weekFromNowEndDateTimeParamPlaceholder = weekFromNowEndDateTimeParamPlaceholder;
|
|
164
180
|
exports.weekFromNowStartDateTimeParamPlaceholder = weekFromNowStartDateTimeParamPlaceholder;
|
|
181
|
+
exports.whereParamPrefix = whereParamPrefix;
|
|
165
182
|
exports.yearAgoDateParamPlaceholder = yearAgoDateParamPlaceholder;
|
|
166
183
|
exports.yearAgoEndDateTimeParamPlaceholder = yearAgoEndDateTimeParamPlaceholder;
|
|
167
184
|
exports.yearAgoStartDateTimeParamPlaceholder = yearAgoStartDateTimeParamPlaceholder;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fibery/expression-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
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
|
|
29
|
+
"@fibery/helpers": "1.2.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.
|
|
37
|
-
"@fibery/eslint-config": "8.
|
|
36
|
+
"@fibery/babel-preset": "7.3.0",
|
|
37
|
+
"@fibery/eslint-config": "8.4.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"@fibery/schema": "^8.
|
|
40
|
+
"@fibery/schema": "^8.2.0"
|
|
41
41
|
},
|
|
42
42
|
"jest": {
|
|
43
43
|
"testEnvironment": "node",
|
|
@@ -63,8 +63,8 @@
|
|
|
63
63
|
"scripts": {
|
|
64
64
|
"build": "rm -rf lib && microbundle paramsPlaceholders.js utils.js visitors.js contextVariables.js index.js -o lib -f cjs --no-compress --target node --sourcemap false --generateTypes false",
|
|
65
65
|
"test": "node scripts/test.js",
|
|
66
|
-
"test:ci": "
|
|
67
|
-
"test:coverage": "
|
|
66
|
+
"test:ci": "pnpm run test --reporters=default --reporters=jest-junit",
|
|
67
|
+
"test:coverage": "pnpm run test --coverage --coverageDirectory=${JEST_COVERAGE_RESULT_DIR:-$(pwd)}/coverage/expression-utils --reporters=default --reporters=jest-junit",
|
|
68
68
|
"lint": "eslint ."
|
|
69
69
|
}
|
|
70
70
|
}
|