#include "smartbotic/runner/js_api.hpp" #include #include #include #include #include namespace smartbotic::runner { auto GetJsApiCode() -> std::string { // This returns the JavaScript API code that's injected into the runtime // Most of the API is already set up in JsEngine::InitializeBuiltins() // This function provides additional utilities if needed return R"( // Additional API extensions can be added here // The core API (db, ctx, utils) is set up in the native code )"; } auto FormatDate(int64_t timestamp, const std::string& format) -> std::string { std::time_t time = static_cast(timestamp); std::tm tm_time{}; #ifdef _WIN32 localtime_s(&tm_time, &time); #else localtime_r(&time, &tm_time); #endif std::ostringstream oss; std::string fmt = format; // Simple replacements auto replace = [&](const std::string& from, const std::string& to) { size_t pos = 0; while ((pos = fmt.find(from, pos)) != std::string::npos) { fmt.replace(pos, from.length(), to); pos += to.length(); } }; std::ostringstream year_ss; year_ss << std::setw(4) << std::setfill('0') << (tm_time.tm_year + 1900); replace("YYYY", year_ss.str()); std::ostringstream month_ss; month_ss << std::setw(2) << std::setfill('0') << (tm_time.tm_mon + 1); replace("MM", month_ss.str()); std::ostringstream day_ss; day_ss << std::setw(2) << std::setfill('0') << tm_time.tm_mday; replace("DD", day_ss.str()); std::ostringstream hour_ss; hour_ss << std::setw(2) << std::setfill('0') << tm_time.tm_hour; replace("HH", hour_ss.str()); std::ostringstream min_ss; min_ss << std::setw(2) << std::setfill('0') << tm_time.tm_min; replace("mm", min_ss.str()); std::ostringstream sec_ss; sec_ss << std::setw(2) << std::setfill('0') << tm_time.tm_sec; replace("ss", sec_ss.str()); return fmt; } auto FormatNumber(double num, int decimals, bool thousands_sep) -> std::string { std::ostringstream oss; oss << std::fixed << std::setprecision(decimals) << num; std::string result = oss.str(); if (thousands_sep) { // Find decimal point auto dot_pos = result.find('.'); std::string integer_part = (dot_pos != std::string::npos) ? result.substr(0, dot_pos) : result; std::string decimal_part = (dot_pos != std::string::npos) ? result.substr(dot_pos) : ""; // Insert thousands separators std::string formatted; int count = 0; for (auto it = integer_part.rbegin(); it != integer_part.rend(); ++it) { if (count > 0 && count % 3 == 0 && *it != '-') { formatted = ',' + formatted; } formatted = *it + formatted; count++; } result = formatted + decimal_part; } return result; } auto Truncate(const std::string& str, size_t max_length) -> std::string { if (str.length() <= max_length) { return str; } if (max_length <= 3) { return str.substr(0, max_length); } return str.substr(0, max_length - 3) + "..."; } auto Capitalize(const std::string& str) -> std::string { if (str.empty()) { return str; } std::string result = str; result[0] = static_cast(std::toupper(static_cast(result[0]))); return result; } auto GenerateUuid() -> std::string { static std::random_device rd; static std::mt19937_64 gen(rd()); static std::uniform_int_distribution dis; uint64_t ab = dis(gen); uint64_t cd = dis(gen); // Set version to 4 and variant to 2 ab = (ab & 0xFFFFFFFFFFFF0FFFULL) | 0x0000000000004000ULL; cd = (cd & 0x3FFFFFFFFFFFFFFFULL) | 0x8000000000000000ULL; char buf[37]; snprintf(buf, sizeof(buf), "%08x-%04x-%04x-%04x-%012llx", static_cast(ab >> 32), static_cast((ab >> 16) & 0xFFFF), static_cast(ab & 0xFFFF), static_cast(cd >> 48), static_cast(cd & 0x0000FFFFFFFFFFFFULL)); return std::string(buf); } } // namespace smartbotic::runner