@react-awesome-query-builder/core
Version:
User-friendly query builder for React. Core
134 lines (130 loc) • 4.49 kB
JavaScript
import _typeof from "@babel/runtime/helpers/typeof";
var spelEscapeString = function spelEscapeString(val) {
// Strings are delimited by single quotes. To put a single quote itself in a string, use two single quote characters.
return "'" + val.replace(/'/g, "''") + "'";
};
var spelInlineList = function spelInlineList(vals) {
var toArray = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
// find java type of values
var javaType;
var jt;
var numberJavaTypes = ["int", "float"];
vals.map(function (v) {
if (v !== undefined && v !== null) {
if (typeof v === "string") {
jt = "String";
} else if (typeof v === "number") {
jt = Number.isInteger(v) ? "int" : "float";
} else throw new Error("spelEscape: Can't use value ".concat(v, " in array"));
if (!javaType) {
javaType = jt;
} else if (javaType != jt) {
if (numberJavaTypes.includes(javaType) && numberJavaTypes.includes(jt)) {
// found int and float in collecton - use float
javaType = "float";
} else throw new Error("spelEscape: Can't use different types in array: found ".concat(javaType, " and ").concat(jt));
}
}
});
if (!javaType) {
javaType = "String"; //default if empty array
}
// for floats we should add 'f' to all items
var escapedVals;
if (javaType == "float") {
escapedVals = vals.map(function (v) {
return spelEscape(v, true);
});
} else {
escapedVals = vals.map(function (v) {
return spelEscape(v);
});
}
// build inline list or array
var res;
if (toArray) {
res = "new ".concat(javaType, "[]{").concat(escapedVals.join(", "), "}");
} else {
res = "{".concat(escapedVals.join(", "), "}");
}
return res;
};
export var spelEscape = function spelEscape(val) {
var numberToFloat = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var arrayToArray = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
// https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/expressions.html#expressions-ref-literal
if (val === undefined || val === null) {
return "null";
}
switch (_typeof(val)) {
case "boolean":
return val ? "true" : "false";
case "number":
if (!Number.isFinite(val) || isNaN(val)) return undefined;
return val + (!Number.isInteger(val) || numberToFloat ? "f" : "");
case "object":
if (Array.isArray(val)) {
return spelInlineList(val, arrayToArray);
} else {
// see `spelFormatValue` for Date, LocalTime
throw new Error("spelEscape: Object is not supported");
}
default:
return spelEscapeString(val);
}
};
// @deprecated
export var spelFormatConcat = function spelFormatConcat(parts) {
if (parts && Array.isArray(parts) && parts.length) {
return parts.map(function (part) {
if (part.type == "const") {
return spelEscape(part.value);
} else if (part.type == "property") {
return "" + part.value;
} else if (part.type == "variable") {
return "#" + part.value;
}
return undefined;
}).filter(function (r) {
return r != undefined;
}).join(" + ");
} else {
return "null";
}
};
// @deprecated
// `val` is {value, valueType, valueSrc}
// If `valueType` == "case_value", `value` is array of such items (to be considered as concatenation)
export var spelImportConcat = function spelImportConcat(val) {
if (val == undefined) return [undefined, []];
var errors = [];
var value = val.valueType == "case_value" ? val.value : val;
var valueArr = Array.isArray(value) ? value : [value];
var res = valueArr.map(function (child) {
if (child.valueSrc === "value") {
if (child.value === null) {
return undefined;
} else {
return {
type: "const",
value: child.value
};
}
} else if (child.valueSrc === "field") {
return {
type: child.isVariable ? "variable" : "property",
value: child.value
};
} else {
errors.push("Unsupported valueSrc ".concat(child.valueSrc, " in concatenation"));
}
}).filter(function (v) {
return v != undefined;
});
return [res, errors];
};
export var spelFixList = function spelFixList(val) {
// `{1,2}.contains(1)` NOT works
// `{1,2}.?[true].contains(1)` works
return "".concat(val, ".?[true]");
};