-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: #43 floating point issue in multipleOf validation
- fix multipleOf validation logic - remove floatingPointPrecision setting
- Loading branch information
Sascha Goldhofer
committed
Aug 15, 2023
1 parent
d6a307c
commit 743ff3d
Showing
12 changed files
with
151 additions
and
16 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* returns the floating point precision of a decimal number or 0 | ||
*/ | ||
export declare function getPrecision(value: number): number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* returns the floating point precision of a decimal number or 0 | ||
*/ | ||
export function getPrecision(value) { | ||
const string = `${value}`; | ||
const index = string.indexOf("."); | ||
return index === -1 ? 0 : string.length - (index + 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* returns the floating point precision of a decimal number or 0 | ||
*/ | ||
export function getPrecision(value: number): number { | ||
const string = `${value}`; | ||
const index = string.indexOf("."); | ||
return index === -1 ? 0 : string.length - (index + 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { expect } from "chai"; | ||
import { Draft07 as Draft } from "../../../lib/draft07"; | ||
|
||
describe("issue#43 - multipleOf .01", () => { | ||
let draft: Draft; | ||
beforeEach(() => { | ||
draft = new Draft({ | ||
type: "number", | ||
multipleOf: 0.01 | ||
}); | ||
}); | ||
|
||
it("should validate 1", () => { | ||
const result = draft.validate(1); | ||
expect(result).to.have.length(0); | ||
}); | ||
|
||
it("should validate .2", () => { | ||
const result = draft.validate(0.2); | ||
expect(result).to.have.length(0); | ||
}); | ||
|
||
it("should validate .02", () => { | ||
const result = draft.validate(0.02); | ||
expect(result).to.have.length(0); | ||
}); | ||
|
||
it("should not validate .025", () => { | ||
const result = draft.validate(0.025); | ||
expect(result).to.have.length(1); | ||
}); | ||
|
||
it("should validate 1.36", () => { | ||
const result = draft.validate(1.36); | ||
expect(result).to.have.length(0); | ||
}); | ||
|
||
it("should validate 2.74", () => { | ||
const result = draft.validate(2.74); | ||
expect(result).to.have.length(0); | ||
}); | ||
|
||
it("should validate 123456789", () => { | ||
const result = draft.validate(123456789); | ||
expect(result).to.have.length(0); | ||
}); | ||
|
||
it("should not validate Infinity", () => { | ||
const result = draft.validate(1e308); | ||
expect(result).to.have.length(1); | ||
}); | ||
|
||
it("should validate all floats with two decimals", () => { | ||
for (let i = 0; i <= 100; i++) { | ||
let num = `2.${i}`; | ||
expect(draft.validate(parseFloat(num))).to.have.length( | ||
0, | ||
`should have validated '${num}'` | ||
); | ||
} | ||
}); | ||
|
||
it("should still validate multiple of integers", () => { | ||
draft = new Draft({ | ||
type: "number", | ||
multipleOf: 3 | ||
}); | ||
const result = draft.validate(9); | ||
expect(result).to.have.length(0); | ||
}); | ||
|
||
it("should still invalidate non-multiples of integers", () => { | ||
draft = new Draft({ | ||
type: "number", | ||
multipleOf: 3 | ||
}); | ||
const result = draft.validate(7); | ||
expect(result).to.have.length(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { expect } from "chai"; | ||
import { getPrecision } from "../../../lib/utils/getPrecision"; | ||
|
||
describe("getPrecision", () => { | ||
it("should return decimal precision", () => { | ||
expect(getPrecision(1.1)).to.equal(1); | ||
expect(getPrecision(0.12)).to.equal(2); | ||
expect(getPrecision(0.123)).to.equal(3); | ||
expect(getPrecision(123.4567)).to.equal(4); | ||
}); | ||
}); |