fix sort value for '[article] [number or quoted string]', add test

This commit is contained in:
rolux 2012-08-29 17:40:27 +02:00
parent 0de55efb96
commit b5a2e80878

View file

@ -583,6 +583,13 @@ Ox.range = function() {
// make lowercase, remove leading non-word characters,
// pad numbers and move leading articles to the end
array.forEach(function(value, i) {
function pad(value) {
return value
.replace(/^\W+/, '')
.replace(/\d+/g, function(match) {
return Ox.pad(match, 'left', length, '0');
});
}
var mappedValue = mappedArray[i];
if (
Ox.isEmpty(mappedValue)
@ -591,17 +598,15 @@ Ox.range = function() {
) {
sort[value] = null;
} else if (Ox.isString(mappedValue)) {
sort[value] = mappedValue.toLowerCase()
.replace(/^\W+/, '')
.replace(/\d+/g, function(match) {
return Ox.pad(match, 'left', length, '0');
});
sort[value] = pad(mappedValue.toLowerCase());
Ox.forEach(['a', 'an', 'the'], function(article) {
var length;
if (new RegExp('^' + article + ' ').test(sort[value])) {
length = article.length;
sort[value] = sort[value].slice(length + 1) + ', '
+ sort[value].slice(0, length);
sort[value] = pad(
sort[value].slice(length + 1) + ', '
+ sort[value].slice(0, length)
);
return false; // break
}
});
@ -626,6 +631,8 @@ Ox.range = function() {
['In 9 Minutes Around the World', 'In 80 Days Around the World']
> Ox.sort(['Man', 'A Plan', 'The Canal'])
['The Canal', 'Man', 'A Plan']
> Ox.sort(['The 9', 'The 10', 'An A', 'A "B"'])
['The 9', 'The 10', 'An A', 'A "B"']
@*/
Ox.sort = function(array, map) {
var values = getSortValues(map ? array.map(map) : array);