@qrvey/formula-lang
Version:
QFormula support for qrvey projects
52 lines • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.dateDifPostgres = exports.dateDif = exports.toDate = void 0;
function toDate(value) {
return `CAST('${value}' AS TIMESTAMP)`;
}
exports.toDate = toDate;
function generalDateDif(start, end, unit) {
const units = {
Y: 'year',
M: 'month',
D: 'day',
};
const currentUnit = units[unit];
return `DATEDIFF('${currentUnit}', ${start}, ${end})`;
}
function customDateDifYear({ start, end, yearDif }) {
const customFun = (date) => {
return `EXTRACT(MONTH FROM ${date}) * 100 + EXTRACT(DAY FROM ${date})`;
};
const startComparison = customFun(start);
const endComparison = customFun(end);
const result = `CASE
WHEN ${startComparison} <= ${endComparison}
THEN ${yearDif}
ELSE ${yearDif} - 1
END`;
return result;
}
function commonSQLDifYear(start, end) {
const yearDif = generalDateDif(start, end, 'Y');
return customDateDifYear({ start, end, yearDif });
}
function dateDif(start, end, unit) {
const units = {
Y: commonSQLDifYear,
M: generalDateDif,
D: generalDateDif,
};
return units[unit](start, end, unit);
}
exports.dateDif = dateDif;
function dateDifPostgres(start, end, unit) {
if (unit === 'D')
return `DATE_PART('day', ${end} - ${start})`;
const yearDif = `DATE_PART('year', ${end}) - DATE_PART('year', ${start})`;
if (unit === 'M')
return `(${yearDif}) * 12 + (DATE_PART('month', ${end}) - DATE_PART('month', ${start}))`;
return customDateDifYear({ start, end, yearDif });
}
exports.dateDifPostgres = dateDifPostgres;
//# sourceMappingURL=scripts.js.map