nukeop/nuclear

View on GitHub
packages/ui/lib/utils.js

Summary

Maintainability
A
1 hr
Test Coverage
import _ from 'lodash';

export function formatDuration(duration) {
  if (!_.isFinite(parseInt(duration)) || duration < 0) {
    return '00:00';
  }

  const secNum = parseInt(duration, 10);
  let hours = Math.floor(secNum / 3600);
  let minutes = Math.floor((secNum - (hours * 3600)) / 60);
  let seconds = secNum - (hours * 3600) - (minutes * 60);

  if (hours < 10) {
    hours = '0' + hours;
  }
  if (minutes < 10) {
    minutes = '0' + minutes;
  }
  if (seconds < 10) {
    seconds = '0' + seconds;
  }

  if (hours === '00') {
    return minutes + ':' + seconds;
  } else {
    return hours + ':' + minutes + ':' + seconds;
  }
}

export const getThumbnail = album => {
  return _.get(album, 'coverImage',
    _.get(album, 'thumb'));
};

export const getTrackItem = track => ({
  artist: track.artist.name,
  name: track.name,
  thumbnail: getThumbnail(track),
  local: track.local,
  streams: track.streams
});