Revision 0.33.12 (#999)

* Avoid losing precision when converting to bigints

* Use `truncateInteger` rather than `parseInt`

* Simplify
This commit is contained in:
Hamish Peebles
2024-09-18 18:23:59 +01:00
committed by GitHub
parent ddd85e1f78
commit 928ebd91fc
2 changed files with 22 additions and 1 deletions

View File

@@ -119,7 +119,8 @@ function TryConvertBoolean(value: unknown) {
return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value
}
function TryConvertBigInt(value: unknown) {
return IsStringNumeric(value) ? BigInt(parseInt(value)) : IsNumber(value) ? BigInt(value | 0) : IsValueFalse(value) ? BigInt(0) : IsValueTrue(value) ? BigInt(1) : value
const truncateInteger = (value: string) => value.split('.')[0];
return IsStringNumeric(value) ? BigInt(truncateInteger(value)) : IsNumber(value) ? BigInt(value | 0) : IsValueFalse(value) ? BigInt(0) : IsValueTrue(value) ? BigInt(1) : value
}
function TryConvertString(value: unknown) {
return IsValueToString(value) ? value.toString() : IsSymbol(value) && value.description !== undefined ? value.description.toString() : value

View File

@@ -23,6 +23,26 @@ describe('value/convert/BigInt', () => {
const R = Value.Convert(T, 'false')
Assert.IsEqual(R, BigInt(0))
})
it('Should convert bigint from string 5', () => {
const T = Type.BigInt()
const R = Value.Convert(T, '12345678901234567890')
Assert.IsEqual(R, BigInt("12345678901234567890"))
})
it('Should convert bigint from string 6', () => {
const T = Type.BigInt()
const R = Value.Convert(T, '-12345678901234567890')
Assert.IsEqual(R, BigInt("-12345678901234567890"))
})
it('Should convert bigint from string 7', () => {
const T = Type.BigInt()
const R = Value.Convert(T, '12345678901234567890.123')
Assert.IsEqual(R, BigInt("12345678901234567890"))
})
it('Should convert bigint from string 8', () => {
const T = Type.BigInt()
const R = Value.Convert(T, '-12345678901234567890.123')
Assert.IsEqual(R, BigInt("-12345678901234567890"))
})
it('Should convert bitint from number 1', () => {
const T = Type.BigInt()
const R = Value.Convert(T, 1)