forked from tkodev/lib-facetor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tony Ko
committed
Feb 23, 2018
1 parent
3344918
commit d9562a5
Showing
5 changed files
with
131 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules/ | ||
/bower_components/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters