@flourish/filter
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -12,3 +12,3 @@ (function (global, factory) { | ||
median: function(arr) { | ||
var sarr = arr.slice().sort(function(a, b){return a > b;}); | ||
var sarr = arr.slice().sort(function(a, b) { return a > b; }); | ||
var len = sarr.length; | ||
@@ -27,3 +27,3 @@ var floor_mid = Math.floor(len/2); | ||
var new_obj = {}; | ||
Object.keys(obj).forEach(function(key){ | ||
Object.keys(obj).forEach(function(key) { | ||
new_obj[key] = obj[key]; | ||
@@ -34,4 +34,4 @@ }); | ||
function objToArray(obj){ | ||
return Object.keys(obj).map(function(key){ return obj[key]; }); | ||
function objToArray(obj) { | ||
return Object.keys(obj).map(function(key) { return obj[key]; }); | ||
} | ||
@@ -61,6 +61,6 @@ | ||
if (!Array.isArray(data)) return; | ||
if (typeof filter_props === "string") filter_props = [filter_props]; | ||
else if (!Array.isArray(filter_props) && filter_props.some(function(f){ return f !== "string"; })) return; | ||
else if (!Array.isArray(filter_props) && filter_props.some(function(f) { return f !== "string"; })) return; | ||
var filter_tree = {}; | ||
@@ -72,22 +72,22 @@ var filter_level = filter_props.length; | ||
var find = function(path){ | ||
var find = function(path) { | ||
if (!Array.isArray(path)) return; | ||
return path.reduce(function(parent, child){ | ||
return path.reduce(function(parent, child) { | ||
return (parent && parent[child]) || undefined; | ||
}, filter_tree); | ||
}; | ||
var replace = function(parent, child_prop, item){ | ||
if(!parent || !child_prop) return; | ||
var replace = function(parent, child_prop, item) { | ||
if (!parent || !child_prop) return; | ||
parent[child_prop] = item; | ||
}; | ||
//Build tree from data | ||
data.forEach(function(obj){ | ||
// Build tree from data | ||
data.forEach(function(obj) { | ||
var parent = filter_tree; | ||
filter_props.every(function(filter_prop, index){ | ||
filter_props.every(function(filter_prop, index) { | ||
var filter_value = obj[filter_prop]; | ||
if (filter_value === undefined) return false; | ||
if (index < (filter_level-1)) { | ||
if (!parent.hasOwnProperty(filter_value)){ // path not currently in tree | ||
if (!parent.hasOwnProperty(filter_value)) { // path not currently in tree | ||
parent[filter_value] = {}; | ||
@@ -100,3 +100,3 @@ } | ||
parent[filter_value] = []; | ||
leaf_nodes.push({parent:parent, prop:filter_value, data:parent[filter_value]}); | ||
leaf_nodes.push({parent: parent, prop: filter_value, data: parent[filter_value]}); | ||
} | ||
@@ -108,26 +108,26 @@ parent[filter_value].push(obj); | ||
}); | ||
var aggregate = function(agg_prop, agg_method){ | ||
if (agg_prop === undefined){ // disaggregate leaf nodes if no aggregate property passed in | ||
var aggregate = function(agg_prop, agg_method) { | ||
if (agg_prop === undefined) { // disaggregate leaf nodes if no aggregate property passed in | ||
min_value = undefined; | ||
max_value = undefined; | ||
leaf_nodes.forEach(function(leaf_obj){ replace(leaf_obj.parent, leaf_obj.prop, leaf_obj.data); }); | ||
leaf_nodes.forEach(function(leaf_obj) { replace(leaf_obj.parent, leaf_obj.prop, leaf_obj.data); }); | ||
aggregation_method = "none"; | ||
} | ||
else { | ||
var aggFunc = agg_method ? aggregators[agg_method] : aggregators["sum"]; | ||
if (typeof agg_prop !== "string" || !aggFunc) return | ||
var aggFunc = agg_method ? aggregators[agg_method] : aggregators.sum; | ||
if (typeof agg_prop !== "string" || !aggFunc) return; | ||
min_value = Infinity; | ||
max_value = -Infinity; | ||
leaf_nodes.forEach(function(leaf_obj, index){ | ||
leaf_nodes.forEach(function(leaf_obj) { | ||
var leaf_array = leaf_obj.data; | ||
var new_obj = cloneObj(leaf_array[0]); // clone first item | ||
delete new_obj[agg_prop]; // remove aggregating property | ||
var vals = leaf_array.map(function(item, idx){ | ||
if (idx) { | ||
Object.keys(new_obj).forEach(function(key){ | ||
if(item[key] !== new_obj[key]){ | ||
var vals = leaf_array.map(function(item, idx) { | ||
if (idx) { | ||
Object.keys(new_obj).forEach(function(key) { | ||
if (item[key] !== new_obj[key]) { | ||
delete new_obj[key]; // remove properties from new_obj whose values aren't shared in current item | ||
@@ -145,3 +145,3 @@ } | ||
}); | ||
aggregation_method = agg_method; | ||
@@ -151,5 +151,5 @@ } | ||
var extracateData = function(_filters){ | ||
var extracateData = function(_filters) { | ||
if (typeof _filters === "string") _filters = [_filters]; | ||
var filters = _filters.slice().filter(function(x) {return x !== undefined;}); | ||
var filters = _filters.slice().filter(function(x) { return x != null; }); // we want to reject both undefined and null | ||
if (filters.length > filter_level) filters.length = filter_level; | ||
@@ -162,3 +162,3 @@ var extractee = find(filters); | ||
for (var i=n_filters; i<filter_level-1; i++) { | ||
output = output.map(function(obj){ return objToArray(obj); }).reduce(function(a, b) { | ||
output = output.map(function(obj) { return objToArray(obj); }).reduce(function(a, b) { | ||
return a.concat(b); | ||
@@ -169,7 +169,7 @@ }); | ||
}; | ||
var extract = function(filters, sort_method, limit) { | ||
var edata = extracateData(filters); | ||
if (edata && sort_method) { | ||
var sortFunc = sort_methods[sort_method]; | ||
var sortFunc = sort_methods[sort_method]; | ||
if (!sortFunc) return; | ||
@@ -183,4 +183,4 @@ edata.sort(sortFunc); | ||
}; | ||
return { | ||
@@ -193,3 +193,3 @@ get tree() { return filter_tree; }, | ||
extract: extract | ||
} | ||
}; | ||
} | ||
@@ -196,0 +196,0 @@ |
{ | ||
"name": "@flourish/filter", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Reusable filter and optional aggregator", | ||
@@ -14,3 +14,4 @@ "main": "filter.js", | ||
"prepublish": "npm run build && npm run minify", | ||
"minify": "uglifyjs -m -o filter.min.js filter.js" | ||
"minify": "uglifyjs -m -o filter.min.js filter.js", | ||
"lint": "eslint src" | ||
}, | ||
@@ -20,3 +21,6 @@ "author": "Kiln Enterprises Ltd", | ||
"devDependencies": { | ||
"@flourish/eslint-plugin-flourish": "^0.7.2", | ||
"eslint": "git://github.com/robinhouston/eslint.git#bug-ignoredNodes", | ||
"mocha": "^3.5.3", | ||
"pre-commit": "^1.2.2", | ||
"rollup": "^0.41.1", | ||
@@ -28,3 +32,6 @@ "uglify-js": "^2.7.5" | ||
"url": "git://github.com/kiln/flourish-filter.git" | ||
} | ||
}, | ||
"precommit": [ | ||
"lint" | ||
] | ||
} |
@@ -6,3 +6,3 @@ var aggregators = { | ||
median: function(arr) { | ||
var sarr = arr.slice().sort(function(a, b){return a > b;}); | ||
var sarr = arr.slice().sort(function(a, b) { return a > b; }); | ||
var len = sarr.length; | ||
@@ -19,2 +19,2 @@ var floor_mid = Math.floor(len/2); | ||
export { aggregators }; | ||
export { aggregators }; |
@@ -22,6 +22,6 @@ import { aggregators } from "./aggregators"; | ||
if (!Array.isArray(data)) return; | ||
if (typeof filter_props === "string") filter_props = [filter_props]; | ||
else if (!Array.isArray(filter_props) && filter_props.some(function(f){ return f !== "string"; })) return; | ||
else if (!Array.isArray(filter_props) && filter_props.some(function(f) { return f !== "string"; })) return; | ||
var filter_tree = {}; | ||
@@ -33,22 +33,22 @@ var filter_level = filter_props.length; | ||
var find = function(path){ | ||
var find = function(path) { | ||
if (!Array.isArray(path)) return; | ||
return path.reduce(function(parent, child){ | ||
return path.reduce(function(parent, child) { | ||
return (parent && parent[child]) || undefined; | ||
}, filter_tree); | ||
}; | ||
var replace = function(parent, child_prop, item){ | ||
if(!parent || !child_prop) return; | ||
var replace = function(parent, child_prop, item) { | ||
if (!parent || !child_prop) return; | ||
parent[child_prop] = item; | ||
}; | ||
//Build tree from data | ||
data.forEach(function(obj){ | ||
// Build tree from data | ||
data.forEach(function(obj) { | ||
var parent = filter_tree; | ||
filter_props.every(function(filter_prop, index){ | ||
filter_props.every(function(filter_prop, index) { | ||
var filter_value = obj[filter_prop]; | ||
if (filter_value === undefined) return false; | ||
if (index < (filter_level-1)) { | ||
if (!parent.hasOwnProperty(filter_value)){ // path not currently in tree | ||
if (!parent.hasOwnProperty(filter_value)) { // path not currently in tree | ||
parent[filter_value] = {}; | ||
@@ -61,3 +61,3 @@ } | ||
parent[filter_value] = []; | ||
leaf_nodes.push({parent:parent, prop:filter_value, data:parent[filter_value]}); | ||
leaf_nodes.push({parent: parent, prop: filter_value, data: parent[filter_value]}); | ||
} | ||
@@ -69,26 +69,26 @@ parent[filter_value].push(obj); | ||
}); | ||
var aggregate = function(agg_prop, agg_method){ | ||
if (agg_prop === undefined){ // disaggregate leaf nodes if no aggregate property passed in | ||
var aggregate = function(agg_prop, agg_method) { | ||
if (agg_prop === undefined) { // disaggregate leaf nodes if no aggregate property passed in | ||
min_value = undefined; | ||
max_value = undefined; | ||
leaf_nodes.forEach(function(leaf_obj){ replace(leaf_obj.parent, leaf_obj.prop, leaf_obj.data); }); | ||
leaf_nodes.forEach(function(leaf_obj) { replace(leaf_obj.parent, leaf_obj.prop, leaf_obj.data); }); | ||
aggregation_method = "none"; | ||
} | ||
else { | ||
var aggFunc = agg_method ? aggregators[agg_method] : aggregators["sum"]; | ||
if (typeof agg_prop !== "string" || !aggFunc) return | ||
var aggFunc = agg_method ? aggregators[agg_method] : aggregators.sum; | ||
if (typeof agg_prop !== "string" || !aggFunc) return; | ||
min_value = Infinity; | ||
max_value = -Infinity; | ||
leaf_nodes.forEach(function(leaf_obj, index){ | ||
leaf_nodes.forEach(function(leaf_obj) { | ||
var leaf_array = leaf_obj.data; | ||
var new_obj = cloneObj(leaf_array[0]); // clone first item | ||
delete new_obj[agg_prop]; // remove aggregating property | ||
var vals = leaf_array.map(function(item, idx){ | ||
if (idx) { | ||
Object.keys(new_obj).forEach(function(key){ | ||
if(item[key] !== new_obj[key]){ | ||
var vals = leaf_array.map(function(item, idx) { | ||
if (idx) { | ||
Object.keys(new_obj).forEach(function(key) { | ||
if (item[key] !== new_obj[key]) { | ||
delete new_obj[key]; // remove properties from new_obj whose values aren't shared in current item | ||
@@ -106,10 +106,10 @@ } | ||
}); | ||
aggregation_method = agg_method; | ||
} | ||
} | ||
}; | ||
var extracateData = function(_filters){ | ||
var extracateData = function(_filters) { | ||
if (typeof _filters === "string") _filters = [_filters]; | ||
var filters = _filters.slice().filter(function(x) {return x !== undefined;}); | ||
var filters = _filters.slice().filter(function(x) { return x != null; }); // we want to reject both undefined and null | ||
if (filters.length > filter_level) filters.length = filter_level; | ||
@@ -122,3 +122,3 @@ var extractee = find(filters); | ||
for (var i=n_filters; i<filter_level-1; i++) { | ||
output = output.map(function(obj){ return objToArray(obj); }).reduce(function(a, b) { | ||
output = output.map(function(obj) { return objToArray(obj); }).reduce(function(a, b) { | ||
return a.concat(b); | ||
@@ -129,7 +129,7 @@ }); | ||
}; | ||
var extract = function(filters, sort_method, limit) { | ||
var edata = extracateData(filters); | ||
if (edata && sort_method) { | ||
var sortFunc = sort_methods[sort_method] | ||
var sortFunc = sort_methods[sort_method]; | ||
if (!sortFunc) return; | ||
@@ -143,4 +143,4 @@ edata.sort(sortFunc); | ||
}; | ||
return { | ||
@@ -153,3 +153,3 @@ get tree() { return filter_tree; }, | ||
extract: extract | ||
} | ||
}; | ||
} | ||
@@ -156,0 +156,0 @@ |
function cloneObj(obj) { | ||
var new_obj = {}; | ||
Object.keys(obj).forEach(function(key){ | ||
Object.keys(obj).forEach(function(key) { | ||
new_obj[key] = obj[key]; | ||
@@ -9,5 +9,5 @@ }); | ||
function objToArray(obj){ | ||
return Object.keys(obj).map(function(key){ return obj[key]; }); | ||
}; | ||
function objToArray(obj) { | ||
return Object.keys(obj).map(function(key) { return obj[key]; }); | ||
} | ||
@@ -19,2 +19,2 @@ var sort_methods = { | ||
export { cloneObj, objToArray, sort_methods }; | ||
export { cloneObj, objToArray, sort_methods }; |
1511658
10
365
6