@qrvey/formula-lang
Version:
QFormula support for qrvey projects
65 lines • 2.1 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';
/**
* `LEFT` Returns the first character or characters in a text string, based on the number of characters you specify.
*/
export const LEFT = {
identifier: 'LEFT',
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) {
return `${ELASTICSEARCH_SCRIPT_NAMES.subString}(${current}, 0, ${length !== null && length !== void 0 ? length : 1})`;
}
function snowflake(current, length) {
return `LEFT(${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
LEFT(${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=left.js.map