-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds DateCompare utility for use by Fetch and Import for cache durations
- Loading branch information
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
const TemplatePath = require("./src/TemplatePath.js"); | ||
const isPlainObject = require("./src/IsPlainObject.js"); | ||
const Merge = require("./src/Merge.js"); | ||
const DateCompare = require("./src/DateCompare.js"); | ||
const { DeepCopy } = Merge; | ||
|
||
module.exports = { | ||
TemplatePath, | ||
isPlainObject, | ||
Merge, | ||
DeepCopy, | ||
DateCompare, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class DateCompare { | ||
static isTimestampWithinDuration(timestamp, duration, compareDate = Date.now()) { | ||
// in the cache and no duration | ||
if (!duration || duration === "*" || duration === Infinity) { | ||
// no duration specified (plugin default is 1d, but if this is falsy assume infinite) | ||
// "*" is infinite duration | ||
return true; | ||
} | ||
|
||
let expiration = timestamp + this.getDurationMs(duration); | ||
|
||
// still valid | ||
if (expiration > compareDate) { | ||
return true; | ||
} | ||
|
||
// expired | ||
return false; | ||
} | ||
|
||
static getDurationMs(duration = "0s") { | ||
let durationUnits = duration.slice(-1); | ||
let durationMultiplier; | ||
if (durationUnits === "s") { | ||
durationMultiplier = 1; | ||
} else if (durationUnits === "m") { | ||
durationMultiplier = 60; | ||
} else if (durationUnits === "h") { | ||
durationMultiplier = 60 * 60; | ||
} else if (durationUnits === "d") { | ||
durationMultiplier = 60 * 60 * 24; | ||
} else if (durationUnits === "w") { | ||
durationMultiplier = 60 * 60 * 24 * 7; | ||
} else if (durationUnits === "y") { | ||
durationMultiplier = 60 * 60 * 24 * 365; | ||
} | ||
|
||
let durationValue = parseInt(duration.slice(0, duration.length - 1), 10); | ||
return durationValue * durationMultiplier * 1000; | ||
} | ||
} | ||
|
||
module.exports = DateCompare; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const assert = require("node:assert/strict") | ||
const test = require("node:test"); | ||
|
||
const DateCompare = require("../src/DateCompare.js"); | ||
|
||
test("Basic usage empty duration is the same as infinite duration", (t) => { | ||
let jan1 = Date.UTC(2024,0,1); | ||
assert.equal(DateCompare.isTimestampWithinDuration(jan1), true); | ||
assert.equal(DateCompare.isTimestampWithinDuration(Date.now()), true); | ||
assert.equal(DateCompare.isTimestampWithinDuration(jan1, Infinity), true); | ||
assert.equal(DateCompare.isTimestampWithinDuration(Date.now(), Infinity), true); | ||
}); | ||
|
||
test("Basic usage false, now", (t) => { | ||
assert.equal(DateCompare.isTimestampWithinDuration(Date.now(), "0s"), false); | ||
assert.equal(DateCompare.isTimestampWithinDuration(Date.now(), "0s", Date.now()), false); | ||
}); | ||
|
||
test("Basic usage false, old date", (t) => { | ||
let jan1 = Date.UTC(2024,0,1); | ||
assert.equal(DateCompare.isTimestampWithinDuration(jan1, "24h"), false); | ||
assert.equal(DateCompare.isTimestampWithinDuration(jan1, "24h", Date.now()), false); | ||
}); | ||
|
||
test("Basic usage true, now", (t) => { | ||
assert.equal(DateCompare.isTimestampWithinDuration(Date.now(), "1s"), true); | ||
}); | ||
|
||
test("Basic usage true, old date", (t) => { | ||
let jan1 = Date.UTC(2024,0,1); | ||
let jan2 = Date.UTC(2024,0,2); | ||
assert.equal(DateCompare.isTimestampWithinDuration(jan1, "25h", jan2), true); | ||
}); | ||
|
||
test("Basic usage equality is false, needs to be > not >=", (t) => { | ||
let jan1 = Date.UTC(2024,0,1); | ||
let jan2 = Date.UTC(2024,0,2); | ||
assert.equal(DateCompare.isTimestampWithinDuration(jan1, "24h", jan2), false); | ||
}); |