memex/logic/datawrap.js

191 lines
4.3 KiB
JavaScript
Raw Normal View History

function DataWrap()
{
// REFERENCE
this.database = null;
this.keys = null;
// SETTINGS
this.statsNumTags = 10;
this.install = function()
{
this.database = new Indental(DATABASE).parse();
this.keys = Object.keys(this.database);
this.processDatabase();
}
this.processDatabase = function()
{
for (i = 0; i < this.keys.length; i++)
{
let value = this.database[this.keys[i]];
// TAGS
if (typeof value.TAGS !== 'undefined')
{
var tags = value.TAGS.split(",");
for (var t = 0; t < tags.length; t++)
{
tags[t] = tags[t].trim().toLowerCase();
}
this.database[this.keys[i]].TAGS = tags;
}
// TERMS
if (typeof value.TERM !== 'undefined')
{
let termRunic = new Runic(value.TERM).raw;
let formattedTerms = [];
for (var t = 0; t < termRunic.length; t++)
{
term = termRunic[t].substr(2).split(':');
for (var e = 0; e < term.length; e++)
{
term[e] = term[e].trim();
}
formattedTerms.push(term);
}
this.database[this.keys[i]].TERM = formattedTerms;
}
this.database[this.keys[i]].DIID = i;
}
console.log(this.database);
}
this.filter = function(target)
{
var tempDatabase = {};
if (target == 'home')
{
console.log('Display \'home\'');
tempDatabase = this.database;
}
else if (target == 'term')
{
console.log('Display \'terms\'');
for (i = 0; i < this.keys.length; i++)
{
let value = this.database[this.keys[i]];
if (typeof value.TERM !== 'undefined')
{
tempDatabase[this.keys[i]] = this.database[this.keys[i]];
}
}
}
else
{
var splitTarget = target.split("-");
if (splitTarget[0] == 'tag')
{
// TAG
console.log('Display tag \'' + splitTarget[1] + '\'');
for (i = 0; i < this.keys.length; i++)
{
let value = this.database[this.keys[i]];
if (typeof value.TAGS !== 'undefined')
{
for (var t = 0; t < value.TAGS.length; t++)
{
if (value.TAGS[t] == splitTarget[1])
{
tempDatabase[this.keys[i]] = this.database[this.keys[i]];
}
}
}
}
}
else if (splitTarget[0] == 'type')
{
// TYPE
console.log('Display type \'' + splitTarget[1] + '\'');
var tempDatabase = {}
for (i = 0; i < this.keys.length; i++)
{
let value = this.database[this.keys[i]];
if (typeof value.TYPE !== 'undefined')
{
if (value.TYPE == splitTarget[1])
{
tempDatabase[this.keys[i]] = this.database[this.keys[i]];
}
}
}
}
}
return tempDatabase;
}
this.getStats = function(db = this.database)
{
// CALCULATE
let dbKeys = Object.keys(db);
var stats =
{
total: this.keys.length,
types: {},
tags: {},
terms: 0,
};
for (var i = 0; i < dbKeys.length; i++)
{
// TYPE
if (typeof db[dbKeys[i]].TYPE !== 'undefined')
{
if (typeof stats.types[db[dbKeys[i]].TYPE] !== 'undefined')
{
stats.types[db[dbKeys[i]].TYPE] ++;
}
else
{
stats.types[db[dbKeys[i]].TYPE] = 1;
}
}
// TAGS
if (typeof db[dbKeys[i]].TAGS !== 'undefined')
{
for (var t = 0; t < db[dbKeys[i]].TAGS.length; t++)
{
if (typeof stats.tags[db[dbKeys[i]].TAGS[t]] !== 'undefined')
{
stats.tags[db[dbKeys[i]].TAGS[t]] ++;
}
else
{
stats.tags[db[dbKeys[i]].TAGS[t]] = 1;
}
}
}
// TERM
if (typeof db[dbKeys[i]].TERM !== 'undefined')
{
stats.terms += db[dbKeys[i]].TERM.length;
}
}
// SORT TAGS, TAKE TOP X
// Create items array
var items = Object.keys(stats.tags).map(function(key)
{
return [key, stats.tags[key]];
});
// Sort the array based on the second element
items.sort(function(first, second)
{
return second[1] - first[1];
});
stats.tags = items.slice(0, this.statsNumTags);
return stats;
}
}