Skip to content

Instantly share code, notes, and snippets.

@joshvermaire
Created October 10, 2012 21:09
Show Gist options
  • Save joshvermaire/3868425 to your computer and use it in GitHub Desktop.
Save joshvermaire/3868425 to your computer and use it in GitHub Desktop.
Conversion of Photos and Videos to Moments
# subscription being the subscription DTO returned from the API
numPhotos = subscription.num_photos
secVideos = subscription.sec_videos
numVideos = secVideos / 60000 * subscription.plan.photos_per_min_video
total = subscription.total_storage
# Change to a string with commas
photosComma = numberWithCommas(numPhotos)
# Ensure numVideos and numPhotos are integers
allMoments = parseInt(numVideos, 10) + parseInt(numPhotos, 10)
# Make the time prettified
time = prettyTime(secVideos)
# Take larger of total_storage or the total number of photos + the converted number of videos
denom = Math.max(allMoments, total)
# Get percentages (i'm always rounding up as I can only use integers in my graphical bar)
photoPercentage = Math.ceil(numPhotos / denom * 100)
videoPercentage = Math.ceil(numVideos / denom * 100)
# Since I'm rounding up on both, make sure the total percentage isn't over 100%
if (photoPercentage + videoPercentage) > 100
if photoPercentage > videoPercentage
photoPercentage -= 1
else
videoPercentage -= 1
# Text to be used:
#
# "#{ time } Video"
# "#{ photosComma } Photos"
# HELPER FUNCTIONS #
numberWithCommas = (number) ->
# Make sure the number is a string
string = number.toString()
# Only add a comma if >= 10k
if string.length > 4
string.replace(/\B(?=(\d{3})+(?!\d))/g, ",")
else
string
prettyTime = (ms) ->
seconds = Math.round ms / 1000
minutes = (tempMinutes = Math.round(seconds / 60)) > 0 and tempMinutes
hours = (tempHours = Math.round(minutes / 60)) > 0 and tempHours
text = if hours
if hours > 100
days = Math.round(hours / 24)
"#{ days } Days"
else if hours > 1
"#{ hours }hrs"
else
"#{ hours }hr"
else if minutes
if minutes > 1
"#{ minutes }mins"
else
"#{ minutes }min"
else if seconds
"#{ seconds }sec"
else
"No"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment