module JosieHealth module Utils module Formatting def self.format_float(value : Float64) : String value == value.to_i ? value.to_i.to_s : value.round(1).to_s end def self.format_dosage(dosage : String) : String dosage.gsub(/(\d+)\.0([a-z]|$|\s)/i) { |_, m| "#{m[1]}#{m[2]}" } end def self.time_ago(time : Time, from : Time = Time.utc) : String diff = from - time days = diff.days hours = (diff.total_hours % 24).to_i minutes = (diff.total_minutes % 60).to_i if days > 0 if hours > 0 "#{days}d#{hours}h ago" else "#{days}d ago" end elsif hours > 0 if minutes > 0 "#{hours}h#{minutes}m ago" else "#{hours}h ago" end else "#{minutes}m ago" end end def self.ordinalize(n : Int32) : String if (11..13).includes?(n % 100) "#{n}th" else case n % 10 when 1 then "#{n}st" when 2 then "#{n}nd" when 3 then "#{n}rd" else "#{n}th" end end end end end end struct Int32 def ordinalize : String JosieHealth::Utils::Formatting.ordinalize(self) end end