Move human_bytes to utilities

This commit is contained in:
Mikko Ahlroth 2013-08-05 21:02:52 +03:00
parent b8ac3dcb7b
commit 5f6f40e299
2 changed files with 24 additions and 20 deletions

View file

@ -243,26 +243,6 @@ defmodule Nulform.Plugins.URLAnalyzer do
:httpc.request mode, {url, @headers}, @http_options, @base_options :httpc.request mode, {url, @headers}, @http_options, @base_options
end end
def human_bytes(size) do
human_bytes size, 1000
end
def human_bytes(size, factor) do
human_bytes size, factor, 2
end
def human_bytes(size, factor, decimals) do
human_bytes size, factor, decimals, ["B", "kB", "MB", "GB", "TP", "PB"]
end
def human_bytes(size, factor, decimals, [ext | rest]) when size >= factor do
human_bytes size / factor, factor, decimals, rest
end
def human_bytes(size, factor, decimals, [ext | rest]) do
float_to_binary(:erlang.float(size), [decimals: decimals]) <> " " <> ext
end
defp parse_content_type(header) do defp parse_content_type(header) do
Enum.at String.split(to_binary(header), ";"), 0 Enum.at String.split(to_binary(header), ";"), 0
end end

View file

@ -29,4 +29,28 @@ defmodule Nulform.Utilities do
def latin1_to_utf8(string) do def latin1_to_utf8(string) do
:unicode.characters_to_binary string, :latin1, :utf8 :unicode.characters_to_binary string, :latin1, :utf8
end end
@doc """
Convert a number of bytes into a human readable binary.
"""
def human_bytes(size) do
human_bytes size, 1000
end
def human_bytes(size, factor) do
human_bytes size, factor, 2
end
def human_bytes(size, factor, decimals) do
human_bytes size, factor, decimals, ["B", "kB", "MB", "GB", "TP", "PB"]
end
def human_bytes(size, factor, decimals, [ext | rest]) when size >= factor do
human_bytes size / factor, factor, decimals, rest
end
def human_bytes(size, factor, decimals, [ext | rest]) do
float_to_binary(:erlang.float(size), [decimals: decimals]) <> " " <> ext
end
end end