• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_is_array';
22
23@Entry
24@Component
25struct napiisarray {
26  @State isSetInstance: Boolean = false;
27  @State imagePixelMap: PixelMap | undefined = undefined;
28  @State textcont: string = 'napi_is_array() 用于判断给定对象是否是数组'
29    + '如果 API 成功,则返回 napi_ok。'
30    + '该 API 表示调用对象上的 IsArray 操作,如 ECMAScript 语言规范的 第 7.2.2 节 中所定义。';
31  @State testcont: string = '// 调用 API 对不同类型的输入进行测试 \n'
32    + 'const testNumIsArrayResult = addon.testNapiIsArray(123); // 数字 -> false \n'
33    + 'const testStrIsArrayResult = addon.testNapiIsArray(\'test123\'); // 字符串 -> false \n'
34    + 'const testBoolIsArrayResult = addon.testNapiIsArray(true); // 布尔值 -> false \n'
35    + 'const testNullIsArrayResult = addon.testNapiIsArray(null); // null -> false \n'
36    + 'const testUndefIsArrayResult = addon.testNapiIsArray(undefined); // undefined -> false \n'
37    + 'const testArrIsArrayResult = addon.testNapiIsArray([1, \'2\', false, null, undefined])'
38    + '// 输出判断结果 \n'
39    + 'console.log(`123 -> ${testNumIsArrayResult}`); \n'
40    + 'console.log(`\'test123\' -> ${testStrIsArrayResult}`); \n'
41    + 'console.log(`true -> ${testBoolIsArrayResult}`); \n'
42    + 'console.log(`null -> ${testNullIsArrayResult}`); \n'
43    + 'console.log(`undefined -> ${testUndefIsArrayResult}`); \n'
44    + 'console.log(`[1, \'2\', false, null, undefined] -> ${testArrIsArrayResult}`); \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_is_array') })
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_is_array'), { 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_is_array')
96              .onClick(() => {
97                try {
98                  // Test if number/string/boolean/null/undefined/array is array
99                  const testNum: number = 123;
100                  const testStr: string = 'test123'
101                  const testBool: boolean = true;
102                  const testNull: null = null;
103                  const testUndef: undefined = undefined;
104                  const testArr: Array<number | string | boolean | null | undefined> = [1, '2', false, null, undefined];
105                  console.log('=======begin===========');
106                  const testNumIsArray = testNapi.testNapiIsArray(testNum);
107                  const testStrIsArray = testNapi.testNapiIsArray(testStr);
108                  const testBoolIsArray = testNapi.testNapiIsArray(testBool);
109                  const testNullIsArray = testNapi.testNapiIsArray(testNull);
110                  const testUndefIsArray = testNapi.testNapiIsArray(testUndef);
111                  const testArrIsArray = testNapi.testNapiIsArray(testArr);
112                  console.log('=========try=========');
113
114                  // Replace result in testcont
115                  this.testcont = this.testcont.replace('${testNumIsArrayResult}', `## ${testNumIsArray} ##`);
116                  this.testcont = this.testcont.replace('${testStrIsArrayResult}', `## ${testStrIsArray} ##`);
117                  this.testcont = this.testcont.replace('${testBoolIsArrayResult}', `## ${testBoolIsArray} ##`);
118                  this.testcont = this.testcont.replace('${testNullIsArrayResult}', `## ${testNullIsArray} ##`);
119                  this.testcont = this.testcont.replace('${testUndefIsArrayResult}', `## ${testUndefIsArray} ##`);
120                  this.testcont = this.testcont.replace('${testArrIsArrayResult}', `## ${testArrIsArray} ##`);
121
122                  // Print the results
123                  hilog.info(0x0000, TAG, `(whether is ${testNum} array? -> ${testNumIsArray}`);
124                  hilog.info(0x0000, TAG, `(whether is ${testStr} array? -> ${testStrIsArray}`);
125                  hilog.info(0x0000, TAG, `(whether is ${testBool} array? -> ${testBoolIsArray}`);
126                  hilog.info(0x0000, TAG, `(whether is ${testNull} array? -> ${testNullIsArray}`);
127                  hilog.info(0x0000, TAG, `(whether is ${testUndef} array? -> ${testUndefIsArray}`);
128                  hilog.info(0x0000, TAG, `(whether is ${testArr} array? -> ${testArrIsArray}`);
129                } catch (error) {
130                  hilog.error(0x0000, TAG, `Catch error testNapiIsArray: ${error.message}}`)
131                }
132              })
133          }
134          .width('100%')
135          .height(48)
136          .alignItems(VerticalAlign.Center)
137          .justifyContent(FlexAlign.SpaceBetween)
138        }
139        .width('100%')
140      }
141    }
142    .width('100%')
143    .height('100%')
144    .align(Alignment.Top)
145    .backgroundColor($r('app.color.background_shallow_grey'))
146  }
147}
148