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