-
-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathalgorithms.ts
More file actions
38 lines (35 loc) · 1.06 KB
/
algorithms.ts
File metadata and controls
38 lines (35 loc) · 1.06 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
import fs from "fs";
import path from "path";
import type { Algorithm } from "./models";
import { DATA_DIR } from "./constants";
import { dataGetDir, dataGetFile } from "./fs";
export async function getAlgorithmSlugs() {
// This function is only used when localization is disabled,
// so only the english URLs get returned.
return (await dataGetDir("algorithms")).map((file) => ({
params: {
algorithm: file.replace(".json", ""),
},
}));
}
export async function getAlgorithm(slug: string, minimal = false) {
const file = await dataGetFile(
path.join(minimal ? "algorithms-min" : "algorithms", `${slug}.json`)
);
if (!file) return undefined;
const algorithm: Algorithm = JSON.parse(file);
return algorithm;
}
export async function getAllAlgorithms() {
return (await Promise.all(
(
await fs.promises.readdir(path.join(DATA_DIR, "algorithms-min"))
).map(async (file) =>
JSON.parse(
(
await fs.promises.readFile(path.join(DATA_DIR, "algorithms", file))
).toString()
)
)
)) as Algorithm[];
}