All files / theatres/with-youtube-spotify/utils youtube-spotify-parser.js

61.54% Statements 8/13
25% Branches 2/8
100% Functions 2/2
61.54% Lines 8/13

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                            1x 98x                 98x     98x 248x 248x             248x     98x        
import { merge } from 'lodash';
 
/**
 * Gather Youtube & Spotify Info
 * Takes a list of of external identifiers,
 * If it has a Youtube or Spotify ID, then create direct links to the item
 *
 * @param { array } externalIdentifiers
 * @return { object } = {
 *  youtube: { urlPrefix, id },
 *  and/or
 *  spotify: { urlPrefix, id }
 * }
 */
const gatherYoutubeAndSpotifyInfo = (externalIdentifiers = []) => {
  const externalSourceParams = {
    spotify: {
      urlPrefix: 'https://embed.spotify.com/?uri=',
    },
    youtube: {
      urlPrefix: 'https://www.youtube.com/embed/',
      urlExtensions: `?fs=1&rel=0&autoplay=1&origin=${window.location.origin}` // todo: add origin='https://www.archive.org'
    }
  };
  const externalIDs = !Array.isArray(externalIdentifiers)
    ? [externalIdentifiers]
    : externalIdentifiers;
  const youtubeAndSpotifyIDs = externalIDs.reduce((allIDs = {}, id) => {
    const isYoutubeOrSpotify = id.match(/(youtube|spotify)/g);
    Iif (isYoutubeOrSpotify) {
      const externalSource = isYoutubeOrSpotify[0];
      const neededParams = externalSourceParams[externalSource];
      const idSeparator = (externalSource === 'youtube') ? id.lastIndexOf(':') + 1 : id.indexOf(':') + 1;
      const sourceId = id.slice(idSeparator);
      allIDs[isYoutubeOrSpotify[0]] = merge({}, neededParams, { id: sourceId });
    }
    return allIDs;
  }, {});
 
  return youtubeAndSpotifyIDs;
};
 
export default gatherYoutubeAndSpotifyInfo;