-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
121 lines (104 loc) · 3.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import patchlogs from './data/patchlogs.json' assert { type: 'json' };
const keys = ['changes', 'fixes', 'additions'];
/**
* @typedef {Object} RawPatchData
* @property {string} name
* @property {string} date
* @property {string} imgUrl
* @property {string} url
*/
/**
* @typedef {Object} PatchData
* @property {string} name
* @property {string} date
* @property {string} imgUrl
* @property {string} url
* @property {string} additions
* @property {string} fixes
* @property {string} changes
*/
/**
* @typedef {Object} PatchOptions
* @property {string} name item name to search for
* @property {string} type item type to match
* @property {Array<Ability>?} abilities item abilities
*/
/**
* @typedef {Object} Ability
* @property {string} name
*/
class Patchlogs {
#posts /** @type {Array<PatchData>} */ = patchlogs;
get posts() {
return Object.freeze(this.#posts);
}
/**
* Retrieve patch logs specific to a certain item. Still very much Beta,
* probably always will be, but I'm trying \o/
* @param {PatchOptions} item item to pull changes from
* @returns {Array<PatchData>}
*/
getItemChanges(item) {
const logs = [];
const target = { ...item }; // Don't mutate the original item
// If item is a Prime Warframe/Sentinel, we should include patchlogs of
// normal variants too, as they share the same abilities.
if (target.type === 'Warframe' && target.name.includes('Prime')) {
target.name = target.name.replace(' Prime', '');
}
this.#posts.forEach((post) => {
/** @type {PatchData} */
const log = {
name: post.name,
date: post.date,
url: post.url,
imgUrl: post.imgUrl,
additions: '',
changes: '',
fixes: '',
};
// Parse changes, fixes, additions
keys.forEach((key) => {
const lines = post[key].split('\n');
lines.forEach((line, i) => {
let includesAbility = false;
// Loop through abilities to see if line contains that name. Could be
// solved easier with some regex, but that causes some memory leak
// that I'm unable to understand.
if (target && target.abilities) {
target.abilities.forEach((ability) => {
includesAbility = line.includes(ability.name) ? true : includesAbility;
});
}
if (line.includes(target.name) || includesAbility) {
const changes = [];
changes.push(line);
// Changes are in multiple lines (until next line with `:`)
if (line.endsWith(':')) {
for (let j = i + 1; j < lines.length; j += 1) {
const subline = lines[i];
if (subline.endsWith(':')) {
// eslint-disable-next-line no-param-reassign
i += j - 1;
break;
} else {
changes.push(subline);
}
}
}
log[key] += log[key] ? `\n${changes.join('\n')}` : changes.join('\n');
}
});
});
if (log.changes || log.fixes || log.additions) {
logs.push(log);
}
});
return logs.sort((a, b) => {
const d1 = new Date(a.date);
const d2 = new Date(b.date);
return d2 - d1;
});
}
}
export default new Patchlogs();