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';
/*
/*@
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
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)
options <o> Options object
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
sort <[o]|[s]> Default sort, for example <code> ['+name', '-age']
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}}
> Ox.test.apiResults[8].data
{items: [{i: 2, size: 'L'}, {i: 1, size: 'M'}]}
*/
@*/
Ox.api = function(items, options) {
var api = {
@ -466,50 +466,50 @@ Ox.range = function() {
(function() {
function getSortValues(arr, fn) {
var arr_ = fn ? arr.map(fn) : arr,
len, matches = [], sort = {};
function getSortValues(array, map) {
var mappedArray = map ? array.map(map) : array,
length, matches = [], sort = {};
// find numbers
arr.forEach(function(val, i) {
var match;
if (Ox.isString(val)) {
match = arr_[i].match(/\d+/g);
array.forEach(function(value, i) {
var match, mappedValue = mappedArray[i];
if (Ox.isString(mappedValue)) {
match = mappedValue.match(/\d+/g);
if (match) {
matches = matches.concat(match);
}
}
});
// get length of longest number
len = Ox.max(Ox.map(matches, function(val) {
return val.length;
length = Ox.max(Ox.map(matches, function(value) {
return value.length;
}));
// make lowercase, remove leading non-word characters,
// pad numbers and move leading articles to the end
arr.forEach(function(val, i) {
var val_ = arr_[i];
array.forEach(function(value, i) {
var mappedValue = mappedArray[i];
if (
Ox.isEmpty(val_)
|| Ox.isNull(val_)
|| Ox.isUndefined(val_)
Ox.isEmpty(mappedValue)
|| Ox.isNull(mappedValue)
|| Ox.isUndefined(mappedValue)
) {
sort[val] = null;
} else if (Ox.isString(val_)) {
sort[val] = val_.toLowerCase()
sort[value] = null;
} else if (Ox.isString(mappedValue)) {
sort[value] = mappedValue.toLowerCase()
.replace(/^\W+/, '')
.replace(/\d+/g, function(match) {
return Ox.pad(match, len);
return Ox.pad(match, length);
});
Ox.forEach(['a', 'an', 'the'], function(article) {
var len;
if (new RegExp('^' + article + ' ').test(sort[val])) {
len = article.length;
sort[val] = sort[val].slice(len + 1) + ', '
+ sort[val].slice(0, len);
var length;
if (new RegExp('^' + article + ' ').test(sort[value])) {
length = article.length;
sort[value] = sort[value].slice(length + 1) + ', '
+ sort[value].slice(0, length);
Ox.Break();
}
});
} else {
sort[val] = val_;
sort[value] = mappedValue;
}
});
return sort;