Util for making filters optional.
import won from "./won-es6.js";
import { isValidNumber, isValidDate, toLocalISODateString } from "./utils.js";
+// export function
+
/**
* returns e.g.:
* ```
@@ -21,7 +23,7 @@ import { isValidNumber, isValidDate, toLocalISODateString } from "./utils.js";
*
* @param {*} returnValue
*/
-function wellFormedFilterReturn(returnValue) {
+export function wellFormedFilterReturn(returnValue) {
return Object.assign(
{
prefixes: {},
@@ -56,6 +58,21 @@ export function concatenateFilters(filters) {
return concatenatedFilter;
}
+
+/**
+ * Collapses a filter's operations and wraps them in
+ * an `OPTIONAL` block.
+ * @param {*} filter
+ */
+export function optionalFilter(filter) {
+ const joinedOperations = filter.operations.join(" ");
+ const operationString =
+ joinedOperations && `OPTION { ${joinedOperations} } .`;
+ return wellFormedFilterReturn({
+ prefixes: filter.prefixes,
+ operations: [operationString],
+ });
+}
/**
* @param {String} rootSubject: a variable name via which the location is connected
* to the rest of the graph-patterns . e.g. `"?location"`. Needs to start with a
