UNPKG

@qrvey/formula-lang

Version:

QFormula support for qrvey projects

216 lines (212 loc) 7.81 kB
import { ELASTICSEARCH_SCRIPT_NAMES as SCRIPT_NAMES } from '../../constants'; export function toDate(value) { return `ZonedDateTime.parse("${value}")`; } export const setTimezoneToColumnDate = ` def ${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)); } `; export const dateFormatScript = ` String ${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(${SCRIPT_NAMES.dateFormat}(${value}, '${format}'))`; } export const dayScript = (value) => dateFormatToInt(value, 'dd'); export const monthScript = (value) => dateFormatToInt(value, 'MM'); export const yearScript = (value) => dateFormatToInt(value, 'yyyy'); export const hourScript = (value, timeFormat) => { if (timeFormat === '12h') { return dateFormatToInt(value, 'hh'); } else { return dateFormatToInt(value, 'HH'); } }; export const minuteScript = (value) => dateFormatToInt(value, 'mm'); export const secondScript = (value) => dateFormatToInt(value, 'ss'); /** * Extracts the nanosecond value from a given date and divides it by 1,000,000. */ export const millisecondScript = (value) => `(${dateFormatToInt(value, 'n')} / 1000000.0)`; export function dateDif(start, end, unit) { const units = { Y: 'YEARS', M: 'MONTHS', D: 'DAYS', H: 'HOURS', MI: 'MINUTES', S: 'SECONDS', }; const currentUnit = units[unit]; return `ChronoUnit.${currentUnit}.between(${start}, ${end})`; } export const getValueScript = `def GET_VALUE(def c){return (c.empty ? null : c.value)}`; export const isNullScript = `def IS_NULL_SCRIPT(def value, def replacement) { return value ?: replacement; }`; export const 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(); }`; export const 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; } }`; export const 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; } }`; export const lengthScript = `def LENGTH(def str){return str != null ? str.length() : 0}`; export const sqrtScript = `def SQRT(def num){return num == null || num < 0 ? null : Math.sqrt(num) }`; export const expScript = `def EXP(def num){return num != null ? Math.exp(num) : null}`; export const 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); }`; export const includeScript = `def INCLUDE(def str, def targetSearch) { if (str == null || targetSearch == null) return null; return str.contains(targetSearch);}`; export const 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; }`; export const 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)); }`; export const 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; }`; export const 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); }`; export const 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; }`; export const roundScript = `${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; }`; export const roundUpScript = `${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; }`; export const subStringScript = `${roundDownScript} String ${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); } `; export const textScript = `def TEXT(def v) { return v == null ? null : v.toString(); }`; export const nowScript = `ZonedDateTime ${SCRIPT_NAMES.now}() { def instant = Instant.ofEpochMilli(new Date().getTime()); return ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); }`; //# sourceMappingURL=scripts.js.map