Skip to content

Commit

Permalink
Flatten object for filter (fixes #79)
Browse files Browse the repository at this point in the history
Related to issue joewalnes/filtrex#12, filtrex
does not support nested properties.
  • Loading branch information
cdauth committed Nov 14, 2017
1 parent 6d760a0 commit 4f847be
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion frontend/common/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,32 @@ const filter = module.exports = {
},

prepareObject: function(obj, type) {
obj = $.extend(true, { }, obj);
obj = filter.flattenObject(obj);

if(type)
obj.type = type.type;

return obj;
},

flattenObject(data) {
// https://stackoverflow.com/a/19101235/242365

var result = {};
function recurse (cur, prop) {
if(prop)
result[prop] = cur;

if(Array.isArray(cur)) {
for(var i=0, l=cur.length; i<l; i++)
recurse(cur[i], prop + "." + i);
} else if(Object(cur) === cur) {
for (var p in cur) {
recurse(cur[p], prop ? prop+"."+p : p);
}
}
}
recurse(data, "");
return result;
}
};

0 comments on commit 4f847be

Please sign in to comment.