1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_GLOBAL_I18N_JS_RELATIVE_TIME_FORMAT_ADDON_H 17 #define OHOS_GLOBAL_I18N_JS_RELATIVE_TIME_FORMAT_ADDON_H 18 19 #include <memory> 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 #include <unordered_map> 24 #include <vector> 25 26 #include "relative_time_format.h" 27 28 namespace OHOS { 29 namespace Global { 30 namespace I18n { 31 class JSRelativeTimeFormatAddon { 32 public: 33 JSRelativeTimeFormatAddon(); 34 ~JSRelativeTimeFormatAddon(); 35 static void Destructor(napi_env env, void *nativeObject, void *hint); 36 static napi_value InitJsRelativeTimeFormat(napi_env env, napi_value exports); 37 38 private: 39 static napi_value RelativeTimeFormatConstructor(napi_env env, napi_callback_info info); 40 static napi_value Format(napi_env env, napi_callback_info info); 41 static napi_value GetNumberResolvedOptions(napi_env env, napi_callback_info info); 42 static napi_value FormatToParts(napi_env env, napi_callback_info info); 43 static napi_value SupportedLocalesOf(napi_env env, napi_callback_info info); 44 static napi_value GetFormatToPartsInner(napi_env env, double number, const std::string &unit, 45 napi_value &thisVar); 46 static napi_value SetNumberFormatParts(napi_env env, 47 const std::vector<std::vector<std::string>> &numberParts); 48 static void GetRelativeTimeOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map); 49 static bool ThrowRangeError(napi_env env); 50 static void CheckOptionValue(napi_env env, std::map<std::string, std::string> &map, const std::string &optionName); 51 static void CheckOptionValue(napi_env env, const std::string &optionValue, const std::string &optionName); 52 53 bool GetParameterLocales(napi_env env, std::vector<std::string> &localeTags, 54 const size_t &argc, napi_value* argv); 55 bool InitRelativeTimeFormatContext(napi_env env, napi_callback_info info, 56 std::vector<std::string> localeTags, std::map<std::string, std::string> &map); 57 58 static std::unordered_map<std::string, std::vector<std::string>> optionAvaliableMap; 59 std::shared_ptr<RelativeTimeFormat> relativetimefmt_ = nullptr; 60 }; 61 } // namespace I18n 62 } // namespace Global 63 } // namespace OHOS 64 #endif