@qrvey/formula-lang
Version:
QFormula support for qrvey projects
63 lines • 1.92 kB
JavaScript
import { hourScript } from '../utils/elasticsearch/scripts';
import { isDateParam, isStringParam, removeQuotes } from '../utils';
import { AST_PRIMITIVES, OPERATION_SCOPE } from '../constants';
import { isAnAllowedValue } from '../utils/isAnAllowedValue';
/**
* Returns the number of the HOUR in a Date.
*/
export const HOUR = {
identifier: 'HOUR',
operationScope: OPERATION_SCOPE.RAW,
functionScope: [OPERATION_SCOPE.RAW],
parameters: [
{
identifier: 'DATE',
optional: false,
expectedPrimitive: AST_PRIMITIVES.DATE,
validator: [isDateParam],
},
{
identifier: 'TIME_FORMAT',
optional: true,
expectedPrimitive: AST_PRIMITIVES.STRING,
validator: [isStringParam, isAnAllowedValue(['24h', '12h'])],
},
],
transpiler: {
elasticsearch,
snowflake,
redshift,
postgresql,
databricks,
},
primitiveResult: AST_PRIMITIVES.NUMBER,
};
function elasticsearch(value, timeFormat) {
return hourScript(value, removeQuotes(timeFormat !== null && timeFormat !== void 0 ? timeFormat : ''));
}
function SQL(value, timeFormat) {
if (removeQuotes(timeFormat !== null && timeFormat !== void 0 ? timeFormat : '') === '12h') {
return `(CASE
WHEN DATE_PART('HOUR', ${value})::INTEGER % 12 = 0 THEN
12
ELSE
DATE_PART('HOUR', ${value})::INTEGER % 12
END)`;
}
else {
return `DATE_PART('HOUR', ${value})`;
}
}
function snowflake(value, timeFormat) {
return SQL(value, timeFormat);
}
function redshift(value, timeFormat) {
return SQL(value, timeFormat);
}
function postgresql(value, timeFormat) {
return SQL(value, timeFormat);
}
function databricks(value, timeFormat) {
return SQL(value, timeFormat);
}
//# sourceMappingURL=hour.js.map