Skip to content

Commit c1fa6de

Browse files
committed
Extract NPM.FormatUtil to deduplicate format_size/format_bytes
1 parent c99f30d commit c1fa6de

8 files changed

Lines changed: 23 additions & 26 deletions

File tree

lib/mix/tasks/npm.cache.ex

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule Mix.Tasks.Npm.Cache do
1818
if File.exists?(cache_dir) do
1919
{size, count} = dir_stats(cache_dir)
2020
Mix.shell().info("Cache: #{cache_dir}")
21-
Mix.shell().info("Size: #{format_bytes(size)}")
21+
Mix.shell().info("Size: #{NPM.FormatUtil.format_size(size)}")
2222
Mix.shell().info("Packages: #{count}")
2323
else
2424
Mix.shell().info("Cache: #{cache_dir} (empty)")
@@ -32,7 +32,7 @@ defmodule Mix.Tasks.Npm.Cache do
3232
if File.exists?(cache_dir) do
3333
{size, count} = dir_stats(cache_dir)
3434
File.rm_rf!(cache_dir)
35-
Mix.shell().info("Removed #{count} packages (#{format_bytes(size)})")
35+
Mix.shell().info("Removed #{count} packages (#{NPM.FormatUtil.format_size(size)})")
3636
else
3737
Mix.shell().info("Cache is already empty.")
3838
end
@@ -53,8 +53,4 @@ defmodule Mix.Tasks.Npm.Cache do
5353
end
5454
end)
5555
end
56-
57-
defp format_bytes(bytes) when bytes < 1024, do: "#{bytes} B"
58-
defp format_bytes(bytes) when bytes < 1_048_576, do: "#{Float.round(bytes / 1024, 1)} KB"
59-
defp format_bytes(bytes), do: "#{Float.round(bytes / 1_048_576, 1)} MB"
6056
end

lib/mix/tasks/npm.size.ex

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule Mix.Tasks.Npm.Size do
2626
total_files = NPM.NodeModules.file_count(dir)
2727

2828
Mix.shell().info("node_modules analysis:")
29-
Mix.shell().info(" Total size: #{format_bytes(total_size)}")
29+
Mix.shell().info(" Total size: #{NPM.FormatUtil.format_size(total_size)}")
3030
Mix.shell().info(" Total files: #{total_files}")
3131

3232
packages = NPM.NodeModules.installed(dir)
@@ -48,11 +48,7 @@ defmodule Mix.Tasks.Npm.Size do
4848
|> Enum.sort_by(fn {_, size} -> -size end)
4949
|> Enum.take(top_n)
5050
|> Enum.each(fn {name, size} ->
51-
Mix.shell().info(" #{format_bytes(size)} #{name}")
51+
Mix.shell().info(" #{NPM.FormatUtil.format_size(size)} #{name}")
5252
end)
5353
end
54-
55-
defp format_bytes(bytes) when bytes < 1024, do: "#{bytes} B"
56-
defp format_bytes(bytes) when bytes < 1_048_576, do: "#{Float.round(bytes / 1024, 1)} KB"
57-
defp format_bytes(bytes), do: "#{Float.round(bytes / 1_048_576, 1)} MB"
5854
end

lib/npm/cache_stats.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ defmodule NPM.CacheStats do
7373
Formats a byte size in human-readable form.
7474
"""
7575
@spec format_size(non_neg_integer()) :: String.t()
76-
def format_size(bytes) when bytes < 1024, do: "#{bytes} B"
77-
def format_size(bytes) when bytes < 1_048_576, do: "#{Float.round(bytes / 1024, 1)} KB"
78-
def format_size(bytes), do: "#{Float.round(bytes / 1_048_576, 1)} MB"
76+
defdelegate format_size(bytes), to: NPM.FormatUtil
7977

8078
defp walk_size(path) do
8179
case File.stat(path) do

lib/npm/file_size.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ defmodule NPM.FileSize do
5555
Formats a byte size to human-readable string.
5656
"""
5757
@spec format_size(non_neg_integer()) :: String.t()
58-
def format_size(bytes) when bytes < 1024, do: "#{bytes} B"
59-
def format_size(bytes) when bytes < 1_048_576, do: "#{Float.round(bytes / 1024, 1)} KB"
60-
def format_size(bytes), do: "#{Float.round(bytes / 1_048_576, 1)} MB"
58+
defdelegate format_size(bytes), to: NPM.FormatUtil
6159

6260
defp list_files(dir) do
6361
if File.dir?(dir) do

lib/npm/format_util.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule NPM.FormatUtil do
2+
@moduledoc """
3+
Shared formatting utilities.
4+
"""
5+
6+
@doc """
7+
Formats a byte count as a human-readable string.
8+
"""
9+
@spec format_size(non_neg_integer()) :: String.t()
10+
def format_size(bytes) when bytes < 1024, do: "#{bytes} B"
11+
def format_size(bytes) when bytes < 1_048_576, do: "#{Float.round(bytes / 1024, 1)} KB"
12+
def format_size(bytes) when bytes < 1_073_741_824, do: "#{Float.round(bytes / 1_048_576, 1)} MB"
13+
def format_size(bytes), do: "#{Float.round(bytes / 1_073_741_824, 1)} GB"
14+
end

lib/npm/lockfile_stats.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ defmodule NPM.LockfileStats do
5252
Formats size in human-readable form.
5353
"""
5454
@spec format_size(non_neg_integer()) :: String.t()
55-
def format_size(bytes) when bytes < 1024, do: "#{bytes} B"
56-
def format_size(bytes) when bytes < 1_048_576, do: "#{Float.round(bytes / 1024, 1)} KB"
57-
def format_size(bytes), do: "#{Float.round(bytes / 1_048_576, 1)} MB"
55+
defdelegate format_size(bytes), to: NPM.FormatUtil
5856

5957
defp has_field?(entry, field) do
6058
case Map.get(entry, field) || Map.get(entry, to_string(field)) do

lib/npm/size.ex

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ defmodule NPM.Size do
5353
Formats a size in bytes as a human-readable string.
5454
"""
5555
@spec format_size(non_neg_integer()) :: String.t()
56-
def format_size(bytes) when bytes < 1024, do: "#{bytes} B"
57-
def format_size(bytes) when bytes < 1_048_576, do: "#{Float.round(bytes / 1024, 1)} KB"
58-
def format_size(bytes) when bytes < 1_073_741_824, do: "#{Float.round(bytes / 1_048_576, 1)} MB"
59-
def format_size(bytes), do: "#{Float.round(bytes / 1_073_741_824, 1)} GB"
56+
defdelegate format_size(bytes), to: NPM.FormatUtil
6057

6158
@doc """
6259
Returns a summary of the node_modules analysis.

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"dialyxir": {:hex, :dialyxir, "1.4.7", "dda948fcee52962e4b6c5b4b16b2d8fa7d50d8645bbae8b8685c3f9ecb7f5f4d", [:mix], [{:erlex, ">= 0.2.8", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "b34527202e6eb8cee198efec110996c25c5898f43a4094df157f8d28f27d9efe"},
55
"earmark_parser": {:hex, :earmark_parser, "1.4.44", "f20830dd6b5c77afe2b063777ddbbff09f9759396500cdbe7523efd58d7a339c", [:mix], [], "hexpm", "4778ac752b4701a5599215f7030989c989ffdc4f6df457c5f36938cc2d2a2750"},
66
"erlex": {:hex, :erlex, "0.2.8", "cd8116f20f3c0afe376d1e8d1f0ae2452337729f68be016ea544a72f767d9c12", [:mix], [], "hexpm", "9d66ff9fedf69e49dc3fd12831e12a8a37b76f8651dd21cd45fcf5561a8a7590"},
7-
"ex_dna": {:hex, :ex_dna, "1.1.0", "3ced06be2d1648074e79617a4f1592dbe929b08a0357d08ad79b3d0025966147", [:mix], [{:gen_lsp, "~> 0.11", [hex: :gen_lsp, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "0274e36c69bee3bb990097c8138a878c7ac416a8a234730a44e042f90b5eefe0"},
7+
"ex_dna": {:hex, :ex_dna, "1.2.0", "cf01b0f06b49e6ba3deb96a235110c1a0d535c9ae205992582c9edc22ff907ec", [:mix], [{:gen_lsp, "~> 0.11", [hex: :gen_lsp, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "6358955a47504fe8bfec898f093fd6727147f7b47bed49045eefacc68321855b"},
88
"ex_doc": {:hex, :ex_doc, "0.40.1", "67542e4b6dde74811cfd580e2c0149b78010fd13001fda7cfeb2b2c2ffb1344d", [:mix], [{:earmark_parser, "~> 1.4.44", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "bcef0e2d360d93ac19f01a85d58f91752d930c0a30e2681145feea6bd3516e00"},
99
"ex_slop": {:hex, :ex_slop, "0.2.0", "28ee70d62975636242dabf47d24e247acfbfdffd9c7cdc5a0d1c396aa42b421d", [:mix], [{:credo, "~> 1.7", [hex: :credo, repo: "hexpm", optional: false]}], "hexpm", "da8ccad55b61eebf7972fee1ab723f5227e58ed052ea09be4f9fe315c5e89cc2"},
1010
"file_system": {:hex, :file_system, "1.1.1", "31864f4685b0148f25bd3fbef2b1228457c0c89024ad67f7a81a3ffbc0bbad3a", [:mix], [], "hexpm", "7a15ff97dfe526aeefb090a7a9d3d03aa907e100e262a0f8f7746b78f8f87a5d"},

0 commit comments

Comments
 (0)