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

Invalid search results #309

Open
edwinvdpol opened this issue Sep 16, 2022 · 1 comment
Open

Invalid search results #309

edwinvdpol opened this issue Sep 16, 2022 · 1 comment

Comments

@edwinvdpol
Copy link

Hi there,

I already saw a few issues which were related to search, but can someone explain this behaviour?
I am searching for a string value.

const { collect } = require('collect.js'); 

const c = collect(['0', '00', '000', '0-0', '0*0', '0&0']);
// Collection { items: [ '0', '00', '000', '0-0', '0*0', '0&0' ] }

c.search('0', true); // Result: 0
c.search('00', true); // Result: 1 (correct!)
c.search('000', true); // Result: 2
c.search('0-0', true); // Result: 3
c.search('0&0', true); // Result: 5
c.search('0&0', true); // Result: 5

Am I doing to something wrong here?

Thanks,
Edwin

@jjjrmy
Copy link

jjjrmy commented Jan 21, 2023

This is expected behavior, as per the documentation:

The search method searches the collection for the given value and returns its key if found. If the item is not found, false is returned.

I have also confirmed the same results using Laravel's Collection.

You are using all strings in your collection, so there is no reason to pass true as the second parameter.

This issue should be closed.

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