All files / Rindu/utils analyzePlaylist.ts

90.47% Statements 38/42
80.76% Branches 21/26
100% Functions 7/7
92.68% Lines 38/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120        44x 44x 44x                     4x 4x 4x 4x 4x 4x   4x 1x     3x       3x       3x       3x 1x         2x 1x         1x           7x                       5x 1x   4x 4x 4x 5x   4x   10x     6x   4x     4x   7x     4x 2x   2x     4x                   4x                 44x  
import { ReactNode } from "react";
 
import { ITrack } from "types/spotify";
import { ITranslations } from "types/translations";
import { findDuplicateSongs } from "utils/findDuplicateSongs";
import { getAllTracksFromPlaylist } from "utils/getAllTracksFromPlaylist";
import { templateReplace } from "utils/templateReplace";
 
function getSummary(
  res: {
    tracks: ITrack[];
    duplicateTracksIndexes: number[];
    corruptedSongsIndexes: number[];
    tracksToRemove: ITrack[];
  },
  translations: ITranslations
): string | ReactNode[] {
  const has0DuplicateTracks = res.duplicateTracksIndexes.length === 0;
  const has0CorruptedSongs = res.corruptedSongsIndexes.length === 0;
  const hasSingleDuplicateTrack = res.duplicateTracksIndexes.length === 1;
  const hasSingleCorruptedTrack = res.corruptedSongsIndexes.length === 1;
  const hasMultipleDuplicateTracks = res.duplicateTracksIndexes.length > 1;
  const hasMultipleCorruptedTracks = res.corruptedSongsIndexes.length > 1;
 
  if (has0DuplicateTracks && has0CorruptedSongs) {
    return translations.removeTracksModal.noCorruptOrDuplicateSongs;
  }
 
  Iif (hasSingleDuplicateTrack && hasSingleCorruptedTrack) {
    return translations.removeTracksModal.oneDuplicateOneCorrupt;
  }
 
  Iif (hasSingleDuplicateTrack && has0CorruptedSongs) {
    return translations.removeTracksModal.oneDuplicate;
  }
 
  Iif (has0DuplicateTracks && hasSingleCorruptedTrack) {
    return translations.removeTracksModal.oneCorrupt;
  }
 
  if (has0DuplicateTracks && hasMultipleCorruptedTracks) {
    return templateReplace(translations.removeTracksModal.multipleCorrupt, [
      res.corruptedSongsIndexes.length,
    ]);
  }
 
  if (hasMultipleDuplicateTracks && has0CorruptedSongs) {
    return templateReplace(translations.removeTracksModal.multipleDuplicate, [
      res.duplicateTracksIndexes.length,
    ]);
  }
 
  return templateReplace(
    translations.removeTracksModal.multipleCorruptAndMultipleDuplicate,
    [res.corruptedSongsIndexes.length, res.duplicateTracksIndexes.length]
  );
}
 
export async function analyzePlaylist(
  id: string | undefined,
  totalTracks: number | undefined,
  isLibrary: boolean,
  translations: ITranslations
): Promise<{
  tracks: ITrack[];
  duplicateTracksIndexes: number[];
  corruptedSongsIndexes: number[];
  tracksToRemove: ITrack[];
  summary: string | ReactNode[];
} | null> {
  if (!id || !totalTracks) {
    return null;
  }
  const tracks = await getAllTracksFromPlaylist(id, totalTracks, isLibrary);
  const duplicatesTracksIdxId = findDuplicateSongs(tracks);
  const duplicateTracksIndexes = duplicatesTracksIdxId.map(({ index }) => {
    return index;
  });
  const corruptedSongsIndexes = tracks
    .filter((track) => {
      return track.corruptedTrack;
    })
    .map((track) => {
      return track.position ?? 0;
    });
  const tracksToRemoveIdx = [
    ...new Set([...corruptedSongsIndexes, ...duplicateTracksIndexes]),
  ];
  const tracksToRemove = tracksToRemoveIdx
    .map((index) => {
      return tracks[index];
    })
    .sort((a, b) => {
      if (a.position && b.position) {
        return a.position - b.position;
      }
      return 0;
    });
 
  const summary = getSummary(
    {
      tracks,
      duplicateTracksIndexes,
      corruptedSongsIndexes,
      tracksToRemove,
    },
    translations
  );
 
  return {
    tracks,
    duplicateTracksIndexes,
    corruptedSongsIndexes,
    tracksToRemove,
    summary,
  };
}
 
export default analyzePlaylist;