-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiiListSort.js
More file actions
257 lines (233 loc) · 6.54 KB
/
Copy pathmiiListSort.js
File metadata and controls
257 lines (233 loc) · 6.54 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
export const MII_LIST_SORT_VALUES = Object.freeze([
"alphabetical",
"trending",
"top",
"latest",
]);
export const MII_LIST_SORT_DIRECTION_VALUES = Object.freeze(["asc", "desc"]);
const MII_LIST_SORT_SET = new Set(MII_LIST_SORT_VALUES);
const MII_LIST_SORT_ALIASES = new Map([
["popular", "top"],
["recent", "latest"],
]);
const MII_LIST_SORT_DIRECTION_SET = new Set(MII_LIST_SORT_DIRECTION_VALUES);
const MII_LIST_SORT_DIRECTION_ALIASES = new Map([
["ascending", "asc"],
["descending", "desc"],
]);
const TRENDING_TIME_DECAY_EXPONENT = 1.25;
export function normalizeMiiListSort(value, fallback = "top") {
const normalizedFallback = MII_LIST_SORT_SET.has(
String(fallback || "").toLowerCase()
)
? String(fallback).toLowerCase()
: "top";
const requested = typeof value === "string" ? value.trim().toLowerCase() : "";
if (MII_LIST_SORT_SET.has(requested)) return requested;
return MII_LIST_SORT_ALIASES.get(requested) || normalizedFallback;
}
export function normalizeMiiSearchSort(value, hasTextSearch = false) {
const requested = typeof value === "string" ? value.trim().toLowerCase() : "";
if (hasTextSearch && (!requested || requested === "relevance")) {
return "relevance";
}
return normalizeMiiListSort(requested, "top");
}
export function getDefaultMiiListSortDirection(value) {
const requested = typeof value === "string" ? value.trim().toLowerCase() : "";
const sort =
requested === "relevance" ? "relevance" : normalizeMiiListSort(requested);
return sort === "alphabetical" ? "asc" : "desc";
}
export function normalizeMiiListSortDirection(value, sort = "top") {
const requested = typeof value === "string" ? value.trim().toLowerCase() : "";
if (MII_LIST_SORT_DIRECTION_SET.has(requested)) return requested;
return (
MII_LIST_SORT_DIRECTION_ALIASES.get(requested) ||
getDefaultMiiListSortDirection(sort)
);
}
function resolveSortContext(value, nowOrDirection, direction) {
const sort = normalizeMiiListSort(value);
const directionInNowPosition = typeof nowOrDirection === "string";
const rawNow = directionInNowPosition ? Date.now() : nowOrDirection;
const rawDirection = directionInNowPosition ? nowOrDirection : direction;
return {
sort,
direction: normalizeMiiListSortDirection(rawDirection, sort),
now: Number.isFinite(Number(rawNow)) ? Number(rawNow) : Date.now(),
};
}
export function getMiiListSortStages(
value,
nowOrDirection = Date.now(),
direction
) {
const context = resolveSortContext(value, nowOrDirection, direction);
const { sort } = context;
const primaryDirection = context.direction === "asc" ? 1 : -1;
if (sort === "alphabetical") {
return [
{
$addFields: {
listSortName: {
$toLower: {
$let: {
vars: {
metaName: {
$trim: { input: { $ifNull: ["$meta.name", ""] } },
},
legacyName: { $trim: { input: { $ifNull: ["$name", ""] } } },
},
in: {
$cond: [
{ $ne: ["$$metaName", ""] },
"$$metaName",
"$$legacyName",
],
},
},
},
},
},
},
{
$sort: {
listSortName: primaryDirection,
uploadedOn: -1,
_id: -1,
},
},
];
}
if (sort === "trending") {
const normalizedNow = context.now;
return [
{
$addFields: {
listAgeHours: {
$max: [
0,
{
$divide: [
{
$subtract: [
normalizedNow,
{ $ifNull: ["$uploadedOn", normalizedNow] },
],
},
1000 * 60 * 60,
],
},
],
},
},
},
{
$addFields: {
listHotness: {
$divide: [
{ $ifNull: ["$votes", 0] },
{
$pow: [
{ $add: ["$listAgeHours", 2] },
TRENDING_TIME_DECAY_EXPONENT,
],
},
],
},
},
},
{
$sort: {
listHotness: primaryDirection,
uploadedOn: -1,
_id: -1,
},
},
];
}
if (sort === "latest") {
return [{ $sort: { uploadedOn: primaryDirection, _id: -1 } }];
}
return [
{
$sort: {
votes: primaryDirection,
uploadedOn: -1,
_id: -1,
},
},
];
}
function getDisplayName(mii) {
return String(mii?.meta?.name || mii?.name || "").trim();
}
function getTrendingScore(mii, now) {
const uploadedOn = Number(mii?.uploadedOn);
const ageHours = Number.isFinite(uploadedOn)
? Math.max(0, (now - uploadedOn) / (1000 * 60 * 60))
: 0;
return (
Number(mii?.votes || 0) /
Math.pow(ageHours + 2, TRENDING_TIME_DECAY_EXPONENT)
);
}
export function compareMiiListItems(
left,
right,
value,
nowOrDirection = Date.now(),
direction
) {
const context = resolveSortContext(value, nowOrDirection, direction);
const { sort } = context;
const directionMultiplier = context.direction === "asc" ? 1 : -1;
let difference = 0;
if (sort === "alphabetical") {
difference =
getDisplayName(left).localeCompare(getDisplayName(right), undefined, {
sensitivity: "base",
numeric: true,
}) * directionMultiplier;
} else if (sort === "trending") {
difference =
(getTrendingScore(left, context.now) -
getTrendingScore(right, context.now)) *
directionMultiplier;
} else if (sort === "latest") {
difference =
(Number(left?.uploadedOn || 0) - Number(right?.uploadedOn || 0)) *
directionMultiplier;
} else {
difference =
(Number(left?.votes || 0) - Number(right?.votes || 0)) *
directionMultiplier;
}
return (
difference ||
Number(right?.uploadedOn || 0) - Number(left?.uploadedOn || 0) ||
String(right?._id || right?.id || "").localeCompare(
String(left?._id || left?.id || "")
)
);
}
export function sortMiiListItems(
items,
value,
nowOrDirection = Date.now(),
direction
) {
const context = resolveSortContext(value, nowOrDirection, direction);
return Array.isArray(items)
? [...items].sort((left, right) =>
compareMiiListItems(
left,
right,
context.sort,
context.now,
context.direction
)
)
: [];
}