1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-14 09:08:32 +00:00

Merge branch '1940-add-split_part-formula-function' into 'develop'

Resolve "Add split_part formula function"

Closes 

See merge request 
This commit is contained in:
Eimantas Stonys 2023-09-13 08:44:06 +00:00
commit f9772b12ac
7 changed files with 88 additions and 0 deletions
backend
src/baserow/contrib/database/formula/ast
tests/baserow/contrib/database/formula
changelog/entries/unreleased/feature
web-frontend
locales
modules/database
test/unit/database/formula

View file

@ -151,6 +151,7 @@ def register_formula_functions(registry):
registry.register(BaserowEven())
registry.register(BaserowOdd())
registry.register(BaserowTrunc())
registry.register(BaserowSplitPart())
registry.register(BaserowLn())
registry.register(BaserowExp())
registry.register(BaserowLog())
@ -854,6 +855,50 @@ class BaserowFloor(OneArgumentBaserowFunction):
return Floor(arg, output_field=int_like_numeric_output_field())
class BaserowSplitPart(ThreeArgumentBaserowFunction):
type = "split_part"
arg1_type = [BaserowFormulaTextType]
arg2_type = [BaserowFormulaTextType]
arg3_type = [BaserowFormulaNumberType]
def type_function(
self,
func_call: BaserowFunctionCall[UnTyped],
arg1: BaserowExpression[BaserowFormulaTextType],
arg2: BaserowExpression[BaserowFormulaTextType],
arg3: BaserowExpression[BaserowFormulaNumberType],
) -> BaserowExpression[BaserowFormulaType]:
return func_call.with_valid_type(
BaserowFormulaTextType(
nullable=arg1.expression_type.nullable
or arg2.expression_type.nullable
or arg3.expression_type.nullable
)
)
def to_django_expression(
self, arg1: Expression, arg2: Expression, arg3: Expression
) -> Expression:
return Case(
When(
condition=(
LessThanEqualOrExpr(
arg3, Value(0), output_field=fields.BooleanField()
)
),
then=Value(""),
),
default=Func(
arg1,
arg2,
trunc_numeric_to_int(arg3),
function="SPLIT_PART",
output_field=fields.CharField(),
),
output_field=fields.CharField(),
)
class BaserowTrunc(OneArgumentBaserowFunction):
type = "trunc"
arg_type = [BaserowFormulaNumberType]

View file

@ -358,6 +358,15 @@ VALID_FORMULA_TESTS = [
"http://example.com/wiki/Se%c3%b1or",
),
("encode_uri_component('Hello World')", "Hello%20World"),
("split_part('John, Jane, Matthew', ', ', 2)", "Jane"),
("split_part('John, Jane, Matthew', 'xxx', 2)", ""),
("split_part('John, Jane, Matthew', ', ', -1.5)", ""),
("split_part('John, Jane, Matthew', ', ', -1)", ""),
("split_part('John, Jane, Matthew', ', ', 0)", ""),
("split_part('John, Jane, Matthew', ', ', 1.5)", "John"),
("split_part('John, Jane, Matthew', ', ', 3.5)", "Matthew"),
("split_part('John, Jane, Matthew', ', ', 4.5)", ""),
("split_part('John, Jane, Matthew', ', ', 9999)", ""),
]

View file

@ -0,0 +1,7 @@
{
"type": "feature",
"message": "add split_part formula function",
"issue_number": 1940,
"bullet_points": [],
"created_at": "2023-09-12"
}

View file

@ -337,6 +337,7 @@
"formulaFunctions": {
"upperDescription": "Returns its argument in upper case.",
"lowerDescription": "Returns its argument in lower case.",
"splitPartDescription": "Extracts a segment from a delimited string based on a delimiter and index (numeric indicator indicating which element from string should be returned)",
"concatDescription": "Returns its arguments joined together as a single piece of text.",
"addDescription": "Returns its two arguments added together.",
"minusDescription": "Returns its two arguments subtracted.",

View file

@ -86,6 +86,29 @@ export class BaserowLower extends BaserowFunctionDefinition {
}
}
export class BaserowSplitPart extends BaserowFunctionDefinition {
static getType() {
return 'split_part'
}
getDescription() {
const { i18n } = this.app
return i18n.t('formulaFunctions.splitPartDescription')
}
getSyntaxUsage() {
return ['split_part(text, delimiter, position)']
}
getExamples() {
return ["split_part('John, Jane, Michael', ', ', 2) = 'Jane'"]
}
getFormulaType() {
return 'text'
}
}
export class BaserowConcat extends BaserowFunctionDefinition {
static getType() {
return 'concat'

View file

@ -130,6 +130,7 @@ import {
BaserowLessThan,
BaserowLessThanOrEqual,
BaserowLower,
BaserowSplitPart,
BaserowMinus,
BaserowMultiply,
BaserowNot,
@ -462,6 +463,7 @@ export default (context) => {
'formula_function',
new BaserowEncodeUriComponent(context)
)
app.$registry.register('formula_function', new BaserowSplitPart(context))
// Number functions
app.$registry.register('formula_function', new BaserowMultiply(context))
app.$registry.register('formula_function', new BaserowDivide(context))

View file

@ -93,6 +93,7 @@ describe('Formula Functions Test', () => {
'button',
'get_link_label',
'get_link_url',
'split_part',
]
const frontendFunctionTypes = Object.keys(
testApp.store.$registry.getAll('formula_function')