-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialize.ts
61 lines (56 loc) · 1.54 KB
/
serialize.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as Unit from "./unit";
import * as UnitMap from "./unit-map";
import * as Amount from "./amount";
/**
* @module Serialize
*/
/**
* Converts a units serialized representation to it's deserialized representation
* @param unitString
*/
export function stringToUnit<T>(
unitString: string,
unitLookup: UnitMap.UnitLookup
): Unit.Unit<T> | undefined {
return unitLookup(unitString) as Unit.Unit<T>;
}
/**
* Converts a unit to it's serialized representation
* @param unit
*/
export function unitToString(unit: Unit.Unit<unknown>): string {
return unit.name;
}
/**
* Convert an amount to it's serialized representation
* @param amount
*/
export function amountToString(amount: Amount.Amount<unknown>): string {
if (!amount.value === null || amount.value === undefined) {
return "";
}
const valueString = amount.value.toFixed(amount.decimalCount);
const unitString = unitToString(amount.unit);
return `${valueString}:${unitString}`;
}
/**
* Convert a serialized amount to it's deserialized representation
* @param amountString
*/
export function stringToAmount(
amountString: string,
unitLookup: UnitMap.UnitLookup
): Amount.Amount<unknown> | undefined {
const parts = amountString.split(":");
const value = parseFloat(parts[0]);
const unit = stringToUnit(parts[1], unitLookup);
if (!unit) {
return undefined;
}
let decimalCount = 0;
const pointIndex = parts[0].indexOf(".");
if (pointIndex >= 0) {
decimalCount = parts[0].length - pointIndex - 1;
}
return Amount.create(value, unit, decimalCount);
}