forked from rashikacodes/MAARG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_next_array.py
More file actions
26 lines (18 loc) · 1.13 KB
/
Copy pathfix_next_array.py
File metadata and controls
26 lines (18 loc) · 1.13 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
import re
with open("app/app/api/routes/route.ts", "r", encoding="utf-8") as f:
code = f.read()
pattern = r"const indexed = rawRoutes\.map\(\(r, i\) => \(\{\s*r,\s*scored: scored\[i\].*?\s*i,\s*\}\)\);\s*indexed\.sort\(\(a, b\) => a\.scored\.disruption_risk - b\.scored\.disruption_risk\);\s*const routes = indexed\.map\(\(\{ r, scored: s \}, rankIdx\) => \{"
replacement = r""" // Iterate over scored since ML might have added detour routes!
const indexed = scored.map((s, i) => {
// Find the original rawRoute that this route is based on
const originalRouteIdMatch = s.route_id.match(/route_(\d+)/);
const originalIndex = originalRouteIdMatch ? parseInt(originalRouteIdMatch[1]) - 1 : 0;
const r = rawRoutes[originalIndex] || rawRoutes[0];
return { r, scored: s, i };
});
indexed.sort((a, b) => a.scored.disruption_risk - b.scored.disruption_risk);
const routes = indexed.map(({ r, scored: s }, rankIdx) => {"""
code = re.sub(pattern, replacement, code, flags=re.DOTALL)
with open("app/app/api/routes/route.ts", "w", encoding="utf-8") as f:
f.write(code)
print("Updated Next.js to map over scored routes.")