@qrvey/formula-lang
Version:
QFormula support for qrvey projects
63 lines • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RIGHT = void 0;
const constants_1 = require("../constants");
const utils_1 = require("../utils");
const greaterThanOrEqualToReference_1 = require("../utils/greaterThanOrEqualToReference");
const isInteger_1 = require("../utils/isInteger");
/**
* `RIGHT` Returns the last character or characters in a text string, based on the number of characters you specify.
*/
exports.RIGHT = {
identifier: 'RIGHT',
parameters: [
{
identifier: 'CURRENT',
optional: false,
expectedPrimitive: constants_1.AST_PRIMITIVES.STRING,
validator: [utils_1.isStringParam],
},
{
identifier: 'EXTRACT_LENGTH',
optional: true,
expectedPrimitive: constants_1.AST_PRIMITIVES.NUMBER,
validator: [
utils_1.isNumberParam,
(0, greaterThanOrEqualToReference_1.greaterThanOrEqualToReference)(0),
isInteger_1.isInteger,
],
},
],
transpiler: {
elasticsearch,
snowflake,
redshift,
postgresql,
},
primitiveResult: constants_1.AST_PRIMITIVES.STRING,
};
function elasticsearch(current, length) {
const _length = length !== null && length !== void 0 ? length : 1;
return `${constants_1.ELASTICSEARCH_SCRIPT_NAMES.subString}(${current}, ${_length} * -1, ${_length})`;
}
function snowflake(current, length) {
return `RIGHT(${current}, FLOOR(${length !== null && length !== void 0 ? length : 1})::INTEGER)`;
}
function SQL(current, length) {
const _length = `FLOOR(${length !== null && length !== void 0 ? length : 1})::INTEGER`;
return `
(case
when ${length !== null && length !== void 0 ? length : 1} IS NULL then NULL
when (${_length} < 0) then ''
else
RIGHT(${current}, ${_length})
end
)`.trim();
}
function redshift(current, length) {
return SQL(current, length);
}
function postgresql(current, length) {
return SQL(current, length);
}
//# sourceMappingURL=right.js.map