Compare commits
8 Commits
72bef1ea47
...
v0.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 1a0d5c8ca7 | |||
| 8e41fdc40a | |||
| bc5f194a50 | |||
| a35f385574 | |||
| 21faadcfc7 | |||
| d744ebf9f2 | |||
| 13897bf625 | |||
| 13767d8c0c |
@@ -1,9 +1,7 @@
|
|||||||
name: CI
|
name: Docs
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ "master" ]
|
branches: [ "master" ]
|
||||||
pull_request:
|
|
||||||
branches: [ "master" ]
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
@@ -23,7 +21,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
v doc -f html -m .
|
v doc -f html -m .
|
||||||
pushd _docs
|
pushd _docs
|
||||||
ln -vs dataunits.html index.html
|
ln -vs ${{ github.event.repository.name }}.html index.html
|
||||||
ls -alFh
|
ls -alFh
|
||||||
popd
|
popd
|
||||||
|
|
||||||
24
.github/workflows/test.yaml
vendored
Normal file
24
.github/workflows/test.yaml
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
name: Tests
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ "master" ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ "master" ]
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup V
|
||||||
|
run: |
|
||||||
|
wget -qO /tmp/v.zip https://github.com/vlang/v/releases/latest/download/v_linux.zip
|
||||||
|
unzip -q /tmp/v.zip -d /tmp
|
||||||
|
echo /tmp/v >> "$GITHUB_PATH"
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: |
|
||||||
|
v -stats test .
|
||||||
23
Makefile
23
Makefile
@@ -1,23 +0,0 @@
|
|||||||
SRC_DIR ?= src
|
|
||||||
DOC_DIR ?= doc
|
|
||||||
TESTS_DIR ?= .
|
|
||||||
|
|
||||||
all: test
|
|
||||||
|
|
||||||
test:
|
|
||||||
v test $(TESTS_DIR)
|
|
||||||
|
|
||||||
doc:
|
|
||||||
v doc -f html -m ./$(SRC_DIR) -o $(DOC_DIR)
|
|
||||||
|
|
||||||
serve: clean doc
|
|
||||||
v -e "import net.http.file; file.serve(folder: '$(DOC_DIR)')"
|
|
||||||
|
|
||||||
build:
|
|
||||||
v -path "$$(realpath $$PWD/../)|@vlib|@vmodules" \
|
|
||||||
-prod -parallel-cc -cflags -static -cflags -s -d no_segfault_handler \
|
|
||||||
cmd/dataunit -o dataunit
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -r $(DOC_DIR) || true
|
|
||||||
rm dataunit || true
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
## Data size units converter
|
# Data Unit Converter
|
||||||
|
|
||||||
Installation:
|
Installation:
|
||||||
|
|
||||||
```
|
```
|
||||||
v install --git https://github.com/gechandesu/dataunits
|
v install https://github.com/gechandesu/dataunits
|
||||||
```
|
```
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|||||||
@@ -23,17 +23,12 @@
|
|||||||
|
|
||||||
// For more information, please refer to <https://unlicense.org/>
|
// For more information, please refer to <https://unlicense.org/>
|
||||||
|
|
||||||
/*
|
|
||||||
dataunit - a simple CLI tool for data units convertion.
|
|
||||||
*/
|
|
||||||
|
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import flag
|
import flag
|
||||||
import dataunits
|
import dataunits
|
||||||
|
|
||||||
@[name: 'dataunit']
|
|
||||||
struct FlagConfig {
|
struct FlagConfig {
|
||||||
help bool
|
help bool
|
||||||
from string @[short: f]
|
from string @[short: f]
|
||||||
@@ -55,7 +50,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
if flags.help {
|
if flags.help {
|
||||||
println('convert the value between data size units.')
|
println('convert the value between data size units.')
|
||||||
println('usage: dataunit -f <unit> -t <unit> <value>')
|
println('usage: datasize -f <unit> -t <unit> <value>')
|
||||||
println('options:')
|
println('options:')
|
||||||
println(' -help print this help message and exit')
|
println(' -help print this help message and exit')
|
||||||
println(' -f, -from source data unit')
|
println(' -f, -from source data unit')
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
// This is free and unencumbered software released into the public domain.
|
// This is free and unencumbered software released into the public domain.
|
||||||
|
//
|
||||||
// Anyone is free to copy, modify, publish, use, compile, sell, or
|
// Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||||
// distribute this software, either in source code form or as a compiled
|
// distribute this software, either in source code form or as a compiled
|
||||||
// binary, for any purpose, commercial or non-commercial, and by any
|
// binary, for any purpose, commercial or non-commercial, and by any
|
||||||
// means.
|
// means.
|
||||||
|
//
|
||||||
// In jurisdictions that recognize copyright laws, the author or authors
|
// In jurisdictions that recognize copyright laws, the author or authors
|
||||||
// of this software dedicate any and all copyright interest in the
|
// of this software dedicate any and all copyright interest in the
|
||||||
// software to the public domain. We make this dedication for the benefit
|
// software to the public domain. We make this dedication for the benefit
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
// successors. We intend this dedication to be an overt act of
|
// successors. We intend this dedication to be an overt act of
|
||||||
// relinquishment in perpetuity of all present and future rights to this
|
// relinquishment in perpetuity of all present and future rights to this
|
||||||
// software under copyright law.
|
// software under copyright law.
|
||||||
|
//
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
@@ -20,13 +20,11 @@
|
|||||||
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
// OTHER DEALINGS IN THE SOFTWARE.
|
// OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
//
|
||||||
// For more information, please refer to <https://unlicense.org/>
|
// For more information, please refer to <https://unlicense.org/>
|
||||||
|
|
||||||
module dataunits
|
module dataunits
|
||||||
|
|
||||||
import maps
|
|
||||||
|
|
||||||
pub type DataSize = f64
|
pub type DataSize = f64
|
||||||
|
|
||||||
pub fn (d DataSize) bit() f64 {
|
pub fn (d DataSize) bit() f64 {
|
||||||
@@ -209,7 +207,7 @@ pub const eibit = pibit * 1024
|
|||||||
pub const zibit = eibit * 1024
|
pub const zibit = eibit * 1024
|
||||||
pub const yibit = zibit * 1024
|
pub const yibit = zibit * 1024
|
||||||
|
|
||||||
const units = {
|
const str_unit = {
|
||||||
'bit': bit
|
'bit': bit
|
||||||
'nibble': nibble
|
'nibble': nibble
|
||||||
'byte': byte
|
'byte': byte
|
||||||
@@ -247,8 +245,46 @@ const units = {
|
|||||||
'Yibit': yibit
|
'Yibit': yibit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const unit_str = {
|
||||||
|
bit: 'bit'
|
||||||
|
nibble: 'nibble'
|
||||||
|
byte: 'byte'
|
||||||
|
kb: 'kB'
|
||||||
|
mb: 'MB'
|
||||||
|
gb: 'GB'
|
||||||
|
tb: 'TB'
|
||||||
|
pb: 'PB'
|
||||||
|
eb: 'EB'
|
||||||
|
zb: 'ZB'
|
||||||
|
yb: 'YB'
|
||||||
|
kib: 'KiB'
|
||||||
|
mib: 'MiB'
|
||||||
|
gib: 'GiB'
|
||||||
|
tib: 'TiB'
|
||||||
|
pib: 'PiB'
|
||||||
|
eib: 'EiB'
|
||||||
|
zib: 'ZiB'
|
||||||
|
yib: 'YiB'
|
||||||
|
kbit: 'kbit'
|
||||||
|
mbit: 'Mbit'
|
||||||
|
gbit: 'Gbit'
|
||||||
|
tbit: 'Tbit'
|
||||||
|
pbit: 'Pbit'
|
||||||
|
ebit: 'Ebit'
|
||||||
|
zbit: 'Zbit'
|
||||||
|
ybit: 'Ybit'
|
||||||
|
kibit: 'Kibit'
|
||||||
|
mibit: 'Mibit'
|
||||||
|
gibit: 'Gibit'
|
||||||
|
tibit: 'Tibit'
|
||||||
|
pibit: 'Pibit'
|
||||||
|
eibit: 'Eibit'
|
||||||
|
zibit: 'Zibit'
|
||||||
|
yibit: 'Yibit'
|
||||||
|
}
|
||||||
|
|
||||||
pub const prefixes = {
|
pub const prefixes = {
|
||||||
// Metric size (1000^N)
|
// Metric size (N * 1000)
|
||||||
'kilo': 'k'
|
'kilo': 'k'
|
||||||
'mega': 'M'
|
'mega': 'M'
|
||||||
'giga': 'G'
|
'giga': 'G'
|
||||||
@@ -257,7 +293,7 @@ pub const prefixes = {
|
|||||||
'exa': 'E'
|
'exa': 'E'
|
||||||
'zetta': 'Z'
|
'zetta': 'Z'
|
||||||
'yotta': 'Y'
|
'yotta': 'Y'
|
||||||
// Binary size (1024^N)
|
// Binary size (N * 1024)
|
||||||
'kibi': 'Ki'
|
'kibi': 'Ki'
|
||||||
'mebi': 'Mi'
|
'mebi': 'Mi'
|
||||||
'gibi': 'Gi'
|
'gibi': 'Gi'
|
||||||
@@ -268,9 +304,19 @@ pub const prefixes = {
|
|||||||
'yobi': 'Yi'
|
'yobi': 'Yi'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// vfmt off
|
||||||
|
const prefixes_array = [
|
||||||
|
'byte', 'B',
|
||||||
|
'kilo', 'k', 'mega', 'M', 'giga', 'G', 'tera', 'T',
|
||||||
|
'peta', 'P', 'exa', 'E', 'zetta', 'Z', 'yotta', 'Y',
|
||||||
|
'kibi', 'Ki', 'mebi', 'Mi', 'gibi', 'Gi', 'tebi', 'Ti',
|
||||||
|
'pebi', 'Pi', 'exbi', 'Ei', 'zebi', 'Zi', 'yobi', 'Yi'
|
||||||
|
]
|
||||||
|
// vfmt on
|
||||||
|
|
||||||
// convert returns the value converted between the *from* and *to* units.
|
// convert returns the value converted between the *from* and *to* units.
|
||||||
// Example:
|
// Example:
|
||||||
// ```
|
// ```v
|
||||||
// assert dataunits.convert(500, dataunits.mbit, dataunits.kb) == 62500
|
// assert dataunits.convert(500, dataunits.mbit, dataunits.kb) == 62500
|
||||||
// ````
|
// ````
|
||||||
pub fn convert(value f64, from DataSize, to DataSize) f64 {
|
pub fn convert(value f64, from DataSize, to DataSize) f64 {
|
||||||
@@ -281,10 +327,10 @@ pub fn convert(value f64, from DataSize, to DataSize) f64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// from_string parses input and returns the actual DataSize.
|
// from_string parses input and returns the actual DataSize.
|
||||||
// Note: Case insensitivity makes unit abbreviations such as `Mb` (megabit) and `MB` (megabyte)
|
// Note: Case insensitivity makes unit abbreviations such as 'Mb' (megabit) and 'MB' (megabyte)
|
||||||
// ambiguous. Use `bit` suffix for values in bits. The `b` suffix will be accepted as byte unit.
|
// ambiguous. Use 'bit' suffix for values in bits. The 'b' suffix will be accepted as byte unit.
|
||||||
// Example:
|
// Example:
|
||||||
// ```
|
// ```v
|
||||||
// assert dataunits.from_string('GiB')! == dataunits.gib
|
// assert dataunits.from_string('GiB')! == dataunits.gib
|
||||||
// assert dataunits.from_string('M')! == dataunits.mb
|
// assert dataunits.from_string('M')! == dataunits.mb
|
||||||
// assert dataunits.from_string('M', in_bits: true)! == dataunits.mbit
|
// assert dataunits.from_string('M', in_bits: true)! == dataunits.mbit
|
||||||
@@ -292,20 +338,20 @@ pub fn convert(value f64, from DataSize, to DataSize) f64 {
|
|||||||
// ```
|
// ```
|
||||||
pub fn from_string(input string, params ParseParams) !DataSize {
|
pub fn from_string(input string, params ParseParams) !DataSize {
|
||||||
if !input.is_pure_ascii() {
|
if !input.is_pure_ascii() {
|
||||||
return error('${input} non-ASCII characters is not allowed in data size unit')
|
return error('${input} non-ASCII characters is not allowed in data unit')
|
||||||
}
|
}
|
||||||
unit := parse_unit_str(input, params)
|
unit := parse_str(input, params)
|
||||||
if params.case_insensitive {
|
if params.case_insensitive {
|
||||||
for key, value in units {
|
for key, value in str_unit {
|
||||||
if key.to_lower_ascii() == unit.to_lower_ascii() {
|
if key.to_lower_ascii() == unit.to_lower_ascii() {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return units[unit] or { error('${input} is not a valid data size unit') }
|
return str_unit[unit] or { error('${input} is not a valid data unit') }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_unit_str(input string, params ParseParams) string {
|
fn parse_str(input string, params ParseParams) string {
|
||||||
mut unit := ''
|
mut unit := ''
|
||||||
match true {
|
match true {
|
||||||
input.to_lower_ascii() in ['byte', 'bytes'] {
|
input.to_lower_ascii() in ['byte', 'bytes'] {
|
||||||
@@ -352,10 +398,7 @@ fn parse_unit_str(input string, params ParseParams) string {
|
|||||||
// prevent Gibit --> Git transform
|
// prevent Gibit --> Git transform
|
||||||
return unit
|
return unit
|
||||||
}
|
}
|
||||||
// transform full names to short ones: megabits --> mbit, etc.
|
unit = unit.all_before('s').replace_each(prefixes_array)
|
||||||
prefixes_array := maps.flat_map[string, string, string](prefixes, |k, v| [k, v])
|
|
||||||
unit = unit.replace_each(prefixes_array)
|
|
||||||
unit = unit.replace_each(['bytes', 'B', 'byte', 'B']).replace_once('bits', 'bit')
|
|
||||||
return unit
|
return unit
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -370,13 +413,7 @@ pub:
|
|||||||
binary_size bool
|
binary_size bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// to_string returns a string representation of data size unit in short form
|
// to_string returns a string representation of data unit in short form e.g. kB, Mbit, GiB, etc.
|
||||||
// e.g. kB, Mbit, GiB, etc.
|
|
||||||
pub fn to_string(input DataSize) !string {
|
pub fn to_string(input DataSize) !string {
|
||||||
for key, value in units {
|
return unit_str[input] or { error('invalid input data unit') }
|
||||||
if value == input {
|
|
||||||
return key
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return error('invalid input data size unit')
|
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ fn test_convert() {
|
|||||||
assert (dataunits.bit * 8).bit() == 8
|
assert (dataunits.bit * 8).bit() == 8
|
||||||
assert (dataunits.bit * 8).byte() == 1
|
assert (dataunits.bit * 8).byte() == 1
|
||||||
assert (dataunits.gib * 10).mib() == 10240
|
assert (dataunits.gib * 10).mib() == 10240
|
||||||
assert (dataunits.gib * 5000).byte() == i64(5368709120000)
|
assert (dataunits.gib * 5000).byte() == f64(5368709120000)
|
||||||
assert (dataunits.mbit * 500).kb() == 62500
|
assert (dataunits.mbit * 500).kb() == 62500
|
||||||
assert dataunits.convert(500, dataunits.mbit, dataunits.kb) == 62500
|
assert dataunits.convert(500, dataunits.mbit, dataunits.kb) == 62500
|
||||||
assert dataunits.DataSize(4000 * dataunits.gib).byte() == f64(4294967296000)
|
assert dataunits.DataSize(4000 * dataunits.gib).byte() == f64(4294967296000)
|
||||||
@@ -37,3 +37,47 @@ fn test_from_string() {
|
|||||||
assert dataunits.from_string('Ki')! == dataunits.kib
|
assert dataunits.from_string('Ki')! == dataunits.kib
|
||||||
assert dataunits.from_string('Gi')! == dataunits.gib
|
assert dataunits.from_string('Gi')! == dataunits.gib
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const units = [
|
||||||
|
dataunits.bit,
|
||||||
|
dataunits.nibble,
|
||||||
|
dataunits.byte,
|
||||||
|
dataunits.kb,
|
||||||
|
dataunits.mb,
|
||||||
|
dataunits.gb,
|
||||||
|
dataunits.tb,
|
||||||
|
dataunits.pb,
|
||||||
|
dataunits.eb,
|
||||||
|
dataunits.zb,
|
||||||
|
dataunits.yb,
|
||||||
|
dataunits.kib,
|
||||||
|
dataunits.mib,
|
||||||
|
dataunits.gib,
|
||||||
|
dataunits.tib,
|
||||||
|
dataunits.pib,
|
||||||
|
dataunits.eib,
|
||||||
|
dataunits.zib,
|
||||||
|
dataunits.yib,
|
||||||
|
dataunits.kbit,
|
||||||
|
dataunits.mbit,
|
||||||
|
dataunits.gbit,
|
||||||
|
dataunits.tbit,
|
||||||
|
dataunits.pbit,
|
||||||
|
dataunits.ebit,
|
||||||
|
dataunits.zbit,
|
||||||
|
dataunits.ybit,
|
||||||
|
dataunits.kibit,
|
||||||
|
dataunits.mibit,
|
||||||
|
dataunits.gibit,
|
||||||
|
dataunits.tibit,
|
||||||
|
dataunits.pibit,
|
||||||
|
dataunits.eibit,
|
||||||
|
dataunits.zibit,
|
||||||
|
dataunits.yibit,
|
||||||
|
]
|
||||||
|
|
||||||
|
fn test_from_string_to_string() {
|
||||||
|
for unit in units {
|
||||||
|
assert dataunits.from_string(dataunits.to_string(unit)!)! == unit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user