增加转格式

This commit is contained in:
yinsx
2025-12-20 11:51:35 +08:00
parent 5315a97613
commit d9abc57b0b
32 changed files with 4339 additions and 229 deletions

35
lib/stats.js Normal file
View File

@ -0,0 +1,35 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const STATS_FILE = path.join(__dirname, "../.stats.json");
let data = null;
function load() {
if (data) return data;
try {
data = JSON.parse(fs.readFileSync(STATS_FILE, "utf8"));
} catch {
data = {};
}
return data;
}
function save() {
fs.writeFileSync(STATS_FILE, JSON.stringify(data, null, 2));
}
export function record(category, key) {
load();
if (!data[category]) data[category] = {};
data[category][key] = (data[category][key] || 0) + 1;
save();
}
export function sortByUsage(category, items, getKey = v => v) {
load();
const counts = data[category] || {};
return [...items].sort((a, b) => (counts[getKey(b)] || 0) - (counts[getKey(a)] || 0));
}