From 354be034d299fcad63338badc4549c8c2fbf0bba Mon Sep 17 00:00:00 2001 From: Ben Pate Date: Tue, 29 Dec 2020 19:47:17 -0700 Subject: [PATCH] Updating ParseInterval - add tests - try replacement function --- src/htmx.js | 18 ++++++++++++++++-- test/core/internals.js | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/htmx.js b/src/htmx.js index 7bdfc0ee..255bbe78 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -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); diff --git a/test/core/internals.js b/test/core/internals.js index 5089daf5..c3ba82b7 100644 --- a/test/core/internals.js +++ b/test/core/internals.js @@ -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 +*/ + }) + }); \ No newline at end of file