Skip to content

Commit

Permalink
Fix converting large numbers to BigInts
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Oct 3, 2024
1 parent 0306823 commit f2e2d59
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/value/convert/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ function TryConvertBoolean(value: unknown) {
return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value
}
function TryConvertBigInt(value: unknown) {
value = IsNumber(value) ? value.toString() : 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
return IsStringNumeric(value) ? BigInt(truncateInteger(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
Expand Down
10 changes: 10 additions & 0 deletions test/runtime/value/convert/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ describe('value/convert/BigInt', () => {
const R = Value.Convert(T, 3.14)
Assert.IsEqual(R, BigInt(3))
})
it('Should convert bigint from number 3', () => {
const T = Type.BigInt()
const R = Value.Convert(T, 2147483648)
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 bitint from boolean 1', () => {
const T = Type.BigInt()
const R = Value.Convert(T, true)
Expand Down

0 comments on commit f2e2d59

Please sign in to comment.