• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "js_number_format_addon.h"
16 
17 #include <cmath>
18 #include "error_util.h"
19 #include "i18n_hilog.h"
20 #include "js_utils.h"
21 #include "locale_helper.h"
22 #include "utils.h"
23 
24 namespace OHOS {
25 namespace Global {
26 namespace I18n {
27 static thread_local napi_ref g_JsNumberFormatConstructor = nullptr;
28 
JSNumberFormatAddon()29 JSNumberFormatAddon::JSNumberFormatAddon()
30 {
31 }
32 
~JSNumberFormatAddon()33 JSNumberFormatAddon::~JSNumberFormatAddon()
34 {
35 }
36 
Destructor(napi_env env,void * nativeObject,void * hint)37 void JSNumberFormatAddon::Destructor(napi_env env, void *nativeObject, void *hint)
38 {
39     if (!nativeObject) {
40         return;
41     }
42     delete reinterpret_cast<JSNumberFormatAddon *>(nativeObject);
43     nativeObject = nullptr;
44 }
45 
InitJsNumberFormat(napi_env env,napi_value exports)46 napi_value JSNumberFormatAddon::InitJsNumberFormat(napi_env env, napi_value exports)
47 {
48     napi_property_descriptor properties[] = {
49         DECLARE_NAPI_FUNCTION("format", FormatNumber),
50         DECLARE_NAPI_FUNCTION("formatToParts", FormatToParts),
51         DECLARE_NAPI_FUNCTION("formatRange", FormatRangeNumber),
52         DECLARE_NAPI_FUNCTION("formatRangeToParts", FormatRangeToParts),
53         DECLARE_NAPI_FUNCTION("resolvedOptions", GetNumberResolvedOptions),
54         DECLARE_NAPI_STATIC_FUNCTION("supportedLocalesOf", SupportedLocalesOf),
55     };
56 
57     napi_value constructor = nullptr;
58     napi_status status = napi_define_class(env, "NumberFormat", NAPI_AUTO_LENGTH, NumberFormatConstructor, nullptr,
59         sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor);
60     if (status != napi_ok) {
61         HILOG_ERROR_I18N("InitJsNumberFormat: Define class failed when InitNumberFormat");
62         return nullptr;
63     }
64     status = napi_set_named_property(env, exports, "NumberFormat", constructor);
65     if (status != napi_ok) {
66         HILOG_ERROR_I18N("InitJsNumberFormat: Set property failed when InitNumberFormat");
67         return nullptr;
68     }
69     status = napi_create_reference(env, constructor, 1, &g_JsNumberFormatConstructor);
70     if (status != napi_ok || g_JsNumberFormatConstructor == nullptr) {
71         HILOG_ERROR_I18N("InitJsNumberFormat: napi_create_reference failed");
72         return nullptr;
73     }
74     return exports;
75 }
76 
NumberFormatConstructor(napi_env env,napi_callback_info info)77 napi_value JSNumberFormatAddon::NumberFormatConstructor(napi_env env, napi_callback_info info)
78 {
79     size_t argc = 2;
80     napi_value argv[2] = { nullptr };
81     napi_value thisVar = nullptr;
82     void *data = nullptr;
83     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
84     if (status != napi_ok) {
85         return nullptr;
86     }
87     napi_value new_target;
88     status = napi_get_new_target(env, info, &new_target);
89     if (status == napi_pending_exception || new_target == nullptr) {
90         return CreateNumberFormatWithoutNew(env, argc, argv);
91     }
92     JSNumberFormatAddon* obj = new (std::nothrow) JSNumberFormatAddon();
93     if (obj == nullptr) {
94         HILOG_ERROR_I18N("NumberFormatConstructor: Create JSNumberFormatAddon object failed");
95         return nullptr;
96     }
97     std::vector<std::string> localeTags;
98     if (!obj->HandleArguments(env, argv[0], argc, localeTags)) {
99         delete obj;
100         return nullptr;
101     }
102     std::map<std::string, std::string> map = {};
103     if (argc > 1) {
104         JSUtils::GetNumberOptionValues(env, argv[1], map);
105     }
106     status =
107         napi_wrap(env, thisVar, reinterpret_cast<void *>(obj), JSNumberFormatAddon::Destructor, nullptr, nullptr);
108     if (status != napi_ok) {
109         delete obj;
110         HILOG_ERROR_I18N("NumberFormatConstructor: Wrap JSNumberFormatAddon failed");
111         return nullptr;
112     }
113     if (!obj->InitNumberFormatContext(env, localeTags, map)) {
114         HILOG_ERROR_I18N("Init NumberFormat failed");
115         return nullptr;
116     }
117     return thisVar;
118 }
119 
HandleArguments(napi_env env,napi_value argVal,size_t argc,std::vector<std::string> & localeTags)120 bool JSNumberFormatAddon::HandleArguments(napi_env env, napi_value argVal, size_t argc,
121     std::vector<std::string> &localeTags)
122 {
123     if (argc < 1) {
124         std::string defaultLocale = NumberFormat::GetIcuDefaultLocale();
125         localeTags.push_back(defaultLocale);
126         return true;
127     }
128     int32_t code = 0;
129     std::vector<std::string> localeArray = JSUtils::GetLocaleArray(env, argVal, code);
130     if (localeArray.empty()) {
131         std::string defaultLocale = NumberFormat::GetIcuDefaultLocale();
132         localeTags.push_back(defaultLocale);
133         return true;
134     }
135     std::string checkResult = LocaleHelper::CheckParamLocales(localeArray);
136     if (!checkResult.empty()) {
137         ErrorUtil::NapiThrowUndefined(env, checkResult);
138         return false;
139     }
140     localeTags.assign(localeArray.begin(), localeArray.end());
141     return true;
142 }
143 
InitNumberFormatContext(napi_env env,const std::vector<std::string> & localeTags,std::map<std::string,std::string> & map)144 bool JSNumberFormatAddon::InitNumberFormatContext(napi_env env,
145     const std::vector<std::string> &localeTags, std::map<std::string, std::string> &map)
146 {
147     int32_t code = 0;
148     std::string message = NumberFormat::CheckNumberFormatOptions(map, code);
149     if (code != 0) {
150         ErrorUtil::NapiThrowUndefined(env, message);
151         return false;
152     }
153     numberfmt_ = std::make_shared<NumberFormat>(localeTags, map, true);
154     return numberfmt_ != nullptr;
155 }
156 
CreateNumberFormatWithoutNew(napi_env env,size_t argc,napi_value * argv)157 napi_value JSNumberFormatAddon::CreateNumberFormatWithoutNew(napi_env env, size_t argc,
158     napi_value *argv)
159 {
160     napi_value exception;
161     napi_status status = napi_get_and_clear_last_exception(env, &exception);
162     if (status != napi_ok) {
163         HILOG_ERROR_I18N("CreateNumberFormatWithoutNew: napi_get_and_clear_last_exception failed");
164         return nullptr;
165     }
166     napi_value constructor = nullptr;
167     status = napi_get_reference_value(env, g_JsNumberFormatConstructor, &constructor);
168     if (status != napi_ok || constructor == nullptr) {
169         HILOG_ERROR_I18N("CreateNumberFormatWithoutNew: napi_get_reference_value failed");
170         return nullptr;
171     }
172     napi_value instance = nullptr;
173     status = napi_new_instance(env, constructor, argc, argv, &instance);
174     if (status != napi_ok) {
175         HILOG_ERROR_I18N("CreateNumberFormatWithoutNew: napi_new_instance failed");
176         return nullptr;
177     }
178     return instance;
179 }
180 
FormatNumber(napi_env env,napi_callback_info info)181 napi_value JSNumberFormatAddon::FormatNumber(napi_env env, napi_callback_info info)
182 {
183     size_t argc = 1;
184     napi_value argv[1] = { nullptr };
185     napi_value thisVar = nullptr;
186     void *data = nullptr;
187     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
188     if (status != napi_ok) {
189         HILOG_ERROR_I18N("JSNumberFormatAddon::FormatNumber: Get patameter info failed");
190         return JSUtils::CreateEmptyString(env);
191     }
192     if (argc < 1) {
193         HILOG_ERROR_I18N("JSNumberFormatAddon::FormatNumber: Param count less then required");
194         ErrorUtil::NapiThrowUndefined(env, "parameter is empty");
195         return nullptr;
196     }
197     napi_valuetype valueType = napi_valuetype::napi_undefined;
198     status = napi_typeof(env, argv[0], &valueType);
199     if (status != napi_ok) {
200         HILOG_ERROR_I18N("JSNumberFormatAddon::FormatNumber: Get parameter type failed");
201         return JSUtils::CreateEmptyString(env);
202     }
203     return FormatWithDiffParamType(env, argv[0], valueType, thisVar);
204 }
205 
FormatRangeNumber(napi_env env,napi_callback_info info)206 napi_value JSNumberFormatAddon::FormatRangeNumber(napi_env env, napi_callback_info info)
207 {
208     size_t argc = 2;
209     napi_value argv[2] = { nullptr };
210     napi_value thisVar = nullptr;
211     void *data = nullptr;
212     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
213     if (status != napi_ok) {
214         HILOG_ERROR_I18N("FormatRangeNumber: Get parameter info failed");
215         return JSUtils::CreateEmptyString(env);
216     }
217     if (argc != 2) { // 2 is parameter count
218         HILOG_ERROR_I18N("FormatRangeNumber: Insufficient parameters");
219         return JSUtils::CreateEmptyString(env);
220     }
221     int32_t errorCode = 0;
222     double start = GetFormatParam(env, argv[0], errorCode);
223     if (errorCode != 0) {
224         HILOG_ERROR_I18N("FormatRangeNumber: Get first param value failed");
225         return JSUtils::CreateEmptyString(env);
226     }
227     double end = GetFormatParam(env, argv[1], errorCode);
228     if (errorCode != 0) {
229         HILOG_ERROR_I18N("FormatRangeNumber: Get second param value failed");
230         return JSUtils::CreateEmptyString(env);
231     }
232     JSNumberFormatAddon *obj = nullptr;
233     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
234     if (status != napi_ok || !obj || !obj->numberfmt_) {
235         HILOG_ERROR_I18N("FormatRangeNumber: Get NumberFormat object failed");
236         return JSUtils::CreateEmptyString(env);
237     }
238     std::string value = obj->numberfmt_->FormatJsRange(start, end);
239     napi_value result;
240     status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
241     if (status != napi_ok) {
242         HILOG_ERROR_I18N("FormatRangeNumber: create string js variable failed.");
243         return JSUtils::CreateEmptyString(env);
244     }
245     return result;
246 }
247 
FormatRangeToParts(napi_env env,napi_callback_info info)248 napi_value JSNumberFormatAddon::FormatRangeToParts(napi_env env, napi_callback_info info)
249 {
250     size_t argc = 2;
251     napi_value argv[2] = { nullptr };
252     napi_value thisVar = nullptr;
253     void *data = nullptr;
254     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
255     if (status != napi_ok) {
256         HILOG_ERROR_I18N("FormatRangeToParts: Get parameter info failed");
257         return JSUtils::CreateEmptyArray(env);
258     }
259     if (argc != 2) { // 2 is parameter count
260         HILOG_ERROR_I18N("FormatRangeToParts: Insufficient parameters");
261         return JSUtils::CreateEmptyArray(env);
262     }
263     int32_t code = 0;
264     double start = GetFormatParam(env, argv[0], code);
265     if (code != 0) {
266         HILOG_ERROR_I18N("FormatRangeToParts: Get first param value failed");
267         return JSUtils::CreateEmptyArray(env);
268     }
269     double end = GetFormatParam(env, argv[1], code);
270     if (code != 0) {
271         HILOG_ERROR_I18N("FormatRangeToParts: Get second param value failed");
272         return JSUtils::CreateEmptyArray(env);
273     }
274     JSNumberFormatAddon *obj = nullptr;
275     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
276     if (status != napi_ok || !obj || !obj->numberfmt_) {
277         HILOG_ERROR_I18N("FormatRangeToParts: Get NumberFormat object failed");
278         return JSUtils::CreateEmptyArray(env);
279     }
280     std::vector<std::vector<std::string>> result;
281     obj->numberfmt_->FormatRangeToParts(start, end, result);
282     return SetNumberFormatParts(env, status, result);
283 }
284 
GetFormatParam(napi_env env,napi_value param,int32_t & code)285 double JSNumberFormatAddon::GetFormatParam(napi_env env, napi_value param, int32_t &code)
286 {
287     napi_valuetype valueType = napi_valuetype::napi_undefined;
288     napi_status status = napi_typeof(env, param, &valueType);
289     if (status != napi_ok) {
290         HILOG_ERROR_I18N("JSNumberFormatAddon::GetFormatParam: Get parameter type failed");
291         code = -1;
292         return 0;
293     }
294     return JSUtils::GetDoubleFromNapiValue(env, param, valueType, code);
295 }
296 
GetNumberResolvedOptions(napi_env env,napi_callback_info info)297 napi_value JSNumberFormatAddon::GetNumberResolvedOptions(napi_env env, napi_callback_info info)
298 {
299     napi_value thisVar = nullptr;
300     void *data = nullptr;
301     napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
302     if (status != napi_ok) {
303         return JSUtils::CreateEmptyObject(env);
304     }
305     JSNumberFormatAddon *obj = nullptr;
306     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
307     if (status != napi_ok || !obj || !obj->numberfmt_) {
308         HILOG_ERROR_I18N("GetNumberResolvedOptions: Get NumberFormat object failed");
309         return JSUtils::CreateEmptyObject(env);
310     }
311     napi_value result = nullptr;
312     status = napi_create_object(env, &result);
313     if (status != napi_ok) {
314         return JSUtils::CreateEmptyObject(env);
315     }
316     std::map<std::string, std::string> options = {};
317     obj->numberfmt_->GetResolvedOptions(options, true);
318     JSUtils::SetOptionProperties(env, result, options, "locale");
319     JSUtils::SetOptionProperties(env, result, options, "currency");
320     JSUtils::SetOptionProperties(env, result, options, "currencySign");
321     JSUtils::SetOptionProperties(env, result, options, "currencyDisplay");
322     JSUtils::SetOptionProperties(env, result, options, "unit");
323     JSUtils::SetOptionProperties(env, result, options, "unitDisplay");
324     JSUtils::SetOptionProperties(env, result, options, "signDisplay");
325     JSUtils::SetOptionProperties(env, result, options, "compactDisplay");
326     JSUtils::SetOptionProperties(env, result, options, "notation");
327     JSUtils::SetOptionProperties(env, result, options, "style");
328     JSUtils::SetOptionProperties(env, result, options, "numberingSystem");
329     JSUtils::SetOptionProperties(env, result, options, "unitUsage");
330     JSUtils::SetBooleanOptionProperties(env, result, options, "useGrouping");
331     JSUtils::SetIntegerOptionProperties(env, result, options, "minimumIntegerDigits");
332     JSUtils::SetIntegerOptionProperties(env, result, options, "minimumFractionDigits");
333     JSUtils::SetIntegerOptionProperties(env, result, options, "maximumFractionDigits");
334     JSUtils::SetIntegerOptionProperties(env, result, options, "minimumSignificantDigits");
335     JSUtils::SetIntegerOptionProperties(env, result, options, "maximumSignificantDigits");
336     JSUtils::SetOptionProperties(env, result, options, "localeMatcher");
337     return result;
338 }
339 
FormatToParts(napi_env env,napi_callback_info info)340 napi_value JSNumberFormatAddon::FormatToParts(napi_env env, napi_callback_info info)
341 {
342     size_t argc = 1;
343     napi_value argv[1] = { nullptr };
344     napi_value thisVar = nullptr;
345     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
346     if (status != napi_ok) {
347         return JSUtils::CreateEmptyArray(env);
348     }
349     napi_valuetype valueType = napi_valuetype::napi_undefined;
350     if (argc < 1) {
351         return GetFormatToPartsInner(env, nullptr, valueType, thisVar);
352     }
353     status = napi_typeof(env, argv[0], &valueType);
354     if (status != napi_ok) {
355         HILOG_ERROR_I18N("JSNumberFormatAddon::FormatToParts: Get param type failed.");
356         return JSUtils::CreateEmptyArray(env);
357     }
358     return GetFormatToPartsInner(env, argv[0], valueType, thisVar);
359 }
360 
FormatWithDiffParamType(napi_env env,napi_value param,napi_valuetype & valueType,napi_value & thisVar)361 napi_value JSNumberFormatAddon::FormatWithDiffParamType(napi_env env, napi_value param,
362     napi_valuetype &valueType, napi_value &thisVar)
363 {
364     JSNumberFormatAddon *obj = nullptr;
365     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
366     if (status != napi_ok || !obj || !obj->numberfmt_) {
367         HILOG_ERROR_I18N("FormatWithDiffParamType: Get NumberFormat object failed");
368         return JSUtils::CreateEmptyString(env);
369     }
370     std::string formatedValue;
371     if (valueType == napi_valuetype::napi_bigint) {
372         int32_t code = 0;
373         std::string bigintValue = JSUtils::GetBigIntStr(env, param, code);
374         if (code != 0) {
375             HILOG_ERROR_I18N("FormatWithDiffParamType: Get bigint parameter value failed");
376             return JSUtils::CreateEmptyString(env);
377         }
378         formatedValue = obj->numberfmt_->FormatBigInt(bigintValue);
379     } else if (valueType == napi_valuetype::napi_number) {
380         double number = 0;
381         status = napi_get_value_double(env, param, &number);
382         if (status != napi_ok) {
383             HILOG_ERROR_I18N("FormatWithDiffParamType: Get double parameter value failed");
384             return JSUtils::CreateEmptyString(env);
385         }
386         formatedValue = obj->numberfmt_->Format(number);
387     } else {
388         int32_t errorCode = 0;
389         double paramValue = JSUtils::GetNumberValue(env, param, errorCode);
390         if (errorCode != 0) {
391             HILOG_ERROR_I18N("FormatWithDiffParamType: Get Number object value failed");
392             return JSUtils::CreateEmptyString(env);
393         }
394         formatedValue = obj->numberfmt_->Format(paramValue);
395     }
396     return JSUtils::CreateString(env, formatedValue);
397 }
398 
GetFormatToPartsInner(napi_env env,napi_value param,napi_valuetype & valueType,napi_value & thisVar)399 napi_value JSNumberFormatAddon::GetFormatToPartsInner(napi_env env, napi_value param,
400     napi_valuetype &valueType, napi_value &thisVar)
401 {
402     JSNumberFormatAddon *obj = nullptr;
403     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
404     if (status != napi_ok || !obj || !obj->numberfmt_) {
405         HILOG_ERROR_I18N("GetFormatToPartsInner: Get NumberFormat object failed");
406         return JSUtils::CreateEmptyArray(env);
407     }
408     std::vector<std::vector<std::string>> numberParts;
409     if (param == nullptr) {
410         obj->numberfmt_->FormatToParts(numberParts);
411         return SetNumberFormatParts(env, status, numberParts);
412     }
413     if (valueType == napi_valuetype::napi_bigint) {
414         int32_t code = 0;
415         std::string bigintValue = JSUtils::GetBigIntStr(env, param, code);
416         if (code != 0) {
417             HILOG_ERROR_I18N("GetFormatToPartsInner: Get bigint parameter value failed");
418             return JSUtils::CreateEmptyArray(env);
419         }
420         obj->numberfmt_->FormatBigIntToParts(bigintValue, numberParts);
421     } else if (valueType == napi_valuetype::napi_number) {
422         double number = 0;
423         status = napi_get_value_double(env, param, &number);
424         if (status != napi_ok) {
425             HILOG_ERROR_I18N("GetFormatToPartsInner: Get double parameter value failed");
426             return JSUtils::CreateEmptyArray(env);
427         }
428         obj->numberfmt_->FormatToParts(number, numberParts);
429     } else {
430         int32_t errorCode = 0;
431         double paramValue = JSUtils::GetNumberValue(env, param, errorCode);
432         if (errorCode != 0) {
433             HILOG_ERROR_I18N("GetFormatToPartsInner: Get Number object value failed");
434             return JSUtils::CreateEmptyArray(env);
435         }
436         obj->numberfmt_->FormatToParts(paramValue, numberParts);
437     }
438     return SetNumberFormatParts(env, status, numberParts);
439 }
440 
SupportedLocalesOf(napi_env env,napi_callback_info info)441 napi_value JSNumberFormatAddon::SupportedLocalesOf(napi_env env, napi_callback_info info)
442 {
443     size_t argc = 2;
444     napi_value argv[2] = { nullptr };
445     napi_value thisVar = nullptr;
446     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
447     if (status != napi_ok) {
448         HILOG_ERROR_I18N("JSNumberFormatAddon::SupportedLocalesOf: Get parameter info failed");
449         return JSUtils::CreateEmptyArray(env);
450     }
451     std::vector<std::string> localeTags;
452     if (argc > 0) {
453         int32_t code = 0;
454         std::vector<std::string> localeArray = JSUtils::GetLocaleArray(env, argv[0], code);
455         localeTags.assign(localeArray.begin(), localeArray.end());
456     }
457     std::map<std::string, std::string> map = {};
458     if (argc > 1) {
459         JSUtils::GetNumberOptionValues(env, argv[1], map);
460     }
461     I18nErrorCode i18nStatus = I18nErrorCode::SUCCESS;
462     std::vector<std::string> resultLocales = NumberFormat::SupportedLocalesOf(localeTags, map, i18nStatus);
463     if (i18nStatus == I18nErrorCode::INVALID_LOCALE_TAG) {
464         HILOG_ERROR_I18N("JSNumberFormatAddon::SupportedLocalesOf: SupportedLocalesOf status fail");
465         ErrorUtil::NapiThrowUndefined(env, "invalid locale");
466         return nullptr;
467     } else if (i18nStatus != I18nErrorCode::SUCCESS) {
468         ErrorUtil::NapiThrowUndefined(env, "getStringOption failed");
469         return nullptr;
470     }
471     return JSUtils::CreateArray(env, resultLocales);
472 }
473 
SetNumberFormatParts(napi_env env,napi_status & status,const std::vector<std::vector<std::string>> & numberParts)474 napi_value JSNumberFormatAddon::SetNumberFormatParts(napi_env env, napi_status &status,
475     const std::vector<std::vector<std::string>> &numberParts)
476 {
477     napi_value result = nullptr;
478     status = napi_create_array_with_length(env, numberParts.size(), &result);
479     if (status != napi_ok) {
480         HILOG_ERROR_I18N("SetNumberFormatParts: Failed to create array");
481         return JSUtils::CreateEmptyArray(env);
482     }
483     for (size_t i = 0; i < numberParts.size(); i++) {
484         napi_value value = nullptr;
485         status = napi_create_string_utf8(env, numberParts[i][1].c_str(), NAPI_AUTO_LENGTH, &value);
486         if (status != napi_ok) {
487             HILOG_ERROR_I18N("Failed to create string item numberParts[i][1].");
488             return JSUtils::CreateEmptyArray(env);
489         }
490         napi_value type = nullptr;
491         status = napi_create_string_utf8(env, numberParts[i][0].c_str(), NAPI_AUTO_LENGTH, &type);
492         if (status != napi_ok) {
493             HILOG_ERROR_I18N("Failed to create string item numberParts[i][0].");
494             return JSUtils::CreateEmptyArray(env);
495         }
496         std::unordered_map<std::string, napi_value> propertys {
497             { "type", type },
498             { "value", value }
499         };
500         std::vector<std::string> keyVect = { "type", "value" };
501         int32_t code = 0;
502         napi_value formatInfo = JSUtils::CreateArrayItem(env, propertys, code, keyVect);
503         if (code != 0) {
504             return JSUtils::CreateEmptyArray(env);
505         }
506         status = napi_set_element(env, result, i, formatInfo);
507         if (status != napi_ok) {
508             HILOG_ERROR_I18N("Failed to set array item");
509             return JSUtils::CreateEmptyArray(env);
510         }
511     }
512     return result;
513 }
514 
GetNumberFormat()515 std::shared_ptr<NumberFormat> JSNumberFormatAddon::GetNumberFormat()
516 {
517     return numberfmt_;
518 }
519 } // namespace I18n
520 } // namespace Global
521 } // namespace OHOS