pointsight/proto/filterTest.js

77 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-03-06 18:36:36 +01:00
const sensemap = require("../apiHandler/opensensemap/main");
const ttnantennas = require("../apiHandler/ttnantennas/main");
const autobahn = require("../apiHandler/autobahn/main");
const libs = require("../libs/metaHandler.lib.js");
const mods = [sensemap, ttnantennas, autobahn]; // Provided by main.ts in a real env.
const jsonData = {
filters: [
["tag", "webcam"],
["country", "DEU"],
],
conjunction: "AND",
}; // Provided by request in a real env
const uMods = mods.filter(function (elm) {
// Loops through all elements in a list and lets you decide if weather to keep 'em or not
let amount = 0;
for (fId in jsonData.filters) {
// Do this is each provided filter
const fi = jsonData.filters[fId]; // Gets the current filters name
switch (fi[0]) {
case "tag":
if (jsonData.conjunction == "OR") {
if (libs.returnTags(elm).includes(fi[1])) {
return elm;
} else {
break;
}
} else {
if (libs.returnTags(elm).includes(fi[1])) {
amount++;
}
break;
}
case "country":
if (jsonData.conjunction == "OR") {
if (libs.returnCountries(elm).includes(fi[1])) {
return elm;
} else {
break;
}
} else {
if (libs.returnCountries(elm).includes(fi[1])) {
amount++;
}
break;
}
}
}
if (jsonData.conjunction == "AND") {
if (amount == jsonData.filters.length) {
return elm;
}
}
});
console.log(uMods);
/**
* This things is sorting all given modules by a JSON array.
* You can supply the ˋfilterˋ array with a list of filters
* They are made up of ["metaData", "metaValue"]
* Another argument is ˋconjunctionˋ it can be either AND or OR
* OR means that any of the given filters should apply
* AND means that all filter HAVE to apply
*
* Current state
* This things can already handle OR requests, AND requests
* are not supported yet. I also use the /libs/metaHandler lib,
* at least I finnaly found a usecase for that waste of space.
*
* This is a small prototype which will be added into /routes/api.route.js
* at some point. But what I was doing before was almost production testing.
* Not good. I will need to test this further and throw some edge cases at it.
*/