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

Optionally ignore casing #1

Closed
shinmen14 opened this issue Jul 5, 2016 · 4 comments
Closed

Optionally ignore casing #1

shinmen14 opened this issue Jul 5, 2016 · 4 comments

Comments

@shinmen14
Copy link

shinmen14 commented Jul 5, 2016

Hi,

It looks like the sorting check fails when a string has an upper case letter in the middle. For instance, the following scenario fails the check:

expect([{name: "Dedoto"}, {name: "DeSoto"}]).to.be.sortedBy('name');

If I change the S to s, it will work as expected:

expect([{name: "Dedoto"}, {name: "Desoto"}]).to.be.sortedBy('name');

I'm currently testing an endpoint that returns an array of US cities and the test case was failing because of that specific scenario. It turns out DeSoto is an actual city located in Texas.

Let me know if you need any additional information.

Thanks.

@johntimothybailey johntimothybailey self-assigned this Mar 22, 2018
@johntimothybailey
Copy link
Owner

johntimothybailey commented Mar 22, 2018

@shinmen14 'DeSoto' weighs differently than 'Desoto' and even 'desoto' Basically, capital letters in javascript compare differently than lower case, so while it looks like 'DeSoto' should be after 'Dedoto' that is not the case unless we push them to the same case

Take for instance,

['Dedoto', 'DeSoto'].sort() 
// outputs ["DeSoto", "Dedoto"]

'S'.charCodeAt(0)
// outputs 83
's'.charCodeAt(0)
// outputs 115

#2 suggests changing the API a bit to allow for a more explicit usage. I could add something similar to sortedBy('name', {ignoreCase: true})

What do you think?

@johntimothybailey johntimothybailey changed the title Sorting fails if a string has an upper case letter in the middle of the word Optionally ignore casing May 11, 2018
@johntimothybailey
Copy link
Owner

@shinmen14 due to response time, I'm going to assume the best compromise is for a feature enhancement allowing. If you have any further thoughts on the matter I'm more than happy to incorporate those as well.

@NoxPhoenix
Copy link

Came here looking for just this. I figured I would help out and open a PR to add this enhancement.
#12

@RayeesMir
Copy link

RayeesMir commented Jul 3, 2019

const keys = ['A', 'AN', 'AO', 'AL', 'Al', 'Ali', 'AP']
const sortedKeys = keys.sort((a, b) => a - b)
expect(sortedKeys).to.be.sorted()

why this isn't working?
shouldn't this work?

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

No branches or pull requests

4 participants