Skip to content

Commit

Permalink
Revision 0.34.15 (#1148)
Browse files Browse the repository at this point in the history
* Use Math.Trunc to avoid 32-bit coersion for Integer

* Version

* ChangeLog
  • Loading branch information
sinclairzx81 authored Jan 30, 2025
1 parent 9ea7eb0 commit e772a4f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelog/0.34.0.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
### 0.34.0
- [Revision 0.34.15](https://github.com/sinclairzx81/typebox/pull/1148)
- [1147](https://github.com/sinclairzx81/typebox/issues/1147) Fix incorrect truncation for integers that exceed 32-bit values in Value.Convert
- [Revision 0.34.14](https://github.com/sinclairzx81/typebox/pull/1140)
- [1139](https://github.com/sinclairzx81/typebox/issues/1139) Update TypeCompiler Check for Promise (use instanceof Promise over Thenable check)
- [Revision 0.34.13](https://github.com/sinclairzx81/typebox/pull/1124)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.34.14",
"version": "0.34.15",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
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 e772a4f

Please sign in to comment.