mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-09-28 13:31:06 +00:00
Merge pull request #296 from benpate/pullrequest-parseInterval
More consistent parseInterval results
This commit is contained in:
commit
686bab07bf
22
src/htmx.js
22
src/htmx.js
@ -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) {
|
||||
|
@ -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
|
||||
})
|
||||
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user