js_api.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "smartbotic/runner/js_api.hpp"
  2. #include <chrono>
  3. #include <ctime>
  4. #include <iomanip>
  5. #include <random>
  6. #include <sstream>
  7. namespace smartbotic::runner {
  8. auto GetJsApiCode() -> std::string {
  9. // This returns the JavaScript API code that's injected into the runtime
  10. // Most of the API is already set up in JsEngine::InitializeBuiltins()
  11. // This function provides additional utilities if needed
  12. return R"(
  13. // Additional API extensions can be added here
  14. // The core API (db, ctx, utils) is set up in the native code
  15. )";
  16. }
  17. auto FormatDate(int64_t timestamp, const std::string& format) -> std::string {
  18. std::time_t time = static_cast<std::time_t>(timestamp);
  19. std::tm tm_time{};
  20. #ifdef _WIN32
  21. localtime_s(&tm_time, &time);
  22. #else
  23. localtime_r(&time, &tm_time);
  24. #endif
  25. std::ostringstream oss;
  26. std::string fmt = format;
  27. // Simple replacements
  28. auto replace = [&](const std::string& from, const std::string& to) {
  29. size_t pos = 0;
  30. while ((pos = fmt.find(from, pos)) != std::string::npos) {
  31. fmt.replace(pos, from.length(), to);
  32. pos += to.length();
  33. }
  34. };
  35. std::ostringstream year_ss;
  36. year_ss << std::setw(4) << std::setfill('0') << (tm_time.tm_year + 1900);
  37. replace("YYYY", year_ss.str());
  38. std::ostringstream month_ss;
  39. month_ss << std::setw(2) << std::setfill('0') << (tm_time.tm_mon + 1);
  40. replace("MM", month_ss.str());
  41. std::ostringstream day_ss;
  42. day_ss << std::setw(2) << std::setfill('0') << tm_time.tm_mday;
  43. replace("DD", day_ss.str());
  44. std::ostringstream hour_ss;
  45. hour_ss << std::setw(2) << std::setfill('0') << tm_time.tm_hour;
  46. replace("HH", hour_ss.str());
  47. std::ostringstream min_ss;
  48. min_ss << std::setw(2) << std::setfill('0') << tm_time.tm_min;
  49. replace("mm", min_ss.str());
  50. std::ostringstream sec_ss;
  51. sec_ss << std::setw(2) << std::setfill('0') << tm_time.tm_sec;
  52. replace("ss", sec_ss.str());
  53. return fmt;
  54. }
  55. auto FormatNumber(double num, int decimals, bool thousands_sep) -> std::string {
  56. std::ostringstream oss;
  57. oss << std::fixed << std::setprecision(decimals) << num;
  58. std::string result = oss.str();
  59. if (thousands_sep) {
  60. // Find decimal point
  61. auto dot_pos = result.find('.');
  62. std::string integer_part = (dot_pos != std::string::npos)
  63. ? result.substr(0, dot_pos)
  64. : result;
  65. std::string decimal_part = (dot_pos != std::string::npos)
  66. ? result.substr(dot_pos)
  67. : "";
  68. // Insert thousands separators
  69. std::string formatted;
  70. int count = 0;
  71. for (auto it = integer_part.rbegin(); it != integer_part.rend(); ++it) {
  72. if (count > 0 && count % 3 == 0 && *it != '-') {
  73. formatted = ',' + formatted;
  74. }
  75. formatted = *it + formatted;
  76. count++;
  77. }
  78. result = formatted + decimal_part;
  79. }
  80. return result;
  81. }
  82. auto Truncate(const std::string& str, size_t max_length) -> std::string {
  83. if (str.length() <= max_length) {
  84. return str;
  85. }
  86. if (max_length <= 3) {
  87. return str.substr(0, max_length);
  88. }
  89. return str.substr(0, max_length - 3) + "...";
  90. }
  91. auto Capitalize(const std::string& str) -> std::string {
  92. if (str.empty()) {
  93. return str;
  94. }
  95. std::string result = str;
  96. result[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(result[0])));
  97. return result;
  98. }
  99. auto GenerateUuid() -> std::string {
  100. static std::random_device rd;
  101. static std::mt19937_64 gen(rd());
  102. static std::uniform_int_distribution<uint64_t> dis;
  103. uint64_t ab = dis(gen);
  104. uint64_t cd = dis(gen);
  105. // Set version to 4 and variant to 2
  106. ab = (ab & 0xFFFFFFFFFFFF0FFFULL) | 0x0000000000004000ULL;
  107. cd = (cd & 0x3FFFFFFFFFFFFFFFULL) | 0x8000000000000000ULL;
  108. char buf[37];
  109. snprintf(buf, sizeof(buf),
  110. "%08x-%04x-%04x-%04x-%012llx",
  111. static_cast<uint32_t>(ab >> 32),
  112. static_cast<uint16_t>((ab >> 16) & 0xFFFF),
  113. static_cast<uint16_t>(ab & 0xFFFF),
  114. static_cast<uint16_t>(cd >> 48),
  115. static_cast<unsigned long long>(cd & 0x0000FFFFFFFFFFFFULL));
  116. return std::string(buf);
  117. }
  118. } // namespace smartbotic::runner