lab 04: node debugging, GET date API, file read/write (OOP)
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b8a795060b
commit
eb3328ff1c
6 files changed
+152
No files matched your search
@@ -0,0 +1,26 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
class FileManager {
|
||||
constructor(baseDir) {
|
||||
this.baseDir = baseDir;
|
||||
}
|
||||
|
||||
resolve(fileName) {
|
||||
return path.join(this.baseDir, path.basename(fileName));
|
||||
}
|
||||
|
||||
exists(fileName) {
|
||||
return fs.existsSync(this.resolve(fileName));
|
||||
}
|
||||
|
||||
append(fileName, text) {
|
||||
fs.appendFileSync(this.resolve(fileName), `${text}\n`);
|
||||
}
|
||||
|
||||
read(fileName) {
|
||||
return fs.readFileSync(this.resolve(fileName), "utf8");
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = FileManager;
|
||||
@@ -0,0 +1,13 @@
|
||||
const PI = 3.141592654;
|
||||
|
||||
exports.add = (a, b) => {
|
||||
return a + b
|
||||
}
|
||||
|
||||
exports.subtract = (a, b) => {
|
||||
return a - b
|
||||
}
|
||||
|
||||
exports.area = (r) => {
|
||||
return 2*PI*r;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
class Utils {
|
||||
getDate() {
|
||||
return new Date().toString();
|
||||
}
|
||||
|
||||
format(template, ...values) {
|
||||
return template.replace(/%(\d+)/g, (match, index) => {
|
||||
const value = values[Number(index) - 1];
|
||||
return value === undefined ? match : value;
|
||||
});
|
||||
}
|
||||
|
||||
escapeHtml(text) {
|
||||
const entities = {"&": "&", "<": "<", ">": ">", "\"": """, "'": "'"};
|
||||
return text.replace(/[&<>"']/g, (char) => entities[char]);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Utils;
|
||||
Reference in new issue
Block a user