Skip to content

Commit

Permalink
fix(util-dynamodb): unmarshall small numbers or those in scientific n…
Browse files Browse the repository at this point in the history
…otation (#2017)
  • Loading branch information
trivikr committed Feb 11, 2021
1 parent 569b572 commit 80a8094
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
21 changes: 9 additions & 12 deletions packages/util-dynamodb/src/convertToNative.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,15 @@ describe("convertToNative", () => {
});
});

[
`${Number.MAX_SAFE_INTEGER}.1`,
`${Number.MIN_SAFE_INTEGER}.1`,
`${Number.MIN_VALUE}1`,
`-${Number.MIN_VALUE}1`,
].forEach((numString) => {
it(`throws if number is outside IEEE 754 Floating-Point Arithmetic: ${numString}`, () => {
expect(() => {
convertToNative({ N: numString });
}).toThrowError(
`Value ${numString} is outside IEEE 754 Floating-Point Arithmetic. Set options.wrapNumbers to get string value.`
);
[`0.0000001`, `0.0000000001`, `0.00000000000000000001`].forEach((numString) => {
it(`returns for small numbers: ${numString}`, () => {
expect(convertToNative({ N: numString })).toEqual(Number(numString));
});
});

[`1e-7`, `1e-20`, `1e15`].forEach((numString) => {
it(`returns numbers stored in scientific notation: ${numString}`, () => {
expect(convertToNative({ N: numString })).toEqual(Number(numString));
});
});

Expand Down
4 changes: 0 additions & 4 deletions packages/util-dynamodb/src/convertToNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ const convertNumber = (numString: string, options?: unmarshallOptions): number |
} else {
throw new Error(`${numString} is outside SAFE_INTEGER bounds. Set options.wrapNumbers to get string value.`);
}
} else if (num.toString() !== numString) {
throw new Error(
`Value ${numString} is outside IEEE 754 Floating-Point Arithmetic. Set options.wrapNumbers to get string value.`
);
}
return num;
};
Expand Down

0 comments on commit 80a8094

Please sign in to comment.