commit 0bcd68bf79db4a6b646b7b02c9a105a02021e066
parent 7871679f3870cea2764046124a1b557581168694
Author: AsherMorgan <59518073+AsherMorgan@users.noreply.github.com>
Date: Sat, 4 Apr 2020 08:41:00 -0700
Refactor Filter.js.
Diffstat:
2 files changed, 96 insertions(+), 80 deletions(-)
diff --git a/Filter.js b/Filter.js
@@ -1,90 +1,106 @@
-class Filter {
- // Create a new Filter
- constructor(io, value) {
- // Set variables
- this.IO = io; // Format: [[<output index>, <input index>]]
- this.Value = value; // Format: [[<index>, [<values>], exclude?]]
+// Filters a vocabulary set given the filter name
+function ApplyFilter(vocabSet, name) {
+ // Declare variables
+ var io; // Format: [[<output index>, <input index>]]
+ var value; // Format: [[<index>, [<values>], exclude?]]
+
+ // Get filter
+ switch (name) {
+ case "All Definitions":
+ io = [[0,1], [1,0]];
+ value = [];
+ break;
+
+ case "English to Spanish":
+ io = [[0,1]];
+ value = [];
+ break;
+
+ case "Spanish to English":
+ io = [[1,0]];
+ value = [];
+ break;
+
+ case "All Conjugations":
+ io = [[0,2], [0,4], [0,5], [0,6], [0,7], [0,8], [0,10], [0,11], [0,12], [0,13], [0,14], [0,16], [0,17], [0,18], [0,19], [0,20]];
+ value = [];
+ break;
+
+ case "Reverse Conjugations":
+ io = [[2,0], [4,0], [5,0], [6,0], [7,0], [8,0], [10,0], [11,0], [12,0], [13,0], [14,0], [16,0], [17,0], [18,0], [19,0], [20,0]];
+ value = [];
+ break;
+
+ case "Present Participles":
+ io = [[0,2]];
+ value = [];
+ break;
+
+ case "Present Tense":
+ io = [[0,4], [0,5], [0,6], [0,7], [0,8]];
+ value = [];
+ break;
+
+ case "Preterite Tense":
+ io = [[0,10], [0,11], [0,12], [0,13], [0,14]]
+ value = [];
+ break;
+
+ case "Imperfect Tense":
+ io = [[0,16], [0,17], [0,18], [0,19], [0,20]];
+ value = [];
+ break;
+
+ case "Nouns":
+ io = [[0,1], [1,0]];
+ value = [[2, ["Noun"], false]];
+ break;
+
+ case "Verbs":
+ io = [[0,1], [1,0]];
+ value = [[2, ["Verb"], false]];
+ break;
+
+ case "Adjectives":
+ io = [[0,1], [1,0]];
+ value = [[2, ["Adjective"], false]];
+ break;
+
+ default:
+ io = [];
+ value = [];
+ break;
}
-
- // Apply the filter to a vocabulary set
- Apply(vocabSet) {
- // Filter terms by value
- var vSet = vocabSet.slice(1); // Format: same as vocabSet but without headers
- for (var i = 0; i < this.Value.length; i++) {
- for (var j = 0; j < vSet.length; j++) {
- if (this.Value[i][2]) {
- // Exclude values
- if (this.Value[i][1].includes(vSet[j][this.Value[i][0]])) {
- vSet.splice(j, 1); // Remove item
- j--; // Adjust for the removal of an item
- }
- }
- else {
- // Include values
- if (!this.Value[i][1].includes(vSet[j][this.Value[i][0]])) {
- vSet.splice(j, 1); // Remove item
- j--; // Adjust for the removal of an item
- }
+ // Filter terms by value
+ var vSet = vocabSet.slice(1); // Format: same as vocabSet but without headers
+ for (var i = 0; i < value.length; i++) {
+ for (var j = 0; j < vSet.length; j++) {
+ if (value[i][2]) {
+ // Exclude values
+ if (value[i][1].includes(vSet[j][value[i][0]])) {
+ vSet.splice(j, 1); // Remove item
+ j--; // Adjust for the removal of an item
}
}
- }
-
- // Filter terms by input/output
- var ioSet = []; // Format: [<output type>, <output>, <input type>, <input>]
- for (var i = 0; i < this.IO.length; i++) {
- for (var j = 0; j < vSet.length; j++) {
- ioSet.push([vocabSet[0][this.IO[i][0]], vSet[j][this.IO[i][0]], vocabSet[0][this.IO[i][1]], vSet[j][this.IO[i][1]]]);
+ else {
+ // Include values
+ if (!value[i][1].includes(vSet[j][value[i][0]])) {
+ vSet.splice(j, 1); // Remove item
+ j--; // Adjust for the removal of an item
+ }
}
}
-
- // Return filtered set
- return ioSet;
}
-
-
- // Get a common filter
- static GetFilter(name) {
- switch (name) {
- case "All Definitions":
- return new Filter([[0,1], [1,0]], []);
-
- case "English to Spanish":
- return new Filter([[0,1]], []);
-
- case "Spanish to English":
- return new Filter([[1,0]], []);
-
- case "All Conjugations":
- return new Filter([[0,2], [0,4], [0,5], [0,6], [0,7], [0,8], [0,10], [0,11], [0,12], [0,13], [0,14], [0,16], [0,17], [0,18], [0,19], [0,20]], []);
-
- case "Reverse Conjugations":
- return new Filter([[2,0], [4,0], [5,0], [6,0], [7,0], [8,0], [10,0], [11,0], [12,0], [13,0], [14,0], [16,0], [17,0], [18,0], [19,0], [20,0]], []);
-
- case "Present Participles":
- return new Filter([[0,2]], []);
-
- case "Present Tense":
- return new Filter([[0,4], [0,5], [0,6], [0,7], [0,8]], []);
-
- case "Preterite Tense":
- return new Filter([[0,10], [0,11], [0,12], [0,13], [0,14]], []);
-
- case "Imperfect Tense":
- return new Filter([[0,16], [0,17], [0,18], [0,19], [0,20]], []);
-
- case "Nouns":
- return new Filter([[0,1], [1,0]], [[2, ["Noun"], false]]);
-
- case "Verbs":
- return new Filter([[0,1], [1,0]], [[2, ["Verb"], false]]);
-
- case "Adjectives":
- return new Filter([[0,1], [1,0]], [[2, ["Adjective"], false]]);
-
- default:
- return new Filter([], []);
+ // Filter terms by input/output
+ var ioSet = []; // Format: [<output type>, <output>, <input type>, <input>]
+ for (var i = 0; i < io.length; i++) {
+ for (var j = 0; j < vSet.length; j++) {
+ ioSet.push([vocabSet[0][io[i][0]], vSet[j][io[i][0]], vocabSet[0][io[i][1]], vSet[j][io[i][1]]]);
}
}
+
+ // Return filtered set
+ return ioSet;
}
\ No newline at end of file
diff --git a/Spanish Quizzer.js b/Spanish Quizzer.js
@@ -269,7 +269,7 @@ function Start() {
var filter = document.getElementById(`settingsSetFilter-${i}`).value;
// Add filtered set
- Terms.push(...Filter.GetFilter(filter).Apply(Sets[set]));
+ Terms.push(...ApplyFilter(Sets[set], filter));
}
}