-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdedup-entries.js
More file actions
85 lines (71 loc) · 2.22 KB
/
dedup-entries.js
File metadata and controls
85 lines (71 loc) · 2.22 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// Get current directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Path to entries.js
const entriesFile = path.join('src', 'data', 'entries.js');
// Read the file content
fs.readFile(entriesFile, 'utf8', (err, fileContent) => {
if (err) {
console.error('Error reading file:', err);
return;
}
// Extract the entries array using regex
const match = fileContent.match(/export const entries = \[([\s\S]*)\];/);
if (!match) {
console.error('Could not find entries array in file');
return;
}
// Parse the entries
let entriesStr = match[1];
// Split the entries by objects - looking for '},{' pattern
const entriesParts = entriesStr.split(/\},[\s\n]*\{/g);
const uniqueEntries = [];
const seenTitles = new Set();
let duplicateCount = 0;
// Process each entry
entriesParts.forEach((part, index) => {
// Clean up the part
let cleanPart = part;
if (index === 0) {
cleanPart = part.trim();
} else if (index === entriesParts.length - 1) {
cleanPart = part.trim();
} else {
cleanPart = part.trim();
}
// Extract title
const titleMatch = cleanPart.match(/title:\s*["']([^"']*)["']/);
if (!titleMatch) {
uniqueEntries.push(cleanPart);
return;
}
const title = titleMatch[1];
if (!seenTitles.has(title)) {
seenTitles.add(title);
uniqueEntries.push(cleanPart);
} else {
duplicateCount++;
console.log(`Found duplicate: ${title}`);
}
});
console.log(`Found ${duplicateCount} duplicates.`);
// Reconstruct the entries array
const newEntriesStr = uniqueEntries.join('},\n {');
const newFileContent =
fileContent.substring(0, match.index) +
'export const entries = [\n {' +
newEntriesStr +
'}\n];' +
fileContent.substring(match.index + match[0].length);
// Write the result back to file
fs.writeFile(entriesFile, newFileContent, 'utf8', (err) => {
if (err) {
console.error('Error writing file:', err);
return;
}
console.log(`Successfully removed ${duplicateCount} duplicate entries.`);
});
});