-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(primitives): implement decimal type
- Loading branch information
Showing
5 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Decimal FHIR Primitive Runtime Type | ||
*/ | ||
|
||
import { Big } from "big.js"; | ||
import { Type, success, failure } from "io-ts"; | ||
|
||
/** | ||
* Class that wraps big.js to maintain precision, including trailing zeros | ||
*/ | ||
export class Decimal extends Big { | ||
private dp: number; | ||
constructor(n) { | ||
super(n); | ||
this.dp = this.decimalPlaces(n); | ||
} | ||
|
||
public toFixed(dp?: number) { | ||
return super.toFixed(this.dp); | ||
} | ||
|
||
private decimalPlaces(n: string | number) { | ||
// Coerce to string | ||
const number = String(n); | ||
const hasDecimal = number.includes("."); | ||
const exponentialForm = number.includes("e"); | ||
if (hasDecimal) { | ||
if (exponentialForm) { | ||
const [mantissa, exponent] = number.split(".")[1].split("e"); | ||
return mantissa.length - parseInt(exponent); | ||
} else { | ||
return number.split(".")[1].length; | ||
} | ||
} else { | ||
return 0; | ||
} | ||
} | ||
} | ||
|
||
export class DecimalType extends Type<Decimal, string, unknown> { | ||
readonly _tag: "DecimalType" = "DecimalType"; | ||
constructor() { | ||
super( | ||
"decimal", | ||
(m): m is Decimal => m instanceof Decimal, | ||
(m, c) => { | ||
try { | ||
const decimal = new Decimal(<any>m); | ||
return success(decimal); | ||
} catch (e) { | ||
return failure(m, c); | ||
} | ||
}, | ||
a => a.toFixed() | ||
); | ||
} | ||
} | ||
|
||
export const decimal = new DecimalType(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Tests for Decimal Runtime Type | ||
*/ | ||
|
||
import { assertSuccess, assertFailure } from "./helpers"; | ||
import { decimal } from "../src"; | ||
import { Decimal } from "../src/decimal"; | ||
|
||
describe("DecimalType", () => { | ||
it("should succeed validating a valid value", () => { | ||
const T = decimal; | ||
const input = "1.01"; | ||
const value = new Decimal(input); | ||
assertSuccess(T.decode(input)); | ||
expect(T.decode(input).value).toEqual(value); | ||
}); | ||
|
||
it("should successfully maintain precision", () => { | ||
const T = decimal; | ||
expect(T.decode("1.0100").map(T.encode).value).toEqual("1.0100"); | ||
expect(T.decode(1.01).map(T.encode).value).toEqual("1.01"); | ||
expect(T.decode("1.01e2").map(T.encode).value).toEqual("101"); | ||
expect(T.decode("1.0100e2").map(T.encode).value).toEqual("101.00"); | ||
}); | ||
|
||
it("should fail validating an invalid value", () => { | ||
const T = decimal; | ||
assertFailure(T.decode("abc"), [ | ||
'Invalid value "abc" supplied to : decimal' | ||
]); | ||
}); | ||
|
||
it("should type guard", () => { | ||
const T = decimal; | ||
const value = new Decimal("1.010"); | ||
expect(T.is(value)).toEqual(true); | ||
expect(T.is("b")).toEqual(false); | ||
expect(T.is(undefined)).toEqual(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters