Skip to content

Commit

Permalink
Merge pull request #35 from PlayNetwork/v1.0.0
Browse files Browse the repository at this point in the history
V1.0.0
  • Loading branch information
brozeph committed Dec 20, 2016
2 parents 9ced9a6 + 5960444 commit 0133bee
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 61 deletions.
4 changes: 4 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v1.0.0 / 2016-12-19

* Refactoring sort using JSON API spec

# v0.3.0 / 2016-10-19

* Introduced fix for keyword filter where empty values caused a runtime exception (#33)
Expand Down
47 changes: 24 additions & 23 deletions lib/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,40 @@ module.exports = function(mongoose) {
}

var
asc = [],
desc = [],
fields = [],
self = this,
sort = options.sort || {},
value = null;

if (sort.desc) {
if (!(sort.desc instanceof Array)) {
desc.push(sort.desc);
} else {
desc = sort.desc;
}

desc.forEach(function (field) {
value = {};
value[field] = -1;
self.sort(value);
if (typeof sort === 'string') {
fields = sort.split(/\,/g).map(function (field) { return field.trim(); });
} else if (Array.isArray(sort) && sort.length) {
fields = sort;
} else if (typeof sort === 'object') {
Object.keys(sort).forEach(function (property) {
if (!isNaN(sort[property])) {
if (parseInt(sort[property], 10) < 0) {
fields.push('-' + property);
} else {
fields.push(property);
}
} else {
//property supplied is NaN; default to 1/ascending
fields.push(property);
}
});
}

if (sort.asc) {
if (!(sort.asc instanceof Array)) {
asc.push(sort.asc);
fields.forEach(function (field) {
value = {};
if (field.startsWith('-')) {
value[field.substring(1)] = -1;
} else {
asc = sort.asc;
value[field] = 1;
}

asc.forEach(function (field) {
value = {};
value[field] = 1;
self.sort(value);
});
}
self.sort(value);
});

return self;
};
Expand Down
36 changes: 15 additions & 21 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ var options = {
}
}
},
sort : {
desc : 'birthday',
asc : 'name'
},
sort : ['-birthday', 'name'],
start : 0,
count : 500
};
Expand Down Expand Up @@ -156,10 +153,7 @@ The options submitted to the `page(options, callback)` middleware method are ech
}
}
},
sort : {
desc : 'birthday',
asc : 'name'
},
sort : ['-birthday', 'name'],
start : 0
},
results : [ ... ], // the first 500 brindled, black or white kittehs named Hamish in Seattle
Expand Down Expand Up @@ -293,15 +287,13 @@ var options = {

### Sorting

Sorting, at this point, is fairly basic. All descending sorts will be applied prior to ascending sorts when specifying multiple sorts of each direction.
Sorting, at this point, is fairly basic. All descending sorts will be applied prior to ascending sorts when specifying multiple sorts of each direction. Supports JSON API specs.

#### Descending

```javascript
var options = {
sort : {
desc : ['name', 'description', 'knownAliases']
}
sort : ['-name', '-description', '-knownAliases']
};

KittehModel
Expand All @@ -312,12 +304,19 @@ KittehModel
});
```

You may also specify a single field (not an array) for both descending and ascending sorts:
You may also specify a single field (not an array) as well as an object for both descending and ascending sorts:

```javascript
var options = {
sort : '-name'
};
```

```javascript
var options = {
sort : {
desc : 'birthday'
'name': -1,
'description': 1
}
};
```
Expand All @@ -326,9 +325,7 @@ var options = {

```javascript
var options = {
sort : {
asc : ['name', 'description', 'knownAliases']
}
sort : ['name', 'description', 'knownAliases']
};

KittehModel
Expand All @@ -343,10 +340,7 @@ You may also specify ascending and descending sorts together:

```javascript
var options = {
sort : {
asc : 'name'
desc : ['birthday', 'home']
}
sort : ['name', '-birthday', '-home']
};
```

Expand Down
5 changes: 1 addition & 4 deletions test/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ describe('index', function () {
}
}
},
sort : {
desc : 'birthday',
asc : 'name'
},
sort: ['-birthday', 'name'],
start : 0,
count : 500
};
Expand Down
98 changes: 85 additions & 13 deletions test/lib/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ describe('order', function () {

it ('should sort fields in descending order when supplied', function () {
var options = {
sort : {
desc : 'name'
}
sort : '-name'
};

Kitteh
Expand All @@ -62,9 +60,24 @@ describe('order', function () {
});

it ('should sort fields in descending order when an array is supplied', function () {
var options = {
sort : ['-name', '-birthday']
};

Kitteh
.find()
.order(options);

sortClauseItems.should.have.length(2);
sortClauseItems[0].name.should.equals(-1);
sortClauseItems[1].birthday.should.equals(-1);
});

it ('should sort fields in descending order when an object is supplied', function () {
var options = {
sort : {
desc : ['name', 'birthday']
'name': -1,
'birthday': -1
}
};

Expand All @@ -79,9 +92,7 @@ describe('order', function () {

it ('should sort fields in ascending order when supplied', function () {
var options = {
sort : {
asc : 'name'
}
sort : ['name']
};

Kitteh
Expand All @@ -93,9 +104,24 @@ describe('order', function () {
});

it ('should sort fields in ascending order when an array is supplied', function () {
var options = {
sort : ['name', 'birthday']
};

Kitteh
.find()
.order(options);

sortClauseItems.should.have.length(2);
sortClauseItems[0].name.should.equals(1);
sortClauseItems[1].birthday.should.equals(1);
});

it ('should sort fields in ascending order when an object is supplied', function () {
var options = {
sort : {
asc : ['name', 'birthday']
'name': 1,
'birthday': 1
}
};

Expand All @@ -108,11 +134,39 @@ describe('order', function () {
sortClauseItems[1].birthday.should.equals(1);
});

it ('should sort fields in both ascending and descending order when supplied', function () {
it ('should sort fields in both ascending and descending order when supplied as a string', function () {
var options = {
sort : 'home, -name'
};

Kitteh
.find()
.order(options);

sortClauseItems.should.have.length(2);
sortClauseItems[0].home.should.equals(1);
sortClauseItems[1].name.should.equals(-1);
});

it ('should sort fields in both ascending and descending order when supplied as an array', function () {
var options = {
sort : ['home', '-name']
};

Kitteh
.find()
.order(options);

sortClauseItems.should.have.length(2);
sortClauseItems[0].home.should.equals(1);
sortClauseItems[1].name.should.equals(-1);
});

it ('should sort fields in both ascending and descending order when supplied as an object', function () {
var options = {
sort : {
asc : ['home'],
desc : ['name']
'home': 1,
'name': -1
}
};

Expand All @@ -121,7 +175,25 @@ describe('order', function () {
.order(options);

sortClauseItems.should.have.length(2);
sortClauseItems[0].name.should.equals(-1);
sortClauseItems[1].home.should.equals(1);
sortClauseItems[0].home.should.equals(1);
sortClauseItems[1].name.should.equals(-1);
});

it ('should sort fields in default ascending order when values in supplied object are not valid numbers', function () {
var options = {
sort : {
'home': 1,
'name': 'invalid number'
}
};

Kitteh
.find()
.order(options);

sortClauseItems.should.have.length(2);
sortClauseItems[0].home.should.equals(1);
sortClauseItems[1].name.should.equals(1);
});

});

0 comments on commit 0133bee

Please sign in to comment.