-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for descending ordering using "-property" (#115)
- Loading branch information
Showing
7 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Order should be a String (with the property name assumed ascending) | ||
* or an Array or property String names. | ||
* | ||
* Examples: | ||
* | ||
* 1. 'property1' (ORDER BY property1 ASC) | ||
* 2. '-property1' (ORDER BY property1 DESC) | ||
* 3. [ 'property1' ] (ORDER BY property1 ASC) | ||
* 4. [ '-property1' ] (ORDER BY property1 DESC) | ||
* 5. [ 'property1', 'A' ] (ORDER BY property1 ASC) | ||
* 6. [ 'property1', 'Z' ] (ORDER BY property1 DESC) | ||
* 7. [ '-property1', 'A' ] (ORDER BY property1 ASC) | ||
* 8. [ 'property1', 'property2' ] (ORDER BY property1 ASC, property2 ASC) | ||
* 9. [ 'property1', '-property2' ] (ORDER BY property1 ASC, property2 DESC) | ||
* ... | ||
*/ | ||
|
||
exports.standardizeOrder = function (order) { | ||
if (typeof order == "string") { | ||
if (order[0] == "-") { | ||
return [ [ order.substr(1), "Z" ] ]; | ||
} | ||
return [ [ order, "A" ] ]; | ||
} | ||
|
||
var new_order = [], minus; | ||
|
||
for (var i = 0; i < order.length; i++) { | ||
minus = (order[i][0] == "-"); | ||
|
||
if (i < order.length - 1 && [ "A", "Z" ].indexOf(order[i + 1].toUpperCase()) >= 0) { | ||
new_order.push([ | ||
(minus ? order[i].substr(1) : order[i]), | ||
order[i + 1] | ||
]); | ||
i += 1; | ||
} else if (minus) { | ||
new_order.push([ order[i].substr(1), "Z" ]); | ||
} else { | ||
new_order.push([ order[i], "A" ]); | ||
} | ||
} | ||
|
||
return new_order; | ||
}; |