Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some new functions to the standard library #217

Merged
merged 4 commits into from
Sep 26, 2024

Conversation

lorenzosinisi
Copy link
Contributor

@lorenzosinisi lorenzosinisi commented Aug 23, 2024

I have added the following functions:

  • MID
  • SWITCH
  • REMOVE_LAST_WORD
  • ROUND

The function MIX, SWITCH and ROUND are done mirroring Excel functionality, instead REMOVE_LAST_WORD is done to mirror the existing REMOVE_FIRST_WORD

This should help adoption and code repetition over multiple projects using this library

@smn
Copy link
Contributor

smn commented Aug 23, 2024

@lorenzosinisi whoa! thanks for taking the time to contribute this 🫶 !

Out of curiosity, how are you using this library currently? Would love to learn how it's being used, and hopefully you're finding it useful?

@lorenzosinisi
Copy link
Contributor Author

Hi @smn thanks, and I hope the functions can be merged in the standard lib!

I am currently using it to allow non-devs to write simple code to transform data, and yes it is useful!

@lorenzosinisi
Copy link
Contributor Author

Hi @smn is there any chance to get this merged?

"Convert a date into any strftime format (ref: https://man7.org/linux/man-pages/man3/strftime.3.html)",
expression: ~s[text(datevalue(date(2022, 09, 14)), "%m/%d/%Y")],
result: "09/14/2022"
def text(ctx, value, format) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this different from supplying the format directly to datevalue here? That function accepts a string formatting argument already:

@doc """
Converts date stored in text to an actual date object and
formats it using `strftime` formatting.
It will fallback to "%Y-%m-%d %H:%M:%S" if no formatting is supplied
"""
@expression_doc doc: "Convert a date from a piece of text to a formatted date string",
expression: "datevalue(\"2022-01-01\")",
result: %{
"__value__" => "2022-01-01 00:00:00",
"date" => ~D[2022-01-01],
"datetime" => ~U[2022-01-01 00:00:00Z]
}
@expression_doc doc: "Convert a date from a piece of text and read the date field",
expression: "datevalue(\"2022-01-01\").date",
result: ~D[2022-01-01]
@expression_doc doc: "Convert a date value and read the date field",
expression: "datevalue(date(2022, 1, 1)).date",
result: ~D[2022-01-01]
def datevalue(ctx, date, format) do
[date, format] = eval!([date, format], ctx)
if datetime = DateHelpers.extract_datetimeish(date) do
%{
"__value__" => Timex.format!(datetime, format, :strftime),
"date" => DateTime.to_date(datetime),
"datetime" => datetime
}
end
end

Screenshot 2024-08-27 at 11 18 07

I can see the text() function name here being ambiguous and likely a source of confusion for folks given this seems like a generic function but is only really useful for date / datetime things.

That said, if you're wanting to use this in your own code, you could perhaps include it in a custom callbacks implementation ? The implementation isn't ideal but here's an example test case that does exactly that: https://github.com/turnhub/expression/blob/develop/test/expression_custom_callbacks_test.exs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading https://support.microsoft.com/en-us/office/text-function-20d5ac4d-7b94-49fd-bb38-93d29371225c I understand why it's called text(), I guess it's a generic formatting function.

However, if we want to implement this we'll want to make sure to support other types of values as well. Until that is ready I would suggest keeping this one in a custom callbacks implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say we could remove this from this MR and leave it as custom callback, sounds good to me!

"Remove the last word from a list of words, using spaces as separator between words ",
expression: "remove_last_word(\"foo bar\")",
result: "foo"
def remove_last_word(ctx, binary) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one's great as it complements the already existing remove_first_word()

~s[The SWITCH function evaluates one value (called the expression) against a list of values, and returns the result corresponding to the first matching value. If there is no match, an optional default value (the last one in the list if the list is odd) may be returned],
expression: ~s[SWITCH(5, 1, "Sunday", 2, "Monday", 3, "Tuesday", "No match")],
result: "No match"
def switch_vargs(ctx, arguments) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes indeed, matches "SWITCH" 👍

"The ROUND function rounds a number to a specified number of digits. For example, if cell A1 contains 23.7825, and you want to round that value to zero decimal places you can do ROUND(23.7825)",
expression: "ROUND(23.7825)",
result: "24"
def round(ctx, value) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@expression_doc expression: ~s[MID("Fluid Flow", 20, 5)],
result: ""

def mid(ctx, text, start_num, num_chars) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@smn smn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for your contribution! It's highly appreciated. Before we merge this, can we discuss the implementation of text() ?

If you're unable to complete it then we can merge the others in the mean time and you can keep the text() implementation in a custom callback module.

If you're able to complete it then that's awesome, we'd love to see a complete implementation of text() merged.

It's up to you!

@lorenzosinisi
Copy link
Contributor Author

To simplify I have opted for removing (for now) the TEXT function, how does it look now?

@smn
Copy link
Contributor

smn commented Aug 28, 2024

Given the CLA that's in place, are you happy for us to merge these and for them to fall under the Turn.io copyright as per the CLA.md document in the repo? It looks like the CLA github action is broken (I'm trying to fix it) but I also don't want to hold up this merging if you're happy with it.

Give me a few hours to see if I can get this CLA to run again. Something with its permissions seems to have changed.

@smn
Copy link
Contributor

smn commented Aug 28, 2024

ok it looks fixed, could you merge in the latest changes in the develop branch in your PR? It should kick off the process.

@lorenzosinisi
Copy link
Contributor Author

lorenzosinisi commented Aug 28, 2024 via email

 - MID
 - TEXT
 - SWITCH
 - REMOVE_LAST_WORD
 - ROUND

The function MIX, TEXT, SWITCH and ROUND are done mirroring Excel functioality,
instead REMOVE_LAST_WORD is done to mirror the existing REMOVE_FIRST_WORD
Copy link
Contributor

github-actions bot commented Aug 29, 2024

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@lorenzosinisi
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@lorenzosinisi
Copy link
Contributor Author

recheck

@lorenzosinisi lorenzosinisi requested a review from smn August 29, 2024 14:15
@lorenzosinisi
Copy link
Contributor Author

Hi @smn any chance of merging this one?

@smn smn merged commit 63e8250 into turnhub:develop Sep 26, 2024
1 of 2 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 26, 2024
@smn
Copy link
Contributor

smn commented Sep 26, 2024

thanks @lorenzosinisi ! Apologies for the delay in the review + merge, appreciate the contribution!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants