mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-10-02 15:25:26 +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
|
// Utilities
|
||||||
//====================================================================
|
//====================================================================
|
||||||
function parseInterval(str) {
|
|
||||||
if (str == null || str === "null" || str === "false" || str === "") {
|
function parseInterval(str) {
|
||||||
return null;
|
if (str == undefined) {
|
||||||
} else if (str.lastIndexOf("ms") === str.length - 2) {
|
return undefined
|
||||||
return parseFloat(str.substr(0, str.length - 2));
|
}
|
||||||
} else if (str.lastIndexOf("s") === str.length - 1) {
|
if (str.slice(-2) == "ms") {
|
||||||
return parseFloat(str.substr(0, str.length - 1)) * 1000;
|
return parseFloat(str.slice(0,-2)) || undefined
|
||||||
} else {
|
}
|
||||||
return parseFloat(str);
|
if (str.slice(-1) == "s") {
|
||||||
}
|
return (parseFloat(str.slice(0,-1)) * 1000) || undefined
|
||||||
|
}
|
||||||
|
return parseFloat(str) || undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRawAttribute(elt, name) {
|
function getRawAttribute(elt, name) {
|
||||||
|
@ -20,4 +20,18 @@ describe("Core htmx internals Tests", function() {
|
|||||||
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
|
// 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