tlux / file_size

A file size calculator, parser and formatter written in Elixir
MIT License
6 stars 1 forks source link

Add optional spacer for parser #5

Open sitch opened 4 years ago

sitch commented 4 years ago

Was parsing output from dstat and you see values like:

504k   11k
1189B 2733B
59k   14k
14M 8959B 

Which fails parsing due to the mandatory single spacer " " in the regex between the value and unit

i.e.:

iex> FileSize.parse("1GB") 
{:error, %FileSize.ParseError{reason: :format, value: "1GB"}}

whereas if the space is there it works

iex> FileSize.parse("1 GB") 
{:ok, #FileSize<"1 GB">}
tlux commented 4 years ago

See the discussion in #7. I suggest to introduce FileSize.parse/2 and allow these options to be passed:

:symbols - map of symbols, as for FileSize.format/2 :ignore_whitespace - boolean to toggle whether the parser should ignore whitespace characters between value and unit