Skip to content
New issue

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

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

Already on GitHub? # to your account

String comparing problem #1283

Closed
SavchenkoVV opened this issue Nov 21, 2022 · 8 comments
Closed

String comparing problem #1283

SavchenkoVV opened this issue Nov 21, 2022 · 8 comments

Comments

@SavchenkoVV
Copy link

'name' === 'name' // true
String('name') === String('name') // false

Why wrapped strings not equals ?
And how it possible to use switch operator with java string?

@p-bakker
Copy link
Collaborator

Most likely due to how you've configured Rhino, see #247 (comment)

@SavchenkoVV
Copy link
Author

Most likely due to how you've configured Rhino

Thanks for replying.
Tried with the setJavaPrimitiveWrap setting (true/false) - no changes in my case.

@p-bakker
Copy link
Collaborator

How are you getting your Java String instances then?

@SavchenkoVV
Copy link
Author

By call lambda, but it is not important. Problem in comparing wrapped strings

String('name') === String('name') // false

@p-bakker
Copy link
Collaborator

By call lambda, but it is not important

Well, depending on the setJavaPrimitiveWrap setting they ought to be wrapped as Native JavaScript strings or not. When they are JavaScript strings, String('name') === String('name') should yield true, but if they are Java String instances it's imho expected that String('name') === String('name') yields false, as it would in Java because you're comparing instances.

@SavchenkoVV
Copy link
Author

I tested setJavaPrimitiveWrap with true and false.
Always String('name') === String('name') // false
Maybe i do something wrong...

As i understand switch operator use === to compare in cases.
So, if yot have string param v and do

switch(String(v).toLowerCase()) {
}

no one case will work.
May be it releted with #891

@p-bakker
Copy link
Collaborator

Without having some sample code that reproduces the issue, there's no telling whether this is expected behavior, a bug or whether you're doing something wrong

@p-bakker
Copy link
Collaborator

Tried new java.lang.String('name').length() && new java.lang.String('name') !== new java.lang.String('name') && String(new java.lang.String('name')) === String(new java.lang.String('name')) but yields true as expected.

This code shows that:

  1. a JavaString is not wrapped (as it has a .length() method (Java), instead of .length property (JavaScript)
  2. comparing to Java String instances returns false
  3. comparing to Java String instances wrapped in a JavaScript string returns true

So afaics all works as expected. As no sample code has been provided that reproduces the issue, I'm closing this issue

Feel free to reopen with a testcode reproducing the issue

@p-bakker p-bakker closed this as not planned Won't fix, can't repro, duplicate, stale Oct 17, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants