Skip to content

Commit

Permalink
Use underscores for attributes, simplify bitmap code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Ko committed Mar 8, 2018
1 parent 866db84 commit 7ac9b14
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var catalogFacets = new Facets({
});

var results = catalogFacets.getResults({
facets: ["index.title"],
facets: ["title"],
operators: ["AND", "OR"],
showCount: true
});
Expand Down
55 changes: 42 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Constructor(config) {
function deepForEach(node, callback, path, level){
path = path || "";
level = level || 0;
if(node.hasOwnProperty("bitmap")){
if(node.hasOwnProperty("_bitmap")){
return callback(node, path, level)
} else {
node = _.mapObject(node, function(childNode, key) {
Expand All @@ -28,8 +28,8 @@ function Constructor(config) {
}
}

// shared - Set an object's deep nested property based on "|" delimited string path
function setObjProp(obj, path, val) {
// shared - Set an object's deep nested property based on "." delimited string path
function setNode(obj, path, val) {
path = path.split('.');
for (i = 0; i < path.length - 1; i++){
if(_.keys(obj).indexOf(path[i]) > -1){
Expand All @@ -41,8 +41,8 @@ function Constructor(config) {
}
}

// shared - Get an object's deep nested property based on "|" delimited string path
function getObjProp(obj, path){
// shared - Get an object's deep nested property based on "." delimited string path
function getNode(obj, path){
for (var i=0, path=path.split('.'), len=path.length; i<len; i++){
if(_.keys(obj).indexOf(path[i]) > -1){
obj = obj[path[i]];
Expand All @@ -53,7 +53,7 @@ function Constructor(config) {

// shared - set status based on boolean
function setStatus(index, boolean){
return deepForEach(index, function(node, path){
return deepForEach(index, function(node, path, level){
if(node._bitmap){
node._status = boolean;
}
Expand All @@ -63,7 +63,7 @@ function Constructor(config) {

// shared - set bitmaps to big int based on boolean
function setBigInt(index, boolean){
return deepForEach(index, function(node, path){
return deepForEach(index, function(node, path, level){
if(node._bitmap){
node._bitmap = boolean ? bigInt(node._bitmap, 2) : bitmap.toString(2);
}
Expand Down Expand Up @@ -115,22 +115,51 @@ function Constructor(config) {
// ****************************************************************************************************

// results - get bitmap based on supplied index, facets and current path
function getBitmap(index, options, node, path, length){
function getBitmap(index, items, options, path){
var tempFacets = deepClone(options.facets)
var tempIndex = deepClone(index);
var tempNode = deepClone(node);
var allOnes = bigInt(1).shiftLeft(length).minus(1);
var allOnes = bigInt(1).shiftLeft(items.length).minus(1);
if(path){
tempFacets.push(path);
}
console.log(tempFacets)
tempFacets.forEach(function(facet){
tempIndex = setObjProp(tempIndex, path, setStatus(tempNode, true));
console.log("test", tempIndex, facet, JSON.stringify(getNode(tempIndex, facet)))
var tempNode = setStatus(getNode(tempIndex, facet), true);
tempIndex = setNode(tempIndex, facet, tempNode);
})
// console.log(tempIndex);
// console.log(JSON.stringify(tempIndex, null, "\t"))
// index to bitmap logic here
// return bigInt(0);
return allOnes
}

// function getBitmap(index, length){
// // return nested function result
// return andCategories(index, length);
// // nested function, traverse categories, use AND logic on each
// function andCategories(categories, length){
// var allOnes = bigInt(1).shiftLeft(length).minus(1);
// return _.values(categories).reduce(function(accumulator, filters){
// var rslt = orFilters(filters);
// // If rslt contains matches, return matches, else match all.
// rslt = rslt.value ? rslt : allOnes;
// return accumulator.and(rslt);
// }, allOnes);
// }
// // nested function - traverse filter items, use OR logic on each (recursive)
// function orFilters(filters){
// return _.values(filters).reduce(function(accumulator, filter){
// if (filter.hasOwnProperty('status') && (bigInt.isInstance(filter.status[0]) || Array.isArray(filter.status))){
// return accumulator.or(filter.status[0] || bigInt(0));
// } else {
// return accumulator.or(orFilters(filter) || bigInt(0));
// }
// }, bigInt(0));
// }
// }

// results - get count based on suppled bitmap
function getCount(bitmap){
var count = 0;
Expand All @@ -151,7 +180,7 @@ function Constructor(config) {

function getAttr(index, items, options){
return deepForEach(index, function(node, path, level){
var bitmap = getBitmap(index, options, node, path, items.length);
var bitmap = getBitmap(index, items, options, path);
if (options.showBitmap){
node._bitmap = bitmap;
} else {
Expand Down Expand Up @@ -190,7 +219,7 @@ function Constructor(config) {
this.getResults = function getResults(options){
var results = deepClone(store);
// results.index = setBigInt(results.index, true);
results.items = getItems(results.items, getBitmap(results.index, options, results.index, "", results.items.length))
results.items = getItems(results.items, getBitmap(results.index, results.items, options, ""))
results.index = getAttr(results.index, results.items, options)
return results;
};
Expand Down

0 comments on commit 7ac9b14

Please sign in to comment.