UNPKG

@qrvey/formula-lang

Version:

QFormula support for qrvey projects

90 lines 2.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DAYOFWEEK = void 0; const constants_1 = require("../constants"); const utils_1 = require("../utils"); const isAnAllowedValue_1 = require("../utils/isAnAllowedValue"); /** * `DAYOFWEEK` Returns a number or string representative of the weekday for a given date. */ exports.DAYOFWEEK = { identifier: 'DAYOFWEEK', parameters: [ { identifier: 'DATE', optional: false, expectedPrimitive: constants_1.AST_PRIMITIVES.DATE, validator: [utils_1.isDateParam], }, { identifier: 'FORMAT', optional: true, expectedPrimitive: constants_1.AST_PRIMITIVES.STRING, validator: [utils_1.isStringParam, (0, isAnAllowedValue_1.isAnAllowedValue)(['Day', 'D'])], }, ], transpiler: { elasticsearch, snowflake, redshift, postgresql, }, primitiveResult(args) { var _a; const argument = args === null || args === void 0 ? void 0 : args[1]; const format = (_a = argument === null || argument === void 0 ? void 0 : argument.value) !== null && _a !== void 0 ? _a : 'D'; if (format === 'Day') return constants_1.AST_PRIMITIVES.STRING; return constants_1.AST_PRIMITIVES.NUMBER; }, }; function elasticsearch(date, format) { let _format = format !== null && format !== void 0 ? format : 'D'; _format = (0, utils_1.removeQuotes)(_format); switch (_format) { case 'D': return `((${date}.getDayOfWeek().getValue() % 7) + 1)`; case 'Day': return `${date}.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH)`; default: return ''; } } function SQL(date, format) { let _format = format !== null && format !== void 0 ? format : 'D'; _format = (0, utils_1.removeQuotes)(_format); switch (_format) { case 'Day': return `TO_CHAR(${date}, 'Day')`; case 'D': return `CAST(TO_CHAR(${date}, 'D') AS NUMERIC)`; default: return ''; } } function snowflake(date, format) { let _format = format !== null && format !== void 0 ? format : 'D'; _format = (0, utils_1.removeQuotes)(_format); switch (_format) { case 'Day': return `DECODE(EXTRACT('dayofweek_iso', ${date}), 1, 'Monday', 2, 'Tuesday', 3, 'Wednesday', 4, 'Thursday', 5, 'Friday', 6, 'Saturday', 7, 'Sunday')`; case 'D': return `DAYOFWEEK(${date}) + 1`; default: return ''; } } function redshift(date, format) { return SQL(date, format); } function postgresql(date, format) { return SQL(date, format); } //# sourceMappingURL=dayofweek.js.map