Merge pull request #296 from benpate/pullrequest-parseInterval

More consistent parseInterval results
This commit is contained in:
1cg 2021-01-01 16:51:08 -07:00 committed by GitHub
commit 686bab07bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 10 deletions

View File

@ -62,16 +62,18 @@ return (function () {
//====================================================================
// Utilities
//====================================================================
function parseInterval(str) {
if (str == null || str === "null" || str === "false" || str === "") {
return null;
} else if (str.lastIndexOf("ms") === str.length - 2) {
return parseFloat(str.substr(0, str.length - 2));
} else if (str.lastIndexOf("s") === str.length - 1) {
return parseFloat(str.substr(0, str.length - 1)) * 1000;
} else {
return parseFloat(str);
}
function parseInterval(str) {
if (str == undefined) {
return undefined
}
if (str.slice(-2) == "ms") {
return parseFloat(str.slice(0,-2)) || undefined
}
if (str.slice(-1) == "s") {
return (parseFloat(str.slice(0,-1)) * 1000) || undefined
}
return parseFloat(str) || undefined
}
function getRawAttribute(elt, name) {

View File

@ -20,4 +20,18 @@ describe("Core htmx internals Tests", function() {
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
})
it("handles parseInterval correctly", function() {
chai.expect(htmx.parseInterval("1ms")).to.be.equal(1);
chai.expect(htmx.parseInterval("300ms")).to.be.equal(300);
chai.expect(htmx.parseInterval("1s")).to.be.equal(1000)
chai.expect(htmx.parseInterval("1.5s")).to.be.equal(1500)
chai.expect(htmx.parseInterval("2s")).to.be.equal(2000)
chai.expect(htmx.parseInterval(null)).to.be.undefined
chai.expect(htmx.parseInterval("")).to.be.undefined
chai.expect(htmx.parseInterval("undefined")).to.be.undefined
chai.expect(htmx.parseInterval("true")).to.be.undefined
chai.expect(htmx.parseInterval("false")).to.be.undefined
})
});