simple-json-filter
v0.3.0
Published
Extremely simple JSON filtering system
Readme
SimpleJSONFilter
Extremely simple, yet configurable, JSON filtering system.
This module provides an object aimed at filtering arrays (or hashes) of hashes.
The need for it is based on wanting an ActiveRecord like 'where' condition using only JSON arrays-of-hashes or hashes-of-hashes.
It can be thought of as an extremely dumbed down and easy to understand RegExp group capture operation.
Examples
Assume the following data structure in all examples:
var data = {
one: {
id: 1,
name: 'Hiro Protagonist',
age: 27
},
two: {
id: 2,
name: 'Y.T.',
age: 16
},
three: {
id: 3,
name: 'Raven',
age: 40,
},
four: {
id: 4,
name: 'Uncle Enzo',
age: 80
},
five: {
id: 5,
name: 'Fisheye',
age: 50
}
};Example of very simple key=val filtering
var simpleJSONFilter = require("./index.js");
var sjf = new simpleJSONFilter();
var filter = {id: 1};
var result = sjf.exec(filter, data); // Returns {one: {id: 1,name: 'Hiro Protagonist',age: 27}}Example of complex filtering with conditionals
var simpleJSONFilter = require("./index.js");
var sjf = new simpleJSONFilter();
var filter = {'age > 40'};
var result = sjf.exec(filter, data); // Returns the keys & values of 'three', 'four' and 'five'Definining your own handlers
var simpleJSONFilter = require("./index.js");
var sjf = new simpleJSONFilter();
sjf.addHandler(/^(.*) ends with$/, function(key, val, data) {
var str = data[key] + '';
return (str.substr(str.length - val.length) == val);
});
var filter = {'age ends with': '0'};
sjf.exec(filter, data), // Returns the keys & values of 'three', 'four' and 'five'