1 /* 2 * Copyright (c) 2021 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 DISTRIBUTEDDATAMGR_APPDATAMGR_JSUTILS_H 17 #define DISTRIBUTEDDATAMGR_APPDATAMGR_JSUTILS_H 18 19 #include <iostream> 20 #include <map> 21 #include <string> 22 #include <vector> 23 24 #include "napi/native_api.h" 25 #include "napi/native_common.h" 26 #include "napi/native_node_api.h" 27 28 namespace OHOS { 29 namespace PreferencesJsKit { 30 class JSUtils final { 31 public: 32 static constexpr int OK = 0; 33 static constexpr int ERR = -1; 34 static constexpr int32_t DEFAULT_BUF_SIZE = 1024; 35 static constexpr int32_t BUF_CACHE_MARGIN = 4; 36 static constexpr int32_t ASYNC_RST_SIZE = 2; 37 static constexpr int32_t MAX_VALUE_LENGTH = 8 * 1024; 38 static constexpr int32_t SYNC_RESULT_ELEMNT_NUM = 2; 39 40 static int32_t Convert2Bool(napi_env env, napi_value jsBool, bool &output); 41 static int32_t Convert2Double(napi_env env, napi_value jsNum, double &output); 42 static int32_t Convert2String(napi_env env, napi_value jsStr, std::string &output); 43 44 static int32_t Convert2StrVector(napi_env env, napi_value value, std::vector<std::string> &output); 45 static int32_t Convert2BoolVector(napi_env env, napi_value value, std::vector<bool> &output); 46 static int32_t Convert2DoubleVector(napi_env env, napi_value value, std::vector<double> &output); 47 48 static int32_t Convert2JSValue(napi_env env, std::string value, napi_value &output); 49 static int32_t Convert2JSValue(napi_env env, bool value, napi_value &output); 50 static int32_t Convert2JSValue(napi_env env, double value, napi_value &output); 51 static int32_t Convert2JSStringArr(napi_env env, std::vector<std::string> value, napi_value &output); 52 static int32_t Convert2JSBoolArr(napi_env env, std::vector<bool> value, napi_value &output); 53 static int32_t Convert2JSDoubleArr(napi_env env, std::vector<double> value, napi_value &output); 54 55 static napi_value Convert2JSValue(napi_env env, int32_t value); 56 57 static bool Equals(napi_env env, napi_value value, napi_ref copy); 58 }; 59 } // namespace PreferencesJsKit 60 } // namespace OHOS 61 62 #endif // DISTRIBUTEDDATAMGR_APPDATAMGR_JSUTILS_H 63