Skip to content

Commit

Permalink
Merge pull request #367 from blackcandy-org/format-library-count-number
Browse files Browse the repository at this point in the history
Format big count number of library to human readable
  • Loading branch information
aidewoode committed Apr 18, 2024
2 parents 0364bf2 + 618fe71 commit 5fe380e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Expand Up @@ -72,6 +72,10 @@ def format_duration(sec)
(sec > 1.hour) ? time.strftime("%T") : time.strftime("%M:%S")
end

def format_number(number)
number_to_human(number, units: {thousand: "K", million: "M", billion: "B"}, precision: 1, significant: false)
end

def is_active?(controller: "", path: "")
params[:controller].in?(Array(controller)) || (path.is_a?(Regexp) ? (path =~ request.path) : (path == request.path))
end
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/controllers/player_controller.js
Expand Up @@ -146,7 +146,7 @@ export default class extends Controller {
window.requestAnimationFrame(this.#setProgress.bind(this))
this.timerInterval = setInterval(this.#setTimer.bind(this), 1000)

// let playlist can show current palying song
// let playlist can show current playing song
dispatchEvent(document, 'playlistSongs:showPlaying')
}

Expand Down
8 changes: 4 additions & 4 deletions app/views/libraries/show.html.erb
Expand Up @@ -8,7 +8,7 @@
<%= icon_tag "album", class: "u-mr-tiny" %>
<%= t("label.albums") %>
</span>
<span class='c-badge'><%= @albums_count %></span>
<span class='c-badge'><%= format_number(@albums_count) %></span>
<% end %>
</li>
<li class='c-list__item'>
Expand All @@ -17,7 +17,7 @@
<%= icon_tag "mic", class: "u-mr-tiny" %>
<%= t("label.artists") %>
</span>
<span class='c-badge'><%= @artists_count %></span>
<span class='c-badge'><%= format_number(@artists_count) %></span>
<% end %>
</li>
<li class='c-list__item'>
Expand All @@ -26,7 +26,7 @@
<%= icon_tag "queue-music", class: "u-mr-tiny" %>
<%= t("label.playlists") %>
</span>
<span class='c-badge'><%= @playlists_count %></span>
<span class='c-badge'><%= format_number(@playlists_count) %></span>
<% end %>
</li>
<li class='c-list__item'>
Expand All @@ -35,7 +35,7 @@
<%= icon_tag "music-note", class: "u-mr-tiny" %>
<%= t("label.songs") %>
</span>
<span class='c-badge'><%= @songs_count %></span>
<span class='c-badge'><%= format_number(@songs_count) %></span>
<% end %>
</li>
</ul>
Expand Down
9 changes: 8 additions & 1 deletion test/helpers/application_helper_test.rb
Expand Up @@ -3,7 +3,7 @@
require "test_helper"

class ApplicationHelperTest < ActionView::TestCase
test "format_duration" do
test "format duration" do
assert_equal "00:09", format_duration(9)
assert_equal "01:30", format_duration(90)
assert_equal "15:00", format_duration(900)
Expand All @@ -17,4 +17,11 @@ class ApplicationHelperTest < ActionView::TestCase
assert_includes empty_alert_tag(has_icon: true), "c-icon"
assert_not_includes empty_alert_tag(has_overlay: false), "c-overlay"
end

test "format number" do
assert_equal "135", format_number(135)
assert_equal "1.4 K", format_number(1350)
assert_equal "1.4 M", format_number(1350000)
assert_equal "1.4 B", format_number(1350000000)
end
end

0 comments on commit 5fe380e

Please sign in to comment.