Updating ParseInterval

- add tests
- try replacement function
This commit is contained in:
Ben Pate 2020-12-29 19:47:17 -07:00
parent 20436f243c
commit 354be034d2
2 changed files with 32 additions and 2 deletions

View File

@ -62,7 +62,7 @@ return (function () {
//====================================================================
// Utilities
//====================================================================
function parseInterval(str) {
/*function parseInterval(str) {
if (str == null || str === "null" || str === "false" || str === "") {
return null;
} else if (str.lastIndexOf("ms") === str.length - 2) {
@ -72,7 +72,21 @@ return (function () {
} 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) {
return elt.getAttribute && elt.getAttribute(name);

View File

@ -1,3 +1,5 @@
const { should } = require("chai");
describe("Core htmx internals Tests", function() {
it("makeFragment works with janky stuff", function(){
@ -20,4 +22,18 @@ describe("Core htmx internals Tests", function() {
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
})
it("handles parseInterval correctly", function() {
htmx._("parseInterval")("1ms").should.equal(1);
htmx._("parseInterval")("300ms").should.equal(300);
htmx._("parseInterval")("1s").should.equal(1000)
htmx._("parseInterval")("1.5s").should.equal(1500)
htmx._("parseInterval")("2s").should.equal(2000)
/* should(htmx.parseInterval(null)).be.undefined
should(htmx.parseInterval("")).be.undefined
should(htmx.parseInterval("false")).be.undefined
should(htmx.parseInterval("true")).be.undefined
*/
})
});