From 293c96a97df9d0c4a39da4dbe02a36121426c52b Mon Sep 17 00:00:00 2001 From: rolux Date: Tue, 18 Dec 2012 11:35:51 +0100 Subject: [PATCH] In Ox.api, allow for sums of text fields (= number of unique values, for example: number of artists in a list of works) --- source/Ox/js/Array.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/source/Ox/js/Array.js b/source/Ox/js/Array.js index d3a3c86a..235bf14b 100644 --- a/source/Ox/js/Array.js +++ b/source/Ox/js/Array.js @@ -214,7 +214,9 @@ Ox.api = function(items, options) { }*/ }); if (options.keys || options.positions) { - result.data.items = sortBy(result.data.items, options.sort, map, options.query); + result.data.items = sortBy( + result.data.items, options.sort, map, options.query + ); } } if (options.positions) { @@ -230,10 +232,13 @@ Ox.api = function(items, options) { // return totals data = {}; api.sums.forEach(function(key) { - data[key] = Ox.sum(result.data.items.map(function(item) { + data[key] = result.data.items.map(function(item) { return item[key]; - })); - }) + }); + data[key] = Ox.isString(data[key][0]) + ? Ox.unique(data[key]).length + : Ox.sum(data[key]); + }); data.items = result.data.items.length; if (api.geo) { /*