Skip to content

Instantly share code, notes, and snippets.

@pkcpkc
Created October 24, 2023 07:31
Show Gist options
  • Save pkcpkc/c1451c951077cb7aad19cda09bec7571 to your computer and use it in GitHub Desktop.
Save pkcpkc/c1451c951077cb7aad19cda09bec7571 to your computer and use it in GitHub Desktop.
Split in disjunct sub-requests
Array.prototype.intersection = function (otherArray) {
return this.filter(element => otherArray.includes(element));
};
Array.prototype.subArray = function (start, end) {
return this.filter(function (element, index) { return index >= start && index < end });
}
Array.prototype.includesAny = function (otherArray) {
return this.intersection(otherArray).length > 0;
}
// Topics
var t1 = "t1", t2 = "t2", t3 = "t3", t4 = "t4", t5 = "t5", t6 = "t6";
// Authors
var a1 = "a1", a2 = "a2", a3 = "a3", a4 = "a4", a5 = "a5", a6 = "a6";
// Example subscriptions
var personSubs = [[t1, t3], [t6, t3, a5], [t2, t3, t6], [t1, t2, t3, t4, t5], [t1, t6, a5, a4], [t1, t2, t3, t4], [t3, t4], [t3, a6], [t5, a3], [a4, a5, a6]];
// Example article
console.log("== Example article ==");
var articleTopics = [t1, t2, t4];
var articleAuthors = [a4, a5, a6];
console.log("Topics: " + articleTopics.join(", "));
console.log("Authors: " + articleAuthors.join(", "));
console.log("\n== Topic Queries ==");
var queries = [];
for (var i = 0; i < articleTopics.length; i++) {
var contains = articleTopics[i];
var mustNotContainAny = articleTopics.subArray(0, i);
queries.push([contains, mustNotContainAny]);
console.log("Subscription contains " + contains + " AND notContainsAny (" + mustNotContainAny.join(",") + ") will get topic '" + articleTopics[i] + "'");
}
console.log("\n== Testing the queries versus topic subscriptions ==");
personSubs.forEach(function (subs, index) {
var found = false;
queries.forEach(query => {
var currentTopic = query[0];
var excludeTopics = query[1];
if (subs.includes(currentTopic) && !subs.includesAny(excludeTopics)) {
console.log("Person with topic subs (" + subs + ") will get push 'topic " + currentTopic + "'");
found = true;
}
});
if (!found) {
console.log("NO topic pushes for person with subs (" + subs + ")");
}
});
console.log("\n== Author Queries ==");
var authorQueries = [];
for (var i = 0; i < articleAuthors.length; i++) {
var contains = articleAuthors[i];
var mustNotContainAny = articleAuthors.subArray(0, i);
authorQueries.push([contains, mustNotContainAny]);
console.log("Subscription contains " + contains + " AND notContainsAny (" + mustNotContainAny.join(",") + ") AND notContainsAnyTopic (" + articleTopics.join(",") + ") will get author " + articleAuthors[i]);
}
console.log("\n== Testing the queries versus author subscriptions ==");
personSubs.forEach(function (subs, index) {
var found = false;
authorQueries.forEach(query => {
var currentAuthor = query[0];
var excludeAuthors = query[1];
if (subs.includes(currentAuthor) && !subs.includesAny(excludeAuthors) && !subs.includesAny(articleTopics)) {
console.log("Person with author subs (" + subs + ") will get push 'author " + currentAuthor + "'");
found = true;
}
});
if (!found) {
console.log("NO author pushes for person with subs (" + subs + ")");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment