Skip to content

Instantly share code, notes, and snippets.

@Aslemammad
Last active March 1, 2025 05:47
Show Gist options
  • Save Aslemammad/de33cab56465af1c9ceed0b015ca9ff5 to your computer and use it in GitHub Desktop.
Save Aslemammad/de33cab56465af1c9ceed0b015ca9ff5 to your computer and use it in GitHub Desktop.
some description
import fs from 'node:fs';
import path from 'node:path';
// a comment
export class SnapshotState {
private snapshotPath: string;
private id: number;
constructor(snapshotDir: string, id: number) {
this.id = id;
this.snapshotPath = path.join(process.cwd(), snapshotDir, 'test.snap');
}
loadSnapshot<T>(actual: T): T | undefined {
try {
const data = fs.readFileSync(this.snapshotPath, 'utf-8');
const snapshots = JSON.parse(data) as Record<number, T>;
return snapshots[this.id];
} catch (e: any) {
return undefined;
}
}
saveSnapshot<T>(actual: T) {
let snapshots: Record<number, T> = {};
try {
const data = fs.readFileSync(this.snapshotPath, 'utf-8');
snapshots = JSON.parse(data) as Record<number, T>;
} catch (e) {
// File doesn't exist or is invalid, start with empty snapshots
}
snapshots[this.id] = actual;
fs.writeFileSync(this.snapshotPath, JSON.stringify(snapshots, null, 2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment