mirror of
https://github.com/zoriya/typebox.git
synced 2025-12-06 06:46:10 +00:00
Revision 0.33.12 (#999)
* Avoid losing precision when converting to bigints * Use `truncateInteger` rather than `parseInt` * Simplify
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user