diff --git a/src/favorites.js b/src/favorites.js index 0a74793..ef6eb56 100644 --- a/src/favorites.js +++ b/src/favorites.js @@ -2,6 +2,8 @@ * @file Compute the preferences of a user. */ +/* jslint latedef:false */ + var groucho = window.groucho || {}; (function($, groucho) { @@ -32,9 +34,11 @@ var groucho = window.groucho || {}; /** * Assemble term counts. * + * @todo Avoid operating on variables scoped outside function. + * * @param {string} vocName */ - function collectTerms(vocName, i) { + function collectTerms (vocName, i) { for (var tid in results[i][termProp][vocName]) { // Non-existant vocab. if (!returnTerms.hasOwnProperty(vocName)) { @@ -47,7 +51,10 @@ var groucho = window.groucho || {}; } else { // New, add it on and create count. - returnTerms[vocName][tid] = { 'name': results[i][termProp][vocName][tid], 'count': 1 }; + returnTerms[vocName][tid] = { + 'name': results[i][termProp][vocName][tid], + 'count': 1 + }; } } } @@ -55,9 +62,11 @@ var groucho = window.groucho || {}; /** * Remove lesser count terms. * + * @todo Make into prototype function, act on returnTerms object. + * * @param {string} vocName */ - function filterByCount(vocName) { + function filterByCount (vocName) { var topCount = threshold; // Find top count. @@ -79,32 +88,6 @@ var groucho = window.groucho || {}; } } - /** - * Utility: Term returns should be an array. - */ - function makeArray(obj) { - var arr = []; - for (var i in obj) { - obj[i].id = i; - arr.push(obj[i]); - } - return arr; - } - - /** - * Utility: check for empty vocab object. - * - * @param {object} obj - */ - function isEmpty(obj) { - for (var prop in obj) { - if (obj.hasOwnProperty(prop)) { - return false; - } - } - return true; - } - // No data will be available. if (typeof termProp !== 'undefined') { // Walk through all tracking records. @@ -161,4 +144,30 @@ var groucho = window.groucho || {}; return returnTerms; }; + /** + * Term returns should be an array. + */ + function makeArray (obj) { + var arr = []; + for (var i in obj) { + obj[i].id = i; + arr.push(obj[i]); + } + return arr; + } + + /** + * Check for empty vocab object. + * + * @param {object} obj + */ + function isEmpty (obj) { + for (var prop in obj) { + if (obj.hasOwnProperty(prop)) { + return false; + } + } + return true; + } + })(window.jQuery || window.Zepto || window.$, groucho); diff --git a/src/utility.js b/src/utility.js index ee5a4f2..5b02aa2 100644 --- a/src/utility.js +++ b/src/utility.js @@ -6,6 +6,8 @@ * groucho.userSet = function({country: "US"}); */ +/* jslint latedef:false */ + var groucho = window.groucho || {}; (function($, groucho) { @@ -110,53 +112,65 @@ var groucho = window.groucho || {}; groucho.getActivities = function (group) { var results = groucho.storage.index(), returnVals = [], - matchable = (group) ? new RegExp("^track." + group + ".", "g") : false, - record; + matchable = (group) ? new RegExp("^track." + group + ".", "g") : false; for (var i in results) { // Safety measure. if (!results.hasOwnProperty(i)) continue; - // Remove unwanted types and return records. - if (group) { - if (results[i].match(matchable) !== null) { - // Collect relevant. - record = groucho.storage.get(results[i]); - // Move key to property. - record._key = results[i]; - returnVals.push(record); - } + // Collect and return all. + if (!group) { + addRecordRow(returnVals, results[i]); } - else { - // Collect and return all. - record = groucho.storage.get(results[i]); - // Move key to property. - record._key = results[i]; - returnVals.push(record); + // Remove unwanted types, return relevant records. + else if (results[i].match(matchable) !== null) { + addRecordRow(returnVals, results[i]); } } - // Ensure proper key sorting regardless of index result order. - returnVals.sort(function (a, b) { - // Created non-standard or outside Groucho. - // Should always contain an original key which contains a dot. - if (!a.hasOwnProperty('_key') || !a._key.match(/\./) || - !b.hasOwnProperty('_key') || !b._key.match(/\./)) { - return 0; - } - // Sort by post-prefix key. - if (parseInt(b._key.split('.')[2], 10) > parseInt(a._key.split('.')[2], 10)) { - return -1; - } - else { - return 1; - } - }); - - return returnVals; + // Ensure key sort regardless of session index, especially for timestamps. + return returnVals.sort(sortActivities); }; + /** + * Add record to collected values list, and move key to property. + * + * @param {object} list + * Array of records to be added to. + * @param {string} name + */ + function addRecordRow (list, name) { + var record = groucho.storage.get(name); + record._key = name; + list.push(record); + } + + + /** + * Sort after Groucho prefix, eliminate other entries. + * + * @param {string} a + * @param {string} b + * + * @return {number} + */ + function sortActivities (a, b) { + // Eliminate storage entries outside Groucho. + if (!a.hasOwnProperty('_key') || !a._key.match(/\./) || + !b.hasOwnProperty('_key') || !b._key.match(/\./)) { + return 0; + } + // Sort by post-prefix key. + if (parseInt(b._key.split('.')[2], 10) > parseInt(a._key.split('.')[2], 10)) { + return -1; + } + else { + return 1; + } + } + + /** * Data transforms due to version updates. Prevents past use data corruption. */