Size v0.1.1 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
Specs
Link to this section Functions
Specs
Returns the value in bits.
iex> Size.bits(1)
1
iex> Size.bits(2.1)
3
iex> Size.bits(2.8)
3
Specs
Returns the value in bytes.
Examples
iex> Size.bytes(2)
2
iex> Size.bytes(2.1)
3
iex> Size.bytes(2.8)
3
Specs
Returns an input value in exabits to bits.
iex> Size.exabits(1)
1000000000000000000
iex> Size.exabits(2)
2000000000000000000
iex> Size.exabits(2.1)
2100000000000000000
Specs
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
Specs
Returns an input value in gigabits to bits.
iex> Size.gigabits(1)
1000000000
iex> Size.gigabits(2)
2000000000
iex> Size.gigabits(2.1)
2100000000
Specs
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
Specs
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 thesize
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 bytesfalse
- 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}
Specs
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 thesize
parameter is too big to be humanizedInvalidOutputError
- 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 bytesfalse
- 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
Specs
Returns an input value in kilobits to bits.
iex> Size.kilobits(1)
1000
iex> Size.kilobits(2)
2000
iex> Size.kilobits(2.1)
2100
Specs
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
Specs
Returns an input value in megabits to bits.
iex> Size.megabits(1)
1000000
iex> Size.megabits(2)
2000000
iex> Size.megabits(2.1)
2100000
Specs
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
Specs
Returns an input value in petabits to bits.
iex> Size.petabits(1)
1000000000000000
iex> Size.petabits(2)
2000000000000000
iex> Size.petabits(2.1)
2100000000000000
Specs
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
Specs
Returns an input value in terabits to bits.
iex> Size.terabits(1)
1000000000000
iex> Size.terabits(2)
2000000000000
iex> Size.terabits(2.1)
2100000000000
Specs
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
Specs
Returns an input value in yottabits to bits.
iex> Size.yottabits(1)
1000000000000000000000000
iex> Size.yottabits(2)
2000000000000000000000000
iex> Size.yottabits(2.1)
2100000000000000125829120
Specs
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
Specs
Returns an input value in zettabits to bits.
iex> Size.zettabits(1)
1000000000000000000000
iex> Size.zettabits(2)
2000000000000000000000
iex> Size.zettabits(2.1)
2100000000000000000000
Specs
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