Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

3.7 KB
Raw Blame
/*
 * Copyright 2025, OpenRemote Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import type { Frequency } from "rrule";
import type { ByRRulePartsKeys } from "./types";

export const FREQUENCIES = {
  YEARLY: "rrule.frequency.YEARLY",
  MONTHLY: "rrule.frequency.MONTHLY",
  WEEKLY: "rrule.frequency.WEEKLY",
  DAILY: "rrule.frequency.DAILY",
  HOURLY: "rrule.frequency.HOURLY",
  MINUTELY: "rrule.frequency.MINUTELY",
  SECONDLY: "rrule.frequency.SECONDLY",
} as const satisfies Record<keyof typeof Frequency, string>;

/**
 * Evaluation order in RFC 5545 (Page 44): BYMONTH, BYWEEKNO, BYYEARDAY, BYMONTHDAY, BYDAY, BYHOUR, BYMINUTE and BYSECOND.
 * @link https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.10
 */
export const BY_RRULE_PARTS = [
  "bymonth",
  "byweekno",
  "byyearday",
  "bymonthday",
  "byweekday",
  "byhour",
  "byminute",
  "bysecond",
] as const;

type ByRRuleCombination = Record<keyof typeof Frequency, ByRRulePartsKeys[]>;

/**
 * Strictly prohibited by RFC 5545 (Page 44).
 * These combinations are invalid according to the specification.
 * @link https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.10
 */
export const RFC_STRICT_NOT_APPLICABLE = {
  SECONDLY: ["byweekno"],
  MINUTELY: ["byweekno"],
  HOURLY: ["byweekno"],
  DAILY: ["byweekno", "byyearday"],
  WEEKLY: ["byweekno", "byyearday", "bymonthday"],
  MONTHLY: ["byweekno", "byyearday"],
  YEARLY: [],
} as const satisfies ByRRuleCombination;

const COMMONLY_NOT_APPLICABLE = ["bymonth", "byweekno", "byyearday", "byhour", "byminute", "bysecond"] as const;
/**
 * Merged rules: RFC prohibitions + non-required/unintuitive parts.
 * Use this to simplify UI options for end-users.
 */
export const INTUITIVE_NOT_APPLICABLE = {
  SECONDLY: ["bymonthday", "byweekday", ...COMMONLY_NOT_APPLICABLE, ...RFC_STRICT_NOT_APPLICABLE.SECONDLY],
  MINUTELY: ["bymonthday", "byweekday", ...COMMONLY_NOT_APPLICABLE, ...RFC_STRICT_NOT_APPLICABLE.MINUTELY],
  HOURLY: ["bymonthday", "byweekday", ...COMMONLY_NOT_APPLICABLE, ...RFC_STRICT_NOT_APPLICABLE.HOURLY],
  DAILY: ["bymonthday", "byweekday", ...COMMONLY_NOT_APPLICABLE, ...RFC_STRICT_NOT_APPLICABLE.DAILY],
  WEEKLY: ["bymonthday", ...COMMONLY_NOT_APPLICABLE, ...RFC_STRICT_NOT_APPLICABLE.WEEKLY],
  MONTHLY: ["byweekday", ...COMMONLY_NOT_APPLICABLE, ...RFC_STRICT_NOT_APPLICABLE.MONTHLY],
  YEARLY: ["bymonthday", "byweekday", ...COMMONLY_NOT_APPLICABLE, ...RFC_STRICT_NOT_APPLICABLE.YEARLY],
} as const satisfies ByRRuleCombination;

export const WEEKDAYS = {
  MO: "monday",
  TU: "tuesday",
  WE: "wednesday",
  TH: "thursday",
  FR: "friday",
  SA: "saturday",
  SU: "sunday",
} as const;

export const MONTHS = {
  "1": "january",
  "2": "february",
  "3": "march",
  "4": "april",
  "5": "may",
  "6": "june",
  "7": "july",
  "8": "august",
  "9": "september",
  "10": "october",
  "11": "november",
  "12": "december",
} as const;

export enum EventTypes {
  default = "default",
  period = "period",
  recurrence = "recurrence",
}

export const RRULE_ENDS = {
  never: "never",
  until: "schedule.ends.until",
  count: "schedule.ends.count",
} as const;