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 WARRAi]kNTIES 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_strict_eaquals'; 22 23@Entry 24@Component 25struct napistrictequals { 26 @State isSetInstance: Boolean = false; 27 @State imagePixelMap: PixelMap | undefined = undefined; 28 @State textcont: string = 'napi_strict_equals() 用于判断两个对象是否相等' 29 + '如果 API 成功,则返回 napi_ok。' 30 + '该 API 表示调用 ECMAScript 语言规范的 第 7.2.14 节 中定义的严格相等算法。'; 31 @State testcont: string = '// 调用 API 对不同类型的输入进行测试 \n' 32 + 'const testNumStrStrictEqualsResult = addon.testNapiStrictEquals(123, \'123\'); // false \n' 33 + 'const testZeroNumStrStrictEqualsResult = addon.testNapiStrictEquals(0, \'0\'); // false \n' 34 + 'const testTrueBoolStrStrictEqualsResult = addon.testNapiStrictEquals(\'true\', true); // false \n' 35 + 'const testNullZeroStrictEqualsResult = addon.testNapiStrictEquals(null, 0); // -> false \n' 36 + 'const testNullUndefStrictEqualsResult = addon.testNapiStrictEquals(null, undefined); // false \n' 37 + 'const testNumStrictEqualsResult = addon.testNapiStrictEquals(123, 123) // true \n' 38 + '// 输出判断结果 \n' 39 + 'console.log(`123 = \'123\'? -> ${testNumStrStrictEqualsResult}`); \n' 40 + 'console.log(`0 = \'0\'? -> ${testZeroNumStrStrictEqualsResult}`); \n' 41 + 'console.log(`\'true\' = true? -> ${testTrueBoolStrStrictEqualsResult}`); \n' 42 + 'console.log(`null = 0? -> ${testNullZeroStrictEqualsResult}`); \n' 43 + 'console.log(`null = undefined? -> ${testNullUndefStrictEqualsResult}`); \n' 44 + 'console.log(`123 = 123? -> ${testNumStrictEqualsResult}`); \n'; 45 controller: TextAreaController = new TextAreaController() 46 private btnFontColor: Resource = $r('app.color.white'); 47 private pixelMapFormat: image.PixelMapFormat = 3; 48 49 build() { 50 Scroll() { 51 Column() { 52 // 标题 53 TitleBar({ title: $r('app.string.napi_strict_equals') }) 54 55 Column() { 56 Column() { 57 TextArea({ 58 text: this.textcont, 59 placeholder: '', 60 }) 61 .placeholderFont({ size: 16, weight: 400 }) 62 .width('90%') 63 .margin(10) 64 .fontSize(16) 65 .fontColor('#182431') 66 .backgroundColor('#FFFFFF') 67 .enabled(false) 68 69 TextArea({ 70 text: this.testcont, 71 placeholder: '', 72 }) 73 .placeholderFont({ size: 16, weight: 400 }) 74 .width('90%') 75 .margin(10) 76 .fontSize(16) 77 .fontColor('#ff400336') 78 .backgroundColor('#FFFFFF') 79 .enabled(false) 80 } 81 .width('100%') 82 .alignItems(HorizontalAlign.Center) 83 .justifyContent(FlexAlign.Start) 84 85 Row() { 86 87 Button($r('app.string.napi_strict_equals'), { type: ButtonType.Capsule }) 88 .backgroundColor(Color.Blue) 89 .width('80%') 90 .height(48) 91 .fontSize(16) 92 .fontWeight(500) 93 .fontColor(this.btnFontColor) 94 .margin({ left: 24 }) 95 .id('napi_strict_equals') 96 .onClick(() => { 97 try { 98 // Check if the two objects are equal 99 const testNum: number = 123; 100 const testNumEqu: number = 123; 101 const testZeroNum: number = 0; 102 const testNumStr: string = '123'; 103 const testZeroNumStr: string = '0'; 104 const testBoolStr: string = 'true'; 105 const testTrueBool: boolean = true; 106 const testNull: null = null; 107 const testUndef: undefined = undefined; 108 const testNumStrStrictEquals = testNapi.testNapiStrictEquals(testNum, testNumStr); 109 const testZeroNumStrStrictEquals = testNapi.testNapiStrictEquals(testZeroNum, testZeroNumStr); 110 const testTrueBoolStrStrictEquals = testNapi.testNapiStrictEquals(testBoolStr, testTrueBool); 111 const testNullZeroStrictEquals = testNapi.testNapiStrictEquals(testNull, testZeroNum); 112 const testNullUndefStrictEquals = testNapi.testNapiStrictEquals(testNull, testUndef); 113 const testNumStrictEquals = testNapi.testNapiStrictEquals(testNum, testNumEqu); 114 115 // Replace result in testcont 116 this.testcont = this.testcont.replace('${testNumStrStrictEqualsResult}', `## ${testNumStrStrictEquals} ##`); 117 this.testcont = this.testcont.replace('${testZeroNumStrStrictEqualsResult}', `## ${testZeroNumStrStrictEquals} ##`); 118 this.testcont = this.testcont.replace('${testTrueBoolStrStrictEqualsResult}', `## ${testTrueBoolStrStrictEquals} ##`); 119 this.testcont = this.testcont.replace('${testNullZeroStrictEqualsResult}', `## ${testNullZeroStrictEquals} ##`); 120 this.testcont = this.testcont.replace('${testNullUndefStrictEqualsResult}', `## ${testNullUndefStrictEquals} ##`); 121 this.testcont = this.testcont.replace('${testNumStrictEqualsResult}', `## ${testNumStrictEquals} ##`); 122 123 // Print the results 124 hilog.info(0x0000, TAG, `(123 = '123'? -> ${testNumStrStrictEquals}`); 125 hilog.info(0x0000, TAG, `(0 = '0'? -> ${testZeroNumStrStrictEquals}`); 126 hilog.info(0x0000, TAG, `('true' = true? -> ${testTrueBoolStrStrictEquals}`); 127 hilog.info(0x0000, TAG, `(null = 0? -> ${testNullZeroStrictEquals}`); 128 hilog.info(0x0000, TAG, `(null = undefined? -> ${testNullUndefStrictEquals}`); 129 hilog.info(0x0000, TAG, `(123 = 123? -> ${testNumStrictEquals}`); 130 } catch (error) { 131 hilog.error(0x0000, TAG, `Catch error testNapiStrictEquals: ${error.message}}`) 132 } 133 }) 134 } 135 .width('100%') 136 .height(48) 137 .alignItems(VerticalAlign.Center) 138 .justifyContent(FlexAlign.SpaceBetween) 139 } 140 .width('100%') 141 } 142 } 143 .width('100%') 144 .height('100%') 145 .align(Alignment.Top) 146 .backgroundColor($r('app.color.background_shallow_grey')) 147 } 148} 149