Fix off-by-one errors
All checks were successful
timw4mail/HummingBirdAnimeClient/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2020-04-21 20:37:42 -04:00
parent fe284d755b
commit 12be7b8a1e

View File

@ -73,30 +73,26 @@ class AnimeHistoryTransformer {
$count = count($singles); $count = count($singles);
for ($i = 0; $i < $count; $i++) for ($i = 0; $i < $count; $i++)
{ {
$entries = [];
$entry = $singles[$i]; $entry = $singles[$i];
$prevTitle = $entry['title']; $prevTitle = $entry['title'];
$nextId = $i + 1; $nextId = $i;
if ($nextId < $count) $next = $singles[$nextId];
{ while (
$entries = []; $next['kind'] === 'progressed' &&
$next = $singles[$nextId]; $next['title'] === $prevTitle
while ( ) {
$next['kind'] === 'progressed' && $entries[] = $next;
$next['title'] === $prevTitle $prevTitle = $next['title'];
) {
$entries[] = $next;
$prevTitle = $next['title'];
if ($nextId + 1 < $count) if ($nextId + 1 < $count)
{ {
$nextId++; $nextId++;
$next = $singles[$nextId]; $next = $singles[$nextId];
} continue;
else
{
break;
}
} }
break;
} }
if (count($entries) > 1) if (count($entries) > 1)
@ -112,9 +108,6 @@ class AnimeHistoryTransformer {
$title = $entries[0]['title']; $title = $entries[0]['title'];
// Get rid of the single entry added before aggregating
// array_pop($output);
$action = (count($entries) > 3) $action = (count($entries) > 3)
? "Marathoned episodes {$firstEpisode}-{$lastEpisode} of {$title}" ? "Marathoned episodes {$firstEpisode}-{$lastEpisode} of {$title}"
: "Watched episodes {$firstEpisode}-{$lastEpisode} of {$title}"; : "Watched episodes {$firstEpisode}-{$lastEpisode} of {$title}";
@ -128,13 +121,11 @@ class AnimeHistoryTransformer {
]); ]);
// Skip the rest of the aggregate in the main loop // Skip the rest of the aggregate in the main loop
$i += count($entries); $i += count($entries) - 1;
$prevTitle = $title;
continue; continue;
} }
else else
{ {
$prevTitle = $entry['title'];
$output[] = $entry; $output[] = $entry;
} }