Skip to content

Commit 8890b3f

Browse files
committed
refactor if statements
1 parent b7d6507 commit 8890b3f

1 file changed

Lines changed: 13 additions & 23 deletions

File tree

lib/attachments/gridstore-storage.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -366,33 +366,23 @@ class GridstoreStorage {
366366
}
367367
}
368368

369-
if (streamOptions.start && streamOptions.start > attachmentData.length) {
370-
streamOptions.start = attachmentData.length;
371-
}
369+
streamOptions.start = Math.min(
370+
streamOptions.start ?? 0, // Default to 0 if undefined/null
371+
attachmentData.length
372+
);
372373

373-
if (streamOptions.end && streamOptions.end > attachmentData.length) {
374-
streamOptions.end = attachmentData.length;
375-
}
374+
streamOptions.end = Math.min(
375+
streamOptions.end ?? attachmentData.length, // Default to length if undefined/null
376+
attachmentData.length
377+
);
376378

377-
// Handle case where start equals or exceeds end
378-
if (streamOptions.start && streamOptions.end && streamOptions.start >= streamOptions.end) {
379+
if (streamOptions.start >= attachmentData.length) {
380+
// If start is at or beyond EOF, set both to EOF
379381
streamOptions.start = attachmentData.length;
380382
streamOptions.end = attachmentData.length;
381-
}
382-
383-
// Handle case where start equals file length (EOF scenario)
384-
if (streamOptions.start === attachmentData.length && !streamOptions.end) {
385-
streamOptions.end = attachmentData.length;
386-
}
387-
388-
if (streamOptions.end === null || streamOptions.end === undefined) {
389-
// still no stream end, default to length
390-
streamOptions.end = attachmentData.length;
391-
}
392-
393-
if (streamOptions.start > streamOptions.end) {
394-
// Ensure in any case that start is never bigger than end
395-
streamOptions.start = streamOptions.end;
383+
} else if (streamOptions.start >= streamOptions.end) {
384+
// If start >= end, clamp start to end (but not beyond file length)
385+
streamOptions.start = Math.min(streamOptions.end, attachmentData.length);
396386
}
397387
}
398388

0 commit comments

Comments
 (0)