https://github.com/hash-bang/simplejsonfilter
Extremely simple JSON filtering system
https://github.com/hash-bang/simplejsonfilter
Last synced: 20 days ago
JSON representation
Extremely simple JSON filtering system
- Host: GitHub
- URL: https://github.com/hash-bang/simplejsonfilter
- Owner: hash-bang
- License: mit
- Created: 2013-09-09T02:02:37.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-09-17T02:38:11.000Z (over 11 years ago)
- Last Synced: 2025-05-05T22:56:26.313Z (20 days ago)
- Language: JavaScript
- Size: 104 KB
- Stars: 9
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
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](http://ellislab.com/codeigniter/user-guide/database/active_record.html) 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'Alternate syntax
var simpleJSONFilter = require("./index.js");
var sjf = new simpleJSONFilter();
sjf
.filter({'age <=': '40'})
.data(data)
.exec(); // Returns keys & values of 'one', 'two' and 'three'Limit the number of returned items
var simpleJSONFilter = require("./index.js");
var sjf = new simpleJSONFilter();
sjf
.filter({'age <=': '40'})
.data(data)
.limit(1)
.exec(); // Returns keys & values of 'one' (as it has a limit of one)Limit the number of returned items & return an array
var simpleJSONFilter = require("./index.js");
var sjf = new simpleJSONFilter();
sjf
.filter({'age <=': '40'})
.data(data)
.limit(1)
.wantArray()
.exec(); // Returns a single item array of 'one'