{"id":17150177,"url":"https://github.com/jfcalvo/size","last_synced_at":"2025-04-13T11:52:12.256Z","repository":{"id":53930609,"uuid":"96622528","full_name":"jfcalvo/size","owner":"jfcalvo","description":"Size is a Elixir library that helps you working with file sizes.","archived":false,"fork":false,"pushed_at":"2020-07-14T15:38:57.000Z","size":20,"stargazers_count":19,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-10T18:12:23.930Z","etag":null,"topics":["elixir","filesize","size"],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jfcalvo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-08T14:03:20.000Z","updated_at":"2025-01-30T20:36:45.000Z","dependencies_parsed_at":"2022-08-13T04:50:10.655Z","dependency_job_id":null,"html_url":"https://github.com/jfcalvo/size","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jfcalvo%2Fsize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jfcalvo%2Fsize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jfcalvo%2Fsize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jfcalvo%2Fsize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jfcalvo","download_url":"https://codeload.github.com/jfcalvo/size/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248710438,"owners_count":21149188,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","filesize","size"],"created_at":"2024-10-14T21:35:01.043Z","updated_at":"2025-04-13T11:52:12.227Z","avatar_url":"https://github.com/jfcalvo.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/jfcalvo/size.svg?branch=master)](https://travis-ci.org/jfcalvo/size)\n\n# Size\nSize is a Elixir library that helps you working with file sizes.\n\n## Installation\n\nAdd `size` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [{:size, \"~\u003e 0.1.0\"}]\nend\n```\n\nAnd run:\n\n```elixir\n$ mix deps.get\n```\n\n## What can you do with Size?\n\n### Specify sizes\n\n`Size` defines a set of macros to improve specification of sizes.\n\nInstead of define a magic number that nobody understand like:\n\n```elixir\n\u003e disk_space = 12884901888\n```\n\nYou can better do the following:\n\n```elixir\n\u003e import Size, only: :macros\n\u003e disk_space = gigabytes(12)\n```\n\nA lot better, right?.\n\nYou can also work using bits instead of bytes:\n\n```elixir\n\u003e import Size, only: :macros\n\u003e network_speed = megabits(3)\n```\n\nThe list of macros available are:\n\nFor bytes (output always in bytes):\n\n```elixir\nbytes(bytes)\nkilobytes(kilobytes)\nmegabytes(megabytes)\ngigabytes(gigabytes)\nterabytes(terabytes)\npetabytes(petabytes)\nexabytes(exabytes)\nzettabytes(zettabytes)\nyottabytes(yottabytes)\n```\n\nFor bits (output always in bits):\n\n```elixir\nbits(bits)\nkilobits(kilobits)\nmegabits(megabits)\ngigabits(gigabits)\nterabits(terabits)\npetabits(petabits)\nexabits(exabits)\nzettabits(zettabits)\nyottabits(yottabits)\n```\n\n#### Examples\n\n```elixir\n\u003e bytes(2.1) # Input in bytes, output in bytes\n3\n\n\u003e kilobytes(1) # Input in kilobytes, output in bytes\n1024\n\n\u003e megabytes(1) # Input in megabytes, output in bytes\n1048576\n\n\u003e megabytes(2) # Input in megabytes, output in bytes\n2097152\n\n\u003e megabytes(2.1) # Input in megabytes, output in bytes\n2202010\n\n\u003e gigabytes(12) # Input in gigabytes, output in bytes\n12884901888\n\n\u003e bits(2.1) # Input in bits, output in bits\n3\n\n\u003e kilobits(1) # Input in kilobits, output in bits\n1000\n\n\u003e megabits(100) # Input in megabits, output in bits\n100000000\n\n\u003e gigabits(12.5) # Input in gigabits, output in bits\n12500000000\n```\n\n### Humanize file sizes\n\n`Size` provide `humanize(size, options)` and `humanize!(size, options)` functions to generate human-readable file sizes.\n\n```elixir\n\u003e Size.humanize(1024)\n{:ok, \"1 KB\"}\n\n\u003e Size.humanize!(1024)\n\"1 KB\"\n```\n\n`humanize` returns `{:ok, string}`, where `string` is the humanized version of the `size` parameter or  `{:error, reason}` if an error occurs.\n\n`humanize!` returns a string with the humanized version of the `size` parameter or an exception is raised if an error occurs.\n\n`size` input parameter should always be a integer quantity of bytes. Not floats or strings are supported.\n\n#### Options\n\n`humanize` allows a set of options to customize the formatting/output of the given size.\n\n* `:bits` specifies whether the output will use bits instead of bytes (default: `false`).\n\n  ```elixir\n  \u003e Size.humanize(1000, bits: true)\n  {:ok, \"8 Kb\"}\n  ```\n\n* `:round` specifies using an integer the round to be done to the result value (default: `2`).\n\n  ```elixir\n  \u003e Size.humanize(1234322, round: 1)\n  {:ok, \"1.2 MB\"}\n\n  \u003e Size.humanize(1234322, round: 0)\n  {:ok, \"1 MB\"}\n  ```\n\n* `:output` specifies the output type to be used, the possible values are `:tuple`, `:map` and `:string` (default: `:string`).\n\n  ```elixir\n  \u003e Size.humanize(1024, output: :tuple)\n  {:ok, {1, \"KB\"}}\n\n  \u003e Size.humanize(1024, output: :map)\n  {:ok, %{symbol: \"KB\", value: 1}}\n\n  \u003e Size.humanize(1024, output: :string)\n  {:ok, \"1 KB\"}\n\n  \u003e Size.humanize!(1024, output: :tuple)\n  {1, \"KB\"}\n\n  \u003e Size.humanize!(1024, output: :map)\n  %{symbol: \"KB\", value: 1}\n\n  \u003e Size.humanize!(1024, output: :string)\n  \"1 KB\"\n  ```\n\n* `:spacer` specifies using a string the spacer to use between the value and the symbol (default: `\" \"`).\n\n  ```elixir\n  \u003e Size.humanize(1024, spacer: \"~\")\n  {:ok, \"1~KB\"}\n\n  \u003e Size.humanize(1024, spacer: \"\")\n  {:ok, \"1KB\"}\n  ```\n\n* `:symbols` specifies a list of 9 string symbols to be used instead of the default one.\n\n  ```elixir\n  \u003e alternative_symbols = ~w(bytes kilobytes megabytes gigabytes terabytes petabytes exabytes zettabytes yottabytes)\n  \u003e Size.humanize(2048, symbols: alternative_symbols)\n  {:ok, \"2 kilobytes\"}\n  ````\n\n## Author\n* **José Francisco Calvo**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjfcalvo%2Fsize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjfcalvo%2Fsize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjfcalvo%2Fsize/lists"}