All files / track-lists track-utils.js

95.65% Statements 22/23
60% Branches 12/20
100% Functions 2/2
95.65% Lines 22/23

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          5x 13x 11x     2x                         5x 16x       16x 3x     13x 7x           6x 6x   6x     6x       6x   6x 6x   2x   6x 6x   6x           6x        
/**
 * For a given track in an album, it formats the track title value
 * @param { string } trackTitle
 * @param { string } fileName
 */
const formatTrackTitle = (trackTitle = '', fileName = '') => {
  if (trackTitle) {
    return trackTitle;
  }
  // strip file extension from its name and return
  return fileName.replace(/.mp3/, '');
};
 
/**
 * For a given track in an album, it formats the track artist value
 * This follows the structured metadata for collections in: `acdc`, `album_recordings`, & `78rpm`
 *
 * @param { string } trackArtist - delimited by `;\s|;`;
 * @param { array|string } albumCreator
 * @param { string } albumTitle
 *
 * @returns { string }
 */
const formatTrackArtist = (trackArtist = '', albumCreator = '', albumTitle = '') => {
  const titleHasTrackArtist = albumTitle.includes(trackArtist);
 
  // if album title has artist name => no
  // we are assuming that this is a "Best of <artist>" album
  if (titleHasTrackArtist) {
    return '';
  }
 
  if (trackArtist === albumCreator) {
    return '';
  }
 
  // Track Artists come in a delimited string
  // `;\s` => acdc, album_recordings
  // `;` => 78rpm
  const parseArtistDelimiter = new RegExp(/;\s?/g);
  const joinArtistDelimiter = '; ';
 
  const listAlbumArtists = !Array.isArray(albumCreator)
    ? albumCreator.split(parseArtistDelimiter)
    : albumCreator;
  const listTrackArtists = trackArtist.split(parseArtistDelimiter);
 
  /* Compliation Check */
  // check for most general flag for a compilation album
  const [initialArtist = ''] = listAlbumArtists;
  // compilation albums list `Various Artists` as the first value
  const isCompilation = initialArtist.toLowerCase().includes('various artist');
  if (isCompilation) {
    // pop `Various Artists` placeholder to give way to album artists
    listAlbumArtists.shift();
  }
  const formattedTrackArtists = listTrackArtists.join(joinArtistDelimiter);
  const trackIsByAlbumArtist = listAlbumArtists.join(joinArtistDelimiter) === formattedTrackArtists;
  // we want to always display when it is a compilation
  Iif (trackIsByAlbumArtist && !isCompilation) {
    return '';
  }
  /* End Compliation Check */
 
  // all else, => yes
  return formattedTrackArtists;
};
 
export { formatTrackArtist, formatTrackTitle };