· 8 years ago · May 15, 2018, 06:58 PM
1const gameDetector = (gamesHash, commentsArray) => {
2 //Create a lookup table from the input hash.
3 let unsorted = createLookup(gamesHash);
4
5 //After creating the lookup table, sort it in descending order to tag larger words over smaller words.
6 let sorted = sortObject(unsorted);
7
8 //Concat the comments into one big string to reduce the runtime complexity.
9 let comments = commentsArray.join('#####');
10
11 //Iterating through each lookup value.
12 for (let index in sorted) {
13 //JSLint does not like iteration through an object.
14 if (sorted.hasOwnProperty(index)) {
15 //Find whether the lookup word exists in the comment.
16 let position = comments.indexOf(index);
17
18 //Define empty comments to store new comments if the lookup word exists.
19 let taggedComments = '';
20
21 //Check to see if the word has been tagged already.
22 let tagCheck = comments.slice(position - 4, position);
23
24 //If the word exists and it hasn't been tagged yet, create new comments.
25 if (position !== -1 && tagCheck !== 'TAG{') {
26 //Create a new anagram with the tag.
27 let tagged = `TAG{${sorted[index]},${index}}`;
28
29 /*
30 Find the word in the comments and replace it with tagged string. The replace function
31 does not mutate the existing comments, so the new comments must be stored in a new variable.
32 */
33 taggedComments = comments.replace(index, tagged);
34 }
35
36 /*
37 Since the tagged comments starts out as a falsy value, the comments will remain
38 the same if nothing is replaced. Otherwise, if the comments were tagged,
39 the statement will short-circuit and set the comments variable as the new, tagged comments.
40 */
41 comments = taggedComments || comments;
42 }
43 }
44
45 //Return the tagged comments array (or the original array if nothing was changed/found).
46 return comments.split('#####');
47};
48
49//Helper method for creating an inverted lookup table from the input hash.
50const createLookup = gamesHash => {
51 //Define an empty lookup table.
52 let lookup = {};
53
54 //Iterate through the hash input.
55 for (let key in gamesHash) {
56 if (gamesHash.hasOwnProperty(key)) {
57 //Retrieve the value from the hash which should be an array.
58 let value = gamesHash[key];
59
60 //Iterate through that values array and set the lookup table key as the value and vice-versa.
61 for (let j = 0; j < value.length; j++) {
62 lookup[value[j]] = key;
63 }
64 }
65 }
66
67 //Return the lookup table.
68 return lookup;
69};
70
71//Helper method for sorting an object.
72const sortObject = object => {
73 //Make an array for sorting purposes since we cannot sort an object normally.
74 let list = [];
75
76 //Iterate through the object and push in arrays of the key-value pairs.
77 for (let key in object) {
78 if (object.hasOwnProperty(key)) {
79 list.push([key, object[key]]);
80 }
81 }
82
83 //Sort the list in descending order by length of the key.
84 list.sort(function(a, b) {
85 return b[0].length - a[0].length;
86 });
87
88 //Define a new lookup table that is in sorted order.
89 let ordered = {};
90
91 //Iterate through the array.
92 for (let i = 0; i < list.length; i++) {
93 let key = list[i][0];
94 let value = list[i][1];
95
96 //Set key-value pairs for the new lookup table.
97 ordered[key] = value;
98 }
99
100 //Return the lookup table sorted.
101 return ordered;
102};