1# @ohos.util (util工具函数) 2 3该模块主要提供常用的工具函数,实现字符串编解码([TextEncoder](#textencoder),[TextDecoder](#textdecoder))、有理数运算([RationalNumber<sup>8+</sup>](#rationalnumber8))、缓冲区管理([LRUCache<sup>9+</sup>](#lrucache9))、范围判断([ScopeHelper<sup>9+</sup>](#scopehelper9))、Base64编解码([Base64Helper<sup>9+</sup>](#base64helper9))、内置对象类型检查([types<sup>8+</sup>](#types8)、对方法进行插桩和替换([Aspect<sup>11+</sup>](#aspect11))等功能。 4 5> **说明:** 6> 7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9 10## 导入模块 11 12```ts 13import { util } from '@kit.ArkTS'; 14``` 15## util.format<sup>9+</sup> 16 17format(format: string, ...args: Object[]): string 18 19通过式样化字符串对输入的内容按特定格式输出。 20 21**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 22 23**系统能力:** SystemCapability.Utils.Lang 24 25**参数:** 26 27| 参数名 | 类型 | 必填 | 说明 | 28| ------- | -------- | ---- | -------------- | 29| format | string | 是 | 格式化字符串,可以包含零个或多个占位符,用于指定要插入的参数的位置和格式。 | 30| ...args | Object[] | 否 | 替换format参数中占位符的数据,此参数缺失时,默认返回第一个参数。| 31 32**返回值:** 33 34| 类型 | 说明 | 35| ------ | -----------------| 36| string | 格式化后的字符串。 | 37 38**错误码:** 39 40以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 41 42| 错误码ID | 错误信息 | 43| -------- | -------- | 44| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 45 46**格式说明符:** 47 48| 格式说明符 | 说明 | 49| ------ | -------------------------------- | 50| %s | 将参数转换为字符串,用于除Object,BigInt和-0之外的所有值。| 51| %d | 将参数作为十进制整数进行格式化输出,用于除Symbol和BigInt之外的所有值。| 52| %i | 将字符串转换为十进制整数,用于除BigInt和Symbol之外的所有值。| 53| %f | 将字符串转换为浮点数,用于除BigInt和Symbol之外的所有值。| 54| %j | 将JavaScript对象转换为JSON字符串进行格式化输出。| 55| %o | 用于将JavaScript对象进行格式化输出,将对象转换为字符串表示,但不包含对象的原型链信息。| 56| %O | 用于将JavaScript对象进行格式化输出,将对象转换为字符串表示。| 57| %c | 只在浏览器环境中有效。其余环境不会产生样式效果。| 58| %% | 转义百分号的特殊格式化占位符。| 59 60**示例:** 61 62```ts 63import { util } from '@kit.ArkTS'; 64 65interface utilAddresstype { 66 city: string; 67 country: string; 68} 69interface utilPersontype { 70 name: string; 71 age: number; 72 address: utilAddresstype; 73} 74 75let name = 'John'; 76let age = 20; 77let formattedString = util.format('My name is %s and I am %s years old', name, age); 78console.info(formattedString); 79// 输出结果:My name is John and I am 20 years old 80let num = 10.5; 81formattedString = util.format('The number is %d', num); 82console.info(formattedString); 83// 输出结果:The number is 10.5 84num = 100.5; 85formattedString = util.format('The number is %i', num); 86console.info(formattedString); 87// 输出结果:The number is 100 88const pi = 3.141592653; 89formattedString = util.format('The value of pi is %f', pi); 90console.info(formattedString); 91// 输出结果:The value of pi is 3.141592653 92const obj: Record<string,number | string> = { "name": 'John', "age": 20 }; 93formattedString = util.format('The object is %j', obj); 94console.info(formattedString); 95// 输出结果:The object is {"name":"John","age":20} 96const person: utilPersontype = { 97 name: 'John', 98 age: 20, 99 address: { 100 city: 'New York', 101 country: 'USA' 102 } 103}; 104console.info(util.format('Formatted object using %%O: %O', person)); 105console.info(util.format('Formatted object using %%o: %o', person)); 106/* 107输出结果: 108Formatted object using %O: { name: 'John', 109 age: 20, 110 address: 111 { city: 'New York', 112 country: 'USA' } } 113Formatted object using %o: { name: 'John', 114 age: 20, 115 address: 116 { city: 'New York', 117 country: 'USA' } } 118*/ 119const percentage = 80; 120let arg = 'homework'; 121formattedString = util.format('John finished %d%% of the %s', percentage, arg); 122console.info(formattedString); 123// 输出结果:John finished 80% of the homework 124``` 125 126## util.errnoToString<sup>9+</sup> 127 128errnoToString(errno: number): string 129 130获取系统错误码对应的详细信息。 131 132**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 133 134**系统能力:** SystemCapability.Utils.Lang 135 136**参数:** 137 138| 参数名 | 类型 | 必填 | 说明 | 139| ------ | ------ | ---- | -------------------------- | 140| errno | number | 是 | 系统发生错误产生的错误码。 | 141 142**返回值:** 143 144| 类型 | 说明 | 145| ------ | ---------------------- | 146| string | 错误码对应的详细信息。 | 147 148**错误码:** 149 150以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 151 152| 错误码ID | 错误信息 | 153| -------- | -------- | 154| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 155 156**示例:** 157 158```ts 159let errnum = -1; // -1 : a system error number 160let result = util.errnoToString(errnum); 161console.info("result = " + result); 162// 输出结果:result = operation not permitted 163``` 164 165**部分错误码及信息示例:** 166 167| 错误码 | 信息 | 168| ------ | -------------------------------- | 169| -1 | operation not permitted | 170| -2 | no such file or directory | 171| -3 | no such process | 172| -4 | interrupted system call | 173| -5 | i/o error | 174| -11 | resource temporarily unavailable | 175| -12 | not enough memory | 176| -13 | permission denied | 177| -100 | network is down | 178 179## util.callbackWrapper 180 181callbackWrapper(original: Function): (err: Object, value: Object )=>void 182 183对异步函数进行回调化处理,回调中第一个参数将是拒绝原因(如果 Promise 已解决,则为 null),第二个参数将是已解决的值。 184 185> **说明:** 186> 187> 该接口要求参数original必须是异步函数类型。如果传入的参数不是异步函数,不会进行拦截,但是会输出错误信息:"callbackWrapper: The type of Parameter must be AsyncFunction"。 188 189**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 190 191**系统能力:** SystemCapability.Utils.Lang 192 193**参数:** 194 195| 参数名 | 类型 | 必填 | 说明 | 196| -------- | -------- | -------- | -------- | 197| original | Function | 是 | 异步函数。 | 198 199**返回值:** 200 201| 类型 | 说明 | 202| -------- | -------- | 203| Function | 返回一个回调函数,该函数第一个参数err是拒绝原因(如果 Promise 已解决,则为 null),第二个参数value是已解决的值。 | 204 205**错误码:** 206 207以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 208 209| 错误码ID | 错误信息 | 210| -------- | -------- | 211| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 212 213**示例:** 214 215```ts 216async function fn() { 217 return 'hello world'; 218} 219let cb = util.callbackWrapper(fn); 220cb(1, (err : Object, ret : string) => { 221 if (err) throw new Error; 222 console.info(ret); 223}); 224// 输出结果:hello world 225``` 226 227## util.promisify<sup>9+</sup> 228 229promisify(original: (err: Object, value: Object) => void): Function 230 231对异步函数处理并返回一个promise的函数。 232 233**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 234 235**系统能力:** SystemCapability.Utils.Lang 236 237**参数:** 238 239| 参数名 | 类型 | 必填 | 说明 | 240| -------- | -------- | -------- | -------- | 241| original | Function | 是 | 回调函数中第一个参数err是拒绝原因(如果 Promise 已解决,则为 null),第二个参数value是已解决的值。 | 242 243**返回值:** 244 245| 类型 | 说明 | 246| -------- | -------- | 247| Function | 返回一个 Promise 的函数。 | 248 249**错误码:** 250 251以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 252 253| 错误码ID | 错误信息 | 254| -------- | -------- | 255| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 256 257**示例:** 258 259```ts 260async function fn() { 261 return 'hello world'; 262} 263const addCall = util.promisify(util.callbackWrapper(fn)); 264(async () => { 265 try { 266 let res: string = await addCall(); 267 console.info(res); 268 // 输出结果:hello world 269 } catch (err) { 270 console.info(err); 271 } 272})(); 273``` 274 275## util.generateRandomUUID<sup>9+</sup> 276 277generateRandomUUID(entropyCache?: boolean): string 278 279使用加密安全随机数生成器生成随机的RFC 4122版本4的string类型UUID。为了提升性能,此接口会默认使用缓存,即入参为true,最多可缓存128个随机的UUID。当缓存中128个UUID都被使用后,会重新进行一次缓存UUID的生成,以保证UUID的随机性。假如不需要使用缓存的UUID,请将入参设置为false。 280 281**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 282 283**系统能力:** SystemCapability.Utils.Lang 284 285**参数:** 286 287| 参数名 | 类型 | 必填 | 说明 | 288| -------- | -------- | -------- | -------- | 289| entropyCache | boolean | 否 | 是否使用已缓存的UUID, 默认true。 | 290 291**返回值:** 292 293| 类型 | 说明 | 294| -------- | -------- | 295| string | 表示此UUID的字符串。 | 296 297**错误码:** 298 299以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 300 301| 错误码ID | 错误信息 | 302| -------- | -------- | 303| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 304 305**示例:** 306 307```ts 308let uuid = util.generateRandomUUID(true); 309console.info("RFC 4122 Version 4 UUID:" + uuid); 310// 输出随机生成的UUID 311``` 312 313## util.generateRandomBinaryUUID<sup>9+</sup> 314 315generateRandomBinaryUUID(entropyCache?: boolean): Uint8Array 316 317使用加密安全随机数生成器生成随机的RFC 4122版本4的Uint8Array类型UUID。 318 319**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 320 321**系统能力:** SystemCapability.Utils.Lang 322 323**参数:** 324 325| 参数名 | 类型 | 必填 | 说明 | 326| -------- | -------- | -------- | -------- | 327| entropyCache | boolean | 否 | 是否使用已缓存的UUID, 默认true。 | 328 329**返回值:** 330 331| 类型 | 说明 | 332| -------- | -------- | 333| Uint8Array | 表示此UUID的Uint8Array值。 | 334 335**错误码:** 336 337以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 338 339| 错误码ID | 错误信息 | 340| -------- | -------- | 341| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 342 343**示例:** 344 345```ts 346let uuid = util.generateRandomBinaryUUID(true); 347console.info(JSON.stringify(uuid)); 348// 输出随机生成的UUID 349``` 350 351## util.parseUUID<sup>9+</sup> 352 353parseUUID(uuid: string): Uint8Array 354 355将generateRandomUUID生成的string类型UUID转换为generateRandomBinaryUUID生成的Uint8Array类型UUID,如RFC 4122版本4中所述。 356 357**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 358 359**系统能力:** SystemCapability.Utils.Lang 360 361**参数:** 362 363| 参数名 | 类型 | 必填 | 说明 | 364| -------- | -------- | -------- | -------- | 365| uuid | string | 是 | UUID字符串。 | 366 367**返回值:** 368 369| 类型 | 说明 | 370| -------- | -------- | 371| Uint8Array | 返回表示此UUID的Uint8Array,如果解析失败,则抛出SyntaxError。 | 372 373**错误码:** 374 375以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 376 377| 错误码ID | 错误信息 | 378| -------- | -------- | 379| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 380| 10200002 | Invalid uuid string. | 381 382**示例:** 383 384```ts 385let uuid = util.parseUUID("84bdf796-66cc-4655-9b89-d6218d100f9c"); 386console.info("uuid = " + uuid); 387// 输出结果:uuid = 132,189,247,150,102,204,70,85,155,137,214,33,141,16,15,156 388``` 389 390## util.printf<sup>(deprecated)</sup> 391 392printf(format: string, ...args: Object[]): string 393 394通过式样化字符串对输入的内容按特定格式输出。 395 396> **说明:** 397> 398> 从API version 7开始支持,从API version 9开始废弃,建议使用[util.format<sup>9+</sup>](#utilformat9)替代。 399 400**系统能力:** SystemCapability.Utils.Lang 401 402**参数:** 403 404| 参数名 | 类型 | 必填 | 说明 | 405| -------- | -------- | -------- | -------- | 406| format | string | 是 | 式样化字符串。 | 407| ...args | Object[] | 否 | 替换式样化字符串通配符的数据,此参数缺失时,默认返回第一个参数。 | 408 409**返回值:** 410 411| 类型 | 说明 | 412| -------- | -------- | 413| string | 按特定格式式样化后的字符串。 | 414 415**示例:** 416 417```ts 418let res = util.printf("%s", "hello world!"); 419console.info(res); 420// 输出结果:hello world! 421``` 422 423 424## util.getErrorString<sup>(deprecated)</sup> 425 426getErrorString(errno: number): string 427 428获取系统错误码对应的详细信息。 429 430> **说明:** 431> 432> 从API version 7开始支持,从API version 9开始废弃,建议使用[util.errnoToString<sup>9+</sup>](#utilerrnotostring9)替代。 433 434**系统能力:** SystemCapability.Utils.Lang 435 436**参数:** 437 438| 参数名 | 类型 | 必填 | 说明 | 439| -------- | -------- | -------- | -------- | 440| errno | number | 是 | 系统发生错误产生的错误码。 | 441 442**返回值:** 443 444| 类型 | 说明 | 445| -------- | -------- | 446| string | 错误码对应的详细信息。 | 447 448**示例:** 449 450```ts 451let errnum = -1; // -1 : a system error number 452let result = util.getErrorString(errnum); 453console.info("result = " + result); 454// 输出结果:result = operation not permitted 455``` 456 457## util.promiseWrapper<sup>(deprecated)</sup> 458 459promiseWrapper(original: (err: Object, value: Object) => void): Object 460 461对异步函数处理并返回一个promise的版本。 462 463> **说明:** 464> 465> 此接口不可用,建议使用[util.promisify<sup>9+</sup>](#utilpromisify9)替代。 466 467**系统能力:** SystemCapability.Utils.Lang 468 469**参数:** 470 471| 参数名 | 类型 | 必填 | 说明 | 472| -------- | -------- | -------- | -------- | 473| original | Function | 是 | 异步函数。 | 474 475**返回值:** 476 477| 类型 | 说明 | 478| -------- | -------- | 479| Function | 采用遵循常见的错误优先的回调风格的函数(也就是将 (err, value) => ... 回调作为最后一个参数),并返回一个返回 promise 的版本。 | 480 481 482## util.getHash<sup>12+</sup> 483 484getHash(object: object): number 485 486获取对象的Hash值。如果是第一次获取,则计算Hash值并保存到对象的Hash域(返回随机的Hash值);如果不是第一次获取,则从Hash域中获取并返回Hash值(同一对象多次返回值保持不变)。 487 488**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 489 490**系统能力:** SystemCapability.Utils.Lang 491 492**参数:** 493 494| 参数名 | 类型 | 必填 | 说明 | 495| -------- | -------- | -------- | -------- | 496| object | object | 是 | 希望获取Hash值的对象。 | 497 498**返回值:** 499 500| 类型 | 说明 | 501| -------- | -------- | 502| number | Hash值。 | 503 504**错误码:** 505 506以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 507 508| 错误码ID | 错误信息 | 509| -------- | -------- | 510| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 511 512**示例:** 513 514```ts 515interface Person { 516 name: string, 517 age: number 518} 519let obj: Person = { name: 'Jack', age: 20 }; 520let result1 = util.getHash(obj); 521console.info('result1 is ' + result1); 522let result2 = util.getHash(obj); 523console.info('result2 is ' + result2); 524// 输出结果:result1 与 result2 的值相等,且为随机的Hash值。 525``` 526 527 528## TextDecoderOptions<sup>11+</sup> 529 530解码相关选项参数,存在两个属性fatal和ignoreBOM。 531 532**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 533 534**系统能力:** SystemCapability.Utils.Lang 535 536| 名称 | 类型 | 必填 | 说明 | 537| --------- | -------- | ---- | ------------------ | 538| fatal | boolean | 否 | 是否显示致命错误,默认值是false。 | 539| ignoreBOM | boolean | 否 | 是否忽略BOM标记,默认值是false。 | 540 541## DecodeToStringOptions<sup>12+</sup> 542 543解码是否使用流处理方式。 544 545**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 546 547**系统能力:** SystemCapability.Utils.Lang 548 549| 名称 | 类型 | 必填 | 说明 | 550| -------- | -------- | -------- | -------- | 551| stream | boolean | 否 | 输入末尾出现的不完整字节序列是否需要追加在下次调用decodeToString的参数中处理。设置为true,则不完整的字节序列会存储在内部缓存区直到下次调用该函数,false则会在当前调用时直接解码。默认为false。 | 552 553## DecodeWithStreamOptions<sup>11+</sup> 554 555解码是否跟随附加数据块相关选项参数。 556 557**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 558 559**系统能力:** SystemCapability.Utils.Lang 560 561| 名称 | 类型 | 必填 | 说明 | 562| -------- | -------- | -------- | -------- | 563| stream | boolean | 否 | 在随后的decodeWithStream()调用中是否跟随附加数据块。如果以块的形式处理数据,则设置为true;如果处理最后的数据块或数据未分块,则设置为false。默认为false。 | 564 565## Aspect<sup>11+</sup> 566 567Aspect类用于封装提供切面能力(Aspect Oriented Programming,简写AOP)的接口,这些接口可以用来对类方法进行前后插桩或者替换实现。 568 569### addBefore<sup>11+</sup> 570 571static addBefore(targetClass: Object, methodName: string, isStatic: boolean, before: Function): void 572 573在指定的类对象的原方法执行前插入一个函数。addBefore接口执行完成后,都会先执行插入的函数逻辑,再执行指定类对象的原方法。 574 575**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 576 577**系统能力:** SystemCapability.Utils.Lang 578 579**参数:** 580 581| 参数名 | 类型 | 必填 | 说明 | 582| -------- | ------- | ---- | -------------------------------------| 583| targetClass | Object | 是 | 指定的类对象。 | 584| methodName | string | 是 | 指定的方法名,不支持read-only方法。 | 585| isStatic | boolean | 是 | 指定的原方法是否为静态方法,true表示静态方法,false表示实例方法。 | 586| before | Function | 是 | 要插入的函数对象。函数有参数,则第一个参数是this对象(若isStatic为true,则为类对象即targetClass;若isStatic为false,则为调用方法的实例对象),其余参数是原方法的参数。函数也可以无参数,无参时不做处理。 | 587 588**错误码:** 589 590以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 591 592| 错误码ID | 错误信息 | 593| -------- | -------- | 594| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 595 596**示例:** 597 598```ts 599class MyClass { 600 msg: string = 'msg000'; 601 foo(arg: string): string { 602 console.info('foo arg is ' + arg); 603 return this.msg; 604 } 605 606 static data: string = 'data000'; 607 static bar(arg: string): string { 608 console.info('bar arg is ' + arg); 609 return MyClass.data; 610 } 611} 612 613let asp = new MyClass(); 614let result = asp.foo('123'); 615// 输出结果:foo arg is 123 616console.info('result is ' + result); 617// 输出结果:result is msg000 618console.info('asp.msg is ' + asp.msg); 619// 输出结果:asp.msg is msg000 620 621util.Aspect.addBefore(MyClass, 'foo', false, (instance: MyClass, arg: string) => { 622 console.info('arg is ' + arg); 623 instance.msg = 'msg111'; 624 console.info('msg is changed to ' + instance.msg) 625}); 626 627result = asp.foo('123'); 628// 输出结果:arg is 123 629// 输出结果:msg is changed to msg111 630// 输出结果:foo arg is 123 631console.info('result is ' + result); 632// 输出结果:result is msg111 633console.info('asp.msg is ' + asp.msg); 634// 输出结果:asp.msg is msg111 635 636 637let res = MyClass.bar('456'); 638// 输出结果:bar arg is 456 639console.info('res is ' + res); 640// 输出结果:res is data000 641console.info('MyClass.data is ' + MyClass.data); 642// 输出结果:MyClass.data is data000 643 644util.Aspect.addBefore(MyClass, 'bar', true, (target: Object, arg: string) => { 645 console.info('arg is ' + arg); 646 let newVal = 'data111'; 647 Reflect.set(target, 'data', newVal); 648 console.info('data is changed to ' + newVal); 649}); 650 651res = MyClass.bar('456'); 652// 输出结果:arg is 456 653// 输出结果:data is changed to data111 654// 输出结果:bar arg is 456 655console.info('res is ' + res); 656// 输出结果:res is data111 657console.info('MyClass.data is ' + MyClass.data); 658// 输出结果:MyClass.data is data111 659``` 660 661### addAfter<sup>11+</sup> 662 663static addAfter(targetClass: Object, methodName: string, isStatic: boolean, after: Function): void 664 665在指定的类方法执行后插入一段逻辑。最终返回值是插入函数执行后的返回值。 666 667**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 668 669**系统能力:** SystemCapability.Utils.Lang 670 671**参数:** 672 673| 参数名 | 类型 | 必填 | 说明 | 674| -------- | ------- | ---- | -------------------------------------| 675| targetClass | Object | 是 | 指定的类对象。 | 676| methodName | string | 是 | 指定的原方法名,不支持read-only方法。 | 677| isStatic | boolean | 是 | 指定的原方法是否为静态方法,true表示静态方法,false表示实例方法。 | 678| after | Function | 是 | 要插入的函数。函数有参数时,则第一个参数是this对象(若isStatic为true,则为类对象即targetClass;若isStatic为false,则为调用方法的实例对象),第二个参数是原方法的返回值(如果原方法没有返回值,则为undefined),其余参数是原方法的参数。函数也可以无参,无参时不做处理。 | 679 680**错误码:** 681 682以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 683 684| 错误码ID | 错误信息 | 685| -------- | -------- | 686| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 687 688**示例:** 689 690```ts 691class MyClass { 692 msg: string = 'msg000'; 693 foo(arg: string): string { 694 console.info('foo arg is ' + arg); 695 return this.msg; 696 } 697} 698 699let asp = new MyClass(); 700let result = asp.foo('123'); 701// 输出结果:foo arg is 123 702console.info('result is ' + result); 703// 输出结果:result is msg000 704console.info('asp.msg is ' + asp.msg); 705// 输出结果:asp.msg is msg000 706 707util.Aspect.addAfter(MyClass, 'foo', false, (instance: MyClass, ret: string, arg: string): string => { 708 console.info('arg is ' + arg); 709 console.info('ret is ' + ret); 710 instance.msg = 'msg111'; 711 console.info('msg is changed to ' + instance.msg); 712 return 'msg222'; 713}); 714 715result = asp.foo('123'); 716// 输出结果:foo arg is 123 717// 输出结果:arg is 123 718// 输出结果:ret is msg000 719// 输出结果:msg is changed to msg111 720console.info('result is ' + result); 721// 输出结果:result is msg222 722console.info('asp.msg is ' + asp.msg); 723// 输出结果:asp.msg is msg111 724 725// 前后插桩的例子 726class AroundTest { 727 foo(arg: string) { 728 console.info('execute foo with arg ' + arg); 729 } 730} 731util.Aspect.addBefore(AroundTest, 'foo', false, () => { 732 console.info('execute before'); 733}); 734util.Aspect.addAfter(AroundTest, 'foo', false, () => { 735 console.info('execute after'); 736}); 737 738(new AroundTest()).foo('hello'); 739// 输出结果:execute before 740// 输出结果:execute foo with arg hello 741// 输出结果:execute after 742``` 743 744### replace<sup>11+</sup> 745 746static replace(targetClass: Object, methodName: string, isStatic: boolean, instead: Function) : void 747 748将指定的类方法的原方法替换为另一个函数。replace接口执行完成后,调用指定的类方法时,只会执行替换后的逻辑。最终返回值为替换函数执行完毕的返回值。 749 750**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 751 752**系统能力:** SystemCapability.Utils.Lang 753 754**参数:** 755 756| 参数名 | 类型 | 必填 | 说明 | 757| -------- | ------- | ---- | -------------------------------------| 758| targetClass | Object | 是 | 指定的类对象。 | 759| methodName | string | 是 | 指定的原方法名,不支持read-only方法。 | 760| isStatic | boolean | 是 | 指定的原方法是否为静态方法,true表示静态方法,false表示实例方法。 | 761| instead | Function | 是 | 要用来替换原方法的函数。函数有参数时,则第一个参数是this对象(若isStatic为true,则为类对象即targetClass;若isStatic为false,则为调用方法的实例对象),其余参数是原方法的参数。函数也可以无参,无参时不做处理。 | 762 763**错误码:** 764 765以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 766 767| 错误码ID | 错误信息 | 768| -------- | -------- | 769| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 770 771**示例:** 772 773```ts 774class MyClass { 775 msg: string = 'msg000'; 776 foo(arg: string): string { 777 console.info('foo arg is ' + arg); 778 return this.msg; 779 } 780} 781 782let asp = new MyClass(); 783let result = asp.foo('123'); 784// 输出结果:foo arg is 123 785console.info('result is ' + result); 786// 输出结果:result is msg000 787console.info('asp.msg is ' + asp.msg); 788// 输出结果:asp.msg is msg000 789 790util.Aspect.replace(MyClass, 'foo', false, (instance: MyClass, arg: string): string => { 791 console.info('execute instead') 792 console.info('arg is ' + arg); 793 instance.msg = 'msg111'; 794 console.info('msg is changed to ' + instance.msg); 795 return 'msg222'; 796}); 797 798result = asp.foo('123'); 799// 输出结果:execute instead 800// 输出结果:arg is 123 801// 输出结果:msg is changed to msg111 802console.info('result is ' + result); 803// 输出结果:result is msg222 804console.info('asp.msg is ' + asp.msg); 805// 输出结果:asp.msg is msg111 806``` 807 808## TextDecoder 809 810TextDecoder用于将字节数组解码为字符串,可以处理多种编码格式,包括utf-8、utf-16le/be、iso-8859和windows-1251等不同的编码格式。 811 812### 属性 813 814**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 815 816**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang。 817 818| 名称 | 类型 | 可读 | 可写 | 说明 | 819| -------- | -------- | -------- | -------- | -------- | 820| encoding | string | 是 | 否 | 编码格式。<br/>- 支持格式:utf-8、ibm866、iso-8859-2、iso-8859-3、iso-8859-4、iso-8859-5、iso-8859-6、iso-8859-7、iso-8859-8、iso-8859-8-i、iso-8859-10、iso-8859-13、iso-8859-14、iso-8859-15、koi8-r、koi8-u、macintosh、windows-874、windows-1250、windows-1251、windows-1252、windows-1253、windows-1254、windows-1255、windows-1256、windows-1257、windows-1258、x-mac-cyrillic、gbk、gb18030、big5、euc-jp、iso-2022-jp、shift_jis、euc-kr、utf-16be、utf-16le、UTF-8、GBK、GB2312、gb2312、GB18030、iso-8859-1。 | 821| fatal | boolean | 是 | 否 | 是否显示致命错误。 | 822| ignoreBOM | boolean | 是 | 否 | 是否忽略BOM(byte order marker)标记,默认值为false ,表示解码结果包含BOM标记。 | 823 824### constructor<sup>9+</sup> 825 826constructor() 827 828TextDecoder的构造函数。 829 830**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 831 832**系统能力:** SystemCapability.Utils.Lang 833 834**示例:** 835 836```ts 837let textDecoder = new util.TextDecoder(); 838let retStr = textDecoder.encoding; 839console.info('retStr = ' + retStr); 840// 输出结果:retStr = utf-8 841``` 842### create<sup>9+</sup> 843 844static create(encoding?: string, options?: TextDecoderOptions): TextDecoder 845 846替代有参构造功能。 847 848**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 849 850**系统能力:** SystemCapability.Utils.Lang 851 852**参数:** 853 854| 参数名 | 类型 | 必填 | 说明 | 855| -------- | ------ | ---- | ------------------------------------------------ | 856| encoding | string | 否 | 编码格式,默认值是'utf-8'。 | 857| options | [TextDecoderOptions](#textdecoderoptions11) | 否 | 解码相关选项参数,存在两个属性fatal和ignoreBOM。| 858 859**错误码:** 860 861以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 862 863| 错误码ID | 错误信息 | 864| -------- | -------- | 865| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 866 867**示例:** 868 869```ts 870let textDecoderOptions: util.TextDecoderOptions = { 871 fatal: false, 872 ignoreBOM : true 873} 874let textDecoder = util.TextDecoder.create('utf-8', textDecoderOptions); 875let retStr = textDecoder.encoding; 876console.info('retStr = ' + retStr); 877// 输出结果:retStr = utf-8 878``` 879 880### decodeToString<sup>12+</sup> 881 882decodeToString(input: Uint8Array, options?: DecodeToStringOptions): string 883 884通过输入参数解码后输出对应文本。 885 886**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 887 888**系统能力:** SystemCapability.Utils.Lang 889 890**参数:** 891 892| 参数名 | 类型 | 必填 | 说明 | 893| -------- | -------- | -------- | -------- | 894| input | Uint8Array | 是 | 符合格式需要解码的数组。 | 895| options | [DecodeToStringOptions](#decodetostringoptions12) | 否 | 解码相关选项参数。默认undefined。| 896 897**返回值:** 898 899| 类型 | 说明 | 900| -------- | -------- | 901| string | 解码后的数据。 | 902 903**错误码:** 904 905以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 906 907| 错误码ID | 错误信息 | 908| -------- | -------- | 909| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 910 911**示例:** 912 913```ts 914let textDecoderOptions: util.TextDecoderOptions = { 915 fatal: false, 916 ignoreBOM : true 917} 918let decodeToStringOptions: util.DecodeToStringOptions = { 919 stream: false 920} 921let textDecoder = util.TextDecoder.create('utf-8', textDecoderOptions); 922let uint8 = new Uint8Array([0xEF, 0xBB, 0xBF, 0x61, 0x62, 0x63]); 923let retStr = textDecoder.decodeToString(uint8, decodeToStringOptions); 924console.info("retStr = " + retStr); 925// 输出结果:retStr = abc 926``` 927 928### decodeWithStream<sup>(deprecated)</sup> 929 930decodeWithStream(input: Uint8Array, options?: DecodeWithStreamOptions): string 931 932通过输入参数解码后输出对应文本。当input是一个空数组时,返回值为undefined。 933 934> **说明:** 935> 936> 从API version 9开始支持,从API version 12开始废弃,建议使用[decodeToString<sup>12+</sup>](#decodetostring12)替代。 937 938**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 939 940**系统能力:** SystemCapability.Utils.Lang 941 942**参数:** 943 944| 参数名 | 类型 | 必填 | 说明 | 945| -------- | -------- | -------- | -------- | 946| input | Uint8Array | 是 | 符合格式需要解码的数组。 | 947| options | [DecodeWithStreamOptions](#decodewithstreamoptions11) | 否 | 解码相关选项参数。 | 948 949**返回值:** 950 951| 类型 | 说明 | 952| -------- | -------- | 953| string | 解码后的数据。 | 954 955**错误码:** 956 957以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 958 959| 错误码ID | 错误信息 | 960| -------- | -------- | 961| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 962 963**示例:** 964 965```ts 966let textDecoderOptions: util.TextDecoderOptions = { 967 fatal: false, 968 ignoreBOM : true 969} 970let decodeWithStreamOptions: util.DecodeWithStreamOptions = { 971 stream: false 972} 973let textDecoder = util.TextDecoder.create('utf-8', textDecoderOptions); 974let uint8 = new Uint8Array(6); 975uint8[0] = 0xEF; 976uint8[1] = 0xBB; 977uint8[2] = 0xBF; 978uint8[3] = 0x61; 979uint8[4] = 0x62; 980uint8[5] = 0x63; 981console.info("input num:"); 982let retStr = textDecoder.decodeWithStream(uint8, decodeWithStreamOptions); 983console.info("retStr = " + retStr); 984// 输出结果:retStr = abc 985``` 986 987### constructor<sup>(deprecated)</sup> 988 989constructor(encoding?: string, options?: { fatal?: boolean; ignoreBOM?: boolean }) 990 991TextDecoder的构造函数。 992 993> **说明:** 994> 995> 从API version 7开始支持,从API version 9开始废弃,建议使用[create<sup>9+</sup>](#create9)替代。 996 997**系统能力:** SystemCapability.Utils.Lang 998 999**参数:** 1000 1001| 参数名 | 类型 | 必填 | 说明 | 1002| -------- | -------- | -------- | -------- | 1003| encoding | string | 否 | 编码格式,默认值是'utf-8'。 | 1004| options | object | 否 | 解码相关选项参数,存在两个属性fatal和ignoreBOM。 | 1005 1006 **表1** options 1007 1008| 名称 | 参数类型 | 必填 | 说明 | 1009| -------- | -------- | -------- | -------- | 1010| fatal | boolean | 否 | 是否显示致命错误,默认值是false。 | 1011| ignoreBOM | boolean | 否 | 是否忽略BOM标记,默认值是false。 | 1012 1013**示例:** 1014 1015```ts 1016let textDecoder = new util.TextDecoder("utf-8",{ignoreBOM: true}); 1017``` 1018 1019### decode<sup>(deprecated)</sup> 1020 1021decode(input: Uint8Array, options?: { stream?: false }): string 1022 1023通过输入参数解码后输出对应文本。 1024 1025> **说明:** 1026> 1027> 从API version 7开始支持,从API version 9开始废弃,建议使用[decodeToString<sup>12+</sup>](#decodetostring12)替代。 1028 1029**系统能力:** SystemCapability.Utils.Lang 1030 1031**参数:** 1032 1033| 参数名 | 类型 | 必填 | 说明 | 1034| -------- | -------- | -------- | -------- | 1035| input | Uint8Array | 是 | 符合格式需要解码的数组。 | 1036| options | object | 否 | 解码相关选项参数。 | 1037 1038**表2** options 1039 1040| 名称 | 参数类型 | 必填 | 说明 | 1041| -------- | -------- | -------- | -------- | 1042| stream | boolean | 否 | 在随后的decode()调用中是否跟随附加数据块。如果以块的形式处理数据,则设置为true;如果处理最后的数据块或数据未分块,则设置为false。默认为false。 | 1043 1044**返回值:** 1045 1046| 类型 | 说明 | 1047| -------- | -------- | 1048| string | 解码后的数据。 | 1049 1050**示例:** 1051 1052```ts 1053let textDecoder = new util.TextDecoder("utf-8",{ignoreBOM: true}); 1054let uint8 = new Uint8Array(6); 1055uint8[0] = 0xEF; 1056uint8[1] = 0xBB; 1057uint8[2] = 0xBF; 1058uint8[3] = 0x61; 1059uint8[4] = 0x62; 1060uint8[5] = 0x63; 1061console.info("input num:"); 1062let retStr = textDecoder.decode(uint8, {stream: false}); 1063console.info("retStr = " + retStr); 1064// 输出结果:retStr = abc 1065``` 1066 1067## EncodeIntoUint8ArrayInfo<sup>11+</sup> 1068 1069**系统能力:** SystemCapability.Utils.Lang 1070 1071编码后的文本。 1072 1073**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 1074 1075| 名称 | 类型 | 可读 |可写 | 说明 | 1076| --------- | -------- | -------- |-------- |------------------ | 1077| read | number | 是 | 否 |表示已读取的字符数。 | 1078| written | number | 是 |否 |表示已写入的字节数。 | 1079 1080 1081## TextEncoder 1082 1083TextEncoder用于将字符串编码为字节数组,支持多种编码格式。 1084需要注意的是,在使用TextEncoder进行编码时,不同编码格式下字符所占的字节数是不同的,在使用TextEncoder时需要明确指定要使用的编码格式,以确保编码结果正确。 1085 1086### 属性 1087 1088**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1089 1090**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang。 1091 1092| 名称 | 类型 | 可读 | 可写 | 说明 | 1093| -------- | -------- | -------- | -------- | -------- | 1094| encoding | string | 是 | 否 | 编码格式。<br/>- 支持格式:utf-8、UTF-8、GBK、GB2312、gb2312、GB18030、gb18030、ibm866、iso-8859-1、iso-8859-2、iso-8859-3、iso-8859-4、iso-8859-5、iso-8859-6、iso-8859-7、iso-8859-8、iso-8859-8-i、iso-8859-10、iso-8859-13、iso-8859-14、iso-8859-15、koi8-r、koi8-u、macintosh、windows-874、windows-1250、windows-1251、windows-1252、windows-1253、windows-1254、windows-1255、windows-1256、windows-1257、windows-1258、gbk、big5、euc-jp、iso-2022-jp、shift_jis、euc-kr、x-mac-cyrillic、utf-16be、utf-16le。 <br/>- 默认值是:'utf-8'。 | 1095 1096 1097### constructor 1098 1099constructor() 1100 1101TextEncoder的构造函数。 1102 1103**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 1104 1105**系统能力:** SystemCapability.Utils.Lang 1106 1107**示例:** 1108 1109```ts 1110let textEncoder = new util.TextEncoder(); 1111``` 1112 1113### constructor<sup>9+</sup> 1114 1115constructor(encoding?: string) 1116 1117TextEncoder的构造函数。 1118 1119**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 1120 1121**系统能力:** SystemCapability.Utils.Lang 1122 1123**参数:** 1124 1125| 参数名 | 类型 | 必填 | 说明 | 1126| ----- | ---- | ---- | ---- | 1127| encoding | string | 否 | 编码格式,默认值为'utf-8'。 | 1128 1129**错误码:** 1130 1131以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1132 1133| 错误码ID | 错误信息 | 1134| -------- | -------- | 1135| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 1136 1137**示例:** 1138 1139```ts 1140let textEncoder = new util.TextEncoder("utf-8"); 1141``` 1142 1143### create<sup>12+</sup> 1144 1145static create(encoding?: string): TextEncoder 1146 1147创建TextEncoder对象的方法。 1148 1149**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1150 1151**系统能力:** SystemCapability.Utils.Lang 1152 1153**参数:** 1154 1155| 参数名 | 类型 | 必填 | 说明 | 1156| ----- | ---- | ---- | ---- | 1157| encoding | string | 否 | 编码格式,默认值为'utf-8'。 | 1158 1159**错误码:** 1160 1161以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1162 1163| 错误码ID | 错误信息 | 1164| -------- | -------- | 1165| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 1166 1167**示例:** 1168 1169```ts 1170let textEncoder = util.TextEncoder.create("utf-8"); 1171``` 1172 1173### encodeInto<sup>9+</sup> 1174 1175encodeInto(input?: string): Uint8Array 1176 1177通过输入参数编码后输出Uint8Array对象。 1178 1179**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 1180 1181**系统能力:** SystemCapability.Utils.Lang 1182 1183**参数:** 1184 1185| 参数名 | 类型 | 必填 | 说明 | 1186| ------ | ------ | ---- | ------------------ | 1187| input | string | 否 | 需要编码的字符串,默认值是空字符串。 | 1188 1189**返回值:** 1190 1191| 类型 | 说明 | 1192| ---------- | ------------------ | 1193| Uint8Array | 返回编码后的Uint8Array对象。 | 1194 1195**错误码:** 1196 1197以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1198 1199| 错误码ID | 错误信息 | 1200| -------- | -------- | 1201| 401 | Parameter error. Possible causes: 1. Incorrect parameter types. | 1202 1203**示例:** 1204 1205```ts 1206let textEncoder = new util.TextEncoder(); 1207let result = textEncoder.encodeInto("\uD800¥¥"); 1208console.info("result = " + result); 1209// 输出结果: result = 237,160,128,194,165,194,165 1210``` 1211 1212### encodeIntoUint8Array<sup>9+</sup> 1213 1214encodeIntoUint8Array(input: string, dest: Uint8Array): EncodeIntoUint8ArrayInfo 1215 1216对字符串进行编码,将结果写入dest数组。 1217 1218**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 1219 1220**系统能力:** SystemCapability.Utils.Lang 1221 1222**参数:** 1223 1224| 参数名 | 类型 | 必填 | 说明 | 1225| ------ | ---------- | ---- | ------------------------------------------------------- | 1226| input | string | 是 | 需要编码的字符串。 | 1227| dest | Uint8Array | 是 | Uint8Array对象实例,用于将生成的UTF-8编码文本放入其中。 | 1228 1229**返回值:** 1230 1231| 类型 | 说明 | 1232| ---------- | ------------------ | 1233| [EncodeIntoUint8ArrayInfo](#encodeintouint8arrayinfo11) | 返回一个对象,read表示已编码的字符数,write表示编码字符所占用的字节数。 | 1234 1235**错误码:** 1236 1237以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1238 1239| 错误码ID | 错误信息 | 1240| -------- | -------- | 1241| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1242 1243**示例:** 1244 1245```ts 1246let textEncoder = new util.TextEncoder(); 1247let buffer = new ArrayBuffer(4); 1248let uint8 = new Uint8Array(buffer); 1249let result = textEncoder.encodeIntoUint8Array('abcd', uint8); 1250console.info("uint8 = " + uint8); 1251// 输出结果: uint8 = 97,98,99,100 1252``` 1253 1254### encodeInto<sup>(deprecated)</sup> 1255 1256encodeInto(input: string, dest: Uint8Array): { read: number; written: number } 1257 1258放置生成的UTF-8编码文本。 1259 1260> **说明:** 1261> 1262> 从API version 7开始支持,从API version 9开始废弃,建议使用[encodeIntoUint8Array<sup>9+</sup>](#encodeintouint8array9)替代。 1263 1264**系统能力:** SystemCapability.Utils.Lang 1265 1266**参数:** 1267 1268| 参数名 | 类型 | 必填 | 说明 | 1269| -------- | -------- | -------- | -------- | 1270| input | string | 是 | 需要编码的字符串。 | 1271| dest | Uint8Array | 是 | Uint8Array对象实例,用于将生成的UTF-8编码文本放入其中。 | 1272 1273**返回值:** 1274 1275| 类型 | 说明 | 1276| -------- | -------- | 1277| Uint8Array | 返回编码后的文本。 | 1278 1279**示例:** 1280 1281```ts 1282let textEncoder = new util.TextEncoder(); 1283let buffer = new ArrayBuffer(4); 1284let uint8 = new Uint8Array(buffer); 1285let result = textEncoder.encodeInto('abcd', uint8); 1286console.info("uint8 = " + uint8); 1287// 输出结果: uint8 = 97,98,99,100 1288``` 1289 1290### encode<sup>(deprecated)</sup> 1291 1292encode(input?: string): Uint8Array 1293 1294通过输入参数编码后输出对应文本。 1295 1296> **说明:** 1297> 1298> 从API version 7开始支持,从API version 9开始废弃,建议使用[encodeInto<sup>9+</sup>](#encodeinto9)替代。 1299 1300**系统能力:** SystemCapability.Utils.Lang 1301 1302**参数:** 1303 1304| 参数名 | 类型 | 必填 | 说明 | 1305| -------- | -------- | -------- | -------- | 1306| input | string | 否 | 需要编码的字符串,默认值是空字符串。 | 1307 1308**返回值:** 1309 1310| 类型 | 说明 | 1311| -------- | -------- | 1312| Uint8Array | 返回编码后的文本。 | 1313 1314**示例:** 1315 1316```ts 1317let textEncoder = new util.TextEncoder(); 1318let result = textEncoder.encode("\uD800¥¥"); 1319console.info("result = " + result); 1320// 输出结果: result = 237,160,128,194,165,194,165 1321``` 1322 1323## RationalNumber<sup>8+</sup> 1324 1325RationalNumber主要是对有理数进行比较,获取分子分母等方法。例如使用toString()方法可以将有理数转换为字符串形式,使用该类可以方便地进行有理数的各种操作。 1326 1327### constructor<sup>9+</sup> 1328 1329constructor() 1330 1331RationalNumber的构造函数。 1332 1333**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1334 1335**系统能力:** SystemCapability.Utils.Lang 1336 1337**示例:** 1338 1339```ts 1340let rationalNumber = new util.RationalNumber(); 1341``` 1342 1343### parseRationalNumber<sup>9+</sup> 1344 1345static parseRationalNumber(numerator: number,denominator: number): RationalNumber 1346 1347用于创建具有给定分子和分母的RationalNumber实例。 1348 1349> **说明:** 1350> 1351> 该接口要求参数numerator和denominator必须是整数类型。如果传入的参数是小数类型,不会进行拦截,但是会输出错误信息:"parseRationalNumber: The type of Parameter must be integer"。 1352 1353**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1354 1355**系统能力:** SystemCapability.Utils.Lang 1356 1357**参数:** 1358 1359| 参数名 | 类型 | 必填 | 说明 | 1360| ----------- | ------ | ---- | ---------------- | 1361| numerator | number | 是 | 分子,整数类型。取值范围:-Number.MAX_VALUE <= numerator <= Number.MAX_VALUE。| 1362| denominator | number | 是 | 分母,整数类型。取值范围:-Number.MAX_VALUE <= denominator <= Number.MAX_VALUE。| 1363 1364**错误码:** 1365 1366以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1367 1368| 错误码ID | 错误信息 | 1369| -------- | -------- | 1370| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1371 1372**示例:** 1373 1374```ts 1375let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1376``` 1377 1378### createRationalFromString<sup>8+</sup> 1379 1380static createRationalFromString(rationalString: string): RationalNumber 1381 1382基于给定的字符串创建一个RationalNumber对象。 1383 1384> **说明:** 1385> 1386> 该接口要求参数rationalString是字符串格式。如果传入的参数是小数类型字符串格式,不会进行拦截,但是会输出错误信息:"createRationalFromString: The type of Parameter must be integer string"。 1387 1388**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1389 1390**系统能力:** SystemCapability.Utils.Lang 1391 1392**参数:** 1393 1394| 参数名 | 类型 | 必填 | 说明 | 1395| -------- | -------- | -------- | -------- | 1396| rationalString | string | 是 | 字符串格式。 | 1397 1398**返回值:** 1399 1400| 类型 | 说明 | 1401| -------- | -------- | 1402| Object | 返回RationalNumber对象。 | 1403 1404**错误码:** 1405 1406以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1407 1408| 错误码ID | 错误信息 | 1409| -------- | -------- | 1410| 401 | The type of rationalString must be string. | 1411 1412**示例:** 1413 1414```ts 1415let rational = util.RationalNumber.createRationalFromString("3/4"); 1416``` 1417 1418### compare<sup>9+</sup> 1419 1420compare(another: RationalNumber): number 1421 1422将当前RationalNumber对象与目标RationalNumber对象进行比较,并返回比较结果。 1423 1424**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1425 1426**系统能力:** SystemCapability.Utils.Lang 1427 1428**参数:** 1429 1430| 参数名 | 类型 | 必填 | 说明 | 1431| ------- | -------------- | ---- | ------------------ | 1432| another | [RationalNumber](#rationalnumber8) | 是 | 其他的有理数对象。 | 1433 1434**返回值:** 1435 1436| 类型 | 说明 | 1437| ------ | ------------------------------------------------------------ | 1438| number | 如果两个对象相等,则返回0;如果给定对象小于当前对象,则返回1;如果给定对象大于当前对象,则返回-1。 | 1439 1440**错误码:** 1441 1442以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1443 1444| 错误码ID | 错误信息 | 1445| -------- | -------- | 1446| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1447 1448**示例:** 1449 1450```ts 1451let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1452let rational = util.RationalNumber.createRationalFromString("3/4"); 1453let result = rationalNumber.compare(rational); 1454console.info("result = " + result); 1455// 输出结果:result = -1 1456``` 1457 1458### valueOf<sup>8+</sup> 1459 1460valueOf(): number 1461 1462以整数形式或者浮点数的形式获取当前RationalNumber对象的值。 1463 1464**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1465 1466**系统能力:** SystemCapability.Utils.Lang 1467 1468**返回值:** 1469 1470| 类型 | 说明 | 1471| -------- | -------- | 1472| number | 返回整数或者浮点数的值。 | 1473 1474**示例:** 1475 1476```ts 1477let rationalNumber = new util.RationalNumber(1,2); 1478let result = rationalNumber.valueOf(); 1479console.info("result = " + result); 1480// 输出结果:result = 0.5 1481``` 1482API 9及以上建议使用以下写法: 1483```ts 1484let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1485let result = rationalNumber.valueOf(); 1486console.info("result = " + result); 1487// 输出结果:result = 0.5 1488``` 1489 1490### equals<sup>8+</sup> 1491 1492equals(obj: Object): boolean 1493 1494将当前的RationalNumber对象与给定的对象进行比较是否相等。 1495 1496**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1497 1498**系统能力:** SystemCapability.Utils.Lang 1499 1500**参数:** 1501 1502| 参数名 | 类型 | 必填 | 说明 | 1503| -------- | -------- | -------- | -------- | 1504| obj | Object | 是 | 其他类型对象。 | 1505 1506**返回值:** 1507 1508| 类型 | 说明 | 1509| -------- | -------- | 1510| boolean | 如果给定对象与当前对象相同,则返回true;否则返回false。 | 1511 1512**示例:** 1513 1514```ts 1515let rationalNumber = new util.RationalNumber(1,2); 1516let rational = util.RationalNumber.createRationalFromString("3/4"); 1517let result = rationalNumber.equals(rational); 1518console.info("result = " + result); 1519// 输出结果:result = false 1520``` 1521API 9及以上建议使用以下写法: 1522```ts 1523let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1524let rational = util.RationalNumber.createRationalFromString("3/4"); 1525let result = rationalNumber.equals(rational); 1526console.info("result = " + result); 1527// 输出结果:result = false 1528``` 1529 1530### getCommonFactor<sup>9+</sup> 1531 1532static getCommonFactor(number1: number,number2: number): number 1533 1534获取两个指定整数的最大公约数。 1535 1536> **说明:** 1537> 1538> 该接口要求参数number1和number2必须是整数类型。如果传入的参数是小数类型,不会进行拦截,但是会输出错误信息:"getCommonFactor: The type of Parameter must be integer"。 1539 1540**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1541 1542**系统能力:** SystemCapability.Utils.Lang 1543 1544**参数:** 1545 1546| 参数名 | 类型 | 必填 | 说明 | 1547| ------- | ------ | ---- | ---------- | 1548| number1 | number | 是 | 整数类型。-Number.MAX_VALUE <= number1 <= Number.MAX_VALUE。| 1549| number2 | number | 是 | 整数类型。-Number.MAX_VALUE <= number2 <= Number.MAX_VALUE。| 1550 1551**返回值:** 1552 1553| 类型 | 说明 | 1554| ------ | ------------------------------ | 1555| number | 返回两个给定数字的最大公约数。 | 1556 1557**错误码:** 1558 1559以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1560 1561| 错误码ID | 错误信息 | 1562| -------- | -------- | 1563| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1564 1565**示例:** 1566 1567```ts 1568let result = util.RationalNumber.getCommonFactor(4,6); 1569console.info("result = " + result); 1570// 输出结果:result = 2 1571``` 1572 1573### getNumerator<sup>8+</sup> 1574 1575getNumerator(): number 1576 1577获取当前RationalNumber对象的分子。 1578 1579**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1580 1581**系统能力:** SystemCapability.Utils.Lang 1582 1583**返回值:** 1584 1585| 类型 | 说明 | 1586| -------- | -------- | 1587| number | 返回RationalNumber对象的分子的值。 | 1588 1589**示例:** 1590 1591```ts 1592let rationalNumber = new util.RationalNumber(1,2); 1593let result = rationalNumber.getNumerator(); 1594console.info("result = " + result); 1595// 输出结果:result = 1 1596``` 1597API 9及以上建议使用以下写法: 1598```ts 1599let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1600let result = rationalNumber.getNumerator(); 1601console.info("result = " + result); 1602// 输出结果:result = 1 1603``` 1604 1605### getDenominator<sup>8+</sup> 1606 1607getDenominator(): number 1608 1609获取当前RationalNumber对象的分母。 1610 1611**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1612 1613**系统能力:** SystemCapability.Utils.Lang 1614 1615**返回值:** 1616 1617| 类型 | 说明 | 1618| -------- | -------- | 1619| number | 返回RationalNumber对象的分母的值。 | 1620 1621**示例:** 1622 1623```ts 1624let rationalNumber = new util.RationalNumber(1,2); 1625let result = rationalNumber.getDenominator(); 1626console.info("result = " + result); 1627// 输出结果:result = 2 1628``` 1629API 9及以上建议使用以下写法: 1630```ts 1631let rationalNumber = util.RationalNumber.parseRationalNumber(1,2) 1632let result = rationalNumber.getDenominator(); 1633console.info("result = " + result); 1634// 输出结果:result = 2 1635``` 1636 1637### isZero<sup>8+</sup> 1638 1639isZero():boolean 1640 1641检查当前RationalNumber对象是否为0。 1642 1643**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1644 1645**系统能力:** SystemCapability.Utils.Lang 1646 1647**返回值:** 1648 1649| 类型 | 说明 | 1650| -------- | -------- | 1651| boolean | 如果当前对象表示的值为0,则返回true;否则返回false。 | 1652 1653**示例:** 1654 1655```ts 1656let rationalNumber = new util.RationalNumber(1,2); 1657let result = rationalNumber.isZero(); 1658console.info("result = " + result); 1659// 输出结果:result = false 1660``` 1661API 9及以上建议使用以下写法: 1662```ts 1663let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1664let result = rationalNumber.isZero(); 1665console.info("result = " + result); 1666// 输出结果:result = false 1667``` 1668 1669### isNaN<sup>8+</sup> 1670 1671isNaN(): boolean 1672 1673检查当前RationalNumber对象是否表示非数字(NaN)值。 1674 1675**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1676 1677**系统能力:** SystemCapability.Utils.Lang 1678 1679**返回值:** 1680 1681| 类型 | 说明 | 1682| -------- | -------- | 1683| boolean | 如果分母和分子都为0,则返回true;否则返回false。 | 1684 1685**示例:** 1686 1687```ts 1688let rationalNumber = new util.RationalNumber(1,2); 1689let result = rationalNumber.isNaN(); 1690console.info("result = " + result); 1691// 输出结果:result = false 1692``` 1693API 9及以上建议使用以下写法: 1694```ts 1695let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1696let result = rationalNumber.isNaN(); 1697console.info("result = " + result); 1698// 输出结果:result = false 1699``` 1700 1701### isFinite<sup>8+</sup> 1702 1703isFinite():boolean 1704 1705检查当前RationalNumber对象是否表示一个有限值。 1706 1707**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1708 1709**系统能力:** SystemCapability.Utils.Lang 1710 1711**返回值:** 1712 1713| 类型 | 说明 | 1714| -------- | -------- | 1715| boolean | 如果分母不为0,则返回true;否则返回false。 | 1716 1717**示例:** 1718 1719```ts 1720let rationalNumber = new util.RationalNumber(1,2); 1721let result = rationalNumber.isFinite(); 1722console.info("result = " + result); 1723// 输出结果:result = true 1724``` 1725API 9及以上建议使用以下写法: 1726```ts 1727let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1728let result = rationalNumber.isFinite(); 1729console.info("result = " + result); 1730// 输出结果:result = true 1731``` 1732 1733### toString<sup>8+</sup> 1734 1735toString(): string 1736 1737获取当前RationalNumber对象的字符串表示形式。 1738 1739**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1740 1741**系统能力:** SystemCapability.Utils.Lang 1742 1743**返回值:** 1744 1745| 类型 | 说明 | 1746| -------- | -------- | 1747| string | 返回Numerator/Denominator格式的字符串,例如3/5,如果当前对象的分子为0,则返回0/1。如果当前对象的分母为0,则返回Infinity。如果当前对象的分子和分母都为0,则返回NaN。| 1748 1749**示例:** 1750 1751```ts 1752let rationalNumber = new util.RationalNumber(1,2); 1753let result = rationalNumber.toString(); 1754console.info("result = " + result); 1755// 输出结果:result = 1/2 1756``` 1757API 9及以上建议使用以下写法: 1758```ts 1759let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1760let result = rationalNumber.toString(); 1761console.info("result = " + result); 1762// 输出结果:result = 1/2 1763``` 1764 1765### constructor<sup>(deprecated)</sup> 1766 1767constructor(numerator: number,denominator: number) 1768 1769RationalNumber的构造函数。 1770 1771> **说明:** 1772> 1773> 从API version 8开始支持,从API version 9开始废弃,建议使用[parseRationalNumber<sup>9+</sup>](#parserationalnumber9)替代。 1774 1775**系统能力:** SystemCapability.Utils.Lang 1776 1777**参数:** 1778 1779| 参数名 | 类型 | 必填 | 说明 | 1780| -------- | -------- | -------- | -------- | 1781| numerator | number | 是 | 分子,整数类型。 | 1782| denominator | number | 是 | 分母,整数类型。 | 1783 1784**示例:** 1785 1786```ts 1787let rationalNumber = new util.RationalNumber(1,2); 1788``` 1789 1790### compareTo<sup>(deprecated)</sup> 1791 1792compareTo(another: RationalNumber): number 1793 1794将当前的RationalNumber对象与给定的对象进行比较。 1795 1796> **说明:** 1797> 1798> 从API version 8开始支持,从API version 9开始废弃,建议使用[compare<sup>9+</sup>](#compare9)替代。 1799 1800**系统能力:** SystemCapability.Utils.Lang 1801 1802**参数:** 1803 1804| 参数名 | 类型 | 必填 | 说明 | 1805| -------- | -------- | -------- | -------- | 1806| another | RationalNumber | 是 | 其他的有理数对象。 | 1807 1808**返回值:** 1809 1810| 类型 | 说明 | 1811| -------- | -------- | 1812| number | 如果两个对象相等,则返回0;如果给定对象小于当前对象,则返回1;如果给定对象大于当前对象,则返回-1。 | 1813 1814**示例:** 1815 1816```ts 1817let rationalNumber = new util.RationalNumber(1,2); 1818let rational = util.RationalNumber.createRationalFromString("3/4"); 1819let result = rationalNumber.compareTo(rational); 1820console.info("result = " + result); 1821// 输出结果:result = -1 1822``` 1823 1824### getCommonDivisor<sup>(deprecated)</sup> 1825 1826static getCommonDivisor(number1: number,number2: number): number 1827 1828获取两个指定整数的最大公约数。 1829 1830> **说明:** 1831> 1832> 从API version 8开始支持,从API version 9开始废弃,建议使用[getCommonFactor<sup>9+</sup>](#getcommonfactor9)替代。 1833 1834**系统能力:** SystemCapability.Utils.Lang 1835 1836**参数:** 1837 1838| 参数名 | 类型 | 必填 | 说明 | 1839| -------- | -------- | -------- | -------- | 1840| number1 | number | 是 | 整数类型。 | 1841| number2 | number | 是 | 整数类型。 | 1842 1843**返回值:** 1844 1845| 类型 | 说明 | 1846| -------- | -------- | 1847| number | 返回两个给定数字的最大公约数。 | 1848 1849 1850 1851## LRUCache<sup>9+</sup> 1852 1853LRUCache用于在缓存空间不够的时候,将近期最少使用的数据替换为新数据。此设计基于资源访问的考虑:近期访问的数据,可能在不久的将来会再次访问。于是最少访问的数据就是价值最小的数据,是最应该踢出缓存空间的数据。 1854 1855### 属性 1856 1857**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1858 1859**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang。 1860 1861| 名称 | 类型 | 可读 | 可写 | 说明 | 1862| ------ | ------ | ---- | ---- | ---------------------- | 1863| length | number | 是 | 否 | 当前缓冲区中值的总数。 | 1864 1865**示例:** 1866 1867```ts 1868let pro = new util.LRUCache<number, number>(); 1869pro.put(2, 10); 1870pro.put(1, 8); 1871let result = pro.length; 1872console.info('result = ' + result); 1873// 输出结果:result = 2 1874``` 1875 1876### constructor<sup>9+</sup> 1877 1878constructor(capacity?: number) 1879 1880默认构造函数用于创建一个新的LRUCache实例,默认容量为64。 1881 1882**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1883 1884**系统能力:** SystemCapability.Utils.Lang 1885 1886**参数:** 1887 1888| 参数名 | 类型 | 必填 | 说明 | 1889| -------- | ------ | ---- | ---------------------------- | 1890| capacity | number | 否 | 指示要为缓冲区自定义的容量,不传默认值为64,最大值不能超过2147483647。 | 1891 1892**错误码:** 1893 1894以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1895 1896| 错误码ID | 错误信息 | 1897| -------- | -------- | 1898| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1899 1900**示例:** 1901 1902```ts 1903let pro = new util.LRUCache<number, number>(); 1904``` 1905 1906 1907### updateCapacity<sup>9+</sup> 1908 1909updateCapacity(newCapacity: number): void 1910 1911将缓冲区容量更新为指定容量,如果newCapacity小于或等于0,则抛出异常。当缓冲区中值的总数大于指定容量时,会执行删除操作。 1912 1913**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1914 1915**系统能力:** SystemCapability.Utils.Lang 1916 1917**参数:** 1918 1919| 参数名 | 类型 | 必填 | 说明 | 1920| ----------- | ------ | ---- | ---------------------------- | 1921| newCapacity | number | 是 | 指示要为缓冲区自定义的容量,最大值不能超过2147483647。 | 1922 1923**错误码:** 1924 1925以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1926 1927| 错误码ID | 错误信息 | 1928| -------- | -------- | 1929| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. | 1930 1931**示例:** 1932 1933```ts 1934let pro = new util.LRUCache<number, number>(); 1935pro.updateCapacity(100); 1936``` 1937 1938### toString<sup>9+</sup> 1939 1940toString(): string 1941 1942返回对象的字符串表示形式。 1943 1944**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1945 1946**系统能力:** SystemCapability.Utils.Lang 1947 1948**返回值:** 1949 1950| 类型 | 说明 | 1951| ------ | -------------------------- | 1952| string | 返回对象的字符串表示形式。 | 1953 1954**示例:** 1955 1956```ts 1957let pro = new util.LRUCache<number, number>(); 1958pro.put(2, 10); 1959pro.get(2); 1960pro.get(3); 1961console.info(pro.toString()); 1962// 输出结果:LRUCache[ maxSize = 64, hits = 1, misses = 1, hitRate = 50% ] 1963// maxSize: 缓存区最大值 hits: 查询值匹配成功的次数 misses: 查询值匹配失败的次数 hitRate: 查询值匹配率 1964``` 1965 1966### getCapacity<sup>9+</sup> 1967 1968getCapacity(): number 1969 1970获取当前缓冲区的容量。 1971 1972**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1973 1974**系统能力:** SystemCapability.Utils.Lang 1975 1976**返回值:** 1977 1978| 类型 | 说明 | 1979| ------ | ---------------------- | 1980| number | 返回当前缓冲区的容量。 | 1981 1982**示例:** 1983 1984```ts 1985let pro = new util.LRUCache<number, number>(); 1986let result = pro.getCapacity(); 1987console.info('result = ' + result); 1988// 输出结果:result = 64 1989``` 1990 1991### clear<sup>9+</sup> 1992 1993clear(): void 1994 1995从当前缓冲区清除键值对。 1996 1997**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1998 1999**系统能力:** SystemCapability.Utils.Lang 2000 2001**示例:** 2002 2003```ts 2004let pro = new util.LRUCache<number, number>(); 2005pro.put(2, 10); 2006let result = pro.length; 2007pro.clear(); 2008let res = pro.length; 2009console.info('result = ' + result); 2010console.info('res = ' + res); 2011// 输出结果:result = 1 2012// 输出结果:res = 0 2013``` 2014 2015### getCreateCount<sup>9+</sup> 2016 2017getCreateCount(): number 2018 2019获取创建对象的次数。 2020 2021**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2022 2023**系统能力:** SystemCapability.Utils.Lang 2024 2025**返回值:** 2026 2027| 类型 | 说明 | 2028| ------ | -------------------| 2029| number | 返回创建对象的次数。 | 2030 2031**示例:** 2032 2033```ts 2034// 创建新类ChildLRUCache继承LRUCache,重写createDefault方法,返回一个非undefined的值。 2035class ChildLRUCache extends util.LRUCache<number, number> { 2036 constructor() { 2037 super(); 2038 } 2039 2040 createDefault(key: number): number { 2041 return key; 2042 } 2043} 2044let lru = new ChildLRUCache(); 2045lru.put(2, 10); 2046lru.get(3); 2047lru.get(5); 2048let res = lru.getCreateCount(); 2049console.info('res = ' + res); 2050// 输出结果:res = 2 2051``` 2052 2053### getMissCount<sup>9+</sup> 2054 2055getMissCount(): number 2056 2057获取查询值不匹配的次数。 2058 2059**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2060 2061**系统能力:** SystemCapability.Utils.Lang 2062 2063**返回值:** 2064 2065| 类型 | 说明 | 2066| ------ | ------------------------ | 2067| number | 返回查询值不匹配的次数。 | 2068 2069**示例:** 2070 2071```ts 2072let pro = new util.LRUCache<number, number>(); 2073pro.put(2, 10); 2074pro.get(2); 2075let result = pro.getMissCount(); 2076console.info('result = ' + result); 2077// 输出结果:result = 0 2078``` 2079 2080### getRemovalCount<sup>9+</sup> 2081 2082getRemovalCount(): number 2083 2084获取缓冲区键值对回收的次数。 2085 2086**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2087 2088**系统能力:** SystemCapability.Utils.Lang 2089 2090**返回值:** 2091 2092| 类型 | 说明 | 2093| ------ | -------------------------- | 2094| number | 返回缓冲区键值对回收的次数。 | 2095 2096**示例:** 2097 2098```ts 2099let pro = new util.LRUCache<number, number>(); 2100pro.put(2, 10); 2101pro.updateCapacity(2); 2102pro.put(50, 22); 2103let result = pro.getRemovalCount(); 2104console.info('result = ' + result); 2105// 输出结果:result = 0 2106``` 2107 2108### getMatchCount<sup>9+</sup> 2109 2110getMatchCount(): number 2111 2112获取查询值匹配成功的次数。 2113 2114**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2115 2116**系统能力:** SystemCapability.Utils.Lang 2117 2118**返回值:** 2119 2120| 类型 | 说明 | 2121| ------ | -------------------------- | 2122| number | 返回查询值匹配成功的次数。 | 2123 2124**示例:** 2125 2126 ```ts 2127 let pro = new util.LRUCache<number, number>(); 2128 pro.put(2, 10); 2129 pro.get(2); 2130 let result = pro.getMatchCount(); 2131 console.info('result = ' + result); 2132 // 输出结果:result = 1 2133 ``` 2134 2135### getPutCount<sup>9+</sup> 2136 2137getPutCount(): number 2138 2139获取将值添加到缓冲区的次数。 2140 2141**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2142 2143**系统能力:** SystemCapability.Utils.Lang 2144 2145**返回值:** 2146 2147| 类型 | 说明 | 2148| ------ | ---------------------------- | 2149| number | 返回将值添加到缓冲区的次数。 | 2150 2151**示例:** 2152 2153```ts 2154let pro = new util.LRUCache<number, number>(); 2155pro.put(2, 10); 2156let result = pro.getPutCount(); 2157console.info('result = ' + result); 2158// 输出结果:result = 1 2159``` 2160 2161### isEmpty<sup>9+</sup> 2162 2163isEmpty(): boolean 2164 2165检查当前缓冲区是否为空。 2166 2167**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2168 2169**系统能力:** SystemCapability.Utils.Lang 2170 2171**返回值:** 2172 2173| 类型 | 说明 | 2174| ------- | ---------------------------------------- | 2175| boolean | 如果当前缓冲区不包含任何值,则返回true。 | 2176 2177**示例:** 2178 2179```ts 2180let pro = new util.LRUCache<number, number>(); 2181pro.put(2, 10); 2182let result = pro.isEmpty(); 2183console.info('result = ' + result); 2184// 输出结果:result = false 2185``` 2186 2187### get<sup>9+</sup> 2188 2189get(key: K): V | undefined 2190 2191返回键对应的值。当键不在缓冲区中时,通过[createDefault<sup>9+</sup>](#createdefault9)接口创建,若createDefault创建的值不为undefined时,此时会调用[afterRemoval<sup>9+</sup>](#afterremoval9)接口,返回createDefault创建的值。 2192 2193**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2194 2195**系统能力:** SystemCapability.Utils.Lang 2196 2197**参数:** 2198 2199| 参数名 | 类型 | 必填 | 说明 | 2200| ------ | ---- | ---- | ------------ | 2201| key | K | 是 | 要查询的键。 | 2202 2203**返回值:** 2204 2205| 类型 | 说明 | 2206| ------------------------ | ------------------------------------------------------------ | 2207| V \| undefined | 如果指定的键存在于缓冲区中,则返回与键关联的值;否则返回createDefault创建的值。 | 2208 2209**错误码:** 2210 2211以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2212 2213| 错误码ID | 错误信息 | 2214| -------- | -------- | 2215| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2216 2217**示例:** 2218 2219```ts 2220let pro = new util.LRUCache<number, number>(); 2221pro.put(2, 10); 2222let result = pro.get(2); 2223console.info('result = ' + result); 2224// 输出结果:result = 10 2225``` 2226 2227### put<sup>9+</sup> 2228 2229put(key: K,value: V): V 2230 2231将键值对添加到缓冲区中,返回与添加的键关联的值。当缓冲区中值的总数大于容量时,会执行删除操作。 2232 2233**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2234 2235**系统能力:** SystemCapability.Utils.Lang 2236 2237**参数:** 2238 2239| 参数名 | 类型 | 必填 | 说明 | 2240| ------ | ---- | ---- | -------------------------- | 2241| key | K | 是 | 要添加的键。 | 2242| value | V | 是 | 指示与要添加的键关联的值。 | 2243 2244**返回值:** 2245 2246| 类型 | 说明 | 2247| ---- | ------------------------------------------------------------ | 2248| V | 返回与添加的键关联的值。如果键或值为空,则抛出此异常。 | 2249 2250**错误码:** 2251 2252以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2253 2254| 错误码ID | 错误信息 | 2255| -------- | -------- | 2256| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2257 2258**示例:** 2259 2260```ts 2261let pro = new util.LRUCache<number, number>(); 2262let result = pro.put(2, 10); 2263console.info('result = ' + result); 2264// 输出结果:result = 10 2265``` 2266 2267### values<sup>9+</sup> 2268 2269values(): V[] 2270 2271获取当前缓冲区中所有值从最近访问到最近最少访问的顺序列表 。 2272 2273**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2274 2275**系统能力:** SystemCapability.Utils.Lang 2276 2277**返回值:** 2278 2279| 类型 | 说明 | 2280| --------- | ------------------------------------------------------------ | 2281| V [] | 按从最近访问到最近最少访问的顺序返回当前缓冲区中所有值的列表。 | 2282 2283**示例:** 2284 2285```ts 2286let pro = new util.LRUCache<number|string,number|string>(); 2287pro.put(2, 10); 2288pro.put(2, "anhu"); 2289pro.put("afaf", "grfb"); 2290let result = pro.values(); 2291console.info('result = ' + result); 2292// 输出结果:result = anhu,grfb 2293``` 2294 2295### keys<sup>9+</sup> 2296 2297keys(): K[] 2298 2299获取当前缓冲区中所有键从最近访问到最近最少访问的升序列表。 2300 2301**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2302 2303**系统能力:** SystemCapability.Utils.Lang 2304 2305**返回值:** 2306 2307| 类型 | 说明 | 2308| --------- | ------------------------------------------------------------ | 2309| K [] | 按升序返回当前缓冲区中所有键的列表,从最近访问到最近最少访问。 | 2310 2311**示例:** 2312 2313```ts 2314let pro = new util.LRUCache<number, number>(); 2315pro.put(2, 10); 2316pro.put(3, 1); 2317let result = pro.keys(); 2318console.info('result = ' + result); 2319// 输出结果:result = 2,3 2320``` 2321 2322### remove<sup>9+</sup> 2323 2324remove(key: K): V | undefined 2325 2326从当前缓冲区中删除指定的键及其关联的值,返回键关联的值。如果键不存在时,则返回undefined。 2327 2328**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2329 2330**系统能力:** SystemCapability.Utils.Lang 2331 2332**参数:** 2333 2334| 参数名 | 类型 | 必填 | 说明 | 2335| ------ | ---- | ---- | -------------- | 2336| key | K | 是 | 要删除的键值。 | 2337 2338**返回值:** 2339 2340| 类型 | 说明 | 2341| ------------------------ | ------------------------------------------------------------ | 2342| V \| undefined | 返回一个包含已删除键值对的Optional对象;如果key不存在,则返回undefined,如果key为null,则抛出异常。 | 2343 2344**错误码:** 2345 2346以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2347 2348| 错误码ID | 错误信息 | 2349| -------- | -------- | 2350| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2351 2352**示例:** 2353 2354```ts 2355let pro = new util.LRUCache<number, number>(); 2356pro.put(2, 10); 2357let result = pro.remove(20); 2358console.info('result = ' + result); 2359// 输出结果:result = undefined 2360``` 2361 2362### afterRemoval<sup>9+</sup> 2363 2364afterRemoval(isEvict: boolean,key: K,value: V,newValue: V): void 2365 2366删除值后执行后续操作,后续操作由开发者自行实现。本接口会在删除操作时被调用,如[get<sup>9+</sup>](#get9)、[put<sup>9+</sup>](#put9)、[remove<sup>9+</sup>](#remove9)、[clear<sup>9+</sup>](#clear9)、[updateCapacity<sup>9+</sup>](#updatecapacity9)接口。 2367 2368**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2369 2370**系统能力:** SystemCapability.Utils.Lang 2371 2372**参数:** 2373 2374| 参数名 | 类型 | 必填 | 说明 | 2375| -------- | ------- | ---- | ------------------------------------------------------------ | 2376| isEvict | boolean | 是 | 因容量不足而调用该方法时,参数值为true,其他情况为false。 | 2377| key | K | 是 | 表示删除的键。 | 2378| value | V | 是 | 表示删除的值。 | 2379| newValue | V | 是 | 如果已调用put方法并且要添加的键已经存在,则参数值是关联的新值。其他情况下参数值为空。 | 2380 2381**错误码:** 2382 2383以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2384 2385| 错误码ID | 错误信息 | 2386| -------- | -------- | 2387| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2388 2389**示例:** 2390 2391```ts 2392class ChildLRUCache<K, V> extends util.LRUCache<K, V> { 2393 constructor(capacity?: number) { 2394 super(capacity); 2395 } 2396 2397 afterRemoval(isEvict: boolean, key: K, value: V, newValue: V): void { 2398 if (isEvict === true) { 2399 console.info('key = ' + key); 2400 // 输出结果:key = 1 2401 console.info('value = ' + value); 2402 // 输出结果:value = 1 2403 console.info('newValue = ' + newValue); 2404 // 输出结果:newValue = null 2405 } 2406 } 2407} 2408let lru = new ChildLRUCache<number, number>(2); 2409lru.put(1, 1); 2410lru.put(2, 2); 2411lru.put(3, 3); 2412``` 2413 2414### contains<sup>9+</sup> 2415 2416contains(key: K): boolean 2417 2418检查当前缓冲区是否包含指定的键。 2419 2420**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2421 2422**系统能力:** SystemCapability.Utils.Lang 2423 2424**参数:** 2425 2426| 参数名 | 类型 | 必填 | 说明 | 2427| ------ | ------ | ---- | ---------------- | 2428| key | K | 是 | 表示要检查的键。 | 2429 2430**返回值:** 2431 2432| 类型 | 说明 | 2433| ------- | ------------------------------------------ | 2434| boolean | 如果缓冲区包含指定的键,则返回 true。 | 2435 2436**错误码:** 2437 2438以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2439 2440| 错误码ID | 错误信息 | 2441| -------- | -------- | 2442| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2443 2444**示例:** 2445 2446```ts 2447let pro = new util.LRUCache<number, number>(); 2448pro.put(2, 10); 2449let result = pro.contains(2); 2450console.info('result = ' + result); 2451// 输出结果:result = true 2452``` 2453 2454### createDefault<sup>9+</sup> 2455 2456createDefault(key: K): V 2457 2458如果在缓冲区未匹配到键,则执行后续操作,参数表示未匹配的键,返回与键关联的值,默认返回undefined。 2459 2460**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2461 2462**系统能力:** SystemCapability.Utils.Lang 2463 2464**参数:** 2465 2466| 参数名 | 类型 | 必填 | 说明 | 2467| ------ | ---- | ---- | -------------- | 2468| key | K | 是 | 表示未匹配的键。 | 2469 2470**返回值:** 2471 2472| 类型 | 说明 | 2473| ---- | ------------------ | 2474| V | 返回与键关联的值。 | 2475 2476**错误码:** 2477 2478以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2479 2480| 错误码ID | 错误信息 | 2481| -------- | -------- | 2482| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2483 2484**示例:** 2485 2486```ts 2487let pro = new util.LRUCache<number, number>(); 2488let result = pro.createDefault(50); 2489console.info('result = ' + result); 2490// 输出结果:result = undefined 2491``` 2492 2493### entries<sup>9+</sup> 2494 2495entries(): IterableIterator<[K,V]> 2496 2497允许迭代包含在这个对象中的所有键值对。 2498 2499**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2500 2501**系统能力:** SystemCapability.Utils.Lang 2502 2503**返回值:** 2504 2505| 类型 | 说明 | 2506| ----------- | -------------------- | 2507| [K, V] | 返回一个可迭代数组。 | 2508 2509**示例:** 2510 2511```ts 2512let pro = new util.LRUCache<number, number>(); 2513pro.put(2, 10); 2514pro.put(3, 15); 2515let pair:Iterable<Object[]> = pro.entries(); 2516let arrayValue = Array.from(pair); 2517for (let value of arrayValue) { 2518 console.info(value[0]+ ', '+ value[1]); 2519 // 输出结果: 2520 // 2, 10 2521 // 3, 15 2522} 2523``` 2524 2525### [Symbol.iterator]<sup>9+</sup> 2526 2527[Symbol.iterator]\(): IterableIterator<[K, V]> 2528 2529返回一个键值对形式的二维数组。 2530 2531**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2532 2533**系统能力:** SystemCapability.Utils.Lang 2534 2535**返回值:** 2536 2537| 类型 | 说明 | 2538| ----------- | ------------------------------ | 2539| [K, V] | 返回一个键值对形式的二维数组。 | 2540 2541**示例:** 2542 2543```ts 2544let pro = new util.LRUCache<number, number>(); 2545pro.put(2, 10); 2546pro.put(3, 15); 2547let pair:Iterable<Object[]> = pro[Symbol.iterator](); 2548let arrayValue = Array.from(pair); 2549for (let value of arrayValue) { 2550 console.info(value[0]+ ', '+ value[1]); 2551 // 输出结果: 2552 // 2, 10 2553 // 3, 15 2554} 2555``` 2556 2557## ScopeComparable<sup>8+</sup> 2558 2559ScopeComparable类型的值需要实现compareTo方法,确保传入的数据具有可比性。 2560 2561**系统能力:** SystemCapability.Utils.Lang 2562 2563### compareTo<sup>8+</sup> 2564 2565compareTo(other: ScopeComparable): boolean 2566 2567比较两个值的大小,返回一个布尔值。 2568 2569**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2570 2571**系统能力:** SystemCapability.Utils.Lang 2572 2573**参数:** 2574 2575| 参数名 | 类型 | 必填 | 说明 | 2576| ------ | ---- | ---- | -------------- | 2577| other | [ScopeComparable](#scopecomparable8) | 是 | 表示要比较的值。 | 2578 2579**返回值:** 2580 2581| 类型 | 说明 | 2582| ---- | ------------------ | 2583| boolean | 调用compareTo的值大于等于传入的值返回true,否则返回false。| 2584 2585**示例:** 2586 2587构造新类,实现compareTo方法。后续示例代码中,均以此Temperature类为例。 2588 2589```ts 2590class Temperature{ 2591 private readonly _temp: number; 2592 constructor(value : number) { 2593 this._temp = value; 2594 } 2595 compareTo(value : Temperature ) { 2596 return this._temp >= value.getTemp(); 2597 } 2598 getTemp() { 2599 return this._temp; 2600 } 2601 toString() : string { 2602 return this._temp.toString(); 2603 } 2604} 2605``` 2606 2607## ScopeType<sup>8+</sup> 2608 2609type ScopeType = ScopeComparable | number 2610 2611用于表示范围中的值的类型。 2612 2613**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2614 2615**系统能力:** SystemCapability.Utils.Lang 2616 2617| 类型 | 说明 | 2618| -------- | -------- | 2619| number | 表示值的类型为数字。 | 2620| [ScopeComparable](#scopecomparable8) | 表示值的类型为ScopeComparable。| 2621 2622## ScopeHelper<sup>9+</sup> 2623 2624ScopeHelper接口用于描述一个字段的有效范围。ScopeHelper实例的构造函数用于创建具有指定下限和上限的对象,并要求这些对象必须具有可比性。 2625 2626### constructor<sup>9+</sup> 2627 2628constructor(lowerObj: ScopeType, upperObj: ScopeType) 2629 2630用于创建指定下限和上限的作用域实例的构造函数,返回一个ScopeHelper对象。 2631 2632**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2633 2634**系统能力:** SystemCapability.Utils.Lang 2635 2636**参数:** 2637 2638| 参数名 | 类型 | 必填 | 说明 | 2639| -------- | ------------------------ | ---- | ---------------------- | 2640| lowerObj | [ScopeType](#scopetype8) | 是 | 指定作用域实例的下限。 | 2641| upperObj | [ScopeType](#scopetype8) | 是 | 指定作用域实例的上限。 | 2642 2643**错误码:** 2644 2645以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2646 2647| 错误码ID | 错误信息 | 2648| -------- | -------- | 2649| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2650 2651**示例:** 2652 2653```ts 2654class Temperature{ 2655 private readonly _temp: number; 2656 constructor(value : number) { 2657 this._temp = value; 2658 } 2659 compareTo(value : Temperature ) { 2660 return this._temp >= value.getTemp(); 2661 } 2662 getTemp() { 2663 return this._temp; 2664 } 2665 toString() : string { 2666 return this._temp.toString(); 2667 } 2668} 2669let tempLower = new Temperature(30); 2670let tempUpper = new Temperature(40); 2671let range = new util.ScopeHelper(tempLower, tempUpper); 2672console.info("range = " + range); 2673// 输出结果:range = [30, 40] 2674``` 2675 2676### toString<sup>9+</sup> 2677 2678toString(): string 2679 2680该字符串化方法返回一个包含当前范围的字符串表示形式。 2681 2682**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2683 2684**系统能力:** SystemCapability.Utils.Lang 2685 2686**返回值:** 2687 2688| 类型 | 说明 | 2689| ------ | -------------------------------------- | 2690| string | 返回包含当前范围对象的字符串表示形式。 | 2691 2692**示例:** 2693 2694```ts 2695class Temperature{ 2696 private readonly _temp: number; 2697 constructor(value : number) { 2698 this._temp = value; 2699 } 2700 compareTo(value : Temperature ) { 2701 return this._temp >= value.getTemp(); 2702 } 2703 getTemp() { 2704 return this._temp; 2705 } 2706 toString() : string { 2707 return this._temp.toString(); 2708 } 2709} 2710 2711let tempLower = new Temperature(30); 2712let tempUpper = new Temperature(40); 2713let range = new util.ScopeHelper(tempLower, tempUpper); 2714let result = range.toString(); 2715console.info("result = " + result); 2716// 输出结果:result = [30, 40] 2717``` 2718 2719### intersect<sup>9+</sup> 2720 2721intersect(range: ScopeHelper): ScopeHelper 2722 2723获取给定范围和当前范围的交集。 2724 2725**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2726 2727**系统能力:** SystemCapability.Utils.Lang 2728 2729**参数:** 2730 2731| 参数名 | 类型 | 必填 | 说明 | 2732| ------ | ---------------------------- | ---- | ------------------ | 2733| range | [ScopeHelper](#scopehelper9) | 是 | 传入一个给定范围。 | 2734 2735**返回值:** 2736 2737| 类型 | 说明 | 2738| ------------------------------ | ------------------------------ | 2739| [ScopeHelper](#scopehelper9) | 返回给定范围和当前范围的交集。 | 2740 2741**错误码:** 2742 2743以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2744 2745| 错误码ID | 错误信息 | 2746| -------- | -------- | 2747| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2748 2749**示例:** 2750 2751```ts 2752class Temperature{ 2753 private readonly _temp: number; 2754 constructor(value : number) { 2755 this._temp = value; 2756 } 2757 compareTo(value : Temperature ) { 2758 return this._temp >= value.getTemp(); 2759 } 2760 getTemp() { 2761 return this._temp; 2762 } 2763 toString() : string { 2764 return this._temp.toString(); 2765 } 2766} 2767 2768let tempLower = new Temperature(30); 2769let tempUpper = new Temperature(40); 2770let range = new util.ScopeHelper(tempLower, tempUpper); 2771let tempMiDF = new Temperature(35); 2772let tempMidS = new Temperature(39); 2773let rangeFir = new util.ScopeHelper(tempMiDF, tempMidS); 2774range.intersect(rangeFir); 2775``` 2776 2777### intersect<sup>9+</sup> 2778 2779intersect(lowerObj:ScopeType,upperObj:ScopeType):ScopeHelper 2780 2781获取当前范围与给定下限和上限范围的交集。 2782 2783**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2784 2785**系统能力:** SystemCapability.Utils.Lang 2786 2787**参数:** 2788 2789| 参数名 | 类型 | 必填 | 说明 | 2790| -------- | ------------------------ | ---- | ---------------- | 2791| lowerObj | [ScopeType](#scopetype8) | 是 | 给定范围的下限。 | 2792| upperObj | [ScopeType](#scopetype8) | 是 | 给定范围的上限。 | 2793 2794**返回值:** 2795 2796| 类型 | 说明 | 2797| ---------------------------- | ---------------------------------------- | 2798| [ScopeHelper](#scopehelper9) | 返回当前范围与给定下限和上限范围的交集。 | 2799 2800**错误码:** 2801 2802以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2803 2804| 错误码ID | 错误信息 | 2805| -------- | -------- | 2806| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2807 2808**示例:** 2809 2810```ts 2811class Temperature{ 2812 private readonly _temp: number; 2813 constructor(value : number) { 2814 this._temp = value; 2815 } 2816 compareTo(value : Temperature ) { 2817 return this._temp >= value.getTemp(); 2818 } 2819 getTemp() { 2820 return this._temp; 2821 } 2822 toString() : string { 2823 return this._temp.toString(); 2824 } 2825} 2826 2827let tempLower = new Temperature(30); 2828let tempUpper = new Temperature(40); 2829let tempMiDF = new Temperature(35); 2830let tempMidS = new Temperature(39); 2831let range = new util.ScopeHelper(tempLower, tempUpper); 2832let result = range.intersect(tempMiDF, tempMidS); 2833console.info("result = " + result); 2834// 输出结果:result = [35, 39] 2835``` 2836 2837### getUpper<sup>9+</sup> 2838 2839getUpper(): ScopeType 2840 2841获取当前范围的上限。 2842 2843**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2844 2845**系统能力:** SystemCapability.Utils.Lang 2846 2847**返回值:** 2848 2849| 类型 | 说明 | 2850| ------------------------ | ---------------------- | 2851| [ScopeType](#scopetype8) | 返回当前范围的上限值。 | 2852 2853**示例:** 2854 2855```ts 2856class Temperature{ 2857 private readonly _temp: number; 2858 constructor(value : number) { 2859 this._temp = value; 2860 } 2861 compareTo(value : Temperature ) { 2862 return this._temp >= value.getTemp(); 2863 } 2864 getTemp() { 2865 return this._temp; 2866 } 2867 toString() : string { 2868 return this._temp.toString(); 2869 } 2870} 2871 2872let tempLower = new Temperature(30); 2873let tempUpper = new Temperature(40); 2874let range = new util.ScopeHelper(tempLower, tempUpper); 2875let result = range.getUpper(); 2876console.info("result = " + result); 2877// 输出结果:result = 40 2878``` 2879 2880### getLower<sup>9+</sup> 2881 2882getLower(): ScopeType 2883 2884获取当前范围的下限。 2885 2886**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2887 2888**系统能力:** SystemCapability.Utils.Lang 2889 2890**返回值:** 2891 2892| 类型 | 说明 | 2893| ------------------------ | ---------------------- | 2894| [ScopeType](#scopetype8) | 返回当前范围的下限值。 | 2895 2896**示例:** 2897 2898```ts 2899class Temperature{ 2900 private readonly _temp: number; 2901 constructor(value : number) { 2902 this._temp = value; 2903 } 2904 compareTo(value : Temperature ) { 2905 return this._temp >= value.getTemp(); 2906 } 2907 getTemp() { 2908 return this._temp; 2909 } 2910 toString() : string { 2911 return this._temp.toString(); 2912 } 2913} 2914 2915let tempLower = new Temperature(30); 2916let tempUpper = new Temperature(40); 2917let range = new util.ScopeHelper(tempLower, tempUpper); 2918let result = range.getLower(); 2919console.info("result = " + result); 2920// 输出结果:result = 30 2921``` 2922 2923### expand<sup>9+</sup> 2924 2925expand(lowerObj: ScopeType,upperObj: ScopeType): ScopeHelper 2926 2927创建并返回包括当前范围和给定下限和上限的并集。 2928 2929**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2930 2931**系统能力:** SystemCapability.Utils.Lang 2932 2933**参数:** 2934 2935| 参数名 | 类型 | 必填 | 说明 | 2936| -------- | ------------------------ | ---- | ---------------- | 2937| lowerObj | [ScopeType](#scopetype8) | 是 | 给定范围的下限。 | 2938| upperObj | [ScopeType](#scopetype8) | 是 | 给定范围的上限。 | 2939 2940**返回值:** 2941 2942| 类型 | 说明 | 2943| ---------------------------- | ------------------------------------ | 2944| [ScopeHelper](#scopehelper9) | 返回当前范围和给定下限和上限的并集。 | 2945 2946**错误码:** 2947 2948以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2949 2950| 错误码ID | 错误信息 | 2951| -------- | -------- | 2952| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2953 2954**示例:** 2955 2956```ts 2957class Temperature{ 2958 private readonly _temp: number; 2959 constructor(value : number) { 2960 this._temp = value; 2961 } 2962 compareTo(value : Temperature ) { 2963 return this._temp >= value.getTemp(); 2964 } 2965 getTemp() { 2966 return this._temp; 2967 } 2968 toString() : string { 2969 return this._temp.toString(); 2970 } 2971} 2972 2973let tempLower = new Temperature(30); 2974let tempUpper = new Temperature(40); 2975let tempMiDF = new Temperature(35); 2976let tempMidS = new Temperature(39); 2977let range = new util.ScopeHelper(tempLower, tempUpper); 2978let result = range.expand(tempMiDF, tempMidS); 2979console.info("result = " + result); 2980// 输出结果:result = [30, 40] 2981``` 2982 2983### expand<sup>9+</sup> 2984 2985expand(range: ScopeHelper): ScopeHelper 2986 2987创建并返回包括当前范围和给定范围的并集。 2988 2989**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2990 2991**系统能力:** SystemCapability.Utils.Lang 2992 2993**参数:** 2994 2995| 参数名 | 类型 | 必填 | 说明 | 2996| ------ | ---------------------------- | ---- | ------------------ | 2997| range | [ScopeHelper](#scopehelper9) | 是 | 传入一个给定范围。 | 2998 2999**返回值:** 3000 3001| 类型 | 说明 | 3002| ---------------------------- | ---------------------------------- | 3003| [ScopeHelper](#scopehelper9) | 返回包括当前范围和给定范围的并集。 | 3004 3005**错误码:** 3006 3007以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3008 3009| 错误码ID | 错误信息 | 3010| -------- | -------- | 3011| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3012 3013**示例:** 3014 3015```ts 3016class Temperature{ 3017 private readonly _temp: number; 3018 constructor(value : number) { 3019 this._temp = value; 3020 } 3021 compareTo(value : Temperature ) { 3022 return this._temp >= value.getTemp(); 3023 } 3024 getTemp() { 3025 return this._temp; 3026 } 3027 toString() : string { 3028 return this._temp.toString(); 3029 } 3030} 3031 3032let tempLower = new Temperature(30); 3033let tempUpper = new Temperature(40); 3034let tempMiDF = new Temperature(35); 3035let tempMidS = new Temperature(39); 3036let range = new util.ScopeHelper(tempLower, tempUpper); 3037let rangeFir = new util.ScopeHelper(tempMiDF, tempMidS); 3038let result = range.expand(rangeFir); 3039console.info("result = " + result); 3040// 输出结果:result = [30, 40] 3041``` 3042 3043### expand<sup>9+</sup> 3044 3045expand(value: ScopeType): ScopeHelper 3046 3047创建并返回包括当前范围和给定值的并集。 3048 3049**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3050 3051**系统能力:** SystemCapability.Utils.Lang 3052 3053**参数:** 3054 3055| 参数名 | 类型 | 必填 | 说明 | 3056| ------ | ------------------------ | ---- | ---------------- | 3057| value | [ScopeType](#scopetype8) | 是 | 传入一个给定值。 | 3058 3059**返回值:** 3060 3061| 类型 | 说明 | 3062| ---------------------------- | -------------------------------- | 3063| [ScopeHelper](#scopehelper9) | 返回包括当前范围和给定值的并集。 | 3064 3065**错误码:** 3066 3067以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3068 3069| 错误码ID | 错误信息 | 3070| -------- | -------- | 3071| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3072 3073**示例:** 3074 3075```ts 3076class Temperature{ 3077 private readonly _temp: number; 3078 constructor(value : number) { 3079 this._temp = value; 3080 } 3081 compareTo(value : Temperature ) { 3082 return this._temp >= value.getTemp(); 3083 } 3084 getTemp() { 3085 return this._temp; 3086 } 3087 toString() : string { 3088 return this._temp.toString(); 3089 } 3090} 3091 3092let tempLower = new Temperature(30); 3093let tempUpper = new Temperature(40); 3094let tempMiDF = new Temperature(35); 3095let range = new util.ScopeHelper(tempLower, tempUpper); 3096let result = range.expand(tempMiDF); 3097console.info("result = " + result); 3098// 输出结果:result = [30, 40] 3099``` 3100 3101### contains<sup>9+</sup> 3102 3103contains(value: ScopeType): boolean 3104 3105检查给定value是否包含在当前范围内。 3106 3107**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3108 3109**系统能力:** SystemCapability.Utils.Lang 3110 3111**参数:** 3112 3113| 参数名 | 类型 | 必填 | 说明 | 3114| ------ | ------------------------ | ---- | ---------------- | 3115| value | [ScopeType](#scopetype8) | 是 | 传入一个给定值。 | 3116 3117**返回值:** 3118 3119| 类型 | 说明 | 3120| ------- | --------------------------------------------------- | 3121| boolean | 如果给定值包含在当前范围内返回true,否则返回false。 | 3122 3123**错误码:** 3124 3125以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3126 3127| 错误码ID | 错误信息 | 3128| -------- | -------- | 3129| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3130 3131**示例:** 3132 3133```ts 3134class Temperature{ 3135 private readonly _temp: number; 3136 constructor(value : number) { 3137 this._temp = value; 3138 } 3139 compareTo(value : Temperature ) { 3140 return this._temp >= value.getTemp(); 3141 } 3142 getTemp() { 3143 return this._temp; 3144 } 3145 toString() : string { 3146 return this._temp.toString(); 3147 } 3148} 3149 3150let tempLower = new Temperature(30); 3151let tempUpper = new Temperature(40); 3152let tempMiDF = new Temperature(35); 3153let range = new util.ScopeHelper(tempLower, tempUpper); 3154let result = range.contains(tempMiDF); 3155console.info("result = " + result); 3156// 输出结果:result = true 3157``` 3158 3159### contains<sup>9+</sup> 3160 3161contains(range: ScopeHelper): boolean 3162 3163检查给定range是否在当前范围内。 3164 3165**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3166 3167**系统能力:** SystemCapability.Utils.Lang 3168 3169**参数:** 3170 3171| 参数名 | 类型 | 必填 | 说明 | 3172| ------ | ---------------------------- | ---- | ------------------ | 3173| range | [ScopeHelper](#scopehelper9) | 是 | 传入一个给定范围。 | 3174 3175**返回值:** 3176 3177| 类型 | 说明 | 3178| ------- | ----------------------------------------------------- | 3179| boolean | 如果给定范围包含在当前范围内返回true,否则返回false。 | 3180 3181**错误码:** 3182 3183以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3184 3185| 错误码ID | 错误信息 | 3186| -------- | -------- | 3187| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3188 3189**示例:** 3190 3191```ts 3192class Temperature{ 3193 private readonly _temp: number; 3194 constructor(value : number) { 3195 this._temp = value; 3196 } 3197 compareTo(value : Temperature ) { 3198 return this._temp >= value.getTemp(); 3199 } 3200 getTemp() { 3201 return this._temp; 3202 } 3203 toString() : string { 3204 return this._temp.toString(); 3205 } 3206} 3207 3208let tempLower = new Temperature(30); 3209let tempUpper = new Temperature(40); 3210let range = new util.ScopeHelper(tempLower, tempUpper); 3211let tempLess = new Temperature(20); 3212let tempMore = new Temperature(45); 3213let rangeSec = new util.ScopeHelper(tempLess, tempMore); 3214let result = range.contains(rangeSec); 3215console.info("result = " + result); 3216// 输出结果:result = false 3217``` 3218 3219### clamp<sup>9+</sup> 3220 3221clamp(value: ScopeType): ScopeType 3222 3223将给定值限定到当前范围内。 3224 3225**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3226 3227**系统能力:** SystemCapability.Utils.Lang 3228 3229**参数:** 3230 3231| 参数名 | 类型 | 必填 | 说明 | 3232| ------ | ------------------------ | ---- | -------------- | 3233| value | [ScopeType](#scopetype8) | 是 | 传入的给定值。 | 3234 3235**返回值:** 3236 3237| 类型 | 说明 | 3238| ------------------------ | ------------------------------------------------------------ | 3239| [ScopeType](#scopetype8) | 如果传入的value小于下限,则返回lowerObj;如果大于上限值则返回upperObj;如果在当前范围内,则返回value。 | 3240 3241**错误码:** 3242 3243以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3244 3245| 错误码ID | 错误信息 | 3246| -------- | -------- | 3247| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3248 3249**示例:** 3250 3251```ts 3252class Temperature{ 3253 private readonly _temp: number; 3254 constructor(value : number) { 3255 this._temp = value; 3256 } 3257 compareTo(value : Temperature ) { 3258 return this._temp >= value.getTemp(); 3259 } 3260 getTemp() { 3261 return this._temp; 3262 } 3263 toString() : string { 3264 return this._temp.toString(); 3265 } 3266} 3267 3268let tempLower = new Temperature(30); 3269let tempUpper = new Temperature(40); 3270let tempMiDF = new Temperature(35); 3271let range = new util.ScopeHelper(tempLower, tempUpper); 3272let result = range.clamp(tempMiDF); 3273console.info("result = " + result); 3274// 输出结果:result = 35 3275``` 3276 3277## Base64Helper<sup>9+</sup> 3278 3279Base64Helper类提供Base64编解码和Base64URL编解码功能。Base64编码表包含A-Z、a-z、0-9这62个字符,以及"+"和"/"这两个特殊字符。在编码时,将原始数据按3个字节一组进行划分,得到若干个6位的数字,然后使用Base64编码表中对应的字符来表示这些数字。如果最后剩余1或2个字节,则需要使用"="字符进行补齐。Base64URL编码表包含A-Z、a-z、0-9以及"-"和"_"64个字符,Base64URL编码结果不含"="。 3280 3281### constructor<sup>9+</sup> 3282 3283constructor() 3284 3285Base64Helper的构造函数。 3286 3287**系统能力:** SystemCapability.Utils.Lang 3288 3289**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 3290 3291**示例:** 3292 3293 ```ts 3294 let base64 = new util.Base64Helper(); 3295 ``` 3296 3297### encodeSync<sup>9+</sup> 3298 3299encodeSync(src: Uint8Array, options?: Type): Uint8Array 3300 3301通过输入参数编码后输出Uint8Array对象。 3302 3303**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 3304 3305**系统能力:** SystemCapability.Utils.Lang 3306 3307**参数:** 3308 3309| 参数名 | 类型 | 必填 | 说明 | 3310| ------ | ---------- | ---- | ------------------- | 3311| src | Uint8Array | 是 | 待编码Uint8Array对象。 | 3312| options<sup>12+</sup> | [Type](#type10) | 否 | 从API version 12开始支持该参数,表示对应的编码格式。<br/>此参数可选,可选值为:util.Type.BASIC和util.Type.BASIC_URL_SAFE,默认值为:util.Type.BASIC。<br/>util.Type.BASIC 表示 Base64编码。<br/>util.Type.BASIC_URL_SAFE 表示 Base64URL编码。 | 3313 3314**返回值:** 3315 3316| 类型 | 说明 | 3317| ---------- | ----------------------------- | 3318| Uint8Array | 返回编码后的Uint8Array对象。 | 3319 3320**错误码:** 3321 3322以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3323 3324| 错误码ID | 错误信息 | 3325| -------- | -------- | 3326| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3327 3328**示例:** 3329 3330 ```ts 3331 let base64Helper = new util.Base64Helper(); 3332 let array = new Uint8Array([115,49,51]); 3333 let result = base64Helper.encodeSync(array); 3334 console.info("result = " + result); 3335 // 输出结果:result = 99,122,69,122 3336 ``` 3337 3338 3339### encodeToStringSync<sup>9+</sup> 3340 3341encodeToStringSync(src: Uint8Array, options?: Type): string 3342 3343通过输入参数编码后输出对应文本。 3344 3345**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 3346 3347**系统能力:** SystemCapability.Utils.Lang 3348 3349**参数:** 3350 3351| 参数名 | 类型 | 必填 | 说明 | 3352| ------ | ---------- | ---- | ------------------- | 3353| src | Uint8Array | 是 | 待编码Uint8Array对象。 | 3354| options<sup>10+</sup> | [Type](#type10) | 否 | 从API version 10开始支持该参数,表示对应的编码格式。<br/>此参数可选,可选值为:util.Type.BASIC,util.Type.MIME,util.Type.BASIC_URL_SAFE 和util.Type.MIME_URL_SAFE,默认值为:util.Type.BASIC。<br/>- 当参数取值为util.Type.BASIC,表示Base64编码,返回值没有回车符、换行符。<br/>- 当参数取值为util.Type.MIME,表示Base64编码,返回值每一行不超过76个字符,而且每行以'\r\n'符结束。<br/>- 当参数取值为util.Type.BASIC_URL_SAFE,表示Base64URL编码,返回值没有回车符、换行符。<br/>- 当参数取值为util.Type.MIME_URL_SAFE,表示Base64URL编码,返回值每一行不超过76个字符,而且每行以'\r\n'符结束。 | 3355 3356**返回值:** 3357 3358| 类型 | 说明 | 3359| ------ | -------------------- | 3360| string | 返回编码后的字符串。 | 3361 3362**错误码:** 3363 3364以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3365 3366| 错误码ID | 错误信息 | 3367| -------- | -------- | 3368| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3369 3370**示例:** 3371 3372 ```ts 3373 let base64Helper = new util.Base64Helper(); 3374 let array = new Uint8Array([77,97,110,105,115,100,105,115,116,105,110,103,117,105,115,104,101,100,110,111,116,111,110,108,121,98,121,104,105,115,114,101,97,115,111,110,98,117,116,98,121,116,104,105,115,115,105,110,103,117,108,97,114,112,97,115,115,105,111,110,102,114,111,109,111,116,104,101,114,97,110,105,109,97,108,115,119,104,105,99,104,105,115,97,108,117,115,116,111,102,116,104,101,109,105,110,100,101,120,99,101,101,100,115,116,104,101,115,104,111,114,116,118,101,104,101,109,101,110,99,101,111,102,97,110,121,99,97,114,110,97,108,112,108,101,97,115,117,114,101]); 3375 let result = base64Helper.encodeToStringSync(array, util.Type.MIME); 3376 console.info("result = " + result); 3377 /* 3378 输出结果:result = TWFuaXNkaXN0aW5ndWlzaGVkbm90b25seWJ5aGlzcmVhc29uYnV0Ynl0aGlzc2luZ3VsYXJwYXNz 3379 aW9uZnJvbW90aGVyYW5pbWFsc3doaWNoaXNhbHVzdG9mdGhlbWluZGV4Y2VlZHN0aGVzaG9ydHZl 3380 aGVtZW5jZW9mYW55Y2FybmFscGxlYXN1cmU= 3381 */ 3382 ``` 3383 3384 3385### decodeSync<sup>9+</sup> 3386 3387decodeSync(src: Uint8Array | string, options?: Type): Uint8Array 3388 3389通过输入参数解码后输出对应Uint8Array对象。 3390 3391**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 3392 3393**系统能力:** SystemCapability.Utils.Lang 3394 3395**参数:** 3396 3397| 参数名 | 类型 | 必填 | 说明 | 3398| ------ | ------------------------------ | ---- | ----------------------------- | 3399| src | Uint8Array \| string | 是 | 待解码Uint8Array对象或者字符串。 | 3400| options<sup>10+</sup> | [Type](#type10) | 否 | 从API version 10开始支持该参数,表示对应的解码格式。<br/>此参数可选,可选值为:util.Type.BASIC,util.Type.MIME,util.Type.BASIC_URL_SAFE 和util.Type.MIME_URL_SAFE,默认值为:util.Type.BASIC。<br/>- 当参数取值为util.Type.BASIC,表示Base64解码。<br/>- 当参数取值为util.Type.MIME,表示Base64解码,src入参包含回车符、换行符。<br/>- 当参数取值为util.Type.BASIC_URL_SAFE,表示Base64URL解码。<br/>- 当参数取值为util.Type.MIME_URL_SAFE,表示Base64URL解码,src入参包含回车符、换行符。 | 3401 3402**返回值:** 3403 3404| 类型 | 说明 | 3405| ---------- | ----------------------------- | 3406| Uint8Array | 返回解码后新分配的Uint8Array对象。 | 3407 3408**错误码:** 3409 3410以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3411 3412| 错误码ID | 错误信息 | 3413| -------- | -------- | 3414| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3415 3416**示例:** 3417 3418 ```ts 3419 let base64Helper = new util.Base64Helper(); 3420 let buff = 'TWFuaXNkaXN0aW5ndWlzaGVkbm90b25seWJ5aGlzcmVhc29uYnV0Ynl0aGlzc2luZ3VsYXJwYXNz\r\naW9uZnJvbW90aGVyYW5pbWFsc3doaWNoaXNhbHVzdG9mdGhlbWluZGV4Y2VlZHN0aGVzaG9ydHZl\r\naGVtZW5jZW9mYW55Y2FybmFscGxlYXN1cmU=\r\n'; 3421 let result = base64Helper.decodeSync(buff, util.Type.MIME); 3422 console.info("result = " + result); 3423 /* 3424 输出结果:result = 77,97,110,105,115,100,105,115,116,105,110,103,117,105,115,104,101,100,110,111,116,111,110,108,121,98,121,104,105,115,114,101,97,115,111,110,98,117,116,98,121,116,104,105,115,115,105,110,103,117,108,97,114,112,97,115,115,105,111,110,102,114,111,109,111,116,104,101,114,97,110,105,109,97,108,115,119,104,105,99,104,105,115,97,108,117,115,116,111,102,116,104,101,109,105,110,100,101,120,99,101,101,100,115,116,104,101,115,104,111,114,116,118,101,104,101,109,101,110,99,101,111,102,97,110,121,99,97,114,110,97,108,112,108,101,97,115,117,114,101 3425 */ 3426 ``` 3427 3428 3429### encode<sup>9+</sup> 3430 3431encode(src: Uint8Array, options?: Type): Promise<Uint8Array> 3432 3433通过输入参数异步编码后输出对应Uint8Array对象。 3434 3435**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。 3436 3437**系统能力:** SystemCapability.Utils.Lang 3438 3439**参数:** 3440 3441| 参数名 | 类型 | 必填 | 说明 | 3442| ------ | ---------- | ---- | ----------------------- | 3443| src | Uint8Array | 是 | 异步编码输入Uint8Array对象。 | 3444| options<sup>12+</sup> | [Type](#type10) | 否 | 从API version 12开始支持该参数,表示对应的编码格式。<br/>此参数可选,可选值为:util.Type.BASIC和util.Type.BASIC_URL_SAFE,默认值为:util.Type.BASIC。<br/>util.Type.BASIC 表示 Base64编码。<br/>util.Type.BASIC_URL_SAFE表示 Base64URL编码。 | 3445 3446**返回值:** 3447 3448| 类型 | 说明 | 3449| ------------------------- | --------------------------------- | 3450| Promise<Uint8Array> | 返回异步编码后新分配的Uint8Array对象。 | 3451 3452**错误码:** 3453 3454以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3455 3456| 错误码ID | 错误信息 | 3457| -------- | -------- | 3458| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3459 3460**示例:** 3461 3462 ```ts 3463 let base64Helper = new util.Base64Helper(); 3464 let array = new Uint8Array([115,49,51]); 3465 base64Helper.encode(array).then((val) => { 3466 console.info(val.toString()); 3467 // 输出结果:99,122,69,122 3468 }) 3469 ``` 3470 3471 3472### encodeToString<sup>9+</sup> 3473 3474encodeToString(src: Uint8Array, options?: Type): Promise<string> 3475 3476通过输入参数异步编码后输出对应文本。 3477 3478**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3479 3480**系统能力:** SystemCapability.Utils.Lang 3481 3482**参数:** 3483 3484| 参数名 | 类型 | 必填 | 说明 | 3485| ------ | ---------- | ---- | ----------------------- | 3486| src | Uint8Array | 是 | 异步编码输入Uint8Array对象。 | 3487| options<sup>10+</sup> | [Type](#type10) | 否 | 从API version 10开始支持该参数,表示对应的编码格式。<br/>此参数可选,可选值为:util.Type.BASIC,util.Type.MIME,util.Type.BASIC_URL_SAFE 和util.Type.MIME_URL_SAFE,默认值为:util.Type.BASIC。<br/>- 当参数取值为util.Type.BASIC,表示Base64编码,返回值没有回车符、换行符。<br/>- 当参数取值为util.Type.MIME,表示Base64编码,返回值每一行不超过76个字符,而且每行以'\r\n'符结束。<br/>- 当参数取值为util.Type.BASIC_URL_SAFE,表示Base64URL编码,返回值没有回车符、换行符。<br/>- 当参数取值为util.Type.MIME_URL_SAFE,表示Base64URL编码,返回值每一行不超过76个字符,而且每行以'\r\n'符结束。 | 3488 3489**返回值:** 3490 3491| 类型 | 说明 | 3492| --------------------- | ------------------------ | 3493| Promise<string> | 返回异步编码后的字符串。 | 3494 3495**错误码:** 3496 3497以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3498 3499| 错误码ID | 错误信息 | 3500| -------- | -------- | 3501| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3502 3503**示例:** 3504 3505 ```ts 3506 let base64Helper = new util.Base64Helper(); 3507 let array = new Uint8Array([77,97,110,105,115,100,105,115,116,105,110,103,117,105,115,104,101,100,110,111,116,111,110,108,121,98,121,104,105,115,114,101,97,115,111,110,98,117,116,98,121,116,104,105,115,115,105,110,103,117,108,97,114,112,97,115,115,105,111,110,102,114,111,109,111,116,104,101,114,97,110,105,109,97,108,115,119,104,105,99,104,105,115,97,108,117,115,116,111,102,116,104,101,109,105,110,100,101,120,99,101,101,100,115,116,104,101,115,104,111,114,116,118,101,104,101,109,101,110,99,101,111,102,97,110,121,99,97,114,110,97,108,112,108,101,97,115,117,114,101]); 3508 base64Helper.encodeToString(array, util.Type.MIME).then((val) => { 3509 console.info(val); 3510 /* 3511 输出结果:TWFuaXNkaXN0aW5ndWlzaGVkbm90b25seWJ5aGlzcmVhc29uYnV0Ynl0aGlzc2luZ3VsYXJwYXNz 3512 aW9uZnJvbW90aGVyYW5pbWFsc3doaWNoaXNhbHVzdG9mdGhlbWluZGV4Y2VlZHN0aGVzaG9ydHZl 3513 aGVtZW5jZW9mYW55Y2FybmFscGxlYXN1cmU= 3514 */ 3515 3516 }) 3517 ``` 3518 3519 3520### decode<sup>9+</sup> 3521 3522decode(src: Uint8Array | string, options?: Type): Promise<Uint8Array> 3523 3524通过输入参数异步解码后输出对应Uint8Array对象。 3525 3526**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3527 3528**系统能力:** SystemCapability.Utils.Lang 3529 3530**参数:** 3531 3532| 参数名 | 类型 | 必填 | 说明 | 3533| ------ | ------------------------------ | ---- | --------------------------------- | 3534| src | Uint8Array \| string | 是 | 异步解码输入Uint8Array对象或者字符串。 | 3535| options<sup>10+</sup> | [Type](#type10) | 否 | 从API version 10开始支持该参数,表示对应的解码格式。<br/>此参数可选,可选值为:util.Type.BASIC,util.Type.MIME,util.Type.BASIC_URL_SAFE 和util.Type.MIME_URL_SAFE,默认值为:util.Type.BASIC。<br/>- 当参数取值为util.Type.BASIC时,表示Base64解码。<br/>- 当参数取值为util.Type.MIME时,表示Base64解码,src入参包含回车符、换行符。<br/>- 当参数取值为util.Type.BASIC_URL_SAFE,表示Base64URL解码。<br/>- 当参数取值为util.Type.MIME_URL_SAFE,表示Base64URL解码,src入参包含回车符、换行符。 | 3536 3537**返回值:** 3538 3539| 类型 | 说明 | 3540| ------------------------- | --------------------------------- | 3541| Promise<Uint8Array> | 返回异步解码后新分配的Uint8Array对象。 | 3542 3543**错误码:** 3544 3545以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3546 3547| 错误码ID | 错误信息 | 3548| -------- | -------- | 3549| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3550 3551**示例:** 3552 3553 ```ts 3554 let base64Helper = new util.Base64Helper(); 3555 let array = 'TWFuaXNkaXN0aW5ndWlzaGVkbm90b25seWJ5aGlzcmVhc29uYnV0Ynl0aGlzc2luZ3VsYXJwYXNz\r\naW9uZnJvbW90aGVyYW5pbWFsc3doaWNoaXNhbHVzdG9mdGhlbWluZGV4Y2VlZHN0aGVzaG9ydHZl\r\naGVtZW5jZW9mYW55Y2FybmFscGxlYXN1cmU=\r\n'; 3556 base64Helper.decode(array, util.Type.MIME).then((val) => { 3557 console.info(val.toString()); 3558 /* 3559 输出结果:77,97,110,105,115,100,105,115,116,105,110,103,117,105,115,104,101,100,110,111,116,111,110,108,121,98,121,104,105,115,114,101,97,115,111,110,98,117,116,98,121,116,104,105,115,115,105,110,103,117,108,97,114,112,97,115,115,105,111,110,102,114,111,109,111,116,104,101,114,97,110,105,109,97,108,115,119,104,105,99,104,105,115,97,108,117,115,116,111,102,116,104,101,109,105,110,100,101,120,99,101,101,100,115,116,104,101,115,104,111,114,116,118,101,104,101,109,101,110,99,101,111,102,97,110,121,99,97,114,110,97,108,112,108,101,97,115,117,114,101 3560 */ 3561 }) 3562 ``` 3563 3564## StringDecoder<sup>12+</sup> 3565 3566提供将二进制流解码为字符串的能力。支持的编码类型包括:utf-8、iso-8859-2、koi8-r、macintosh、windows-1250、windows-1251、gbk、gb18030、big5、utf-16be、utf-16le等。 3567 3568### constructor<sup>12+</sup> 3569 3570constructor(encoding?: string) 3571 3572StringDecoder的构造函数。 3573 3574**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 3575 3576**系统能力:** SystemCapability.Utils.Lang 3577 3578**参数:** 3579 3580| 参数名 | 类型 | 必填 | 说明 | 3581| ------ | ------------------------------ | ---- | --------------------------------- | 3582| encoding | string | 否 | 输入数据的编码类型。默认值:'utf-8'。 | 3583 3584**错误码:** 3585 3586以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3587 3588| 错误码ID | 错误信息 | 3589| -------- | -------- | 3590| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3591 3592**示例:** 3593 3594 ```ts 3595 let decoder = new util.StringDecoder(); 3596 ``` 3597 3598### write<sup>12+</sup> 3599 3600write(chunk: string | Uint8Array): string 3601 3602返回一个解码后的字符串,确保Uint8Array末尾的任何不完整的多字节字符从返回的字符串中被过滤,并保存在一个内部的buffer中用于下次调用。 3603 3604**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 3605 3606**系统能力:** SystemCapability.Utils.Lang 3607 3608**参数:** 3609 3610| 参数名 | 类型 | 必填 | 说明 | 3611| ------ | ---------- | ---- | ------------------- | 3612| chunk | string \| Uint8Array | 是 | 需要解码的字符串。会根据输入的编码类型进行解码,参数为Uint8Array时会正常解码,参数为string类型时会原路返回。 | 3613 3614**返回值:** 3615 3616| 类型 | 说明 | 3617| ---------- | ----------------------------- | 3618| string | 返回解码后的字符串。 | 3619 3620**错误码:** 3621 3622以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3623 3624| 错误码ID | 错误信息 | 3625| -------- | -------- | 3626| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3627 3628**示例:** 3629 3630 ```ts 3631 let decoder = new util.StringDecoder('utf-8'); 3632 let input = new Uint8Array([0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD]); 3633 const decoded = decoder.write(input); 3634 console.info("decoded:", decoded); 3635 // 输出结果:decoded: 你好 3636 ``` 3637 3638### end<sup>12+</sup> 3639 3640end(chunk?: string | Uint8Array): string 3641 3642结束解码过程,以字符串形式返回存储在内部缓冲区中的任何剩余输入。 3643 3644**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 3645 3646**系统能力:** SystemCapability.Utils.Lang 3647 3648**参数:** 3649 3650| 参数名 | 类型 | 必填 | 说明 | 3651| ------ | ---------- | ---- | ------------------- | 3652| chunk | string \| Uint8Array | 否 | 需要解码的字符串。默认为undefined。 | 3653 3654**返回值:** 3655 3656| 类型 | 说明 | 3657| ---------- | ----------------------------- | 3658| string | 返回解码后的字符串。 | 3659 3660**错误码:** 3661 3662以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3663 3664| 错误码ID | 错误信息 | 3665| -------- | -------- | 3666| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3667 3668**示例:** 3669 3670 ```ts 3671 let decoder = new util.StringDecoder('utf-8'); 3672 let input = new Uint8Array([0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD]); 3673 const writeString = decoder.write(input.slice(0, 5)); 3674 const endString = decoder.end(input.slice(5)); 3675 console.info("writeString:", writeString); 3676 // 输出结果:writeString: 你 3677 console.info("endString:", endString); 3678 // 输出结果:endString: 好 3679 ``` 3680 3681## Type<sup>10+</sup> 3682 3683Base64编码格式枚举。 3684 3685**系统能力:** SystemCapability.Utils.Lang 3686 3687 3688| 名称 |值| 说明 | 3689| ----- |---| ----------------- | 3690| BASIC | 0 | 表示BASIC编码格式。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。| 3691| MIME | 1 | 表示MIME编码格式。**原子化服务API**:从API version 11 开始,该接口支持在原子化服务中使用。| 3692| BASIC_URL_SAFE<sup>12+</sup> | 2 | 表示BASIC_URL_SAFE编码格式。<br/>从API version 12开始支持此枚举。**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。| 3693| MIME_URL_SAFE<sup>12+</sup> | 3 | 表示MIME_URL_SAFE编码格式。<br/>从API version 12开始支持此枚举。**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 | 3694 3695 3696## types<sup>8+</sup> 3697 3698types为不同类型的内置对象提供类型检查,可以避免由于类型错误导致的异常或崩溃。该模块包含了多个工具函数,用于判断JS对象是否属于各种类型例如:ArrayBuffer、Map、Set等。 3699 3700### constructor<sup>8+</sup> 3701 3702constructor() 3703 3704Types的构造函数。 3705 3706**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3707 3708**系统能力:** SystemCapability.Utils.Lang 3709 3710**示例:** 3711 3712 ```ts 3713 let type = new util.types(); 3714 ``` 3715 3716 3717### isAnyArrayBuffer<sup>8+</sup> 3718 3719isAnyArrayBuffer(value: Object): boolean 3720 3721检查输入的value是否是ArrayBuffer或SharedArrayBuffer类型。 3722 3723**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3724 3725**系统能力:** SystemCapability.Utils.Lang 3726 3727**参数:** 3728 3729| 参数名 | 类型 | 必填 | 说明 | 3730| -------- | -------- | -------- | -------- | 3731| value | Object | 是 | 待检测对象。 | 3732 3733**返回值:** 3734 3735| 类型 | 说明 | 3736| -------- | -------- | 3737| boolean | 判断的结果,如果是ArrayBuffer或SharedArrayBuffer类型为true,反之为false。 | 3738 3739**示例:** 3740 3741 ```ts 3742 let type = new util.types(); 3743 let result = type.isAnyArrayBuffer(new ArrayBuffer(0)); 3744 console.info("result = " + result); 3745 // 输出结果:result = true 3746 ``` 3747 3748 3749### isArrayBufferView<sup>8+</sup> 3750 3751isArrayBufferView(value: Object): boolean 3752 3753检查输入的value是否是内置ArrayBufferView辅助类型。 3754 3755ArrayBufferView辅助类型包括:Int8Array、Int16Array、Int32Array、Uint8Array、Uint8ClampedArray、Uint32Array、Float32Array、Float64Array、DataView。 3756 3757**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3758 3759**系统能力:** SystemCapability.Utils.Lang 3760 3761**参数:** 3762 3763| 参数名 | 类型 | 必填 | 说明 | 3764| -------- | -------- | -------- | -------- | 3765| value | Object | 是 | 待检测对象。 | 3766 3767**返回值:** 3768 3769| 类型 | 说明 | 3770| -------- | -------- | 3771| boolean | 判断的结果,如果是内置包含的ArrayBufferView辅助类型为true,反之为false。 | 3772 3773**示例:** 3774 3775 ```ts 3776 let type = new util.types(); 3777 let result = type.isArrayBufferView(new Int8Array([])); 3778 console.info("result = " + result); 3779 // 输出结果:result = true 3780 ``` 3781 3782 3783### isArgumentsObject<sup>8+</sup> 3784 3785isArgumentsObject(value: Object): boolean 3786 3787检查输入的value是否是一个arguments对象。 3788 3789**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3790 3791**系统能力:** SystemCapability.Utils.Lang 3792 3793**参数:** 3794 3795| 参数名 | 类型 | 必填 | 说明 | 3796| -------- | -------- | -------- | -------- | 3797| value | Object | 是 | 待检测对象。 | 3798 3799**返回值:** 3800 3801| 类型 | 说明 | 3802| -------- | -------- | 3803| boolean | 判断的结果,如果是一个arguments对象为true,反之为false。 | 3804 3805**示例:** 3806 3807 ```ts 3808 let type = new util.types(); 3809 function foo() { 3810 let result = type.isArgumentsObject(arguments); 3811 console.info("result = " + result); 3812 } 3813 let f = foo(); 3814 // 输出结果:result = true 3815 ``` 3816 3817 3818### isArrayBuffer<sup>8+</sup> 3819 3820isArrayBuffer(value: Object): boolean 3821 3822检查输入的value是否是ArrayBuffer类型。 3823 3824**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3825 3826**系统能力:** SystemCapability.Utils.Lang 3827 3828**参数:** 3829 3830| 参数名 | 类型 | 必填 | 说明 | 3831| -------- | -------- | -------- | -------- | 3832| value | Object | 是 | 待检测对象。 | 3833 3834**返回值:** 3835 3836| 类型 | 说明 | 3837| -------- | -------- | 3838| boolean | 判断的结果,如果是内置包含的ArrayBuffer类型为true,反之为false。 | 3839 3840**示例:** 3841 3842 ```ts 3843 let type = new util.types(); 3844 let result = type.isArrayBuffer(new ArrayBuffer(0)); 3845 console.info("result = " + result); 3846 // 输出结果:result = true 3847 ``` 3848 3849 3850### isAsyncFunction<sup>8+</sup> 3851 3852isAsyncFunction(value: Object): boolean 3853 3854检查输入的value是否是一个异步函数类型。 3855 3856**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3857 3858**系统能力:** SystemCapability.Utils.Lang 3859 3860**参数:** 3861 3862| 参数名 | 类型 | 必填 | 说明 | 3863| -------- | -------- | -------- | -------- | 3864| value | Object | 是 | 待检测对象。 | 3865 3866**返回值:** 3867 3868| 类型 | 说明 | 3869| -------- | -------- | 3870| boolean | 判断的结果,如果是内置包含的异步函数类型为true,反之为false。 | 3871 3872**示例:** 3873 3874 ```ts 3875 let type = new util.types(); 3876 let result = type.isAsyncFunction(async () => {}); 3877 console.info("result = " + result); 3878 // 输出结果:result = true 3879 ``` 3880 3881 3882### isBooleanObject<sup>(deprecated)</sup> 3883 3884isBooleanObject(value: Object): boolean 3885 3886检查输入的value是否是一个Boolean对象类型。 3887 3888> **说明:** 3889> 3890> 从API version 8开始支持,从API version 14开始废弃,没有替代接口。 3891 3892**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3893 3894**系统能力:** SystemCapability.Utils.Lang 3895 3896**参数:** 3897 3898| 参数名 | 类型 | 必填 | 说明 | 3899| -------- | -------- | -------- | -------- | 3900| value | Object | 是 | 待检测对象。 | 3901 3902**返回值:** 3903 3904| 类型 | 说明 | 3905| -------- | -------- | 3906| boolean | 判断的结果,如果是内置包含的Boolean对象类型为true,反之为false。 | 3907 3908**示例:** 3909 3910 ```ts 3911 let type = new util.types(); 3912 let result = type.isBooleanObject(new Boolean(true)); 3913 console.info("result = " + result); 3914 // 输出结果:result = true 3915 ``` 3916 3917 3918### isBoxedPrimitive<sup>(deprecated)</sup> 3919 3920isBoxedPrimitive(value: Object): boolean 3921 3922检查输入的value是否是Boolean或Number或String或Symbol对象类型。 3923 3924> **说明:** 3925> 3926> 从API version 8开始支持,从API version 14开始废弃,没有替代接口。 3927 3928**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3929 3930**系统能力:** SystemCapability.Utils.Lang 3931 3932**参数:** 3933 3934| 参数名 | 类型 | 必填 | 说明 | 3935| -------- | -------- | -------- | -------- | 3936| value | Object | 是 | 待检测对象。 | 3937 3938**返回值:** 3939 3940| 类型 | 说明 | 3941| -------- | -------- | 3942| boolean | 判断的结果,如果是内置包含的Boolean或Number或String或Symbol对象类型为true,反之为false。 | 3943 3944**示例:** 3945 3946 ```ts 3947 let type = new util.types(); 3948 let result = type.isBoxedPrimitive(new Boolean(false)); 3949 console.info("result = " + result); 3950 // 输出结果:result = true 3951 ``` 3952 3953 3954### isDataView<sup>8+</sup> 3955 3956isDataView(value: Object): boolean 3957 3958检查输入的value是否是DataView类型。 3959 3960**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3961 3962**系统能力:** SystemCapability.Utils.Lang 3963 3964**参数:** 3965 3966| 参数名 | 类型 | 必填 | 说明 | 3967| -------- | -------- | -------- | -------- | 3968| value | Object | 是 | 待检测对象。 | 3969 3970**返回值:** 3971 3972| 类型 | 说明 | 3973| -------- | -------- | 3974| boolean | 判断的结果,如果是内置包含的DataView对象类型为true,反之为false。 | 3975 3976**示例:** 3977 3978 ```ts 3979 let type = new util.types(); 3980 const ab = new ArrayBuffer(20); 3981 let result = type.isDataView(new DataView(ab)); 3982 console.info("result = " + result); 3983 // 输出结果:result = true 3984 ``` 3985 3986 3987### isDate<sup>8+</sup> 3988 3989isDate(value: Object): boolean 3990 3991检查输入的value是否是Date类型。 3992 3993**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3994 3995**系统能力:** SystemCapability.Utils.Lang 3996 3997**参数:** 3998 3999| 参数名 | 类型 | 必填 | 说明 | 4000| -------- | -------- | -------- | -------- | 4001| value | Object | 是 | 待检测对象。 | 4002 4003**返回值:** 4004 4005| 类型 | 说明 | 4006| -------- | -------- | 4007| boolean | 判断的结果,如果是内置包含的Date对象类型为true,反之为false。 | 4008 4009**示例:** 4010 4011 ```ts 4012 let type = new util.types(); 4013 let result = type.isDate(new Date()); 4014 console.info("result = " + result); 4015 // 输出结果:result = true 4016 ``` 4017 4018 4019### isExternal<sup>8+</sup> 4020 4021isExternal(value: Object): boolean 4022 4023检查输入的value是否是native External类型。 4024 4025**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4026 4027**系统能力:** SystemCapability.Utils.Lang 4028 4029**参数:** 4030 4031| 参数名 | 类型 | 必填 | 说明 | 4032| -------- | -------- | -------- | -------- | 4033| value | Object | 是 | 待检测对象。 | 4034 4035**返回值:** 4036 4037| 类型 | 说明 | 4038| -------- | -------- | 4039| boolean | 判断的结果,如果是内置包含native External类型为true,反之为false。 | 4040 4041**示例:** 4042 4043 ```cpp 4044 // /entry/src/main/cpp/napi_init.cpp 4045 #include "napi/native_api.h" 4046 #include <js_native_api.h> 4047 #include <stdlib.h> 4048 4049 napi_value result; 4050 static napi_value Testexternal(napi_env env, napi_callback_info info) { 4051 int* raw = (int*) malloc(1024); 4052 napi_status status = napi_create_external(env, (void*) raw, NULL, NULL, &result); 4053 if (status != napi_ok) { 4054 napi_throw_error(env, NULL, "create external failed"); 4055 return NULL; 4056 } 4057 return result; 4058 } 4059 4060 EXTERN_C_START 4061 static napi_value Init(napi_env env, napi_value exports) 4062 { 4063 napi_property_descriptor desc[] = { 4064 {"testexternal", nullptr, Testexternal, nullptr, nullptr, nullptr, napi_default, nullptr}, 4065 }; 4066 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 4067 return exports; 4068 } 4069 EXTERN_C_END 4070 // 此处已省略模块注册的代码, 你可能需要自行注册Testexternal方法 4071 ... 4072 4073 ``` 4074 4075 <!--code_no_check--> 4076 ```ts 4077 import testNapi from 'libentry.so'; 4078 4079 let type = new util.types(); 4080 const data = testNapi.testexternal(); 4081 let result = type.isExternal(data); 4082 4083 let result01 = type.isExternal(true); 4084 console.info("result = " + result); 4085 console.info("result01 = " + result01); 4086 // 输出结果:result = true 4087 // 输出结果:result01 = false 4088 ``` 4089 4090 4091### isFloat32Array<sup>8+</sup> 4092 4093isFloat32Array(value: Object): boolean 4094 4095检查输入的value是否是Float32Array数组类型。 4096 4097**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4098 4099**系统能力:** SystemCapability.Utils.Lang 4100 4101**参数:** 4102 4103| 参数名 | 类型 | 必填 | 说明 | 4104| -------- | -------- | -------- | -------- | 4105| value | Object | 是 | 待检测对象。 | 4106 4107**返回值:** 4108 4109| 类型 | 说明 | 4110| -------- | -------- | 4111| boolean | 判断的结果,如果是内置包含的Float32Array数组类型为true,反之为false。 | 4112 4113**示例:** 4114 4115 ```ts 4116 let type = new util.types(); 4117 let result = type.isFloat32Array(new Float32Array()); 4118 console.info("result = " + result); 4119 // 输出结果:result = true 4120 ``` 4121 4122 4123### isFloat64Array<sup>8+</sup> 4124 4125isFloat64Array(value: Object): boolean 4126 4127检查输入的value是否是Float64Array数组类型。 4128 4129**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4130 4131**系统能力:** SystemCapability.Utils.Lang 4132 4133**参数:** 4134 4135| 参数名 | 类型 | 必填 | 说明 | 4136| -------- | -------- | -------- | -------- | 4137| value | Object | 是 | 待检测对象。 | 4138 4139**返回值:** 4140 4141| 类型 | 说明 | 4142| -------- | -------- | 4143| boolean | 判断的结果,如果是内置包含的Float64Array数组类型为true,反之为false。 | 4144 4145**示例:** 4146 4147 ```ts 4148 let type = new util.types(); 4149 let result = type.isFloat64Array(new Float64Array()); 4150 console.info("result = " + result); 4151 // 输出结果:result = true 4152 ``` 4153 4154 4155### isGeneratorFunction<sup>8+</sup> 4156 4157isGeneratorFunction(value: Object): boolean 4158 4159检查输入的value是否是generator函数类型。 4160 4161**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4162 4163**系统能力:** SystemCapability.Utils.Lang 4164 4165**参数:** 4166 4167| 参数名 | 类型 | 必填 | 说明 | 4168| -------- | -------- | -------- | -------- | 4169| value | Object | 是 | 待检测对象。 | 4170 4171**返回值:** 4172 4173| 类型 | 说明 | 4174| -------- | -------- | 4175| boolean | 判断的结果,如果是内置包含的generator函数类型为true,反之为false。 | 4176 4177**示例:** 4178 4179 ```ts 4180 // /entry/src/main/ets/pages/test.ts 4181 export function* foo() {} 4182 ``` 4183 4184 <!--code_no_check--> 4185 ```ts 4186 import { foo } from './test' 4187 4188 let type = new util.types(); 4189 let result = type.isGeneratorFunction(foo); 4190 console.info("result = " + result); 4191 // 输出结果:result = true 4192 ``` 4193 4194 4195### isGeneratorObject<sup>8+</sup> 4196 4197isGeneratorObject(value: Object): boolean 4198 4199检查输入的value是否是generator对象类型。 4200 4201**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4202 4203**系统能力:** SystemCapability.Utils.Lang 4204 4205**参数:** 4206 4207| 参数名 | 类型 | 必填 | 说明 | 4208| -------- | -------- | -------- | -------- | 4209| value | Object | 是 | 待检测对象。 | 4210 4211**返回值:** 4212 4213| 类型 | 说明 | 4214| -------- | -------- | 4215| boolean | 判断的结果,如果是内置包含的generator对象类型为true,反之为false。 | 4216 4217**示例:** 4218 4219 ```ts 4220 // /entry/src/main/ets/pages/test.ts 4221 function* foo() {} 4222 export const generator = foo(); 4223 ``` 4224 4225 <!--code_no_check--> 4226 ```ts 4227 import { generator } from './test' 4228 4229 let type = new util.types(); 4230 let result = type.isGeneratorObject(generator); 4231 console.info("result = " + result); 4232 // 输出结果:result = true 4233 ``` 4234 4235 4236### isInt8Array<sup>8+</sup> 4237 4238isInt8Array(value: Object): boolean 4239 4240检查输入的value是否是Int8Array数组类型。 4241 4242**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4243 4244**系统能力:** SystemCapability.Utils.Lang 4245 4246**参数:** 4247 4248| 参数名 | 类型 | 必填 | 说明 | 4249| -------- | -------- | -------- | -------- | 4250| value | Object | 是 | 待检测对象。 | 4251 4252**返回值:** 4253 4254| 类型 | 说明 | 4255| -------- | -------- | 4256| boolean | 判断的结果,如果是内置包含的Int8Array数组类型为true,反之为false。 | 4257 4258**示例:** 4259 4260 ```ts 4261 let type = new util.types(); 4262 let result = type.isInt8Array(new Int8Array([])); 4263 console.info("result = " + result); 4264 // 输出结果:result = true 4265 ``` 4266 4267 4268### isInt16Array<sup>8+</sup> 4269 4270isInt16Array(value: Object): boolean 4271 4272检查输入的value是否是Int16Array数组类型。 4273 4274**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4275 4276**系统能力:** SystemCapability.Utils.Lang 4277 4278**参数:** 4279 4280| 参数名 | 类型 | 必填 | 说明 | 4281| -------- | -------- | -------- | -------- | 4282| value | Object | 是 | 待检测对象。 | 4283 4284**返回值:** 4285 4286| 类型 | 说明 | 4287| -------- | -------- | 4288| boolean | 判断的结果,如果是内置包含的Int16Array数组类型为true,反之为false。 | 4289 4290**示例:** 4291 4292 ```ts 4293 let type = new util.types(); 4294 let result = type.isInt16Array(new Int16Array([])); 4295 console.info("result = " + result); 4296 // 输出结果:result = true 4297 ``` 4298 4299 4300### isInt32Array<sup>8+</sup> 4301 4302isInt32Array(value: Object): boolean 4303 4304检查输入的value是否是Int32Array数组类型。 4305 4306**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4307 4308**系统能力:** SystemCapability.Utils.Lang 4309 4310**参数:** 4311 4312| 参数名 | 类型 | 必填 | 说明 | 4313| -------- | -------- | -------- | -------- | 4314| value | Object | 是 | 待检测对象。 | 4315 4316**返回值:** 4317 4318| 类型 | 说明 | 4319| -------- | -------- | 4320| boolean | 判断的结果,如果是内置包含的Int32Array数组类型为true,反之为false。 | 4321 4322**示例:** 4323 4324 ```ts 4325 let type = new util.types(); 4326 let result = type.isInt32Array(new Int32Array([])); 4327 console.info("result = " + result); 4328 // 输出结果:result = true 4329 ``` 4330 4331 4332### isMap<sup>8+</sup> 4333 4334isMap(value: Object): boolean 4335 4336检查输入的value是否是Map类型。 4337 4338**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4339 4340**系统能力:** SystemCapability.Utils.Lang 4341 4342**参数:** 4343 4344| 参数名 | 类型 | 必填 | 说明 | 4345| -------- | -------- | -------- | -------- | 4346| value | Object | 是 | 待检测对象。 | 4347 4348**返回值:** 4349 4350| 类型 | 说明 | 4351| -------- | -------- | 4352| boolean | 判断的结果,如果是内置包含的Map类型为true,反之为false。 | 4353 4354**示例:** 4355 4356 ```ts 4357 let type = new util.types(); 4358 let result = type.isMap(new Map()); 4359 console.info("result = " + result); 4360 // 输出结果:result = true 4361 ``` 4362 4363 4364### isMapIterator<sup>8+</sup> 4365 4366isMapIterator(value: Object): boolean 4367 4368检查输入的value是否是Map的Iterator类型。 4369 4370**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4371 4372**系统能力:** SystemCapability.Utils.Lang 4373 4374**参数:** 4375 4376 4377| 参数名 | 类型 | 必填 | 说明 | 4378| -------- | -------- | -------- | -------- | 4379| value | Object | 是 | 待检测对象。 | 4380 4381**返回值:** 4382 4383| 类型 | 说明 | 4384| -------- | -------- | 4385| boolean | 判断的结果,如果是内置包含的Map的Iterator类型为true,反之为false。 | 4386 4387**示例:** 4388 4389 ```ts 4390 let type = new util.types(); 4391 const map : Map<number,number> = new Map(); 4392 let result = type.isMapIterator(map.keys()); 4393 console.info("result = " + result); 4394 // 输出结果:result = true 4395 ``` 4396 4397 4398### isNativeError<sup>8+</sup> 4399 4400isNativeError(value: Object): boolean 4401 4402检查输入的value是否是Error类型。 4403 4404**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4405 4406**系统能力:** SystemCapability.Utils.Lang 4407 4408**参数:** 4409 4410| 参数名 | 类型 | 必填 | 说明 | 4411| -------- | -------- | -------- | -------- | 4412| value | Object | 是 | 待检测对象。 | 4413 4414**返回值:** 4415 4416| 类型 | 说明 | 4417| -------- | -------- | 4418| boolean | 判断的结果,如果是内置包含的Error类型为true,反之为false。 | 4419 4420**示例:** 4421 4422 ```ts 4423 let type = new util.types(); 4424 let result = type.isNativeError(new TypeError()); 4425 console.info("result = " + result); 4426 // 输出结果:result = true 4427 ``` 4428 4429 4430### isNumberObject<sup>(deprecated)</sup> 4431 4432isNumberObject(value: Object): boolean 4433 4434检查输入的value是否是Number对象类型。 4435 4436> **说明:** 4437> 4438> 从API version 8开始支持,从API version 14开始废弃,没有替代接口。 4439 4440**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4441 4442**系统能力:** SystemCapability.Utils.Lang 4443 4444**参数:** 4445 4446| 参数名 | 类型 | 必填 | 说明 | 4447| -------- | -------- | -------- | -------- | 4448| value | Object | 是 | 待检测对象。 | 4449 4450**返回值:** 4451 4452| 类型 | 说明 | 4453| -------- | -------- | 4454| boolean | 判断的结果,如果是内置包含的Number对象类型为true,反之为false。 | 4455 4456**示例:** 4457 4458 ```ts 4459 let type = new util.types(); 4460 let result = type.isNumberObject(new Number(0)); 4461 console.info("result = " + result); 4462 // 输出结果:result = true 4463 ``` 4464 4465 4466### isPromise<sup>8+</sup> 4467 4468isPromise(value: Object): boolean 4469 4470检查输入的value是否是Promise类型。 4471 4472**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4473 4474**系统能力:** SystemCapability.Utils.Lang 4475 4476**参数:** 4477 4478| 参数名 | 类型 | 必填 | 说明 | 4479| -------- | -------- | -------- | -------- | 4480| value | Object | 是 | 待检测对象。 | 4481 4482**返回值:** 4483 4484| 类型 | 说明 | 4485| -------- | -------- | 4486| boolean | 判断的结果,如果是内置包含的Promise类型为true,反之为false。 | 4487 4488**示例:** 4489 4490 ```ts 4491 let type = new util.types(); 4492 let result = type.isPromise(Promise.resolve(1)); 4493 console.info("result = " + result); 4494 // 输出结果:result = true 4495 ``` 4496 4497 4498### isProxy<sup>8+</sup> 4499 4500isProxy(value: Object): boolean 4501 4502检查输入的value是否是Proxy类型。 4503 4504**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4505 4506**系统能力:** SystemCapability.Utils.Lang 4507 4508**参数:** 4509 4510| 参数名 | 类型 | 必填 | 说明 | 4511| -------- | -------- | -------- | -------- | 4512| value | Object | 是 | 待检测对象。 | 4513 4514**返回值:** 4515 4516| 类型 | 说明 | 4517| -------- | -------- | 4518| boolean | 判断的结果,如果是内置包含的Proxy类型为true,反之为false。 | 4519 4520**示例:** 4521 4522 ```ts 4523 class Target{ 4524 } 4525 let type = new util.types(); 4526 const target : Target = {}; 4527 const proxy = new Proxy(target, target); 4528 let result = type.isProxy(proxy); 4529 console.info("result = " + result); 4530 // 输出结果:result = true 4531 ``` 4532 4533 4534### isRegExp<sup>8+</sup> 4535 4536isRegExp(value: Object): boolean 4537 4538检查输入的value是否是RegExp类型。 4539 4540**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4541 4542**系统能力:** SystemCapability.Utils.Lang 4543 4544**参数:** 4545 4546| 参数名 | 类型 | 必填 | 说明 | 4547| -------- | -------- | -------- | -------- | 4548| value | Object | 是 | 待检测对象。 | 4549 4550**返回值:** 4551 4552| 类型 | 说明 | 4553| -------- | -------- | 4554| boolean | 判断的结果,如果是内置包含的RegExp类型为true,反之为false。 | 4555 4556**示例:** 4557 4558 ```ts 4559 let type = new util.types(); 4560 let result = type.isRegExp(new RegExp('abc')); 4561 console.info("result = " + result); 4562 // 输出结果:result = true 4563 ``` 4564 4565 4566### isSet<sup>8+</sup> 4567 4568isSet(value: Object): boolean 4569 4570检查输入的value是否是Set类型。 4571 4572**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4573 4574**系统能力:** SystemCapability.Utils.Lang 4575 4576**参数:** 4577 4578| 参数名 | 类型 | 必填 | 说明 | 4579| -------- | -------- | -------- | -------- | 4580| value | Object | 是 | 待检测对象。 | 4581 4582**返回值:** 4583 4584| 类型 | 说明 | 4585| -------- | -------- | 4586| boolean | 判断的结果,如果是内置包含的Set类型为true,反之为false。 | 4587 4588**示例:** 4589 4590 ```ts 4591 let type = new util.types(); 4592 let set : Set<number> = new Set(); 4593 let result = type.isSet(set); 4594 console.info("result = " + result); 4595 // 输出结果:result = true 4596 ``` 4597 4598 4599### isSetIterator<sup>8+</sup> 4600 4601isSetIterator(value: Object): boolean 4602 4603检查输入的value是否是Set的Iterator类型。 4604 4605**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4606 4607**系统能力:** SystemCapability.Utils.Lang 4608 4609**参数:** 4610 4611| 参数名 | 类型 | 必填 | 说明 | 4612| -------- | -------- | -------- | -------- | 4613| value | Object | 是 | 待检测对象。 | 4614 4615**返回值:** 4616 4617| 类型 | 说明 | 4618| -------- | -------- | 4619| boolean | 判断的结果,如果是内置包含的Set的Iterator类型为true,反之为false。 | 4620 4621**示例:** 4622 4623 ```ts 4624 let type = new util.types(); 4625 const set : Set<number> = new Set(); 4626 let result = type.isSetIterator(set.keys()); 4627 console.info("result = " + result); 4628 // 输出结果:result = true 4629 ``` 4630 4631 4632### isStringObject<sup>(deprecated)</sup> 4633 4634isStringObject(value: Object): boolean 4635 4636检查输入的value是否是String对象类型。 4637 4638> **说明:** 4639> 4640> 从API version 8开始支持,从API version 14开始废弃,没有替代接口。 4641 4642**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4643 4644**系统能力:** SystemCapability.Utils.Lang 4645 4646**参数:** 4647 4648| 参数名 | 类型 | 必填 | 说明 | 4649| -------- | -------- | -------- | -------- | 4650| value | Object | 是 | 待检测对象。 | 4651 4652**返回值:** 4653 4654| 类型 | 说明 | 4655| -------- | -------- | 4656| boolean | 判断的结果,如果是内置包含的String对象类型为true,反之为false。 | 4657 4658**示例:** 4659 4660 ```ts 4661 let type = new util.types(); 4662 let result = type.isStringObject(new String('foo')); 4663 console.info("result = " + result); 4664 // 输出结果:result = true 4665 ``` 4666 4667 4668### isSymbolObject<sup>(deprecated)</sup> 4669 4670isSymbolObject(value: Object): boolean 4671 4672检查输入的value是否是Symbol对象类型。 4673 4674> **说明:** 4675> 4676> 从API version 8开始支持,从API version 14开始废弃,没有替代接口。 4677 4678**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4679 4680**系统能力:** SystemCapability.Utils.Lang 4681 4682**参数:** 4683 4684| 参数名 | 类型 | 必填 | 说明 | 4685| -------- | -------- | -------- | -------- | 4686| value | Object | 是 | 待检测对象。 | 4687 4688**返回值:** 4689 4690| 类型 | 说明 | 4691| -------- | -------- | 4692| boolean | 判断的结果,如果是内置包含的Symbol对象类型为true,反之为false。 | 4693 4694**示例:** 4695 4696 ```ts 4697 // /entry/src/main/ets/pages/test.ts 4698 export const symbols = Symbol('foo'); 4699 ``` 4700 4701 <!--code_no_check--> 4702 ```ts 4703 import { symbols } from './test' 4704 4705 let type = new util.types(); 4706 let result = type.isSymbolObject(Object(symbols)); 4707 console.info("result = " + result); 4708 // 输出结果:result = true 4709 ``` 4710 4711 4712### isTypedArray<sup>8+</sup> 4713 4714isTypedArray(value: Object): boolean 4715 4716检查输入的value是否是TypedArray类型的辅助类型。 4717 4718TypedArray类型的辅助类型,包括Int8Array、Int16Array、Int32Array、Uint8Array、Uint8ClampedArray、Uint16Array、Uint32Array、Float32Array、Float64Array、BigInt64Array、BigUint64Array。 4719 4720**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4721 4722**系统能力:** SystemCapability.Utils.Lang 4723 4724**参数:** 4725 4726| 参数名 | 类型 | 必填 | 说明 | 4727| -------- | -------- | -------- | -------- | 4728| value | Object | 是 | 待检测对象。 | 4729 4730**返回值:** 4731 4732| 类型 | 说明 | 4733| -------- | -------- | 4734| boolean | 判断的结果,如果是内置包含的TypedArray包含的类型为true,反之为false。 | 4735 4736**示例:** 4737 4738 ```ts 4739 let type = new util.types(); 4740 let result = type.isTypedArray(new Float64Array([])); 4741 console.info("result = " + result); 4742 // 输出结果:result = true 4743 ``` 4744 4745 4746### isUint8Array<sup>8+</sup> 4747 4748isUint8Array(value: Object): boolean 4749 4750检查输入的value是否是Uint8Array数组类型。 4751 4752**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4753 4754**系统能力:** SystemCapability.Utils.Lang 4755 4756**参数:** 4757 4758| 参数名 | 类型 | 必填 | 说明 | 4759| -------- | -------- | -------- | -------- | 4760| value | Object | 是 | 待检测对象。 | 4761 4762**返回值:** 4763 4764| 类型 | 说明 | 4765| -------- | -------- | 4766| boolean | 判断的结果,如果是内置包含的Uint8Array数组类型为true,反之为false。 | 4767 4768**示例:** 4769 4770 ```ts 4771 let type = new util.types(); 4772 let result = type.isUint8Array(new Uint8Array([])); 4773 console.info("result = " + result); 4774 // 输出结果:result = true 4775 ``` 4776 4777 4778### isUint8ClampedArray<sup>8+</sup> 4779 4780isUint8ClampedArray(value: Object): boolean 4781 4782检查输入的value是否是Uint8ClampedArray数组类型。 4783 4784**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4785 4786**系统能力:** SystemCapability.Utils.Lang 4787 4788**参数:** 4789 4790| 参数名 | 类型 | 必填 | 说明 | 4791| -------- | -------- | -------- | -------- | 4792| value | Object | 是 | 待检测对象。 | 4793 4794**返回值:** 4795 4796| 类型 | 说明 | 4797| -------- | -------- | 4798| boolean | 判断的结果,如果是内置包含的Uint8ClampedArray数组类型为true,反之为false。 | 4799 4800**示例:** 4801 4802 ```ts 4803 let type = new util.types(); 4804 let result = type.isUint8ClampedArray(new Uint8ClampedArray([])); 4805 console.info("result = " + result); 4806 // 输出结果:result = true 4807 ``` 4808 4809 4810### isUint16Array<sup>8+</sup> 4811 4812isUint16Array(value: Object): boolean 4813 4814检查输入的value是否是Uint16Array数组类型。 4815 4816**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4817 4818**系统能力:** SystemCapability.Utils.Lang 4819 4820**参数:** 4821 4822| 参数名 | 类型 | 必填 | 说明 | 4823| -------- | -------- | -------- | -------- | 4824| value | Object | 是 | 待检测对象。 | 4825 4826**返回值:** 4827 4828| 类型 | 说明 | 4829| -------- | -------- | 4830| boolean | 判断的结果,如果是内置包含的Uint16Array数组类型为true,反之为false。 | 4831 4832**示例:** 4833 4834 ```ts 4835 let type = new util.types(); 4836 let result = type.isUint16Array(new Uint16Array([])); 4837 console.info("result = " + result); 4838 // 输出结果:result = true 4839 ``` 4840 4841 4842### isUint32Array<sup>8+</sup> 4843 4844isUint32Array(value: Object): boolean 4845 4846检查输入的value是否是Uint32Array数组类型。 4847 4848**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4849 4850**系统能力:** SystemCapability.Utils.Lang 4851 4852**参数:** 4853 4854| 参数名 | 类型 | 必填 | 说明 | 4855| -------- | -------- | -------- | -------- | 4856| value | Object | 是 | 待检测对象。 | 4857 4858**返回值:** 4859 4860| 类型 | 说明 | 4861| -------- | -------- | 4862| boolean | 判断的结果,如果是内置包含的Uint32Array数组类型为true,反之为false。 | 4863 4864**示例:** 4865 4866 ```ts 4867 let type = new util.types(); 4868 let result = type.isUint32Array(new Uint32Array([])); 4869 console.info("result = " + result); 4870 // 输出结果:result = true 4871 ``` 4872 4873 4874### isWeakMap<sup>8+</sup> 4875 4876isWeakMap(value: Object): boolean 4877 4878检查输入的value是否是WeakMap类型。 4879 4880**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4881 4882**系统能力:** SystemCapability.Utils.Lang 4883 4884**参数:** 4885 4886| 参数名 | 类型 | 必填 | 说明 | 4887| -------- | -------- | -------- | -------- | 4888| value | Object | 是 | 待检测对象。 | 4889 4890**返回值:** 4891 4892| 类型 | 说明 | 4893| -------- | -------- | 4894| boolean | 判断的结果,如果是内置包含的WeakMap类型为true,反之为false。 | 4895 4896**示例:** 4897 4898 ```ts 4899 let type = new util.types(); 4900 let value : WeakMap<object, number> = new WeakMap(); 4901 let result = type.isWeakMap(value); 4902 console.info("result = " + result); 4903 // 输出结果:result = true 4904 ``` 4905 4906 4907### isWeakSet<sup>8+</sup> 4908 4909isWeakSet(value: Object): boolean 4910 4911检查输入的value是否是WeakSet类型。 4912 4913**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4914 4915**系统能力:** SystemCapability.Utils.Lang 4916 4917**参数:** 4918 4919| 参数名 | 类型 | 必填 | 说明 | 4920| -------- | -------- | -------- | -------- | 4921| value | Object | 是 | 待检测对象。 | 4922 4923**返回值:** 4924 4925| 类型 | 说明 | 4926| -------- | -------- | 4927| boolean | 判断的结果,如果是内置包含的WeakSet类型为true,反之为false。 | 4928 4929**示例:** 4930 4931 ```ts 4932 let type = new util.types(); 4933 let result = type.isWeakSet(new WeakSet()); 4934 console.info("result = " + result); 4935 // 输出结果:result = true 4936 ``` 4937 4938 4939### isBigInt64Array<sup>8+</sup> 4940 4941isBigInt64Array(value: Object): boolean 4942 4943检查输入的value是否是BigInt64Array类型。 4944 4945**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4946 4947**系统能力:** SystemCapability.Utils.Lang 4948 4949**参数:** 4950 4951| 参数名 | 类型 | 必填 | 说明 | 4952| -------- | -------- | -------- | -------- | 4953| value | Object | 是 | 待检测对象。 | 4954 4955**返回值:** 4956 4957| 类型 | 说明 | 4958| -------- | -------- | 4959| boolean | 判断的结果,如果是内置包含的BigInt64Array类型为true,反之为false。 | 4960 4961**示例:** 4962 4963 ```ts 4964 let type = new util.types(); 4965 let result = type.isBigInt64Array(new BigInt64Array([])); 4966 console.info("result = " + result); 4967 // 输出结果:result = true 4968 ``` 4969 4970 4971### isBigUint64Array<sup>8+</sup> 4972 4973isBigUint64Array(value: Object): boolean 4974 4975检查输入的value是否是BigUint64Array类型。 4976 4977**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4978 4979**系统能力:** SystemCapability.Utils.Lang 4980 4981**参数:** 4982 4983| 参数名 | 类型 | 必填 | 说明 | 4984| -------- | -------- | -------- | -------- | 4985| value | Object | 是 | 待检测对象。 | 4986 4987**返回值:** 4988 4989| 类型 | 说明 | 4990| -------- | -------- | 4991| boolean | 判断的结果,如果是内置包含的BigUint64Array类型为true,反之为false。 | 4992 4993**示例:** 4994 4995 ```ts 4996 let type = new util.types(); 4997 let result = type.isBigUint64Array(new BigUint64Array([])); 4998 console.info("result = " + result); 4999 // 输出结果:result = true 5000 ``` 5001 5002 5003### isModuleNamespaceObject<sup>8+</sup> 5004 5005isModuleNamespaceObject(value: Object): boolean 5006 5007检查输入的value是否是Module Namespace Object类型。 5008 5009**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5010 5011**系统能力:** SystemCapability.Utils.Lang 5012 5013**参数:** 5014 5015| 参数名 | 类型 | 必填 | 说明 | 5016| -------- | -------- | -------- | -------- | 5017| value | Object | 是 | 待检测对象。 | 5018 5019**返回值:** 5020 5021| 类型 | 说明 | 5022| -------- | -------- | 5023| boolean | 判断的结果,如果是内置包含的Module Namespace Object类型为true,反之为false。 | 5024 5025**示例:** 5026 5027 ```ts 5028 // /entry/src/main/ets/pages/test.ts 5029 export function func() { 5030 console.info("hello world"); 5031 } 5032 ``` 5033 5034 <!--code_no_check--> 5035 ```ts 5036 import * as nameSpace from './test'; 5037 5038 let type = new util.types(); 5039 let result = type.isModuleNamespaceObject(nameSpace); 5040 console.info("result = " + result); 5041 // 输出结果:result = true 5042 ``` 5043 5044 5045### isSharedArrayBuffer<sup>8+</sup> 5046 5047isSharedArrayBuffer(value: Object): boolean 5048 5049检查输入的value是否是SharedArrayBuffer类型。 5050 5051**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5052 5053**系统能力:** SystemCapability.Utils.Lang 5054 5055**参数:** 5056 5057| 参数名 | 类型 | 必填 | 说明 | 5058| -------- | -------- | -------- | -------- | 5059| value | Object | 是 | 待检测对象。 | 5060 5061**返回值:** 5062 5063| 类型 | 说明 | 5064| -------- | -------- | 5065| boolean | 判断的结果,如果是内置包含的SharedArrayBuffer类型为true,反之为false。 | 5066 5067**示例:** 5068 5069 ```ts 5070 let type = new util.types(); 5071 let result = type.isSharedArrayBuffer(new SharedArrayBuffer(0)); 5072 console.info("result = " + result); 5073 // 输出结果:result = true 5074 ``` 5075 5076## LruBuffer<sup>(deprecated)</sup> 5077 5078> **说明:** 5079> 5080> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache<sup>9+</sup>](#lrucache9)替代。 5081 5082### 属性 5083 5084**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang。 5085 5086| 名称 | 类型 | 可读 | 可写 | 说明 | 5087| -------- | -------- | -------- | -------- | -------- | 5088| length | number | 是 | 否 | 当前缓冲区中值的总数。 | 5089 5090**示例:** 5091 5092 ```ts 5093 let pro : util.LruBuffer<number,number>= new util.LruBuffer(); 5094 pro.put(2,10); 5095 pro.put(1,8); 5096 let result = pro.length; 5097 console.info("result = " + result); 5098 // 输出结果:result = 2 5099 ``` 5100 5101### constructor<sup>(deprecated)</sup> 5102 5103constructor(capacity?: number) 5104 5105默认构造函数用于创建一个新的LruBuffer实例,默认容量为64。 5106 5107> **说明:** 5108> 5109> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.constructor<sup>9+</sup>](#constructor9-3)替代。 5110 5111**系统能力:** SystemCapability.Utils.Lang 5112 5113**参数:** 5114 5115| 参数名 | 类型 | 必填 | 说明 | 5116| -------- | -------- | -------- | -------- | 5117| capacity | number | 否 | 指示要为缓冲区自定义的容量,默认值为64。 | 5118 5119**示例:** 5120 5121 ```ts 5122 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5123 ``` 5124 5125### updateCapacity<sup>(deprecated)</sup> 5126 5127updateCapacity(newCapacity: number): void 5128 5129将缓冲区容量更新为指定容量,如果newCapacity小于或等于0,则抛出异常。 5130 5131> **说明:** 5132> 5133> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.updateCapacity<sup>9+</sup>](#updatecapacity9)替代。 5134 5135**系统能力:** SystemCapability.Utils.Lang 5136 5137**参数:** 5138 5139| 参数名 | 类型 | 必填 | 说明 | 5140| -------- | -------- | -------- | -------- | 5141| newCapacity | number | 是 | 指示要为缓冲区自定义的容量。 | 5142 5143**示例:** 5144 5145 ```ts 5146 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5147 pro.updateCapacity(100); 5148 ``` 5149 5150### toString<sup>(deprecated)</sup> 5151 5152toString(): string 5153 5154返回对象的字符串表示形式。 5155 5156> **说明:** 5157> 5158> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.toString<sup>9+</sup>](#tostring9)替代。 5159 5160**系统能力:** SystemCapability.Utils.Lang 5161 5162**返回值:** 5163 5164| 类型 | 说明 | 5165| -------- | -------- | 5166| string | 返回对象的字符串表示形式。 | 5167 5168**示例:** 5169 5170 ```ts 5171 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5172 pro.put(2,10); 5173 pro.get(2); 5174 pro.remove(20); 5175 let result = pro.toString(); 5176 console.info("result = " + result); 5177 // 输出结果:result = Lrubuffer[ maxSize = 64, hits = 1, misses = 0, hitRate = 100% ] 5178 ``` 5179 5180### getCapacity<sup>(deprecated)</sup> 5181 5182getCapacity(): number 5183 5184获取当前缓冲区的容量。 5185 5186> **说明:** 5187> 5188> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getCapacity<sup>9+</sup>](#getcapacity9)替代。 5189 5190**系统能力:** SystemCapability.Utils.Lang 5191 5192**返回值:** 5193 5194| 类型 | 说明 | 5195| -------- | -------- | 5196| number | 返回当前缓冲区的容量。 | 5197 5198**示例:** 5199 5200 ```ts 5201 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5202 let result = pro.getCapacity(); 5203 console.info("result = " + result); 5204 // 输出结果:result = 64 5205 ``` 5206 5207### clear<sup>(deprecated)</sup> 5208 5209clear(): void 5210 5211从当前缓冲区清除键值对。后续会调用afterRemoval()方法执行后续操作。 5212 5213> **说明:** 5214> 5215> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.clear<sup>9+</sup>](#clear9)替代。 5216 5217**系统能力:** SystemCapability.Utils.Lang 5218 5219**示例:** 5220 5221 ```ts 5222 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5223 pro.put(2,10); 5224 let result = pro.length; 5225 pro.clear(); 5226 ``` 5227 5228### getCreateCount<sup>(deprecated)</sup> 5229 5230getCreateCount(): number 5231 5232获取createDefault()返回值的次数。 5233 5234> **说明:** 5235> 5236> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getCreateCount<sup>9+</sup>](#getcreatecount9)替代。 5237 5238**系统能力:** SystemCapability.Utils.Lang 5239 5240**返回值:** 5241 5242| 类型 | 说明 | 5243| -------- | -------- | 5244| number | 返回createDefault()返回值的次数。 | 5245 5246**示例:** 5247 5248 ```ts 5249 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5250 pro.put(1,8); 5251 let result = pro.getCreateCount(); 5252 console.info("result = " + result); 5253 // 输出结果:result = 0 5254 ``` 5255 5256### getMissCount<sup>(deprecated)</sup> 5257 5258getMissCount(): number 5259 5260获取查询值不匹配的次数。 5261 5262> **说明:** 5263> 5264> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getMissCount<sup>9+</sup>](#getmisscount9)替代。 5265 5266**系统能力:** SystemCapability.Utils.Lang 5267 5268**返回值:** 5269 5270| 类型 | 说明 | 5271| -------- | -------- | 5272| number | 返回查询值不匹配的次数。 | 5273 5274**示例:** 5275 5276 ```ts 5277 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5278 pro.put(2,10); 5279 pro.get(2); 5280 let result = pro.getMissCount(); 5281 console.info("result = " + result); 5282 // 输出结果:result = 0 5283 ``` 5284 5285### getRemovalCount<sup>(deprecated)</sup> 5286 5287getRemovalCount(): number 5288 5289获取从缓冲区中逐出值的次数。 5290 5291> **说明:** 5292> 5293> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getRemovalCount<sup>9+</sup>](#getremovalcount9)替代。 5294 5295**系统能力:** SystemCapability.Utils.Lang 5296 5297**返回值:** 5298 5299| 类型 | 说明 | 5300| -------- | -------- | 5301| number | 返回从缓冲区中驱逐的次数。 | 5302 5303**示例:** 5304 5305 ```ts 5306 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5307 pro.put(2,10); 5308 pro.updateCapacity(2); 5309 pro.put(50,22); 5310 let result = pro.getRemovalCount(); 5311 console.info("result = " + result); 5312 // 输出结果:result = 0 5313 ``` 5314 5315### getMatchCount<sup>(deprecated)</sup> 5316 5317getMatchCount(): number 5318 5319获取查询值匹配成功的次数。 5320 5321> **说明:** 5322> 5323> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getMatchCount<sup>9+</sup>](#getmatchcount9)替代。 5324 5325**系统能力:** SystemCapability.Utils.Lang 5326 5327**返回值:** 5328 5329| 类型 | 说明 | 5330| -------- | -------- | 5331| number | 返回查询值匹配成功的次数。 | 5332 5333**示例:** 5334 5335 ```ts 5336 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5337 pro.put(2,10); 5338 pro.get(2); 5339 let result = pro.getMatchCount(); 5340 console.info("result = " + result); 5341 // 输出结果:result = 1 5342 ``` 5343 5344### getPutCount<sup>(deprecated)</sup> 5345 5346getPutCount(): number 5347 5348获取将值添加到缓冲区的次数。 5349 5350> **说明:** 5351> 5352> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getPutCount<sup>9+</sup>](#getputcount9)替代。 5353 5354**系统能力:** SystemCapability.Utils.Lang 5355 5356**返回值:** 5357 5358| 类型 | 说明 | 5359| -------- | -------- | 5360| number | 返回将值添加到缓冲区的次数。 | 5361 5362**示例:** 5363 5364 ```ts 5365 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5366 pro.put(2,10); 5367 let result = pro.getPutCount(); 5368 console.info("result = " + result); 5369 // 输出结果:result = 1 5370 ``` 5371 5372### isEmpty<sup>(deprecated)</sup> 5373 5374isEmpty(): boolean 5375 5376检查当前缓冲区是否为空。 5377 5378> **说明:** 5379> 5380> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.isEmpty<sup>9+</sup>](#isempty9)替代。 5381 5382**系统能力:** SystemCapability.Utils.Lang 5383 5384**返回值:** 5385 5386| 类型 | 说明 | 5387| -------- | -------- | 5388| boolean | 如果当前缓冲区不包含任何值,则返回true。 | 5389 5390**示例:** 5391 5392 ```ts 5393 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5394 pro.put(2,10); 5395 let result = pro.isEmpty(); 5396 console.info("result = " + result); 5397 // 输出结果:result = false 5398 ``` 5399 5400### get<sup>(deprecated)</sup> 5401 5402get(key: K): V | undefined 5403 5404表示要查询的键。 5405 5406> **说明:** 5407> 5408> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.get<sup>9+</sup>](#get9)替代。 5409 5410**系统能力:** SystemCapability.Utils.Lang 5411 5412**参数:** 5413 5414| 参数名 | 类型 | 必填 | 说明 | 5415| -------- | -------- | -------- | -------- | 5416| key | K | 是 | 要查询的键。 | 5417 5418**返回值:** 5419 5420| 类型 | 说明 | 5421| -------- | -------- | 5422| V \| undefined | 如果指定的键存在于缓冲区中,则返回与键关联的值;否则返回undefined。 | 5423 5424**示例:** 5425 5426 ```ts 5427 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5428 pro.put(2,10); 5429 let result = pro.get(2); 5430 console.info("result = " + result); 5431 // 输出结果:result = 10 5432 ``` 5433 5434### put<sup>(deprecated)</sup> 5435 5436put(key: K,value: V): V 5437 5438将键值对添加到缓冲区。 5439 5440> **说明:** 5441> 5442> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.put<sup>9+</sup>](#put9)替代。 5443 5444**系统能力:** SystemCapability.Utils.Lang 5445 5446**参数:** 5447 5448| 参数名 | 类型 | 必填 | 说明 | 5449| -------- | -------- | -------- | -------- | 5450| key | K | 是 | 要添加的密钥。 | 5451| value | V | 是 | 指示与要添加的键关联的值。 | 5452 5453**返回值:** 5454 5455| 类型 | 说明 | 5456| -------- | -------- | 5457| V | 返回与添加的键关联的值;如果要添加的键已经存在,则返回原始值,如果键或值为空,则抛出此异常。 | 5458 5459**示例:** 5460 5461 ```ts 5462 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5463 let result = pro.put(2,10); 5464 console.info("result = " + result); 5465 // 输出结果:result = 10 5466 ``` 5467 5468### values<sup>(deprecated)</sup> 5469 5470values(): V[] 5471 5472获取当前缓冲区中所有值从最近访问到最近最少访问的顺序列表。 5473 5474> **说明:** 5475> 5476> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.values<sup>9+</sup>](#values9)替代。 5477 5478**系统能力:** SystemCapability.Utils.Lang 5479 5480**返回值:** 5481 5482| 类型 | 说明 | 5483| -------- | -------- | 5484| V [] | 按从最近访问到最近最少访问的顺序返回当前缓冲区中所有值的列表。 | 5485 5486**示例:** 5487 5488 ```ts 5489 let pro : util.LruBuffer<number|string,number|string> = new util.LruBuffer(); 5490 pro.put(2,10); 5491 pro.put(2,"anhu"); 5492 pro.put("afaf","grfb"); 5493 let result = pro.values(); 5494 console.info("result = " + result); 5495 // 输出结果:result = anhu,grfb 5496 ``` 5497 5498### keys<sup>(deprecated)</sup> 5499 5500keys(): K[] 5501 5502获取当前缓冲区中所有键从最近访问到最近最少访问的升序列表。 5503 5504> **说明:** 5505> 5506> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.keys<sup>9+</sup>](#keys9)替代。 5507 5508**系统能力:** SystemCapability.Utils.Lang 5509 5510**返回值:** 5511 5512| 类型 | 说明 | 5513| -------- | -------- | 5514| K [] | 按升序返回当前缓冲区中所有键的列表,从最近访问到最近最少访问。 | 5515 5516**示例:** 5517 5518 ```ts 5519 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5520 pro.put(2,10); 5521 let result = pro.keys(); 5522 console.info("result = " + result); 5523 // 输出结果:result = 2 5524 ``` 5525 5526### remove<sup>(deprecated)</sup> 5527 5528remove(key: K): V | undefined 5529 5530从当前缓冲区中删除指定的键及其关联的值。 5531 5532> **说明:** 5533> 5534> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.remove<sup>9+</sup>](#remove9)替代。 5535 5536**系统能力:** SystemCapability.Utils.Lang 5537 5538**参数:** 5539 5540| 参数名 | 类型 | 必填 | 说明 | 5541| -------- | -------- | -------- | -------- | 5542| key | K | 是 | 要删除的密钥。 | 5543 5544**返回值:** 5545 5546| 类型 | 说明 | 5547| -------- | -------- | 5548| V \| undefined | 返回一个包含已删除键值对的Optional对象;如果key不存在,则返回一个空的Optional对象,如果key为null,则抛出异常。 | 5549 5550**示例:** 5551 5552 ```ts 5553 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5554 pro.put(2,10); 5555 let result = pro.remove(20); 5556 console.info("result = " + result); 5557 // 输出结果:result = undefined 5558 ``` 5559 5560### afterRemoval<sup>(deprecated)</sup> 5561 5562afterRemoval(isEvict: boolean,key: K,value: V,newValue: V): void 5563 5564删除值后执行后续操作。 5565 5566> **说明:** 5567> 5568> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.afterRemoval<sup>9+</sup>](#afterremoval9)替代。 5569 5570**系统能力:** SystemCapability.Utils.Lang 5571 5572**参数:** 5573 5574| 参数名 | 类型 | 必填 | 说明 | 5575| -------- | -------- | -------- | -------- | 5576| isEvict | boolean | 是 | 因容量不足而调用该方法时,参数值为true,其他情况为false。 | 5577| key | K | 是 | 表示删除的键。 | 5578| value | V | 是 | 表示删除的值。 | 5579| newValue | V | 是 | 如果已调用put方法并且要添加的键已经存在,则参数值是关联的新值。其他情况下参数值为空。 | 5580 5581**示例:** 5582 5583```ts 5584class ChildLruBuffer<K, V> extends util.LruBuffer<K, V> { 5585 constructor(capacity?: number) { 5586 super(capacity); 5587 } 5588 5589 afterRemoval(isEvict: boolean, key: K, value: V, newValue: V): void { 5590 if (isEvict === true) { 5591 console.info('key: ' + key); 5592 // 输出结果:key: 11 5593 console.info('value: ' + value); 5594 // 输出结果:value: 1 5595 console.info('newValue: ' + newValue); 5596 // 输出结果:newValue: null 5597 } 5598 } 5599} 5600let lru: ChildLruBuffer<number, number> = new ChildLruBuffer(2); 5601lru.put(11, 1); 5602lru.put(22, 2); 5603lru.put(33, 3); 5604``` 5605 5606### contains<sup>(deprecated)</sup> 5607 5608contains(key: K): boolean 5609 5610检查当前缓冲区是否包含指定的键。 5611 5612 5613> **说明:** 5614> 5615> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.contains<sup>9+</sup>](#contains9)替代。 5616 5617**系统能力:** SystemCapability.Utils.Lang 5618 5619**参数:** 5620 5621| 参数名 | 类型 | 必填 | 说明 | 5622| -------- | -------- | -------- | -------- | 5623| key | K | 是 | 表示要检查的键。 | 5624 5625**返回值:** 5626 5627| 类型 | 说明 | 5628| -------- | -------- | 5629| boolean | 如果缓冲区包含指定的键,则返回 true。 | 5630 5631**示例:** 5632 5633 ```ts 5634 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5635 pro.put(2,10); 5636 let result = pro.contains(20); 5637 console.info('result = ' + result); 5638 // 输出结果:result = false 5639 ``` 5640 5641### createDefault<sup>(deprecated)</sup> 5642 5643createDefault(key: K): V 5644 5645如果未计算特定键的值,则执行后续操作,参数表示丢失的键,返回与键关联的值。 5646 5647> **说明:** 5648> 5649> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.createDefault<sup>9+</sup>](#createdefault9)替代。 5650 5651**系统能力:** SystemCapability.Utils.Lang 5652 5653**参数:** 5654 5655| 参数名 | 类型 | 必填 | 说明 | 5656| -------- | -------- | -------- | -------- | 5657| key | K | 是 | 表示丢失的键。 | 5658 5659**返回值:** 5660 5661| 类型 | 说明 | 5662| -------- | -------- | 5663| V | 返回与键关联的值。 | 5664 5665**示例:** 5666 5667 ```ts 5668 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5669 let result = pro.createDefault(50); 5670 ``` 5671 5672### entries<sup>(deprecated)</sup> 5673 5674entries(): IterableIterator<[K,V]> 5675 5676允许迭代包含在这个对象中的所有键值对。 5677 5678> **说明:** 5679> 5680> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.entries<sup>9+</sup>](#entries9)替代。 5681 5682**系统能力:** SystemCapability.Utils.Lang 5683 5684**返回值:** 5685 5686| 类型 | 说明 | 5687| -------- | -------- | 5688| [K, V] | 返回一个可迭代数组。 | 5689 5690**示例:** 5691 5692 ```ts 5693 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5694 pro.put(2,10); 5695 let result = pro.entries(); 5696 ``` 5697 5698### [Symbol.iterator]<sup>(deprecated)</sup> 5699 5700[Symbol.iterator]\(): IterableIterator<[K, V]> 5701 5702返回一个键值对形式的二维数组。 5703 5704> **说明:** 5705> 5706> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.Symbol.iterator<sup>9+</sup>](#symboliterator9)替代。 5707 5708**系统能力:** SystemCapability.Utils.Lang 5709 5710**返回值:** 5711 5712| 类型 | 说明 | 5713| -------- | -------- | 5714| [K, V] | 返回一个键值对形式的二维数组。 | 5715 5716**示例:** 5717 5718 ```ts 5719 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5720 pro.put(2,10); 5721 let result = pro[Symbol.iterator](); 5722 ``` 5723 5724## Scope<sup>(deprecated)</sup> 5725 5726> **说明:** 5727> 5728> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper<sup>9+</sup>](#scopehelper9)替代。 5729 5730### constructor<sup>(deprecated)</sup> 5731 5732constructor(lowerObj: ScopeType, upperObj: ScopeType) 5733 5734用于创建指定下限和上限的作用域实例的构造函数,返回一个Scope对象。 5735 5736> **说明:** 5737> 5738> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.constructor<sup>9+</sup>](#constructor9-4)替代。 5739 5740 5741**系统能力:** SystemCapability.Utils.Lang 5742 5743**参数:** 5744 5745| 参数名 | 类型 | 必填 | 说明 | 5746| -------- | -------- | -------- | -------- | 5747| lowerObj | [ScopeType](#scopetype8) | 是 | 指定作用域实例的下限。 | 5748| upperObj | [ScopeType](#scopetype8) | 是 | 指定作用域实例的上限。 | 5749 5750**示例:** 5751 ```ts 5752 class Temperature{ 5753 private readonly _temp: number; 5754 constructor(value : number) { 5755 this._temp = value; 5756 } 5757 compareTo(value : Temperature ) { 5758 return this._temp >= value.getTemp(); 5759 } 5760 getTemp() { 5761 return this._temp; 5762 } 5763 toString() : string { 5764 return this._temp.toString(); 5765 } 5766 } 5767 let tempLower = new Temperature(30); 5768 let tempUpper = new Temperature(40); 5769 let range = new util.Scope(tempLower, tempUpper); 5770 console.info("range = " + range); 5771 // 输出结果:range = [30, 40] 5772 ``` 5773 5774### toString<sup>(deprecated)</sup> 5775 5776toString(): string 5777 5778该字符串化方法返回一个包含当前范围的字符串表示形式。 5779 5780> **说明:** 5781> 5782> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.toString<sup>9+</sup>](#tostring9-1)替代。 5783 5784**系统能力:** SystemCapability.Utils.Lang 5785 5786**返回值:** 5787 5788| 类型 | 说明 | 5789| -------- | -------- | 5790| string | 返回包含当前范围对象的字符串表示形式。 | 5791 5792**示例:** 5793 5794 ```ts 5795 class Temperature{ 5796 private readonly _temp: number; 5797 constructor(value : number) { 5798 this._temp = value; 5799 } 5800 compareTo(value : Temperature ) { 5801 return this._temp >= value.getTemp(); 5802 } 5803 getTemp() { 5804 return this._temp; 5805 } 5806 toString() : string { 5807 return this._temp.toString(); 5808 } 5809 } 5810 5811 let tempLower = new Temperature(30); 5812 let tempUpper = new Temperature(40); 5813 let range = new util.Scope(tempLower, tempUpper); 5814 let result = range.toString(); 5815 console.info("result = " + result); 5816 // 输出结果:result = [30, 40] 5817 ``` 5818 5819### intersect<sup>(deprecated)</sup> 5820 5821intersect(range: Scope): Scope 5822 5823获取给定范围和当前范围的交集。 5824 5825> **说明:** 5826> 5827> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.intersect<sup>9+</sup>](#intersect9)替代。 5828 5829**系统能力:** SystemCapability.Utils.Lang 5830 5831**参数:** 5832 5833| 参数名 | 类型 | 必填 | 说明 | 5834| -------- | -------- | -------- | -------- | 5835| range | [Scope](#scopedeprecated) | 是 | 传入一个给定范围。 | 5836 5837**返回值:** 5838 5839| 类型 | 说明 | 5840| -------- | -------- | 5841| [Scope](#scopedeprecated) | 返回给定范围和当前范围的交集。 | 5842 5843**示例:** 5844 5845 ```ts 5846 class Temperature{ 5847 private readonly _temp: number; 5848 constructor(value : number) { 5849 this._temp = value; 5850 } 5851 compareTo(value : Temperature ) { 5852 return this._temp >= value.getTemp(); 5853 } 5854 getTemp() { 5855 return this._temp; 5856 } 5857 toString() : string { 5858 return this._temp.toString(); 5859 } 5860 } 5861 5862 let tempLower = new Temperature(30); 5863 let tempUpper = new Temperature(40); 5864 let range = new util.Scope(tempLower, tempUpper); 5865 let tempMiDF = new Temperature(35); 5866 let tempMidS = new Temperature(39); 5867 let rangeFir = new util.Scope(tempMiDF, tempMidS); 5868 let result = range.intersect(rangeFir ); 5869 console.info("result = " + result); 5870 // 输出结果:result = [35, 39] 5871 ``` 5872 5873### intersect<sup>(deprecated)</sup> 5874 5875intersect(lowerObj:ScopeType,upperObj:ScopeType):Scope 5876 5877获取当前范围与给定下限和上限范围的交集。 5878 5879> **说明:** 5880> 5881> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.intersect<sup>9+</sup>](#intersect9-1)替代。 5882 5883**系统能力:** SystemCapability.Utils.Lang 5884 5885**参数:** 5886 5887| 参数名 | 类型 | 必填 | 说明 | 5888| -------- | -------- | -------- | -------- | 5889| lowerObj | [ScopeType](#scopetype8) | 是 | 给定范围的下限。 | 5890| upperObj | [ScopeType](#scopetype8) | 是 | 给定范围的上限。 | 5891 5892**返回值:** 5893 5894| 类型 | 说明 | 5895| -------- | -------- | 5896| [Scope](#scopedeprecated) | 返回当前范围与给定下限和上限范围的交集。 | 5897 5898**示例:** 5899 5900 ```ts 5901 class Temperature{ 5902 private readonly _temp: number; 5903 constructor(value : number) { 5904 this._temp = value; 5905 } 5906 compareTo(value : Temperature ) { 5907 return this._temp >= value.getTemp(); 5908 } 5909 getTemp() { 5910 return this._temp; 5911 } 5912 toString() : string { 5913 return this._temp.toString(); 5914 } 5915 } 5916 5917 let tempLower = new Temperature(30); 5918 let tempUpper = new Temperature(40); 5919 let tempMiDF = new Temperature(35); 5920 let tempMidS = new Temperature(39); 5921 let range = new util.Scope(tempLower, tempUpper); 5922 let result = range.intersect(tempMiDF, tempMidS); 5923 console.info("result = " + result); 5924 // 输出结果:result = [35, 39] 5925 ``` 5926 5927### getUpper<sup>(deprecated)</sup> 5928 5929getUpper(): ScopeType 5930 5931获取当前范围的上限。 5932 5933> **说明:** 5934> 5935> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.getUpper<sup>9+</sup>](#getupper9)替代。 5936 5937**系统能力:** SystemCapability.Utils.Lang 5938 5939**返回值:** 5940 5941| 类型 | 说明 | 5942| -------- | -------- | 5943| [ScopeType](#scopetype8) | 返回当前范围的上限值。 | 5944 5945**示例:** 5946 5947 ```ts 5948 class Temperature{ 5949 private readonly _temp: number; 5950 constructor(value : number) { 5951 this._temp = value; 5952 } 5953 compareTo(value : Temperature ) { 5954 return this._temp >= value.getTemp(); 5955 } 5956 getTemp() { 5957 return this._temp; 5958 } 5959 toString() : string { 5960 return this._temp.toString(); 5961 } 5962 } 5963 5964 let tempLower = new Temperature(30); 5965 let tempUpper = new Temperature(40); 5966 let range = new util.Scope(tempLower, tempUpper); 5967 let result = range.getUpper(); 5968 console.info("result = " + result); 5969 // 输出结果:result = 40 5970 ``` 5971 5972### getLower<sup>(deprecated)</sup> 5973 5974getLower(): ScopeType 5975 5976获取当前范围的下限。 5977 5978> **说明:** 5979> 5980> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.getLower<sup>9+</sup>](#getlower9)替代。 5981 5982**系统能力:** SystemCapability.Utils.Lang 5983 5984**返回值:** 5985 5986| 类型 | 说明 | 5987| -------- | -------- | 5988| [ScopeType](#scopetype8) | 返回当前范围的下限值。 | 5989 5990**示例:** 5991 5992 ```ts 5993 class Temperature{ 5994 private readonly _temp: number; 5995 constructor(value : number) { 5996 this._temp = value; 5997 } 5998 compareTo(value : Temperature ) { 5999 return this._temp >= value.getTemp(); 6000 } 6001 getTemp() { 6002 return this._temp; 6003 } 6004 toString() : string { 6005 return this._temp.toString(); 6006 } 6007 } 6008 6009 let tempLower = new Temperature(30); 6010 let tempUpper = new Temperature(40); 6011 let range = new util.Scope(tempLower, tempUpper); 6012 let result = range.getLower(); 6013 console.info("result = " + result); 6014 // 输出结果:result = 30 6015 ``` 6016 6017### expand<sup>(deprecated)</sup> 6018 6019expand(lowerObj: ScopeType,upperObj: ScopeType): Scope 6020 6021创建并返回包括当前范围和给定下限和上限的并集。 6022 6023> **说明:** 6024> 6025> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.expand<sup>9+</sup>](#expand9)替代。 6026 6027**系统能力:** SystemCapability.Utils.Lang 6028 6029**参数:** 6030 6031| 参数名 | 类型 | 必填 | 说明 | 6032| -------- | -------- | -------- | -------- | 6033| lowerObj | [ScopeType](#scopetype8) | 是 | 给定范围的下限。 | 6034| upperObj | [ScopeType](#scopetype8) | 是 | 给定范围的上限。 | 6035 6036**返回值:** 6037 6038| 类型 | 说明 | 6039| -------- | -------- | 6040| [Scope](#scopedeprecated) | 返回当前范围和给定下限和上限的并集。 | 6041 6042**示例:** 6043 6044 ```ts 6045 class Temperature{ 6046 private readonly _temp: number; 6047 constructor(value : number) { 6048 this._temp = value; 6049 } 6050 compareTo(value : Temperature ) { 6051 return this._temp >= value.getTemp(); 6052 } 6053 getTemp() { 6054 return this._temp; 6055 } 6056 toString() : string { 6057 return this._temp.toString(); 6058 } 6059 } 6060 6061 let tempLower = new Temperature(30); 6062 let tempUpper = new Temperature(40); 6063 let tempMiDF = new Temperature(35); 6064 let tempMidS = new Temperature(39); 6065 let range = new util.Scope(tempLower, tempUpper); 6066 let result = range.expand(tempMiDF, tempMidS); 6067 console.info("result = " + result); 6068 // 输出结果:result = [30, 40] 6069 ``` 6070 6071### expand<sup>(deprecated)</sup> 6072 6073expand(range: Scope): Scope 6074 6075创建并返回包括当前范围和给定范围的并集。 6076 6077> **说明:** 6078> 6079> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.expand<sup>9+</sup>](#expand9-1)替代。 6080 6081**系统能力:** SystemCapability.Utils.Lang 6082 6083**参数:** 6084 6085| 参数名 | 类型 | 必填 | 说明 | 6086| -------- | -------- | -------- | -------- | 6087| range | [Scope](#scopedeprecated) | 是 | 传入一个给定范围。 | 6088 6089**返回值:** 6090 6091| 类型 | 说明 | 6092| -------- | -------- | 6093| [Scope](#scopedeprecated) | 返回包括当前范围和给定范围的并集。 | 6094 6095**示例:** 6096 6097 ```ts 6098 class Temperature{ 6099 private readonly _temp: number; 6100 constructor(value : number) { 6101 this._temp = value; 6102 } 6103 compareTo(value : Temperature ) { 6104 return this._temp >= value.getTemp(); 6105 } 6106 getTemp() { 6107 return this._temp; 6108 } 6109 toString() : string { 6110 return this._temp.toString(); 6111 } 6112 } 6113 6114 let tempLower = new Temperature(30); 6115 let tempUpper = new Temperature(40); 6116 let tempMiDF = new Temperature(35); 6117 let tempMidS = new Temperature(39); 6118 let range = new util.Scope(tempLower, tempUpper); 6119 let rangeFir = new util.Scope(tempMiDF, tempMidS); 6120 let result = range.expand(rangeFir); 6121 console.info("result = " + result); 6122 // 输出结果:result = [30, 40] 6123 ``` 6124 6125### expand<sup>(deprecated)</sup> 6126 6127expand(value: ScopeType): Scope 6128 6129创建并返回包括当前范围和给定值的并集。 6130 6131> **说明:** 6132> 6133> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.expand<sup>9+</sup>](#expand9-2)替代。 6134 6135**系统能力:** SystemCapability.Utils.Lang 6136 6137**参数:** 6138 6139| 参数名 | 类型 | 必填 | 说明 | 6140| -------- | -------- | -------- | -------- | 6141| value | [ScopeType](#scopetype8) | 是 | 传入一个给定值。 | 6142 6143**返回值:** 6144 6145| 类型 | 说明 | 6146| -------- | -------- | 6147| [Scope](#scopedeprecated) | 返回包括当前范围和给定值的并集。 | 6148 6149**示例:** 6150 6151 ```ts 6152 class Temperature{ 6153 private readonly _temp: number; 6154 constructor(value : number) { 6155 this._temp = value; 6156 } 6157 compareTo(value : Temperature ) { 6158 return this._temp >= value.getTemp(); 6159 } 6160 getTemp() { 6161 return this._temp; 6162 } 6163 toString() : string { 6164 return this._temp.toString(); 6165 } 6166 } 6167 6168 let tempLower = new Temperature(30); 6169 let tempUpper = new Temperature(40); 6170 let tempMiDF = new Temperature(35); 6171 let range = new util.Scope(tempLower, tempUpper); 6172 let result = range.expand(tempMiDF); 6173 console.info("result = " + result); 6174 // 输出结果:result = [30, 40] 6175 ``` 6176 6177### contains<sup>(deprecated)</sup> 6178 6179contains(value: ScopeType): boolean 6180 6181检查给定value是否包含在当前范围内。 6182 6183> **说明:** 6184> 6185> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.contains<sup>9+</sup>](#contains9-1)替代。 6186 6187**系统能力:** SystemCapability.Utils.Lang 6188 6189**参数:** 6190 6191| 参数名 | 类型 | 必填 | 说明 | 6192| -------- | -------- | -------- | -------- | 6193| value | [ScopeType](#scopetype8) | 是 | 传入一个给定值。 | 6194 6195**返回值:** 6196 6197| 类型 | 说明 | 6198| -------- | -------- | 6199| boolean | 如果给定值包含在当前范围内返回true,否则返回false。 | 6200 6201**示例:** 6202 6203 ```ts 6204 class Temperature{ 6205 private readonly _temp: number; 6206 constructor(value : number) { 6207 this._temp = value; 6208 } 6209 compareTo(value : Temperature ) { 6210 return this._temp >= value.getTemp(); 6211 } 6212 getTemp() { 6213 return this._temp; 6214 } 6215 toString() : string { 6216 return this._temp.toString(); 6217 } 6218 } 6219 6220 let tempLower = new Temperature(30); 6221 let tempUpper = new Temperature(40); 6222 let tempMiDF = new Temperature(35); 6223 let range = new util.Scope(tempLower, tempUpper); 6224 let result = range.contains(tempMiDF); 6225 console.info("result = " + result); 6226 // 输出结果:result = true 6227 ``` 6228 6229### contains<sup>(deprecated)</sup> 6230 6231contains(range: Scope): boolean 6232 6233检查给定range是否在当前范围内。 6234 6235> **说明:** 6236> 6237> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.contains<sup>9+</sup>](#contains9-2)替代。 6238 6239**系统能力:** SystemCapability.Utils.Lang 6240 6241**参数:** 6242 6243| 参数名 | 类型 | 必填 | 说明 | 6244| -------- | -------- | -------- | -------- | 6245| range | [Scope](#scopedeprecated) | 是 | 传入一个给定范围。 | 6246 6247**返回值:** 6248 6249| 类型 | 说明 | 6250| -------- | -------- | 6251| boolean | 如果给定范围包含在当前范围内返回true,否则返回false。 | 6252 6253**示例:** 6254 6255 ```ts 6256 class Temperature{ 6257 private readonly _temp: number; 6258 constructor(value : number) { 6259 this._temp = value; 6260 } 6261 compareTo(value : Temperature ) { 6262 return this._temp >= value.getTemp(); 6263 } 6264 getTemp() { 6265 return this._temp; 6266 } 6267 toString() : string { 6268 return this._temp.toString(); 6269 } 6270 } 6271 6272 let tempLower = new Temperature(30); 6273 let tempUpper = new Temperature(40); 6274 let range = new util.Scope(tempLower, tempUpper); 6275 let tempLess = new Temperature(20); 6276 let tempMore = new Temperature(45); 6277 let rangeSec = new util.Scope(tempLess, tempMore); 6278 let result = range.contains(rangeSec); 6279 console.info("result = " + result); 6280 // 输出结果:result = false 6281 ``` 6282 6283### clamp<sup>(deprecated)</sup> 6284 6285 6286clamp(value: ScopeType): ScopeType 6287 6288将给定值限定到当前范围内。 6289 6290> **说明:** 6291> 6292> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.clamp<sup>9+</sup>](#clamp9)替代。 6293 6294**系统能力:** SystemCapability.Utils.Lang 6295 6296**参数:** 6297 6298| 参数名 | 类型 | 必填 | 说明 | 6299| -------- | -------- | -------- | -------- | 6300| value | [ScopeType](#scopetype8) | 是 | 传入的给定值。 | 6301 6302**返回值:** 6303 6304| 类型 | 说明 | 6305| -------- | -------- | 6306| [ScopeType](#scopetype8) | 如果传入的value小于下限,则返回lowerObj;如果大于上限值则返回upperObj;如果在当前范围内,则返回value。 | 6307 6308**示例:** 6309 6310 ```ts 6311 class Temperature{ 6312 private readonly _temp: number; 6313 constructor(value : number) { 6314 this._temp = value; 6315 } 6316 compareTo(value : Temperature ) { 6317 return this._temp >= value.getTemp(); 6318 } 6319 getTemp() { 6320 return this._temp; 6321 } 6322 toString() : string { 6323 return this._temp.toString(); 6324 } 6325 } 6326 6327 let tempLower = new Temperature(30); 6328 let tempUpper = new Temperature(40); 6329 let tempMiDF = new Temperature(35); 6330 let range = new util.Scope(tempLower, tempUpper); 6331 let result = range.clamp(tempMiDF); 6332 console.info("result = " + result); 6333 // 输出结果:result = 35 6334 ``` 6335 6336 6337## Base64<sup>(deprecated)</sup> 6338 6339> **说明:** 6340> 6341> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper<sup>9+</sup>](#base64helper9)替代。 6342 6343### constructor<sup>(deprecated)</sup> 6344 6345constructor() 6346 6347Base64的构造函数。 6348 6349> **说明:** 6350> 6351> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.constructor<sup>9+</sup>](#constructor9-5)替代。 6352 6353**系统能力:** SystemCapability.Utils.Lang 6354 6355**示例:** 6356 6357 ```ts 6358 let base64 = new util.Base64(); 6359 ``` 6360 6361### encodeSync<sup>(deprecated)</sup> 6362 6363encodeSync(src: Uint8Array): Uint8Array 6364 6365通过输入参数编码后输出对应文本。 6366 6367> **说明:** 6368> 6369> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.encodeSync<sup>9+</sup>](#encodesync9)替代。 6370 6371**系统能力:** SystemCapability.Utils.Lang 6372 6373**参数:** 6374 6375| 参数名 | 类型 | 必填 | 说明 | 6376| -------- | -------- | -------- | -------- | 6377| src | Uint8Array | 是 | 编码输入Uint8数组。 | 6378 6379**返回值:** 6380 6381| 类型 | 说明 | 6382| -------- | -------- | 6383| Uint8Array | 返回编码后新分配的Uint8数组。 | 6384 6385**示例:** 6386 6387 ```ts 6388 let base64 = new util.Base64(); 6389 let array = new Uint8Array([115,49,51]); 6390 let result = base64.encodeSync(array); 6391 console.info("result = " + result); 6392 // 输出结果:result = 99,122,69,122 6393 ``` 6394 6395### encodeToStringSync<sup>(deprecated)</sup> 6396 6397encodeToStringSync(src: Uint8Array): string 6398 6399通过输入参数编码后输出对应文本。 6400 6401> **说明:** 6402> 6403> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.encodeToStringSync<sup>9+</sup>](#encodetostringsync9)替代。 6404 6405**系统能力:** SystemCapability.Utils.Lang 6406 6407**参数:** 6408 6409| 参数名 | 类型 | 必填 | 说明 | 6410| -------- | -------- | -------- | -------- | 6411| src | Uint8Array | 是 | 编码输入Uint8数组。 | 6412 6413**返回值:** 6414 6415| 类型 | 说明 | 6416| -------- | -------- | 6417| string | 返回编码后的字符串。 | 6418 6419**示例:** 6420 6421 ```ts 6422 let base64 = new util.Base64(); 6423 let array = new Uint8Array([115,49,51]); 6424 let result = base64.encodeToStringSync(array); 6425 console.info("result = " + result); 6426 // 输出结果:result = czEz 6427 ``` 6428 6429### decodeSync<sup>(deprecated)</sup> 6430 6431decodeSync(src: Uint8Array | string): Uint8Array 6432 6433通过输入参数解码后输出对应文本。 6434 6435> **说明:** 6436> 6437> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.decodeSync<sup>9+</sup>](#decodesync9)替代。 6438 6439**系统能力:** SystemCapability.Utils.Lang 6440 6441**参数:** 6442 6443| 参数名 | 类型 | 必填 | 说明 | 6444| -------- | -------- | -------- | -------- | 6445| src | Uint8Array \| string | 是 | 解码输入Uint8数组或者字符串。 | 6446 6447**返回值:** 6448 6449| 类型 | 说明 | 6450| -------- | -------- | 6451| Uint8Array | 返回解码后新分配的Uint8数组。 | 6452 6453**示例:** 6454 6455 ```ts 6456 let base64 = new util.Base64(); 6457 let buff = 'czEz'; 6458 let result = base64.decodeSync(buff); 6459 console.info("result = " + result); 6460 // 输出结果:result = 115,49,51 6461 ``` 6462 6463### encode<sup>(deprecated)</sup> 6464 6465encode(src: Uint8Array): Promise<Uint8Array> 6466 6467通过输入参数异步编码后输出对应文本。 6468 6469> **说明:** 6470> 6471> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.encode<sup>9+</sup>](#encode9)替代。 6472 6473**系统能力:** SystemCapability.Utils.Lang 6474 6475**参数:** 6476 6477| 参数名 | 类型 | 必填 | 说明 | 6478| -------- | -------- | -------- | -------- | 6479| src | Uint8Array | 是 | 异步编码输入Uint8数组。 | 6480 6481**返回值:** 6482 6483| 类型 | 说明 | 6484| -------- | -------- | 6485| Promise<Uint8Array> | 返回异步编码后新分配的Uint8数组。 | 6486 6487**示例:** 6488 6489 ```ts 6490 let base64 = new util.Base64(); 6491 let array = new Uint8Array([115,49,51]); 6492 base64.encode(array).then((val) => { 6493 console.info(val.toString()); 6494 // 输出结果:99,122,69,122 6495 }) 6496 ``` 6497 6498### encodeToString<sup>(deprecated)</sup> 6499 6500encodeToString(src: Uint8Array): Promise<string> 6501 6502通过输入参数异步编码后输出对应文本。 6503 6504> **说明:** 6505> 6506> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.encodeToString<sup>9+</sup>](#encodetostring9)替代。 6507 6508**系统能力:** SystemCapability.Utils.Lang 6509 6510**参数:** 6511 6512| 参数名 | 类型 | 必填 | 说明 | 6513| -------- | -------- | -------- | -------- | 6514| src | Uint8Array | 是 | 异步编码输入Uint8数组。 | 6515 6516**返回值:** 6517 6518| 类型 | 说明 | 6519| -------- | -------- | 6520| Promise<string> | 返回异步编码后的字符串。 | 6521 6522**示例:** 6523 6524 ```ts 6525 let base64 = new util.Base64(); 6526 let array = new Uint8Array([115,49,51]); 6527 base64.encodeToString(array).then((val) => { 6528 console.info(val); 6529 // 输出结果:czEz 6530 }) 6531 ``` 6532 6533### decode<sup>(deprecated)</sup> 6534 6535 6536decode(src: Uint8Array | string): Promise<Uint8Array> 6537 6538通过输入参数异步解码后输出对应文本。 6539 6540> **说明:** 6541> 6542> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.decode<sup>9+</sup>](#decode9)替代。 6543 6544**系统能力:** SystemCapability.Utils.Lang 6545 6546**参数:** 6547 6548| 参数名 | 类型 | 必填 | 说明 | 6549| -------- | -------- | -------- | -------- | 6550| src | Uint8Array \| string | 是 | 异步解码输入Uint8数组或者字符串。 | 6551 6552**返回值:** 6553 6554| 类型 | 说明 | 6555| -------- | -------- | 6556| Promise<Uint8Array> | 返回异步解码后新分配的Uint8数组。 | 6557 6558**示例:** 6559 6560 ```ts 6561 let base64 = new util.Base64(); 6562 let array = new Uint8Array([99,122,69,122]); 6563 base64.decode(array).then((val) => { 6564 console.info(val.toString()); 6565 // 输出结果:115,49,51 6566 }) 6567 ``` 6568