Skip to content

Commit

Permalink
Use Math.Trunc to avoid 32-bit coersion for Integer
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Jan 30, 2025
1 parent 9ea7eb0 commit c94f7d6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/value/convert/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function TryConvertNumber(value: unknown) {
return IsStringNumeric(value) ? parseFloat(value) : IsValueTrue(value) ? 1 : IsValueFalse(value) ? 0 : value
}
function TryConvertInteger(value: unknown) {
return IsStringNumeric(value) ? parseInt(value) : IsNumber(value) ? value | 0 : IsValueTrue(value) ? 1 : IsValueFalse(value) ? 0 : value
return IsStringNumeric(value) ? parseInt(value) : IsNumber(value) ? Math.trunc(value) : IsValueTrue(value) ? 1 : IsValueFalse(value) ? 0 : value
}
function TryConvertNull(value: unknown) {
return IsString(value) && value.toLowerCase() === 'null' ? null : value
Expand Down
23 changes: 23 additions & 0 deletions test/runtime/value/convert/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,27 @@ describe('value/convert/Integer', () => {
const result = Value.Convert(Type.Integer(), value)
Assert.IsEqual(result, [])
})
// ----------------------------------------------------------
// https://github.com/sinclairzx81/typebox/issues/1147
// ----------------------------------------------------------
it('Should convert large Integer 1', () => {
const N = 1738213389080
const R = Value.Convert(Type.Integer(), N)
Assert.IsEqual(R, N)
})
it('Should convert large Integer 2', () => {
const N = 1738213389080.5555
const R = Value.Convert(Type.Integer(), N)
Assert.IsEqual(R, 1738213389080)
})
it('Should convert large Integer 3', () => {
const N = '1738213389080'
const R = Value.Convert(Type.Integer(), N)
Assert.IsEqual(R, 1738213389080)
})
it('Should convert large Integer 3', () => {
const N = '1738213389080.555'
const R = Value.Convert(Type.Integer(), N)
Assert.IsEqual(R, 1738213389080)
})
})

0 comments on commit c94f7d6

Please sign in to comment.