Skip to content

Commit

Permalink
Revision 0.33.12 (#999)
Browse files Browse the repository at this point in the history
* Avoid losing precision when converting to bigints

* Use `truncateInteger` rather than `parseInt`

* Simplify
  • Loading branch information
hpeebles authored Sep 18, 2024
1 parent ddd85e1 commit 928ebd9
Show file tree
Hide file tree
Showing 2 changed files with 22 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,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
Expand Down
20 changes: 20 additions & 0 deletions test/runtime/value/convert/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 928ebd9

Please sign in to comment.