1/* 2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 16import image from '@ohos.multimedia.image'; 17import testNapi from 'libentry.so'; 18import { TitleBar } from '../../../common/TitleBar'; 19import hilog from '@ohos.hilog'; 20 21const TAG: string = 'napi_coerce_to_number'; 22 23@Entry 24@Component 25struct napicoercetonumber { 26 @State isSetInstance: Boolean = false; 27 @State imagePixelMap: PixelMap | undefined = undefined; 28 @State textcont: string = 'napi_coerce_to_number() 用于将任意类型的 JavaScript 值' 29 + '(例如 boolean 或 string)强制转换为 number。' 30 + '如果 API 成功,则返回 napi_ok。' 31 + '该 API 实现了 ECMAScript 语言规范的 第 7.1.3 节 中定义的抽象操作 ToNumber()。' 32 + '如果传入的值是对象,此函数可能会运行 JS 代码。'; 33 @State testcont: string = '// 调用 API 对不同类型的输入进行测试 \n' 34 + 'const testNumStr2NumResult = addon.testNapiCoerceToNumber(\'-123.456\'); // 数字字符串 -> -123.456 \n' 35 + 'const testZeroStr2NumberResult = addon.testNapiCoerceToNumber(\'0\'); // 数字字符串 -> 0 \n' 36 + 'const testTextStr2NumResult = addon.testNapiCoerceToNumber(\'test\'); // 非数字字符串 -> NaN \n' 37 + 'const testTrueBool2NumberResult = addon.testNapiCoerceToNumber(true); // true -> 1 \n' 38 + 'const testFalseBool2NumberResult = addon.testNapiCoerceToNumber(false); // false -> 0 \n' 39 + 'const testNull2NumberResult = addon.testNapiCoerceToNumber(null); // null -> 0 \n' 40 + 'const testUndef2NumberResult = addon.testNapiCoerceToNumber(undefined); // undefined -> NaN \n' 41 + '// 输出强制转换结果 \n' 42 + 'console.log(`\'-123.456\' -> ${testNumStr2NumResult}`); \n' 43 + 'console.log(`\'0\' -> ${testZeroStr2NumberResult}`); \n' 44 + 'console.log(`\'test\' -> ${testTextStr2NumResult}`); \n' 45 + 'console.log(`true -> ${testTrueBool2NumberResult}`); \n' 46 + 'console.log(`false -> ${testFalseBool2NumberResult}`); \n' 47 + 'console.log(`null -> ${testNull2NumberResult}`); \n' 48 + 'console.log(`undefined -> ${testUndef2NumberResult}`); \n'; 49 controller: TextAreaController = new TextAreaController() 50 private btnFontColor: Resource = $r('app.color.white'); 51 private pixelMapFormat: image.PixelMapFormat = 3; 52 53 build() { 54 Scroll() { 55 Column() { 56 // 标题 57 TitleBar({ title: $r('app.string.napi_coerce_to_number') }) 58 59 Column() { 60 Column() { 61 TextArea({ 62 text: this.textcont, 63 placeholder: '', 64 }) 65 .placeholderFont({ size: 16, weight: 400 }) 66 .width('90%') 67 .margin(10) 68 .fontSize(16) 69 .fontColor('#182431') 70 .backgroundColor('#FFFFFF') 71 .enabled(false) 72 73 TextArea({ 74 text: this.testcont, 75 placeholder: '', 76 }) 77 .placeholderFont({ size: 16, weight: 400 }) 78 .width('90%') 79 .margin(10) 80 .fontSize(16) 81 .fontColor('#ff400336') 82 .backgroundColor('#ff985307') 83 .enabled(false) 84 } 85 .width('100%') 86 .alignItems(HorizontalAlign.Center) 87 .justifyContent(FlexAlign.Start) 88 89 Row() { 90 91 Button($r('app.string.napi_coerce_to_number'), { type: ButtonType.Capsule }) 92 .backgroundColor(Color.Blue) 93 .width('80%') 94 .height(48) 95 .fontSize(16) 96 .fontWeight(500) 97 .fontColor(this.btnFontColor) 98 .margin({ left: 24 }) 99 .id('napi_coerce_to_number') 100 .onClick(() => { 101 try { 102 // Test coercing string/boolean/null/undefined values to number 103 const testNumStr: string = '-123.456'; 104 const testZeroStr: string = '0'; 105 const testTextStr: string = 'test' 106 const testTrueBool: boolean = true; 107 const testFalseBool: boolean = false; 108 const testNull: null = null; 109 const testUndef: undefined = undefined; 110 const testNumStr2NumResult = testNapi.testNapiCoerceToNumber(testNumStr); 111 const testZeroStr2NumberResult = testNapi.testNapiCoerceToNumber(testZeroStr); 112 const testTextStr2NumResult = testNapi.testNapiCoerceToNumber(testTextStr); 113 const testTrueBool2NumberResult = testNapi.testNapiCoerceToNumber(testTrueBool); 114 const testFalseBool2NumberResult = testNapi.testNapiCoerceToNumber(testFalseBool); 115 const testNull2NumberResult = testNapi.testNapiCoerceToNumber(testNull); 116 const testUndef2NumberResult = testNapi.testNapiCoerceToNumber(testUndef); 117 118 // Replace result in testcont 119 this.testcont = this.testcont.replace('${testNumStr2NumResult}', `## ${testNumStr2NumResult} ##`); 120 this.testcont = this.testcont.replace('${testZeroStr2NumberResult}', `## ${testZeroStr2NumberResult} ##`); 121 this.testcont = this.testcont.replace('${testTextStr2NumResult}', `## ${testTextStr2NumResult} ##`); 122 this.testcont = this.testcont.replace('${testTrueBool2NumberResult}', `## ${testTrueBool2NumberResult} ##`); 123 this.testcont = this.testcont.replace('${testFalseBool2NumberResult}', `## ${testFalseBool2NumberResult} ##`); 124 this.testcont = this.testcont.replace('${testNull2NumberResult}', `## ${testNull2NumberResult} ##`); 125 this.testcont = this.testcont.replace('${testUndef2NumberResult}', `## ${testUndef2NumberResult} ##`); 126 127 // Print the results 128 hilog.info(0x0000, TAG, `(${typeof (testNumStr)})${testNumStr} -> ${testNumStr2NumResult}`); 129 hilog.info(0x0000, TAG, `(${typeof (testZeroStr)})${testZeroStr} -> ${testZeroStr2NumberResult}`); 130 hilog.info(0x0000, TAG, `(${typeof (testTextStr)})${testTextStr} -> ${testTextStr2NumResult}`); 131 hilog.info(0x0000, TAG, `(${typeof (testTrueBool)})${testTrueBool} -> ${testTrueBool2NumberResult}`); 132 hilog.info(0x0000, TAG, `(${typeof (testFalseBool)})${testFalseBool} -> ${testFalseBool2NumberResult}`); 133 hilog.info(0x0000, TAG, `(${typeof (testNull)})${testNull} -> ${testNull2NumberResult}`); 134 hilog.info(0x0000, TAG, `(${typeof (testUndef)})${testUndef} -> ${testUndef2NumberResult}`); 135 } catch (error) { 136 hilog.error(0x0000, TAG, `Catch error testNapiCoerceToNumber: ${error.message}}`) 137 } 138 }) 139 } 140 .width('100%') 141 .height(48) 142 .alignItems(VerticalAlign.Center) 143 .justifyContent(FlexAlign.SpaceBetween) 144 } 145 .width('100%') 146 } 147 } 148 .width('100%') 149 .height('100%') 150 .align(Alignment.Top) 151 .backgroundColor($r('app.color.background_shallow_grey')) 152 } 153} 154