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

Features/add ignore case enhancement #12

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
20 changes: 16 additions & 4 deletions lib/chai-sorted.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
var isSorted = require('./is.sorted')

var chaiIsSorted = function (chai, array, options) {
options = options === undefined ? {} : options

var useDescendingOrder = typeof options === 'object' ? !!options.descending : false

if (options.ignoreCase) {
return array.map(function (item) {
Copy link
Owner

Choose a reason for hiding this comment

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

@NoxPhoenix While this should cause a failure in the tests I think it passes because it results in being truthy. Nonetheless, this should be array = array.map(function

Copy link
Author

Choose a reason for hiding this comment

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

Ah, yeah. That makes sense. It was completely skipping the assertion. That is a good catch. Pushing the fix now.

return typeof item === 'string' ? item.toUpperCase() : item
})
}

// Backwards compatibility
if (typeof options === 'boolean') {
useDescendingOrder = options
Expand Down Expand Up @@ -32,24 +40,28 @@ module.exports = function (chai, utils) {
chaiIsSorted.call(this, chai, this._obj)
})

utils.addProperty(chai.Assertion.prototype, 'ignoreCase', function () {
chaiIsSorted.call(this, chai, this._obj, { ignoreCase: true })
})

chai.Assertion.addMethod('sortedBy', function (key, options) {
var array = utils.flag(this, 'object').map(function (item) {
return item[key]
})
chaiIsSorted.call(this, chai, array, options)
})

chai.Assertion.addMethod('descendingBy', function (key) {
chai.Assertion.addMethod('descendingBy', function (key, options) {
var array = utils.flag(this, 'object').map(function (item) {
return item[key]
})
chaiIsSorted.call(this, chai, array, { descending: true })
chaiIsSorted.call(this, chai, array, Object.assign({descending: true}, options))
})

chai.Assertion.addMethod('ascendingBy', function (key) {
chai.Assertion.addMethod('ascendingBy', function (key, options) {
var array = utils.flag(this, 'object').map(function (item) {
return item[key]
})
chaiIsSorted.call(this, chai, array)
chaiIsSorted.call(this, chai, array, options)
})
}
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ Test for descending sort order of array by `name` attribute
expect([{id:2,name:"bat"},{id:3,name:"apples"}]).to.be.sortedBy("name", {descending: true})
```

Test for sort order of array by `name` attribute ignoring case

```javascript
expect([{id:2,name:"bat"},{id:3,name:"Apples"}]).to.be.sortedBy("name", {ignoreCase: true})
```

### `.ascendingBy` method
Alternate of `sortedBy` but more explicit

Expand All @@ -78,6 +84,12 @@ Test for ascending sort order of array by `name` attribute
expect([{id:2,name:"apple"},{id:3,name:"bat"}]).to.be.ascendingBy("name")
```

Test for ascending sort order of array by `name` attribute ignoring case

```javascript
expect([{id:2,name:"apple"},{id:3,name:"Bat"}]).to.be.ascendingBy("name", {ignoreCase: true})
```

### `.descendingBy` method
Alternate of `sortedBy` but does not require passing `true` as a second parameter to `sortedBy`. It is the same as doing `sortBy("name",true)`

Expand Down
36 changes: 36 additions & 0 deletions test/chai-sorted.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,39 @@ describe('to.be.ascendingBy(property)', function () {
expect([{id: 1, name: 'a'}, {id: 34, name: 'boy'}, {id: 3, name: 'c'}, {size: 'large', name: 'cat'}]).to.be.ascendingBy('name')
})
})

describe('to.be.sortedBy({ignoreCase: true})', function () {
it('key id of numbers', function () {
expect([{id: 1}, {id: 2}, {id: 3}]).to.be.sortedBy('id', {ignoreCase: true})
})
it('key id of strings', function () {
expect([{id: 'a'}, {id: 'B'}, {id: 'c'}]).to.be.sortedBy('id', {ignoreCase: true})
})
it('key name of words and letters', function () {
expect([{id: 1, name: 'a'}, {id: 34, name: 'Boy'}, {id: 3, name: 'c'}, {size: 'large', name: 'cat'}]).to.be.sortedBy('name', {ignoreCase: true})
})
})

describe('to.be.ascendingBy({ignoreCase: true})', function () {
it('key id of numbers', function () {
expect([{id: 1}, {id: 2}, {id: 3}]).to.be.ascendingBy('id', {ignoreCase: true})
})
it('key id of strings', function () {
expect([{id: 'a'}, {id: 'B'}, {id: 'c'}]).to.be.ascendingBy('id', {ignoreCase: true})
})
it('key name of words and letters', function () {
expect([{id: 1, name: 'a'}, {id: 34, name: 'Boy'}, {id: 3, name: 'c'}, {size: 'large', name: 'cat'}]).to.be.ascendingBy('name', {ignoreCase: true})
})
})

describe('to.be.descendingBy({ignoreCase: true})', function () {
it('key id of numbers', function () {
expect([{id: 3}, {id: 2}, {id: 1}]).to.be.descendingBy('id', {ignoreCase: true})
})
it('key id of strings', function () {
expect([{id: 'c'}, {id: 'B'}, {id: 'a'}]).to.be.descendingBy('id', {ignoreCase: true})
})
it('key name of words and letters', function () {
expect([{id: 1, name: 'cat'}, {id: 34, name: 'C'}, {id: 3, name: 'boy'}, {size: 'large', name: 'b'}]).to.be.descendingBy('name', {ignoreCase: true})
})
})