• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef JS_UTILS_H
16 #define JS_UTILS_H
17 
18 #include <map>
19 #include <set>
20 #include <vector>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 
25 #include "i18n_hilog.h"
26 
27 namespace OHOS {
28 namespace Global {
29 namespace I18n {
30 class JSUtils {
31 public:
32     static napi_value DefaultConstructor(napi_env env, napi_callback_info info);
33     static napi_value CreateEmptyString(napi_env env);
34     static napi_value CreateEmptyArray(napi_env env);
35     static napi_value CreateEmptyObject(napi_env env);
36 
37     static void GetNumberOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map);
38     static int64_t GetIntegerOptionValue(napi_env env, napi_value options, const std::string &optionName,
39         std::map<std::string, std::string> &map);
40 
41     static void SetOptionProperties(napi_env env, napi_value &result,
42         std::map<std::string, std::string> &options, const std::string &option);
43     static void SetBooleanOptionProperties(napi_env env, napi_value &result,
44         std::map<std::string, std::string> &options, const std::string &option);
45     static void SetIntegerOptionProperties(napi_env env, napi_value &result,
46         std::map<std::string, std::string> &options, const std::string &option);
47     static void SetNamedStringProperties(napi_env env, napi_value &result, const std::string &key,
48         const std::string &value);
49     static void SetNamedIntegerProperties(napi_env env, napi_value &result, const std::string &key, int64_t value);
50     static void SetNamedVectorProperties(napi_env env, napi_value &result, const std::string &key,
51         const std::vector<std::string> &value);
52     static std::string GetString(napi_env env, napi_value value, int32_t& code);
53     static std::string GetBigIntStr(napi_env env, napi_value value, int32_t& code);
54     static std::vector<std::string> GetStringArray(napi_env env, napi_value value, int32_t& code);
55     static std::vector<std::string> GetLocaleArray(napi_env env, napi_value value, int32_t& code);
56     static napi_value CreateObject(napi_env env, std::unordered_map<std::string, napi_value>& propertys,
57         int32_t& code);
58     static napi_value CreateArrayItem(napi_env env, std::unordered_map<std::string, napi_value>& propertys,
59         int32_t& code, std::vector<std::string> &keySequence);
60     static napi_value CreateArray(napi_env env, const std::vector<std::string>& value);
61     static napi_value CreateString(napi_env env, const std::string &str);
62     static bool GetPropertyFormObject(napi_env env, napi_value object, const std::string& property,
63         napi_valuetype type, std::string& value);
64     static napi_value GetNapiPropertyFormObject(napi_env env, napi_value object, const std::string& property,
65         int32_t& errCode);
66     static double GetNumberValue(napi_env env, napi_value param, int32_t &errorCode);
67     static double GetDoubleFromNapiValue(napi_env env, napi_value param, napi_valuetype &valueType, int32_t &errorCode);
68 
69     // std::map and std::unordered_map
70     template<typename T>
GetOptionValue(napi_env env,napi_value options,const std::string & optionName,T & map)71     static void GetOptionValue(napi_env env, napi_value options, const std::string &optionName, T &map)
72     {
73         napi_value optionValue = nullptr;
74         napi_valuetype type = napi_undefined;
75         napi_status status = napi_typeof(env, options, &type);
76         if (status != napi_ok || type != napi_object) {
77             HILOG_ERROR_I18N("Get option failed, option is not an object");
78             return;
79         }
80         bool hasProperty = false;
81         status = napi_has_named_property(env, options, optionName.c_str(), &hasProperty);
82         if (status != napi_ok) {
83             HILOG_ERROR_I18N("GetOptionValue: Has not named property:%{public}s", optionName.c_str());
84             return;
85         }
86         if (!hasProperty) {
87             return;
88         }
89         status = napi_get_named_property(env, options, optionName.c_str(), &optionValue);
90         if (status != napi_ok) {
91             HILOG_ERROR_I18N("GetOptionValue: Get named property for %{public}s failed.", optionName.c_str());
92             return;
93         }
94         size_t len = 0;
95         status = napi_get_value_string_utf8(env, optionValue, nullptr, 0, &len);
96         if (status != napi_ok) {
97             HILOG_ERROR_I18N("GetOptionValue: get string length failed");
98             return;
99         }
100 
101         std::vector<char> optionBuf(len + 1);
102         status = napi_get_value_string_utf8(env, optionValue, optionBuf.data(), len + 1, &len);
103         if (status != napi_ok) {
104             HILOG_ERROR_I18N("GetOptionValue: get string value failed");
105             return;
106         }
107         auto ret = map.insert(make_pair(optionName, optionBuf.data()));
108         if (!ret.second) {
109             HILOG_ERROR_I18N("GetOptionValue: map insert failed");
110         }
111     }
112 
113     template<typename T>
GetBoolOptionValue(napi_env env,napi_value options,const std::string & optionName,T & map)114     static void GetBoolOptionValue(napi_env env, napi_value options, const std::string &optionName, T &map)
115     {
116         napi_value optionValue = nullptr;
117         napi_valuetype type = napi_undefined;
118         napi_status status = napi_typeof(env, options, &type);
119         if (status != napi_ok || type != napi_object) {
120             HILOG_ERROR_I18N("GetBoolOptionValue: Set option failed, option is not an object");
121             return;
122         }
123         bool hasProperty = false;
124         status = napi_has_named_property(env, options, optionName.c_str(), &hasProperty);
125         if (status != napi_ok) {
126             HILOG_ERROR_I18N("GetBoolOptionValue: Has not named property.");
127             return;
128         }
129         if (!hasProperty) {
130             return;
131         }
132         status = napi_get_named_property(env, options, optionName.c_str(), &optionValue);
133         if (status != napi_ok) {
134             HILOG_ERROR_I18N("GetBoolOptionValue: Get named property for %{public}s failed.", optionName.c_str());
135             return;
136         }
137         bool boolValue = false;
138         status = napi_get_value_bool(env, optionValue, &boolValue);
139         if (status != napi_ok) {
140             HILOG_ERROR_I18N("GetBoolOptionValue: Get bool value failed.");
141             return;
142         }
143         std::string value = boolValue ? "true" : "false";
144         auto ret = map.insert(make_pair(optionName, value));
145         if (!ret.second) {
146             HILOG_ERROR_I18N("GetBoolOptionValue: map insert failed");
147         }
148     }
149 
150     template<typename T>
GetDoubleOptionValue(napi_env env,napi_value options,const std::string & optionName,T & map)151     static void GetDoubleOptionValue(napi_env env, napi_value options, const std::string &optionName, T &map)
152     {
153         napi_value optionValue = nullptr;
154         napi_valuetype type = napi_undefined;
155         napi_status status = napi_typeof(env, options, &type);
156         if (status != napi_ok || type != napi_object) {
157             HILOG_ERROR_I18N("GetDoubleOptionValue: Get option failed, option is not an object.");
158             return;
159         }
160         bool hasProperty = false;
161         status = napi_has_named_property(env, options, optionName.c_str(), &hasProperty);
162         if (status != napi_ok || !hasProperty) {
163             HILOG_INFO_I18N("GetDoubleOptionValue: No named %{public}s param.", optionName.c_str());
164             return;
165         }
166         status = napi_get_named_property(env, options, optionName.c_str(), &optionValue);
167         if (status != napi_ok) {
168             HILOG_ERROR_I18N("GetDoubleOptionValue: get named %{public}s param failed.", optionName.c_str());
169             return;
170         }
171         double option;
172         status = napi_get_value_double(env, optionValue, &option);
173         if (status != napi_ok) {
174             HILOG_ERROR_I18N("GetDoubleOptionValue: param %{public}s get double value failed.", optionName.c_str());
175             return;
176         }
177         try {
178             map.insert(make_pair(optionName, std::to_string(option)));
179         } catch (const std::bad_alloc& except) {
180             HILOG_ERROR_I18N("GetDoubleOptionValue: double to string failed.");
181             return;
182         }
183     }
184 };
185 } // namespace I18n
186 } // namespace Global
187 } // namespace OHOS
188 #endif