fix a bug in sort, rename vars, re-enable Ox.api documentation

This commit is contained in:
rolux 2012-05-28 07:52:46 +02:00
parent 01493c1fe5
commit 8210a0c5cd

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
/* /*@
Ox.api <f> Turns an array into a list API Ox.api <f> Turns an array into a list API
<code>Ox.api</code> takes an array and returns a function that allows you to <code>Ox.api</code> takes an array and returns a function that allows you to
run complex queries against it. See the examples below for details. run complex queries against it. See the examples below for details.
@ -8,7 +8,7 @@ Ox.api <f> Turns an array into a list API
items <[o]> An array of objects (key/value stores) items <[o]> An array of objects (key/value stores)
options <o> Options object options <o> Options object
cache <b|false> If true, cache results cache <b|false> If true, cache results
enums <o> Enumerables, for example <code>{size: ['S', 'M', 'L', 'XL']}</code> enums <o> Enumerables, for example <code>{size: ['S', 'M', 'L']}</code>
geo <b|false> If true, return combined area with totals geo <b|false> If true, return combined area with totals
sort <[o]|[s]> Default sort, for example <code> ['+name', '-age'] sort <[o]|[s]> Default sort, for example <code> ['+name', '-age']
sums <[s]> List of keys to be included in totals sums <[s]> List of keys to be included in totals
@ -113,7 +113,7 @@ Ox.api <f> Turns an array into a list API
{positions: {foo: 2, bar: 0}} {positions: {foo: 2, bar: 0}}
> Ox.test.apiResults[8].data > Ox.test.apiResults[8].data
{items: [{i: 2, size: 'L'}, {i: 1, size: 'M'}]} {items: [{i: 2, size: 'L'}, {i: 1, size: 'M'}]}
*/ @*/
Ox.api = function(items, options) { Ox.api = function(items, options) {
var api = { var api = {
@ -466,50 +466,50 @@ Ox.range = function() {
(function() { (function() {
function getSortValues(arr, fn) { function getSortValues(array, map) {
var arr_ = fn ? arr.map(fn) : arr, var mappedArray = map ? array.map(map) : array,
len, matches = [], sort = {}; length, matches = [], sort = {};
// find numbers // find numbers
arr.forEach(function(val, i) { array.forEach(function(value, i) {
var match; var match, mappedValue = mappedArray[i];
if (Ox.isString(val)) { if (Ox.isString(mappedValue)) {
match = arr_[i].match(/\d+/g); match = mappedValue.match(/\d+/g);
if (match) { if (match) {
matches = matches.concat(match); matches = matches.concat(match);
} }
} }
}); });
// get length of longest number // get length of longest number
len = Ox.max(Ox.map(matches, function(val) { length = Ox.max(Ox.map(matches, function(value) {
return val.length; return value.length;
})); }));
// make lowercase, remove leading non-word characters, // make lowercase, remove leading non-word characters,
// pad numbers and move leading articles to the end // pad numbers and move leading articles to the end
arr.forEach(function(val, i) { array.forEach(function(value, i) {
var val_ = arr_[i]; var mappedValue = mappedArray[i];
if ( if (
Ox.isEmpty(val_) Ox.isEmpty(mappedValue)
|| Ox.isNull(val_) || Ox.isNull(mappedValue)
|| Ox.isUndefined(val_) || Ox.isUndefined(mappedValue)
) { ) {
sort[val] = null; sort[value] = null;
} else if (Ox.isString(val_)) { } else if (Ox.isString(mappedValue)) {
sort[val] = val_.toLowerCase() sort[value] = mappedValue.toLowerCase()
.replace(/^\W+/, '') .replace(/^\W+/, '')
.replace(/\d+/g, function(match) { .replace(/\d+/g, function(match) {
return Ox.pad(match, len); return Ox.pad(match, length);
}); });
Ox.forEach(['a', 'an', 'the'], function(article) { Ox.forEach(['a', 'an', 'the'], function(article) {
var len; var length;
if (new RegExp('^' + article + ' ').test(sort[val])) { if (new RegExp('^' + article + ' ').test(sort[value])) {
len = article.length; length = article.length;
sort[val] = sort[val].slice(len + 1) + ', ' sort[value] = sort[value].slice(length + 1) + ', '
+ sort[val].slice(0, len); + sort[value].slice(0, length);
Ox.Break(); Ox.Break();
} }
}); });
} else { } else {
sort[val] = val_; sort[value] = mappedValue;
} }
}); });
return sort; return sort;