mirror of
https://github.com/zoriya/typebox.git
synced 2026-05-30 01:39:02 +00:00
Fix: Converting Large Numbers to BigInt (#1019)
* Fix converting large numbers to BigInts * Clarify numeric value * Add test
This commit is contained in:
@@ -120,7 +120,7 @@ function TryConvertBoolean(value: unknown) {
|
||||
}
|
||||
function TryConvertBigInt(value: unknown) {
|
||||
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
|
||||
return IsStringNumeric(value) ? BigInt(truncateInteger(value)) : IsNumber(value) ? BigInt(Math.trunc(value)) : 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
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Type } from '@sinclair/typebox'
|
||||
import { Assert } from '../../assert/index'
|
||||
|
||||
describe('value/convert/BigInt', () => {
|
||||
it('Should convert bitint from string 1', () => {
|
||||
it('Should convert bigint from string 1', () => {
|
||||
const T = Type.BigInt()
|
||||
const R = Value.Convert(T, '1')
|
||||
Assert.IsEqual(R, BigInt(1))
|
||||
@@ -13,7 +13,7 @@ describe('value/convert/BigInt', () => {
|
||||
const R = Value.Convert(T, '3.14')
|
||||
Assert.IsEqual(R, BigInt(3))
|
||||
})
|
||||
it('Should convert bitint from string 3', () => {
|
||||
it('Should convert bigint from string 3', () => {
|
||||
const T = Type.BigInt()
|
||||
const R = Value.Convert(T, 'true')
|
||||
Assert.IsEqual(R, BigInt(1))
|
||||
@@ -43,7 +43,7 @@ describe('value/convert/BigInt', () => {
|
||||
const R = Value.Convert(T, '-12345678901234567890.123')
|
||||
Assert.IsEqual(R, BigInt('-12345678901234567890'))
|
||||
})
|
||||
it('Should convert bitint from number 1', () => {
|
||||
it('Should convert bigint from number 1', () => {
|
||||
const T = Type.BigInt()
|
||||
const R = Value.Convert(T, 1)
|
||||
Assert.IsEqual(R, BigInt(1))
|
||||
@@ -53,7 +53,27 @@ describe('value/convert/BigInt', () => {
|
||||
const R = Value.Convert(T, 3.14)
|
||||
Assert.IsEqual(R, BigInt(3))
|
||||
})
|
||||
it('Should convert bitint from boolean 1', () => {
|
||||
it('Should convert bigint from number 3', () => {
|
||||
const T = Type.BigInt()
|
||||
const R = Value.Convert(T, Math.pow(2, 31))
|
||||
Assert.IsEqual(R, BigInt(2147483648))
|
||||
})
|
||||
it('Should convert bigint from number 4', () => {
|
||||
const T = Type.BigInt()
|
||||
const R = Value.Convert(T, Number.MAX_SAFE_INTEGER)
|
||||
Assert.IsEqual(R, BigInt(9007199254740991))
|
||||
})
|
||||
it('Should convert bigint from number 5', () => {
|
||||
const T = Type.BigInt()
|
||||
const R = Value.Convert(T, 123456789012345.6789)
|
||||
Assert.IsEqual(R, BigInt(123456789012345))
|
||||
})
|
||||
it('Should convert bigint from number 6', () => {
|
||||
const T = Type.BigInt()
|
||||
const R = Value.Convert(T, -123456789012345.6789)
|
||||
Assert.IsEqual(R, BigInt(-123456789012345))
|
||||
})
|
||||
it('Should convert bigint from boolean 1', () => {
|
||||
const T = Type.BigInt()
|
||||
const R = Value.Convert(T, true)
|
||||
Assert.IsEqual(R, BigInt(1))
|
||||
|
||||
Reference in New Issue
Block a user