mirror of
https://github.com/xx254/linkedin_skills.git
synced 2026-06-10 07:24:56 +03:00
57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
function ensureDir(dirPath) {
|
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
}
|
|
|
|
function readJson(filePath, fallback = null) {
|
|
if (!fs.existsSync(filePath)) {
|
|
return fallback;
|
|
}
|
|
return JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
}
|
|
|
|
function writeJson(filePath, data) {
|
|
ensureDir(path.dirname(filePath));
|
|
fs.writeFileSync(filePath, JSON.stringify(data, null, 2) + "\n", "utf8");
|
|
}
|
|
|
|
function readText(filePath, fallback = "") {
|
|
if (!fs.existsSync(filePath)) {
|
|
return fallback;
|
|
}
|
|
return fs.readFileSync(filePath, "utf8");
|
|
}
|
|
|
|
function writeText(filePath, content) {
|
|
ensureDir(path.dirname(filePath));
|
|
fs.writeFileSync(filePath, content, "utf8");
|
|
}
|
|
|
|
function formatDateKey(date = new Date()) {
|
|
const y = date.getFullYear();
|
|
const m = String(date.getMonth() + 1).padStart(2, "0");
|
|
const d = String(date.getDate()).padStart(2, "0");
|
|
return `${y}_${m}_${d}`;
|
|
}
|
|
|
|
function getWeekKey(date = new Date()) {
|
|
const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
|
|
const dayNum = d.getUTCDay() || 7;
|
|
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
|
|
const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
|
|
const weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
|
|
return `${d.getUTCFullYear()}-W${String(weekNo).padStart(2, "0")}`;
|
|
}
|
|
|
|
module.exports = {
|
|
ensureDir,
|
|
readJson,
|
|
writeJson,
|
|
readText,
|
|
writeText,
|
|
formatDateKey,
|
|
getWeekKey
|
|
};
|