Size v0.1.0 Size View Source

Size provides a set of functions to facilitate working with file sizes.

Specifying file sizes

iex> Size.kilobytes(1)
1024

iex> Size.megabytes(2.3)
2411725

iex> Size.gigabytes(1)
1073741824

Humanizing file sizes

iex> Size.humanize(1073741824)
{:ok, "1 GB"}

iex> Size.humanize(1024, spacer: "")
{:ok, "1KB"}

iex> Size.humanize!(1073741824)
"1 GB"

iex> Size.humanize!(500, bits: true)
"4 Kb"

Link to this section Summary

Functions

Returns the value in bits

Returns the value in bytes

Returns an input value in exabits to bits

Returns an input value in exabytes to bytes

Returns an input value in gigabits to bits

Returns an input value in gigabytes to bytes

Returns {:ok, string}, where string is the humanized version of the size parameter or {:error, reason} if an error occurs

Returns a string with the humanized version of the size parameter or an exception is raised if an error occurs

Returns an input value in kilobits to bits

Returns an input value in kilobytes to bytes

Returns an input value in megabits to bits

Returns an input value in megabytes to bytes

Returns an input value in petabits to bits

Returns an input value in petabytes to bytes

Returns an input value in terabits to bits

Returns an input value in terabytes to bytes

Returns an input value in yottabits to bits

Returns an input value in yottabytes to bytes

Returns an input value in zettabits to bits

Returns an input value in zettabytes to bytes

Link to this section Types

Link to this type humanize_output() View Source
humanize_output ::
  {number, String.t} |
  %{value: number, symbol: String.t} |
  String.t

Link to this section Functions

Link to this macro bits(bits) View Source (macro)
bits(term, number) :: integer

Returns the value in bits.

iex> Size.bits(1)
1

iex> Size.bits(2.1)
3

iex> Size.bits(2.8)
3
Link to this macro bytes(bytes) View Source (macro)
bytes(term, number) :: integer

Returns the value in bytes.

Examples

iex> Size.bytes(2)
2

iex> Size.bytes(2.1)
3

iex> Size.bytes(2.8)
3
Link to this macro exabits(exabits) View Source (macro)
exabits(term, number) :: integer

Returns an input value in exabits to bits.

iex> Size.exabits(1)
1000000000000000000

iex> Size.exabits(2)
2000000000000000000

iex> Size.exabits(2.1)
2100000000000000000
Link to this macro exabytes(exabytes) View Source (macro)
exabytes(term, number) :: integer

Returns an input value in exabytes to bytes.

Examples

iex> Size.exabytes(1)
1152921504606846976

iex> Size.exabytes(2)
2305843009213693952

iex> Size.exabytes(2.1)
2421135159674378752
Link to this macro gigabits(gigabits) View Source (macro)
gigabits(term, number) :: integer

Returns an input value in gigabits to bits.

iex> Size.gigabits(1)
1000000000

iex> Size.gigabits(2)
2000000000

iex> Size.gigabits(2.1)
2100000000
Link to this macro gigabytes(gigabytes) View Source (macro)
gigabytes(term, number) :: integer

Returns an input value in gigabytes to bytes.

Examples

iex> Size.gigabytes(1)
1073741824

iex> Size.gigabytes(2)
2147483648

iex> Size.gigabytes(2.3)
2469606196
Link to this function humanize(size, options \\ []) View Source
humanize(integer, keyword) ::
  {:ok, humanize_output} |
  {:error, atom}

Returns {:ok, string}, where string is the humanized version of the size parameter or {:error, reason} if an error occurs.

size must be an integer indicating a number of bytes to be humanized.

Error reasons:

  • :size_too_big - the number specified by the size parameter is too big to be humanized.
  • :invalid_output_option - the value specified on the optional parameter :output is not valid.

Options

The accepted options are:

  • :bits - specifies whether the output will use bits instead of bytes (default: false)
  • :round - specifies using an integer the round to be done to the result value (default: 2)
  • :output - specifies the ouput type to be used (default: :string)
  • :spacer - specifies using a string the spacer to use between the value and the symbol (default: " ")
  • :symbols - specifies a list of 9 string symbols to be used instead of the default one

The values for :bits can be:

  • true - the output will use bits instead of bytes
  • false - the output will use bytes (default)

The values for :output can be:

  • :tuple - the output will include a tuple like {1024, KB}
  • :map - the output will include a map like {value: 1024, symbol: "KB"}
  • :string - the output will include a string like "1024 KB" (default)

Examples

iex> Size.humanize(500)
{:ok, "500 B"}

iex> Size.humanize(1024)
{:ok, "1 KB"}

iex> Size.humanize(1024, spacer: "")
{:ok, "1KB"}

iex> Size.humanize(1024, output: :tuple)
{:ok, {1, "KB"}}

iex> Size.humanize(1024, output: :map)
{:ok, %{value: 1, symbol: "KB"}}

Examples that will return an error

iex> Size.humanize(9999999999999999999999999999)
{:error, :size_too_big}

iex> Size.humanize(500, output: :not_valid_output)
{:error, :invalid_output_option}
Link to this function humanize!(size, options \\ []) View Source
humanize!(integer, keyword) :: humanize_output

Returns a string with the humanized version of the size parameter or an exception is raised if an error occurs.

size must be an integer indicating a number of bytes to be humanized.

Exceptions:

  • SizeTooBigError - the number specified by the size parameter is too big to be humanized
  • InvalidOutputError - the value specified on the optional parameter :output is not valid

Options

The accepted options are:

  • :bits - specifies whether the output will use bits instead of bytes (default: false)
  • :round - specifies using an integer the round to be done to the result value (default: 2)
  • :output - specifies the ouput type to be used (default: :string)
  • :spacer - specifies using a string the spacer to use between the value and the symbol (default: " ")
  • :symbols - specifies a list of 9 string symbols to be used instead of the default one

The values for :bits can be:

  • true - the output will use bits instead of bytes
  • false - the output will use bytes (default)

The values for :output can be:

  • :tuple - the output will include a tuple like {1024, KB}
  • :map - the output will include a map like {value: 1024, symbol: "KB"}
  • :string - the output will include a string like "1024 KB" (default)

Examples

iex> Size.humanize!(500)
"500 B"

iex> Size.humanize!(1024)
"1 KB"

iex> Size.humanize!(1024, spacer: "")
"1KB"

iex> Size.humanize!(1024, output: :tuple)
{1, "KB"}

iex> Size.humanize!(1024, output: :map)
%{value: 1, symbol: "KB"}

Examples that will return an exception

iex> Size.humanize!(9999999999999999999999999999)
** (Size.SizeTooBigError) `9999999999999999999999999999` size argument is out of the limits to be humanized

iex> Size.humanize!(500, output: :not_valid_output)
** (Size.InvalidOutputError) `not_valid_output` is not a valid output type
Link to this macro kilobits(kilobits) View Source (macro)
kilobits(term, number) :: integer

Returns an input value in kilobits to bits.

iex> Size.kilobits(1)
1000

iex> Size.kilobits(2)
2000

iex> Size.kilobits(2.1)
2100
Link to this macro kilobytes(kilobytes) View Source (macro)
kilobytes(term, number) :: integer

Returns an input value in kilobytes to bytes.

Examples

iex> Size.kilobytes(1)
1024

iex> Size.kilobytes(2)
2048

iex> Size.kilobytes(2.3)
2356
Link to this macro megabits(megabits) View Source (macro)
megabits(term, number) :: integer

Returns an input value in megabits to bits.

iex> Size.megabits(1)
1000000

iex> Size.megabits(2)
2000000

iex> Size.megabits(2.1)
2100000
Link to this macro megabytes(megabytes) View Source (macro)
megabytes(term, number) :: integer

Returns an input value in megabytes to bytes.

Examples

iex> Size.megabytes(1)
1048576

iex> Size.megabytes(2)
2097152

iex> Size.megabytes(2.1)
2202010
Link to this macro petabits(petabits) View Source (macro)
petabits(term, number) :: integer

Returns an input value in petabits to bits.

iex> Size.petabits(1)
1000000000000000

iex> Size.petabits(2)
2000000000000000

iex> Size.petabits(2.1)
2100000000000000
Link to this macro petabytes(petabytes) View Source (macro)
petabytes(term, number) :: integer

Returns an input value in petabytes to bytes.

Examples

iex> Size.petabytes(1)
1125899906842624

iex> Size.petabytes(2)
2251799813685248

iex> Size.petabytes(2.1)
2364389804369511
Link to this macro terabits(terabits) View Source (macro)
terabits(term, number) :: integer

Returns an input value in terabits to bits.

iex> Size.terabits(1)
1000000000000

iex> Size.terabits(2)
2000000000000

iex> Size.terabits(2.1)
2100000000000
Link to this macro terabytes(terabytes) View Source (macro)
terabytes(term, number) :: integer

Returns an input value in terabytes to bytes.

Examples

iex> Size.terabytes(1)
1099511627776

iex> Size.terabytes(2)
2199023255552

iex> Size.terabytes(2.1)
2308974418330
Link to this macro yottabits(yottabits) View Source (macro)
yottabits(term, number) :: integer

Returns an input value in yottabits to bits.

iex> Size.yottabits(1)
1000000000000000000000000

iex> Size.yottabits(2)
2000000000000000000000000

iex> Size.yottabits(2.1)
2100000000000000125829120
Link to this macro yottabytes(yottabytes) View Source (macro)
yottabytes(term, number) :: integer

Returns an input value in yottabytes to bytes.

Examples

iex> Size.yottabytes(1)
1208925819614629174706176

iex> Size.yottabytes(2)
2417851639229258349412352

iex> Size.yottabytes(2.1)
2538744221190721374257152
Link to this macro zettabits(zettabits) View Source (macro)
zettabits(term, number) :: integer

Returns an input value in zettabits to bits.

iex> Size.zettabits(1)
1000000000000000000000

iex> Size.zettabits(2)
2000000000000000000000

iex> Size.zettabits(2.1)
2100000000000000000000
Link to this macro zettabytes(zettabytes) View Source (macro)
zettabytes(term, number) :: integer

Returns an input value in zettabytes to bytes.

Examples

iex> Size.zettabytes(1)
1180591620717411303424

iex> Size.zettabytes(2)
2361183241434822606848

iex> Size.zettabytes(2.1)
2479242403506563842048