Skip to content

Commit 8a4ecee

Browse files
committed
Review changes
1 parent 54fb0aa commit 8a4ecee

1 file changed

Lines changed: 15 additions & 22 deletions

File tree

plugins/google-sheets/src/sheets.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ interface ProcessSheetRowParams {
254254
fieldTypes: CollectionFieldType[]
255255
row: Row
256256
rowIndex: number
257-
rowLength: number
257+
columnCount: number
258258
uniqueHeaderRowNames: string[]
259259
slugFieldColumnIndex: number
260260
ignoredFieldColumnIndexes: number[]
@@ -326,7 +326,7 @@ function getFieldDataEntryInput(type: CollectionFieldType, cellValue: CellValue)
326326
return isDefined(cellValue) ? { type, value: CELL_BOOLEAN_VALUES.includes(cellValue) } : null
327327
}
328328
case "date": {
329-
// Google Sheets numeric date values
329+
// Google Sheets numeric date values (Lotus 1-2-3 format)
330330
if (typeof cellValue === "number") {
331331
try {
332332
const date = extractDateFromSerialNumber(cellValue)
@@ -337,11 +337,8 @@ function getFieldDataEntryInput(type: CollectionFieldType, cellValue: CellValue)
337337
}
338338

339339
// ISO date format
340-
if (typeof cellValue === "string") {
341-
if (isValidISODate(cellValue)) {
342-
return { type, value: cellValue }
343-
}
344-
return null
340+
if (typeof cellValue === "string" && isValidISODate(cellValue)) {
341+
return { type, value: cellValue }
345342
}
346343

347344
return null
@@ -363,7 +360,7 @@ function getFieldDataEntryInput(type: CollectionFieldType, cellValue: CellValue)
363360
function processSheetRow({
364361
row,
365362
rowIndex,
366-
rowLength,
363+
columnCount,
367364
uniqueHeaderRowNames,
368365
ignoredFieldColumnIndexes,
369366
slugFieldColumnIndex,
@@ -374,7 +371,7 @@ function processSheetRow({
374371
let slugValue: string | null = null
375372
let itemId: string | null = null
376373

377-
for (let i = 0; i < rowLength; i++) {
374+
for (let i = 0; i < columnCount; i++) {
378375
const cell = row[i] ?? null
379376

380377
const fieldType = fieldTypes[i]
@@ -484,30 +481,26 @@ function processSheet(rows: Row[], processRowParams: Omit<ProcessSheetRowParams,
484481
)
485482
const collectionItems = result.filter(isDefined)
486483

487-
// Detect duplicate slugs and report error if any are found
488-
const slugCounts = new Map<string, number>()
484+
// Find duplicate slugs and report error if any are found
485+
const seenSlugs = new Set<string>()
489486
const duplicateSlugs = new Set<string>()
490487

491-
// Count occurrences of each slug
492-
collectionItems.forEach(item => {
493-
const count = slugCounts.get(item.slug) ?? 0
494-
slugCounts.set(item.slug, count + 1)
495-
496-
if (count > 0) {
488+
for (const item of collectionItems) {
489+
if (seenSlugs.has(item.slug)) {
497490
duplicateSlugs.add(item.slug)
491+
} else {
492+
seenSlugs.add(item.slug)
498493
}
499-
})
494+
}
500495

501496
if (duplicateSlugs.size > 0) {
502497
const slugList = formatListWithAnd(Array.from(duplicateSlugs))
503498
const pluralSuffix = duplicateSlugs.size > 1 ? "s" : ""
504499
throw new Error(`Duplicate slug${pluralSuffix} found: ${slugList}. Each item must have a unique slug.`)
505500
}
506501

507-
const processedItems = collectionItems
508-
509502
return {
510-
collectionItems: processedItems,
503+
collectionItems,
511504
status,
512505
}
513506
}
@@ -569,7 +562,7 @@ export async function syncSheet({
569562
fieldTypes: colFieldTypes,
570563
ignoredFieldColumnIndexes: ignoredColumns.map(col => uniqueHeaderRowNames.indexOf(col)),
571564
slugFieldColumnIndex: slugColumn ? uniqueHeaderRowNames.indexOf(slugColumn) : -1,
572-
rowLength: headerRow.length,
565+
columnCount: headerRow.length,
573566
})
574567

575568
// Calculate items to delete based on what's in the collection vs what we processed

0 commit comments

Comments
 (0)