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 AppDataMgrJsKit { 30 class JSUtils final { 31 public: 32 static constexpr int32_t DEFAULT_BUF_SIZE = 1024; 33 static constexpr int32_t BUF_CACHE_MARGIN = 4; 34 static constexpr int32_t ASYNC_RST_SIZE = 2; 35 static constexpr int32_t SYNC_RESULT_ELEMNT_NUM = 2; 36 37 static std::string Convert2String(napi_env env, napi_value jsStr, bool useDefaultBufSize = true); 38 static std::vector<std::string> Convert2StrVector(napi_env env, napi_value value); 39 static std::vector<uint8_t> Convert2U8Vector(napi_env env, napi_value jsValue); 40 static std::string ConvertAny2String(napi_env env, const napi_value jsValue); 41 42 static napi_value Convert2JSValue(napi_env env, const std::vector<std::string> &value); 43 static napi_value Convert2JSValue(napi_env env, const std::string &value); 44 static napi_value Convert2JSValue(napi_env env, const std::vector<uint8_t> &value); 45 static napi_value Convert2JSValue(napi_env env, int32_t value); 46 static napi_value Convert2JSValue(napi_env env, int64_t value); 47 static napi_value Convert2JSValue(napi_env env, double value); 48 static napi_value Convert2JSValue(napi_env env, bool value); 49 static napi_value Convert2JSValue(napi_env env, const std::map<std::string, int>& value); 50 }; 51 } // namespace AppDataMgrJsKit 52 } // namespace OHOS 53 54 #endif // DISTRIBUTEDDATAMGR_APPDATAMGR_JSUTILS_H 55