Skip to content

Commit d9fcfac

Browse files
committed
feat(core utils): Add is_iso_date_time method to check for valid ISO 8601 date/time strings.
1 parent 07f9133 commit d9fcfac

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/core/utils.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ const ensureArray = (it, force_array) => {
576576

577577
const localized_isodate = (date) => {
578578
// Return a iso date (date only) in the current timezone instead of a
579-
// UTC ISO 8602 date+time component which toISOString returns.
579+
// UTC ISO 8601 date+time component which toISOString returns.
580580

581581
const day = date.getDate().toString().padStart(2, "0");
582582
const month = (date.getMonth() + 1).toString().padStart(2, "0");
@@ -628,6 +628,20 @@ const unescape_html = (escaped_html) => {
628628
.replace(/"/g, '"');
629629
};
630630

631+
/**
632+
* Return true, if the given value is a valid ISO 8601 date/time string with or without an optional time component.
633+
*
634+
* @param {String} value - The date/time value to be checked.
635+
* @param {Boolean} [optional_time=false] - True, if time component is optional.
636+
* @return {Boolean} - True, if the given value is a valid Date string. False if not.
637+
*/
638+
const is_iso_date_time = (value, optional_time = false) => {
639+
const re_date_time = optional_time
640+
? /^\d{4}-[01]\d-[0-3]\d(T[0-2]\d:[0-5]\d)?$/
641+
: /^\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d$/;
642+
return re_date_time.test(value);
643+
};
644+
631645
var utils = {
632646
// pattern pimping - own module?
633647
jqueryPlugin: jqueryPlugin,
@@ -658,6 +672,7 @@ var utils = {
658672
localized_isodate: localized_isodate,
659673
escape_html: escape_html,
660674
unescape_html: unescape_html,
675+
is_iso_date_time: is_iso_date_time,
661676
getCSSValue: dom.get_css_value, // BBB: moved to dom. TODO: Remove in upcoming version.
662677
};
663678

src/core/utils.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,3 +721,31 @@ describe("escape/unescape html ...", function () {
721721
expect(utils.unescape_html(undefined)).toBe("");
722722
});
723723
});
724+
725+
describe("is_iso_date_time ...", function () {
726+
it("detects valid date/time objects", () => {
727+
expect(utils.is_iso_date_time("2022-05-04T21:00")).toBe(true);
728+
729+
// We actually do not strictly check for a valid datetime, just if the
730+
// format is correct.
731+
expect(utils.is_iso_date_time("2222-19-39T29:59")).toBe(true);
732+
733+
// But some basic constraints are in place
734+
expect(utils.is_iso_date_time("2222-20-40T30:60")).toBe(false);
735+
736+
// And this is for sure no valid date/time
737+
expect(utils.is_iso_date_time("not2-ok-40T30:60")).toBe(false);
738+
739+
// Also, the time component cannot be left out
740+
expect(utils.is_iso_date_time("2022-05-04")).toBe(false);
741+
742+
// Not even partially.
743+
expect(utils.is_iso_date_time("2022-05-04T21")).toBe(false);
744+
745+
// Unless we set optional_time to true.
746+
expect(utils.is_iso_date_time("2022-05-04", true)).toBe(true);
747+
748+
// But still, partial time component does not pass.
749+
expect(utils.is_iso_date_time("2022-05-04T21", true)).toBe(false);
750+
});
751+
});

0 commit comments

Comments
 (0)