From b5a2e8087812b21ba246cd5b06c76d17964d3d24 Mon Sep 17 00:00:00 2001 From: rolux Date: Wed, 29 Aug 2012 17:40:27 +0200 Subject: [PATCH] fix sort value for '[article] [number or quoted string]', add test --- source/Ox/js/Array.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/source/Ox/js/Array.js b/source/Ox/js/Array.js index da243f4f..7471b1f7 100644 --- a/source/Ox/js/Array.js +++ b/source/Ox/js/Array.js @@ -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);