Skip to content

Commit 586ad40

Browse files
authored
fix: should not lose precision when uncasting string to number (#438)
1 parent 9b92302 commit 586ad40

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

src/data_types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class INTEGER extends DataType {
177177
const originValue = value;
178178
if (value == null || value instanceof Raw) return value;
179179
if (typeof value === 'string') value = parseInt(value, 10);
180+
if (value > Number.MAX_SAFE_INTEGER) return originValue;
180181
if (Number.isNaN(value)) {
181182
if (strict) throw new Error(util.format('invalid integer: %s', originValue));
182183
return originValue;

test/unit/data_types.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ describe('=> DataTypes type casting', function() {
156156
assert.equal(new INTEGER().uncast(undefined), undefined);
157157
assert.equal(new INTEGER().uncast('1'), 1);
158158
assert.equal(new INTEGER().uncast(1), 1);
159+
const value = Number.MAX_SAFE_INTEGER.toString() + 1234;
160+
assert.equal(new INTEGER().uncast(value), value);
159161

160162
await assert.rejects(async () => {
161163
new Postgres_DataTypes.INTEGER().uncast('yes?');
@@ -168,7 +170,6 @@ describe('=> DataTypes type casting', function() {
168170
}, /Error: invalid integer: yes?/);
169171

170172
assert.equal(new INTEGER().uncast('yes?', false), 'yes?');
171-
172173
});
173174

174175
it('STRING', async function() {

0 commit comments

Comments
 (0)