Skip to content

Commit 7e04e0b

Browse files
committed
Add documentation
1 parent ed803a7 commit 7e04e0b

File tree

4 files changed

+243
-4
lines changed

4 files changed

+243
-4
lines changed

README.md

+242-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,243 @@
1-
# United
21

3-
WIP / Units manipulation in Vala made easy
2+
![United logo](resources/united.png)
3+
4+
![CI](https://github.com/lcallarec/united/workflows/CI/badge.svg)
5+
[![codecov](https://codecov.io/gh/lcallarec/united/branch/master/graph/badge.svg)](https://codecov.io/gh/lcallarec/united)
6+
[![Release](https://img.shields.io/github/release/lcallarec/united.svg)](https://github.com/lcallarec/united/releases)
7+
[![License](https://img.shields.io/github/license/lcallarec/united)](https://github.com/lcallarec/united/blob/master/LICENSE)
8+
9+
#### Unit manipulation made easy
10+
11+
# Units
12+
13+
There's two main units in [United](https://github.com/lcallarec/united/) :
14+
15+
* [`Value`](#value), which hold any [SI derived units](https://en.wikipedia.org/wiki/International_System_of_Units)
16+
* [`Bytes`](#bytes), which can be SI prefixed (ex. kB, MB...) or IEC / Binary prefixed (ex. KiB, GiB...)
17+
18+
And lists of prefixes :
19+
20+
* [`Prefix`](#prefixes), which represent some [SI derived prefixes](https://en.wikipedia.org/wiki/International_System_of_Units)
21+
* [`BinaryPrefix`](#binary-prefixes), which meant to represent [Binary prefixes](https://en.wikipedia.org/wiki/Binary_prefix) to be used with `Bytes`.
22+
23+
24+
## Value
25+
26+
* Create a value :
27+
28+
```vala
29+
// From value and unit (potentially prefixed)
30+
var weight = new Value(5, "g");
31+
var weight = new Value(5.97, "kg");
32+
33+
// From string
34+
var weight = new Value.from_string("15,8g");
35+
36+
//From known attributes
37+
var weight = new Value.from_attributes(8.15, "g", Prefix.kilo());
38+
```
39+
40+
* Public attributes and presentation methods
41+
42+
```vala
43+
var power = new Value.from_string("2.21GW");
44+
45+
power.measure; // 2.21
46+
power.unit; // "W"
47+
power.prefix; // Prefix.GIGA
48+
power.to_string(); // "2.21GW"
49+
power.to_string("%.1f"); // "2.2GW"
50+
```
51+
52+
* Convert to another Prefix
53+
54+
```vala
55+
var power_in_GW = new Value(2.21, "GW");
56+
57+
power_in_GW.measure; // 2.21
58+
power_in_GW.unit; // "W"
59+
power_in_GW.prefix; // Prefix.GIGA
60+
power_in_GW.to_string(); // "2.21GW"
61+
62+
63+
var power_in_kW = power_in_GW.to(Prefix.KILO);
64+
65+
power_in_kW.measure; // 2210000
66+
power_in_kW.unit; // "W"
67+
power_in_kW.prefix; // Prefix.KILO
68+
power_in_kW.to_string(); // "2210000kW"
69+
```
70+
71+
* Convert to an human readable format
72+
73+
Humans don't like to read 2210000000W. They prefer reading number lower than 1000.
74+
75+
```vala
76+
var power_in_GW = new Value(2210000000, "W");
77+
78+
var human_readable = power_in_GW.to_human();
79+
80+
human_readable.measure; // 2.21
81+
human_readable.unit; // "W"
82+
human_readable.prefix; // Prefix.GIGA
83+
human_readable.to_string(); // "2.21GW"
84+
```
85+
86+
# Prefixes
87+
88+
Prefixes are mostly used to convert a value to another value, or print a custom value.
89+
90+
```vala
91+
var yotta = Prefix.yotta(); //Shortcut for new Prefix(Prefix.YOTTA);
92+
var zeta = Prefix.zeta(); //Shortcut for new Prefix(Prefix.ZETA);
93+
var exa = Prefix.exa(); //Shortcut for new Prefix(Prefix.EXA);
94+
var peta = Prefix.peta(); //Shortcut for new Prefix(Prefix.PETA);
95+
var tera = Prefix.tera(); //Shortcut for new Prefix(Prefix.TERA);
96+
var giga = Prefix.giga(); //Shortcut for new Prefix(Prefix.GIGA);
97+
var mega = Prefix.mega(); //Shortcut for new Prefix(Prefix.MEGA);
98+
var kilo = Prefix.kilo(); //Shortcut for new Prefix(Prefix.KILO);
99+
var none = Prefix.none(); //Shortcut for new Prefix(Prefix.NONE);
100+
var milli = Prefix.milli(); //Shortcut for new Prefix(Prefix.MILLI);
101+
var micro = Prefix.micro(); //Shortcut for new Prefix(Prefix.MICRO);
102+
var nano = Prefix.nano(); //Shortcut for new Prefix(Prefix.NANO);
103+
var pico = Prefix.pico(); //Shortcut for new Prefix(Prefix.PICO);
104+
var femto = Prefix.femto(); //Shortcut for new Prefix(Prefix.FEMTO);
105+
var atto = Prefix.atto(); //Shortcut for new Prefix(Prefix.ATTO);
106+
var zepto = Prefix.zepto(); //Shortcut for new Prefix(Prefix.ZEPTO);
107+
var yocto = Prefix.yocto(); //Shortcut for new Prefix(Prefix.YOCTO);
108+
109+
// Convert
110+
var power = new Value(2.21, "GW");
111+
var MW = power.to(Prefix.mega());
112+
113+
MW.measure; // 2210
114+
MW.unit; // "W"
115+
MW.prefix; // Prefix.MEGA
116+
MW.to_string(); // "2210MW"
117+
118+
```
119+
120+
Print a prefix :
121+
122+
```vala
123+
yotta.to_string(); // "Y"
124+
zeta.to_string(); // "Z"
125+
exa.to_string(); // "E"
126+
peta.to_string(); // "P"
127+
tera.to_string(); // "T"
128+
giga.to_string(); // "G"
129+
mega.to_string(); // "M"
130+
kilo.to_string(); // "k"
131+
none.to_string(); // ""
132+
milli.to_string(); // "m"
133+
micro.to_string(); // "μ"
134+
nano.to_string(); // "n"
135+
pico.to_string(); // "p"
136+
femto.to_string(); // "f"
137+
atto.to_string(); // "a"
138+
zepto.to_string(); // "z"
139+
yocto.to_string(); // "y"
140+
```
141+
142+
## Bytes
143+
144+
Bytes share most of the `Value` features.
145+
146+
* Create a Bytes :
147+
148+
```vala
149+
// From value and unit (potentially prefixed)
150+
var size = new Bytes(5, "B");
151+
var size = new Value(5.97, "MB");
152+
153+
```
154+
155+
* Public attributes and presentation methods
156+
157+
```vala
158+
var size = new Bytes(2097152, "B");
159+
160+
size.value; // 2097152
161+
size.unit; // "B"
162+
size.prefix; // Prefix.NONE
163+
size.to_string(); // "2097152BW"
164+
```
165+
166+
* Convert to another Prefix
167+
168+
SI prefixes :
169+
170+
```vala
171+
var b = new Bytes(2097152, "B");
172+
173+
var kb = b.to(Prefix.kilo());
174+
175+
kb.value; // 2097,152
176+
kb.unit; // "B"
177+
kb.prefix; // Prefix.KILO
178+
kb.to_string(); // "2097,152kB"
179+
```
180+
181+
To binary prefixes :
182+
183+
```vala
184+
var b = new Bytes(2097152, "B");
185+
var kb = b.to(Prefix.kilo());
186+
var MiB = kb.to(BinaryPrefix.mebi());
187+
188+
kb.value; // 2
189+
kb.unit; // "B"
190+
kb.prefix; // BinaryPrefix.MEBI;
191+
kb.to_string(); // "2MiB"
192+
```
193+
194+
* Convert to an human readable format
195+
196+
Humans don't like to read 2097152B. They prefer reading number lower than 1000.
197+
198+
```vala
199+
var b = new Bytes(2097152, "B");
200+
var human_readable = b.to_human();
201+
202+
human_readable.to_string(); // "2MiB"
203+
```
204+
205+
# Binary prefixes
206+
207+
Binary prefixes should only be used with Bytes type.
208+
209+
```vala
210+
var yobi = BinaryPrefix.yobi(); // Shortcut for new BinaryPrefix(BinaryPrefix.YOBI);
211+
var zebi = BinaryPrefix.zebi(); // Shortcut for new BinaryPrefix(BinaryPrefix.ZEBI);
212+
var exbi = BinaryPrefix.exbi(); // Shortcut for new BinaryPrefix(BinaryPrefix.EXBI);
213+
var pebi = BinaryPrefix.pebi(); // Shortcut for new BinaryPrefix(BinaryPrefix.PEBI);
214+
var tebi = BinaryPrefix.tebi(); // Shortcut for new BinaryPrefix(BinaryPrefix.TEBI);
215+
var gibi = BinaryPrefix.gibi(); // Shortcut for new BinaryPrefix(BinaryPrefix.GIBI);
216+
var mebi = BinaryPrefix.mebi(); // Shortcut for new BinaryPrefix(BinaryPrefix.MEBI);
217+
var kibi = BinaryPrefix.kibi(); // Shortcut for new BinaryPrefix(BinaryPrefix.KIBI);
218+
var none = BinaryPrefix.none(); // Shortcut for new BinaryPrefix(BinaryPrefix.NONE);
219+
220+
// Convert
221+
var size = new Bytes(1024);
222+
var KB = size.to(BinaryPrefix.kilo());
223+
224+
KB.value; // 1
225+
KB.unit; // "B"
226+
KB.prefix; // BinaryPrefix.KILO;
227+
KB.to_string(); // "1KiB"
228+
229+
```
230+
231+
Print a prefix :
232+
233+
```vala
234+
yobi.to_string(); // "Yi"
235+
zebi.to_string(); // "Zi"
236+
exbi.to_string(); // "Ei"
237+
pebi.to_string(); // "Pi"
238+
tebi.to_string(); // "Ti"
239+
gibi.to_string(); // "Gi"
240+
mebi.to_string(); // "Mi"
241+
kibi.to_string(); // "Ki"
242+
none.to_string(); // ""
243+
```

meson.build

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
project('united', ['vala', 'c'], version: '0.1')
1+
project('united', ['vala', 'c'], version: '0.7')
22

33
cc = meson.get_compiler('c')
44
libm = cc.find_library('m', required: true)

resources/united.png

2.35 KB
Loading

src/Prefix.vala

-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ namespace United {
161161
return this.value;
162162
}
163163

164-
165164
public Prefixable create_at(int8 distance) {
166165
return new Prefix(this.get_value() + distance);
167166
}

0 commit comments

Comments
 (0)