@qrvey/formula-lang
Version:
QFormula support for qrvey projects
66 lines • 2.15 kB
JavaScript
import { AST_PRIMITIVES, ELASTICSEARCH_SCRIPT_NAMES, OPERATION_SCOPE, } from '../constants';
import { isNumberParam, isStringParam } from '../utils';
import { greaterThanOrEqualToReference } from '../utils/greaterThanOrEqualToReference';
import { isInteger } from '../utils/isInteger';
/**
* `RIGHT` Returns the last character or characters in a text string, based on the number of characters you specify.
*/
export const RIGHT = {
identifier: 'RIGHT',
operationScope: OPERATION_SCOPE.RAW,
functionScope: [OPERATION_SCOPE.RAW],
parameters: [
{
identifier: 'CURRENT',
optional: false,
expectedPrimitive: AST_PRIMITIVES.STRING,
validator: [isStringParam],
},
{
identifier: 'EXTRACT_LENGTH',
optional: true,
expectedPrimitive: AST_PRIMITIVES.NUMBER,
validator: [
isNumberParam,
greaterThanOrEqualToReference(0),
isInteger,
],
},
],
transpiler: {
elasticsearch,
snowflake,
redshift,
postgresql,
databricks,
},
primitiveResult: AST_PRIMITIVES.STRING,
};
function elasticsearch(current, length) {
const _length = length !== null && length !== void 0 ? length : 1;
return `${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);
}
function databricks(current, length) {
return SQL(current, length);
}
//# sourceMappingURL=right.js.map