Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify getting activities #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 38 additions & 29 deletions src/favorites.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* @file Compute the preferences of a user.
*/

/* jslint latedef:false */

var groucho = window.groucho || {};

(function($, groucho) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -47,17 +51,22 @@ 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
};
}
}
}

/**
* 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.
Expand All @@ -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.
Expand Down Expand Up @@ -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);
84 changes: 49 additions & 35 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* groucho.userSet = function({country: "US"});
*/

/* jslint latedef:false */

var groucho = window.groucho || {};

(function($, groucho) {
Expand Down Expand Up @@ -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.
*/
Expand Down