85 lines
2.0 KiB
Plaintext
85 lines
2.0 KiB
Plaintext
// utils.rhai - Utility functions for the calendar backend
|
|
|
|
// Function to find an item by ID in an array
|
|
fn find_by_id(array, id) {
|
|
for item in array {
|
|
if item.id == id {
|
|
return item;
|
|
}
|
|
}
|
|
return #{}; // Return empty object if not found
|
|
}
|
|
|
|
// Function to filter array by a property value
|
|
fn filter_by_property(array, property, value) {
|
|
let result = [];
|
|
|
|
for item in array {
|
|
if item[property] == value {
|
|
result.push(item);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// Function to check if an object is empty
|
|
fn is_empty(obj) {
|
|
if obj.type_of() == "object" {
|
|
return obj.keys().len() == 0;
|
|
}
|
|
if obj.type_of() == "array" {
|
|
return obj.len() == 0;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Function to format date string
|
|
fn format_date(date_str) {
|
|
// Simple formatting - in a real app, we would use proper date formatting
|
|
return date_str.substr(0, 10);
|
|
}
|
|
|
|
// Function to format time string
|
|
fn format_time(date_str) {
|
|
// Simple formatting - in a real app, we would use proper time formatting
|
|
if date_str.len() >= 16 {
|
|
return date_str.substr(11, 5);
|
|
}
|
|
return "";
|
|
}
|
|
|
|
// Function to check if an array contains a value
|
|
fn array_contains(array, value) {
|
|
for item in array {
|
|
if item == value {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Function to get current date (simplified for demo)
|
|
fn get_current_date() {
|
|
return "2025-04-03";
|
|
}
|
|
|
|
// Function to calculate a future date (simplified for demo)
|
|
fn add_days_to_date(date_str, days) {
|
|
let year = date_str.substr(0, 4).parse_int();
|
|
let month = date_str.substr(5, 2).parse_int();
|
|
let day = date_str.substr(8, 2).parse_int() + days;
|
|
|
|
// Very simplified date calculation - in a real app, we would handle month/year boundaries
|
|
if day > 30 {
|
|
day = day - 30;
|
|
month += 1;
|
|
}
|
|
if month > 12 {
|
|
month = 1;
|
|
year += 1;
|
|
}
|
|
|
|
return `${year}-${month.to_string().pad_left(2, '0')}-${day.to_string().pad_left(2, '0')}`;
|
|
}
|