| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- #include "smartbotic/runner/js_api.hpp"
- #include <chrono>
- #include <ctime>
- #include <iomanip>
- #include <random>
- #include <sstream>
- 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<std::time_t>(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<char>(std::toupper(static_cast<unsigned char>(result[0])));
- return result;
- }
- auto GenerateUuid() -> std::string {
- static std::random_device rd;
- static std::mt19937_64 gen(rd());
- static std::uniform_int_distribution<uint64_t> 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<uint32_t>(ab >> 32),
- static_cast<uint16_t>((ab >> 16) & 0xFFFF),
- static_cast<uint16_t>(ab & 0xFFFF),
- static_cast<uint16_t>(cd >> 48),
- static_cast<unsigned long long>(cd & 0x0000FFFFFFFFFFFFULL));
- return std::string(buf);
- }
- } // namespace smartbotic::runner
|