Skip to content

Commit

Permalink
Add facet building logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Ko committed Feb 23, 2018
1 parent 3344918 commit d9562a5
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/bower_components/
50 changes: 27 additions & 23 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
var Facets = require('./index.js')

var facetable = [
"title",
"category"
}
var products = [
{
title: "rural",
category: "stroller"
var catalogFacets = new Facets({
facets: {
"title": {},
"category": {}
},
{
title: "urban",
category: "stroller"
},
{
title: "rural",
category: "locks"
},
{
title: "urban",
category: "locks"
}
]
var boolean = ["AND", "OR"]
var catalogFacets = new Facets({facetable, products, boolean});
items: [
{
title: "rural",
category: "stroller"
},
{
title: "urban",
category: "stroller"
},
{
title: "rural",
category: "locks"
},
{
title: "urban",
category: "locks"
}
],
boolean: [
"AND",
"OR"
]
});

catalogFacets.search([
"title.rural"
Expand Down
118 changes: 79 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,87 @@

var bigInt = require('big-integer')

function Constructor(input) {
// define functions
function deepClone(obj){
return obj ? JSON.parse(JSON.stringify(obj)) : null;

// init functions
function deepClone(obj){
return obj ? JSON.parse(JSON.stringify(obj)) : null;
}
function traverse(node, callback, path, level){
if(!Array.isArray(node) && typeof node == "object" && Object.keys(node).length){
Object.keys(node).forEach(function(key){
var nodePath = path ? path+"."+key: key;
var nodeLevel = level ? level + 1 : 0;
node[key] = traverse(node[key], callback, nodePath, nodeLevel)
})
return node;
} else {
return callback(node, path, level)
}
}
function traverse(node, callback, path){
if(!Array.isArray(node) && typeof node == "object" && Object.keys(node).length){
Object.keys(node).forEach(function(key){
var nodePath = path ? path+"."+key: key
node[key] = traverse(node[key], callback, nodePath)
})
return node;
} else {
return callback(node, path)
}
function search(facets){
// console.log("[facets]", facets);
// build store
// _results = traverse(_store.index, function(node, path, level){
// console.log(node, path);

// return node
// })
// console.log("[output]", _output);
return _output;
}
// add to prototype
this.traverse = traverse;
this._input = input || {
facetable: [],
items: [],
boolean: []
};
this._index = {};
this._results = {};
// build store

// init variables
var _input = input || {
facets: [],
items: [],
boolean: []
};
var _index = deepClone(_input) || {};
var _output = {};

// build index - add item facets to index facetKeys
_index.items.forEach(function(item){
Object.keys(_index.facets).forEach(function(facetKey){
if(item.hasOwnProperty(facetKey)){
var itemValues = Array.isArray(item[facetKey]) ? item[facetKey] : [ item[facetKey] ];
itemValues.forEach(function (facet) {
_index.facets[facetKey][facet] = _index.facets[facetKey][facet] || {
bitmap: ''
};
});
}
})
})

// build index - build bitmaps per subkey
_index.items.forEach(function(item){
Object.keys(_index.facets).forEach(function(facetKey){
Object.keys(_index.facets[facetKey]).forEach(function(facet){
var itemValues = Array.isArray(item[facetKey]) ? item[facetKey] : [ item[facetKey] ];
if(itemValues.indexOf(facet) > -1){
_index.facets[facetKey][facet].bitmap = '1' + _index.facets[facetKey][facet].bitmap;
} else {
_index.facets[facetKey][facet].bitmap = '0' + _index.facets[facetKey][facet].bitmap;
}
})
})
})

// build index - convert results to big int
Object.keys(_index.facets).forEach(function(facetKey){
Object.keys(_index.facets[facetKey]).forEach(function(facet){
_index.facets[facetKey][facet].bitmap = bigInt(_index.facets[facetKey][facet].bitmap, 2).toString();
})
})

// feedback
console.log("[init input]", this._input);
console.log("[init index]", this._index);
console.log("[init ouput]", this._output);
};
console.log("[init input]", _input);
console.log("[init index]", JSON.stringify(_index.facets, null, "\t"));
console.log("[init ouput]", _output);

// add to prototype
this.search = search;

Constructor.prototype.search = function(facets){
console.log("[facets]", facets);
// build store
// this._results = this.traverse(this._store.index, function(node, path){
// console.log(node, path);

// return node
// })
console.log("[results]", this._results);
return this._results;
}
};

module.exports = exports = Constructor;
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@
"bugs": {
"url": "https://github.com/htkoca/facets.js/issues"
},
"homepage": "https://github.com/htkoca/facets.js#readme"
"homepage": "https://github.com/htkoca/facets.js#readme",
"dependencies": {
"big-integer": "^1.6.26",
"underscore": "^1.8.3"
}
}

0 comments on commit d9562a5

Please sign in to comment.