Skip to content

Commit 4ddf7e8

Browse files
authored
feat: update date and pipe format checking
add correct date formatting check and fix, as well as end pipes `|` auto check and fix as well
1 parent 31d0c99 commit 4ddf7e8

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

Diff for: scripts/add-dates.js

+36-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
const fs = require('fs');
22

3+
// Helper function to format a date into YYYY-MM-DD format
4+
function formatDate(date) {
5+
const d = new Date(date);
6+
if (isNaN(d)) {
7+
return null; // Invalid date
8+
}
9+
const year = d.getFullYear();
10+
const month = String(d.getMonth() + 1).padStart(2, '0');
11+
const day = String(d.getDate()).padStart(2, '0');
12+
return `${year}-${month}-${day}`;
13+
}
14+
315
const path = 'README.md';
416
const content = fs.readFileSync(path, 'utf8');
517
const lines = content.split('\n');
618

7-
const currentDate = new Date().toISOString().split('T')[0]; // e.g., 2023-10-15
19+
const currentDate = formatDate(new Date()); // Get current date in YYYY-MM-DD format
820
const updatedLines = [];
921
let changesCount = 0;
1022

@@ -41,7 +53,7 @@ for (let i = 0; i < lines.length; i++) {
4153
headerLineCount++;
4254
}
4355

44-
const parts = line.split('|');
56+
const parts = line.split('|').map(part => part.trim());
4557

4658
// Skip header and separator rows
4759
if (headerLineCount <= 2) {
@@ -51,8 +63,14 @@ for (let i = 0; i < lines.length; i++) {
5163

5264
// Handle data rows
5365
if (tableHasDateColumn && dateColumnIndex > 0) {
54-
// Check if this row needs a date
55-
const dateValue = parts[dateColumnIndex].trim();
66+
// Ensure the row has enough columns
67+
if (parts.length <= dateColumnIndex) {
68+
console.warn(`Skipping malformed row (not enough columns): ${line}`);
69+
updatedLines.push(line); // Skipping malformed row
70+
continue;
71+
}
72+
73+
let dateValue = parts[dateColumnIndex].trim();
5674

5775
if (!dateValue || dateValue === '') {
5876
// This row needs a date
@@ -63,8 +81,20 @@ for (let i = 0; i < lines.length; i++) {
6381
changesCount++;
6482
console.log(`Added date to row: ${parts[1].trim()}`);
6583
} else {
66-
// Row already has a date
67-
updatedLines.push(line);
84+
// Check if the existing date is valid and in the correct format
85+
const formattedDate = formatDate(dateValue);
86+
if (formattedDate !== dateValue) {
87+
// The date is not in the correct format, so we update it
88+
parts[dateColumnIndex] = ` ${formattedDate} `;
89+
let newLine = parts.join('|').trimEnd();
90+
if (!newLine.endsWith('|')) newLine += ' |';
91+
updatedLines.push(newLine);
92+
changesCount++;
93+
console.log(`Updated date for row: ${parts[1].trim()} to ${formattedDate}`);
94+
} else {
95+
// Row already has a valid date
96+
updatedLines.push(line);
97+
}
6898
}
6999
} else {
70100
// No date column in this table

0 commit comments

Comments
 (0)