MultiFilter
What is MultiFilter?
This is a open-source, free, library to apply multiple filters on JSON data. The library is designed to be lightweight, extensible and easy to use.
You can use this library to filter on combinations of:
- Dates
- Strings
- Numbers
- Booleans
100% Written in JavaScript.
How do I use MultiFilter?
Install the module from npm
npm i -S @interstellarjay/multifilter
Create a new MultiFilter instance
const MultiFilter = require("@interstellarjay/mutlifilter@latest");
const mf = new MultiFilter();
Fetch your JSON data
const dataSet = [
{
name: "Nintendo® Switch",
age: 2
},
{
name: "PlayStation® 4",
age: 6
},
{
name: "PS4® Pro",
age: 3
}
];
List your filters as shown below, you can use the preset filters in MultiFilter or write your own filter functions. Don't use parentheses ()
.
/**
* Store your conditions in an array of objects like this..
* -> filter takes a function (see MultiFilter helper functions)
* -> onProperty is the key in the object to use to validate.
* -> condition is the value that to use to validate.
**/
let filters = [
{
filter: mf.filterLessThan,
onProperty: "age"
condition: 6,
},
{
filter: mf.filterGreaterThan,
onProperty: "age"
condition: 2,
}
];
Apply the filters
mf.applyFilters(filters);
Filter the JSON data with the applied filters
const result = mf.filter(dataSet);
console.log(result); // [{ name: "PS4® Pro", age: 3 }]
Clear the filters when you no longer need them.
mf.clearFilters();
MultiFilter API
MultiFilter core API
Creating filters
In MultiFilter each filter is written in the following format.
{
filter: FILTER_FUNCTION,
onProperty: KEY_IN_EACH_OBJECT,
condition: VALIDATE_AGAINST_THIS_VALUE
}
Then store all the filters you would like to apply in an array.
let filters = [
{
filter: mf.filterLessThan,
onProperty: "age"
condition: 6,
},
{
filter: mf.filterGreaterThan,
onProperty: "age"
condition: 2,
}
];
(Array of filters)
mf.applyFilters Registers all the filters to be used before the data is filtered.
mf.applyFilters(filters)
()
mf.clearFilters Removes all the applied filters, your data will now be unfiltered.
mf.clearFilters()
(JSON)
mf.filter Filters the JSON with every filter from the mf.applyFilters(filters)
function. Returns JSON of filtered data.
mf.filter(JSON)
MultiFilter functions API
MultiFilter comes with helper filter functions for testing against numbers, strings and booleans.
Number
mf.filterLessThan Filter everything < 70
from the weightKG
key of every object in the JSON.
{
filter: mf.filterLessThan,
onProperty: "weightKG",
condition: 100
}
Number
mf.filterGreaterThan Filter everything > 20
from the likes
key of every object in the JSON.
{
filter: mf.filterGreaterThan,
onProperty: "likes",
condition: 20
}
Number, String, Boolean
mf.filterEqualTo
⚠️ By default "-"
, and "_"
, and " "
are escaped from strings.
Filter everything equal to "PlayStation"
from the brand
key of every object in the JSON.
{
filter: mf.filterGreaterThan,
onProperty: "brand",
condition: "PlayStation"
}
Number, String, Boolean
mf.filterNotEqualTo
⚠️ By default "-"
, and "_"
, and " "
are escaped from strings.
Filter everything not equal to "God of War"
from the title
key of every object in the JSON.
{
filter: mf.filterGreaterThan,
onProperty: "title",
condition: "God of War"
}
String
as "YYYY-MM-DD"
mf.filterBeforeDate Filter everything before October 5th 2019
from the launchdate
key of every object in the JSON.
{
filter: mf.filterGreaterThan,
onProperty: "launchdate",
condition: "2019-10-05",
}
String
as "YYYY-MM-DD"
mf.filterAfterDate Filter everything after June 25th 2020
from the bookingdate
key of every object in the JSON.
{
filter: mf.filterGreaterThan,
onProperty: "bookingdate",
condition: "2020-06-25"
}
String
as "YYYY-MM-DD"
mf.filterEqualToDate Filter everything equal to February 28th 2020
from the departuredate
key of every object in the JSON.
{
filter: mf.filterGreaterThan,
onProperty: "departuredate",
condition: "2020-02-18",
}
String
as "YYYY-MM-DD"
mf.filterNotEqualToDate Filter everything not equal to March 3rd 2020
from the returndate
key of every object in the JSON.
{
filter: mf.filterGreaterThan,
onProperty: "returndate",
condition: "2020-03-01"
}
Where would I use this?
Filter your data set by name, release date, rating, color, price, age, condition... anything you like!
Why create MultiFilter?
MultiFilter was created because I felt like the process of applying multiple filters on JSON needed to be simpler.
Enjoying MultiFilter?
If you're enjoying MultiFilter and would like to support the project, please consider becoming a contributor.