@qrvey/formula-lang
Version:
QFormula support for qrvey projects
208 lines (204 loc) • 8.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.subStringScript = exports.roundUpScript = exports.roundScript = exports.roundDownScript = exports.powerScript = exports.oddScript = exports.logScript = exports.evenScript = exports.includeScript = exports.replaceScript = exports.expScript = exports.sqrtScript = exports.lengthScript = exports.leastScript = exports.greatestScript = exports.properScript = exports.isNullScript = exports.getValueScript = exports.dateDif = exports.secondScript = exports.minuteScript = exports.hourScript = exports.yearScript = exports.monthScript = exports.dayScript = exports.dateFormatScript = exports.setTimezoneToColumnDate = exports.toDate = void 0;
const constants_1 = require("../../constants");
function toDate(value) {
return `ZonedDateTime.parse("${value}")`;
}
exports.toDate = toDate;
exports.setTimezoneToColumnDate = `
def ${constants_1.ELASTICSEARCH_SCRIPT_NAMES.setTimezone}(def date, String timezone) {
if (date == null) return null;
def value;
if (date instanceof List) {
if (date.empty) return null;
value = date.value;
} else {
value = date;
}
return value.withZoneSameInstant(ZoneId.of(timezone));
}
`;
exports.dateFormatScript = `
String ${constants_1.ELASTICSEARCH_SCRIPT_NAMES.dateFormat}(def date_value, String format) {
def isCustomDate = (date_value instanceof JodaCompatibleZonedDateTime || date_value instanceof ZonedDateTime);
if (date_value instanceof List && !isCustomDate) {
if (date_value.empty) return null;
date_value = date_value.value;
}
return DateTimeFormatter.ofPattern(format).format(date_value)
}
`;
function dateFormatToInt(value, format) {
return `Integer.parseInt(${constants_1.ELASTICSEARCH_SCRIPT_NAMES.dateFormat}(${value}, '${format}'))`;
}
const dayScript = (value) => dateFormatToInt(value, 'dd');
exports.dayScript = dayScript;
const monthScript = (value) => dateFormatToInt(value, 'MM');
exports.monthScript = monthScript;
const yearScript = (value) => dateFormatToInt(value, 'yyyy');
exports.yearScript = yearScript;
const hourScript = (value) => dateFormatToInt(value, 'HH');
exports.hourScript = hourScript;
const minuteScript = (value) => dateFormatToInt(value, 'mm');
exports.minuteScript = minuteScript;
const secondScript = (value) => dateFormatToInt(value, 'ss');
exports.secondScript = secondScript;
function dateDif(start, end, unit) {
const units = {
Y: 'YEARS',
M: 'MONTHS',
D: 'DAYS',
};
const currentUnit = units[unit];
return `ChronoUnit.${currentUnit}.between(${start}, ${end})`;
}
exports.dateDif = dateDif;
exports.getValueScript = `def GET_VALUE(def c){return (c.empty ? null : c.value)}`;
exports.isNullScript = `def IS_NULL_SCRIPT(def value, def replacement) { return value ?: replacement; }`;
exports.properScript = `String PROPER_STRING(String input) {
if (input == null) return null;
def str = input.toLowerCase();
def builder = new StringBuilder(str);
def chartLists = [" ", "!", "\\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "\\\\", "]", "^", "_", "\`", "{", "|", "}", "~"];
boolean flag = true;
for(int i = 0; i < builder.length(); i++) {
String ch = builder.charAt(i).toString();
if (flag) {
builder.replace(i, i + 1, ch.toUpperCase());
flag = false;
}
flag = chartLists.contains(ch);
}
return builder.toString();
}`;
exports.greatestScript = `def GREATEST(def numbers) {
def max;
if (numbers[0] instanceof ZonedDateTime) {
max = numbers[0];
if (max === null) return null;
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] === null) return null;
def current = numbers[i];
if (current.isAfter(max)) {
max = current;
}
}
return max;
} else {
max = numbers[0];
if (max === null) return null;
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] === null) return null;
if (numbers[i] > max) {
max = numbers[i];
}
}
return max;
}
}`;
exports.leastScript = `def LEAST(def numbers) {
def min;
if (numbers[0] instanceof ZonedDateTime) {
min = numbers[0];
if (min === null) return null;
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] === null) return null;
def current = numbers[i];
if (current.isBefore(min)) {
min = current;
}
}
return min;
} else {
min = numbers[0];
if (min === null) return null;
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] === null) return null;
if (numbers[i] < min) {
min = numbers[i];
}
}
return min;
}
}`;
exports.lengthScript = `def LENGTH(def str){return str != null ? str.length() : 0}`;
exports.sqrtScript = `def SQRT(def num){return num == null || num < 0 ? null : Math.sqrt(num) }`;
exports.expScript = `def EXP(def num){return num != null ? Math.exp(num) : null}`;
exports.replaceScript = `def REPLACE(def str, def target, def replacement) {
if (str == null || target == null || replacement == null) return null;
if (target == '') return str;
return str.replace(target, replacement);
}`;
exports.includeScript = `def INCLUDE(def str, def targetSearch) { if (str == null || targetSearch == null) return null; return str.contains(targetSearch);}`;
exports.evenScript = `def EVEN(def value) {
if (value == null) return null;
def _value = Math.abs(value);
def sign = value < 0 ? -1 : 1;
return ((long) Math.ceil(_value / 2.0) * 2) * sign;
}`;
exports.logScript = `def LOG(def base, def value) {
if (base == null || value == null) return null;
if (base <= 1 || value <= 0) return null;
return (Math.log(value) / Math.log(base));
}`;
exports.oddScript = `def ODD(def value) {
if (value == null) return null;
def _value = Math.abs(value);
def sign = value < 0 ? -1 : 1;
return ((long) Math.ceil((_value + 1)/ 2.0) * 2 - 1) * sign;
}`;
exports.powerScript = `def POWER(def value, def numberToRaise) {
if (value == null || numberToRaise == null) return null;
if (value == 0 && numberToRaise < 0) return null;
return Math.pow(value, numberToRaise);
}`;
exports.roundDownScript = `def ROUNDDOWN(def value, def places) {
if (value == null || places == null) return null;
if (places > 10) places = 10;
if (places < 0) places = 0;
def _value = Math.abs(value);
def sign = value < 0 ? -1 : 1;
def scale = Math.pow(10, places);
return (Math.floor(_value * scale) / scale) * sign;
}`;
exports.roundScript = `${exports.roundDownScript}
def ROUND(def value, def places) {
if (value == null || places == null) return null;
if (places > 10) places = 10;
if (places < 0) places = 0;
def _value = Math.abs(value);
def sign = value < 0 ? -1 : 1;
def scale = Math.pow(10, places);
return (Math.round(_value * scale) / scale) * sign;
}`;
exports.roundUpScript = `${exports.roundDownScript}
def ROUNDUP(def value, def places) {
if (value == null || places == null) return null;
if (places > 10) places = 10;
if (places < 0) places = 0;
def _value = Math.abs(value);
def sign = value < 0 ? -1 : 1;
def scale = Math.pow(10, places);
return (Math.ceil(_value * scale) / scale) * sign;
}`;
exports.subStringScript = `${exports.roundDownScript}
String ${constants_1.ELASTICSEARCH_SCRIPT_NAMES.subString}(def text, def start, def end) {
if (text == null || start == null || end == null) return null;
text = (String) text;
start = ROUNDDOWN(start, 0);
end = ROUNDDOWN(end, 0);
int first = 0;
if (end < 0 || (start * -1 > text.length())) return "";
if (start < 0) {
first = (int) Math.max(text.length() + start, 0);
} else if (start == 0) {
first = 0;
} else {
first = (int) Math.min(start - 1, text.length())
}
int last = (int) Math.min(first + end, text.length());
return text.substring(first, last);
}
`;
//# sourceMappingURL=scripts.js.map