Skip to content

Commit

Permalink
extend number decoder to support parsing precision point numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Coquand committed May 25, 2021
1 parent 90e9bae commit 9392764
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "6.1.0",
"version": "6.1.1",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ const isDate = (d: Date): boolean => !isNaN(d.getDate());
const isISO = (str: string): boolean =>
str.match(/(\d{4})-(\d{2})-(\d{2})/) !== null;
const isInteger = (n: number): boolean => Math.floor(n) === n && n !== Infinity;
var numberReSnippet =
'(?:NaN|-?(?:(?:\\d+|\\d*\\.\\d+)(?:[E|e][+|-]?\\d+)?|Infinity))';
var matchOnlyNumberRe = new RegExp('^(' + numberReSnippet + ')$');

const isStringNumber = (n: string): boolean =>
n.length !== 0 && n.match(/^[+-]?\d+(\.\d+)?$/) !== null;
n.length !== 0 && n.match(matchOnlyNumberRe) !== null;

export { DecodeError };
export class ValidationFailedError extends Error {
Expand Down
5 changes: 5 additions & 0 deletions test/decoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ describe('Number decoder', () => {
})
);
});
it('decodes floating numbers', () => {
const res = Decoder.number.run('-4.44089e-16');

expect(res).toEqual({ type: 'OK', value: -4.44089e-16 });
});
it('does not decode invalid data', () => {
fc.assert(
fc.property(fc.anything(), (anything: any) => {
Expand Down

0 comments on commit 9392764

Please sign in to comment.