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

unwrap issues #49

Open
shyamrajprasad opened this issue Jan 28, 2023 · 2 comments
Open

unwrap issues #49

shyamrajprasad opened this issue Jan 28, 2023 · 2 comments

Comments

@shyamrajprasad
Copy link

There is an issue when we have to unwrap characters at the start or at the end only. I am highlighting 4 issues in below examples.

%dw 2.0
import unwrap from dw::core::Strings
output application/json
---
{
  "a": unwrap(null, ""),
  "b": unwrap(null, '\0'),
  "c": unwrap("'abc'", "'"),
  "d": unwrap("AABabcBAA", 'A'),
  "e": unwrap("A", '#'),
  "f": unwrap("#A", '#'), //current ouptut is "A#", which is wrong and this is not available in input also. Output should be "A"
  "g": unwrap("A#", '#') // current output is "#A", which is wrong and this is not available in input also. Output should be "A",
  "h": unwrap("ABC", 'A'), //current output is "B", which is wrong, it should not remove C from the input. 
  "i": unwrap("ABC",'C') //current output is "B", which is wrong, it should not remove A from the input. 
}

My suggestion will be to update the unwrap function to consider the wrapping characters from the end or start. I have the below suggestions.

fun unwrap(text: String, wrapper: String): String = do {
    if(!startsWith(text, wrapper) and !endsWith(text, wrapper))
        text
    else if(startsWith(text, wrapper) and !endsWith(text, wrapper))
        text[1 to -1]
    else if(!startsWith(text, wrapper) and endsWith(text, wrapper))
        text[0 to -2]         
    else
        text[1 to -2] default text
}
@machaval
Copy link
Contributor

machaval commented Jan 30, 2023

Hi @shyamrajprasad though the bug you are reporting does exits, I don't think that the solution is the one I would suggest. For me it should only remove or unwrap if it starts and ends with the specified character.

So I would propose

@Since(version = "2.2.0")
fun unwrap(text: String, wrapper: String): String = do {
    if(!startsWith(text, wrapper) or !endsWith(text, wrapper)) //changing from and to or
        text
    else
        text[1 to -2] default text
}

So the output for your input is going to be

{
  "a": null,
  "b": null,
  "c": "abc",
  "d": "ABabcBA",
  "e": "A",
  "f": "#A",
  "g": "A#",
  "h": "ABC",
  "i": "ABC"
}

What are your thoughts?

@shyamrajprasad
Copy link
Author

@machaval I am fine with this proposal.

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

No branches or pull requests

2 participants