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的随机性。如需禁用缓存,请将入参设置为false。 280 281**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 282 283**系统能力:** SystemCapability.Utils.Lang 284 285**参数:** 286 287| 参数名 | 类型 | 必填 | 说明 | 288| -------- | -------- | -------- | -------- | 289| entropyCache | boolean | 否 | 是否使用已缓存的UUID,true表示使用缓存的UUID,false表示不使用缓存的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表示使用缓存的UUID,false表示不使用缓存的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版本规范。 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 | 否 | 是否显示致命错误,true表示显示致命错误,false表示不显示致命错误,默认值是false。 | 539| ignoreBOM | boolean | 否 | 是否忽略BOM标记,true表示忽略待解码数据的BOM标记,false表示会对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 | 是 | 否 | 是否显示致命错误,true表示显示,false表示不显示。 | 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 | 否 | 是否显示致命错误,true表示显示致命错误,false表示不显示致命错误,默认值是false。 | 1011| ignoreBOM | boolean | 否 | 是否忽略BOM标记,true表示忽略待解码数据的BOM标记,false表示会对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进行编码时,需要注意不同编码格式下字符所占的字节数不同。务必明确指定编码格式,以确保编码结果正确。 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 | 否 | 需要编码的字符串,默认值是空字符串。当入参是空字符串时,返回undefined。 | 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 1252console.info("result.read = " + result.read); 1253// 输出结果: result.read = 4 1254console.info("result.written = " + result.written); 1255// 输出结果: result.written = 4 1256``` 1257 1258### encodeInto<sup>(deprecated)</sup> 1259 1260encodeInto(input: string, dest: Uint8Array): { read: number; written: number } 1261 1262将生成的UTF-8编码文本写入dest数组。 1263 1264> **说明:** 1265> 1266> 从API version 7开始支持,从API version 9开始废弃,建议使用[encodeIntoUint8Array<sup>9+</sup>](#encodeintouint8array9)替代。 1267 1268**系统能力:** SystemCapability.Utils.Lang 1269 1270**参数:** 1271 1272| 参数名 | 类型 | 必填 | 说明 | 1273| -------- | -------- | -------- | -------- | 1274| input | string | 是 | 需要编码的字符串。 | 1275| dest | Uint8Array | 是 | Uint8Array对象实例,用于将生成的UTF-8编码文本放入其中。 | 1276 1277**返回值:** 1278 1279| 类型 | 说明 | 1280| -------- | -------- | 1281| Uint8Array | 返回编码后的Uint8Array对象。 | 1282 1283**示例:** 1284 1285```ts 1286let textEncoder = new util.TextEncoder(); 1287let buffer = new ArrayBuffer(4); 1288let uint8 = new Uint8Array(buffer); 1289let result = textEncoder.encodeInto('abcd', uint8); 1290console.info("uint8 = " + uint8); 1291// 输出结果: uint8 = 97,98,99,100 1292``` 1293 1294### encode<sup>(deprecated)</sup> 1295 1296encode(input?: string): Uint8Array 1297 1298将输入参数编码后输出对应文本。 1299 1300> **说明:** 1301> 1302> 从API version 7开始支持,从API version 9开始废弃,建议使用[encodeInto<sup>9+</sup>](#encodeinto9)替代。 1303 1304**系统能力:** SystemCapability.Utils.Lang 1305 1306**参数:** 1307 1308| 参数名 | 类型 | 必填 | 说明 | 1309| -------- | -------- | -------- | -------- | 1310| input | string | 否 | 需要编码的字符串,默认值是空字符串。 | 1311 1312**返回值:** 1313 1314| 类型 | 说明 | 1315| -------- | -------- | 1316| Uint8Array | 返回编码后的文本。 | 1317 1318**示例:** 1319 1320```ts 1321let textEncoder = new util.TextEncoder(); 1322let result = textEncoder.encode("\uD800¥¥"); 1323console.info("result = " + result); 1324// 输出结果: result = 237,160,128,194,165,194,165 1325``` 1326 1327## RationalNumber<sup>8+</sup> 1328 1329RationalNumber主要用于有理数的比较,并提供获取分子和分母的方法。使用toString()方法可以将有理数转换为字符串形式,使用该类可以方便地进行有理数的各种操作。 1330 1331### constructor<sup>9+</sup> 1332 1333constructor() 1334 1335RationalNumber的构造函数。 1336 1337**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1338 1339**系统能力:** SystemCapability.Utils.Lang 1340 1341**示例:** 1342 1343```ts 1344let rationalNumber = new util.RationalNumber(); 1345``` 1346 1347### parseRationalNumber<sup>9+</sup> 1348 1349static parseRationalNumber(numerator: number,denominator: number): RationalNumber 1350 1351创建具有给定分子和分母的RationalNumber实例。 1352 1353> **说明:** 1354> 1355> 该接口要求参数numerator和denominator必须是整数类型。如果传入的参数是小数类型,不会进行拦截,但是会输出错误信息:"parseRationalNumber: The type of Parameter must be integer"。 1356 1357**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1358 1359**系统能力:** SystemCapability.Utils.Lang 1360 1361**参数:** 1362 1363| 参数名 | 类型 | 必填 | 说明 | 1364| ----------- | ------ | ---- | ---------------- | 1365| numerator | number | 是 | 分子,整数类型。取值范围:-Number.MAX_VALUE <= numerator <= Number.MAX_VALUE。| 1366| denominator | number | 是 | 分母,整数类型。取值范围:-Number.MAX_VALUE <= denominator <= Number.MAX_VALUE。| 1367 1368**返回值:** 1369 1370| 类型 | 说明 | 1371| -------- | -------- | 1372| [RationalNumber](#rationalnumber8) | RationalNumber对象。 | 1373 1374**错误码:** 1375 1376以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1377 1378| 错误码ID | 错误信息 | 1379| -------- | -------- | 1380| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1381 1382**示例:** 1383 1384```ts 1385let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1386``` 1387 1388### createRationalFromString<sup>8+</sup> 1389 1390static createRationalFromString(rationalString: string): RationalNumber 1391 1392使用给定的字符串创建RationalNumber对象。 1393 1394> **说明:** 1395> 1396> 该接口要求参数rationalString是字符串格式。如果传入的参数是小数类型字符串格式,不会进行拦截,但是会输出错误信息:"createRationalFromString: The type of Parameter must be integer string"。 1397 1398**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1399 1400**系统能力:** SystemCapability.Utils.Lang 1401 1402**参数:** 1403 1404| 参数名 | 类型 | 必填 | 说明 | 1405| -------- | -------- | -------- | -------- | 1406| rationalString | string | 是 | 字符串格式。 | 1407 1408**返回值:** 1409 1410| 类型 | 说明 | 1411| -------- | -------- | 1412| [RationalNumber](#rationalnumber8) | RationalNumber对象。 | 1413 1414**错误码:** 1415 1416以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1417 1418| 错误码ID | 错误信息 | 1419| -------- | -------- | 1420| 401 | The type of rationalString must be string. | 1421 1422**示例:** 1423 1424```ts 1425let rational = util.RationalNumber.createRationalFromString("3/4"); 1426``` 1427 1428### compare<sup>9+</sup> 1429 1430compare(another: RationalNumber): number 1431 1432将当前RationalNumber对象与目标RationalNumber对象进行比较,并返回比较结果。 1433 1434**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1435 1436**系统能力:** SystemCapability.Utils.Lang 1437 1438**参数:** 1439 1440| 参数名 | 类型 | 必填 | 说明 | 1441| ------- | -------------- | ---- | ------------------ | 1442| another | [RationalNumber](#rationalnumber8) | 是 | 其他的有理数对象。 | 1443 1444**返回值:** 1445 1446| 类型 | 说明 | 1447| ------ | ------------------------------------------------------------ | 1448| number | 两个对象相等时返回0;给定对象小于当前对象时返回1;给定对象大于当前对象时返回-1。 | 1449 1450**错误码:** 1451 1452以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1453 1454| 错误码ID | 错误信息 | 1455| -------- | -------- | 1456| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1457 1458**示例:** 1459 1460```ts 1461let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1462let rational = util.RationalNumber.createRationalFromString("3/4"); 1463let result = rationalNumber.compare(rational); 1464console.info("result = " + result); 1465// 输出结果:result = -1 1466``` 1467 1468### valueOf<sup>8+</sup> 1469 1470valueOf(): number 1471 1472获取当前RationalNumber对象的整数或浮点数值。 1473 1474**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1475 1476**系统能力:** SystemCapability.Utils.Lang 1477 1478**返回值:** 1479 1480| 类型 | 说明 | 1481| -------- | -------- | 1482| number | 返回整数或者浮点数的值。 | 1483 1484**示例:** 1485 1486```ts 1487let rationalNumber = new util.RationalNumber(1,2); 1488let result = rationalNumber.valueOf(); 1489console.info("result = " + result); 1490// 输出结果:result = 0.5 1491``` 1492API 9及以上建议使用以下写法: 1493```ts 1494let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1495let result = rationalNumber.valueOf(); 1496console.info("result = " + result); 1497// 输出结果:result = 0.5 1498``` 1499 1500### equals<sup>8+</sup> 1501 1502equals(obj: Object): boolean 1503 1504比较当前的RationalNumber对象与给定对象是否相等。 1505 1506**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1507 1508**系统能力:** SystemCapability.Utils.Lang 1509 1510**参数:** 1511 1512| 参数名 | 类型 | 必填 | 说明 | 1513| -------- | -------- | -------- | -------- | 1514| obj | Object | 是 | 其他类型对象。 | 1515 1516**返回值:** 1517 1518| 类型 | 说明 | 1519| -------- | -------- | 1520| boolean | 如果给定对象与当前对象相同,则返回true;否则返回false。 | 1521 1522**示例:** 1523 1524```ts 1525let rationalNumber = new util.RationalNumber(1,2); 1526let rational = util.RationalNumber.createRationalFromString("3/4"); 1527let result = rationalNumber.equals(rational); 1528console.info("result = " + result); 1529// 输出结果:result = false 1530``` 1531API 9及以上建议使用以下写法: 1532```ts 1533let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1534let rational = util.RationalNumber.createRationalFromString("3/4"); 1535let result = rationalNumber.equals(rational); 1536console.info("result = " + result); 1537// 输出结果:result = false 1538``` 1539 1540### getCommonFactor<sup>9+</sup> 1541 1542static getCommonFactor(number1: number, number2: number): number 1543 1544获取两个整数的最大公约数。 1545 1546> **说明:** 1547> 1548> 该接口要求参数number1和number2必须是整数类型。如果传入的参数是小数类型,不会进行拦截,但是会输出错误信息:"getCommonFactor: The type of Parameter must be integer"。 1549 1550**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1551 1552**系统能力:** SystemCapability.Utils.Lang 1553 1554**参数:** 1555 1556| 参数名 | 类型 | 必填 | 说明 | 1557| ------- | ------ | ---- | ---------- | 1558| number1 | number | 是 | 整数类型。-Number.MAX_VALUE <= number1 <= Number.MAX_VALUE。| 1559| number2 | number | 是 | 整数类型。-Number.MAX_VALUE <= number2 <= Number.MAX_VALUE。| 1560 1561**返回值:** 1562 1563| 类型 | 说明 | 1564| ------ | ------------------------------ | 1565| number | 返回两个给定数字的最大公约数。 | 1566 1567**错误码:** 1568 1569以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1570 1571| 错误码ID | 错误信息 | 1572| -------- | -------- | 1573| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1574 1575**示例:** 1576 1577```ts 1578let result = util.RationalNumber.getCommonFactor(4,6); 1579console.info("result = " + result); 1580// 输出结果:result = 2 1581``` 1582 1583### getNumerator<sup>8+</sup> 1584 1585getNumerator(): number 1586 1587获取当前RationalNumber对象的分子。 1588 1589**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1590 1591**系统能力:** SystemCapability.Utils.Lang 1592 1593**返回值:** 1594 1595| 类型 | 说明 | 1596| -------- | -------- | 1597| number | 返回RationalNumber对象的分子。 | 1598 1599**示例:** 1600 1601```ts 1602let rationalNumber = new util.RationalNumber(1,2); 1603let result = rationalNumber.getNumerator(); 1604console.info("result = " + result); 1605// 输出结果:result = 1 1606``` 1607API 9及以上建议使用以下写法: 1608```ts 1609let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1610let result = rationalNumber.getNumerator(); 1611console.info("result = " + result); 1612// 输出结果:result = 1 1613``` 1614 1615### getDenominator<sup>8+</sup> 1616 1617getDenominator(): number 1618 1619获取当前RationalNumber对象的分母。 1620 1621**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1622 1623**系统能力:** SystemCapability.Utils.Lang 1624 1625**返回值:** 1626 1627| 类型 | 说明 | 1628| -------- | -------- | 1629| number | 返回RationalNumber对象的分母。 | 1630 1631**示例:** 1632 1633```ts 1634let rationalNumber = new util.RationalNumber(1,2); 1635let result = rationalNumber.getDenominator(); 1636console.info("result = " + result); 1637// 输出结果:result = 2 1638``` 1639API 9及以上建议使用以下写法: 1640```ts 1641let rationalNumber = util.RationalNumber.parseRationalNumber(1,2) 1642let result = rationalNumber.getDenominator(); 1643console.info("result = " + result); 1644// 输出结果:result = 2 1645``` 1646 1647### isZero<sup>8+</sup> 1648 1649isZero():boolean 1650 1651检查当前RationalNumber对象是否为0。 1652 1653**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1654 1655**系统能力:** SystemCapability.Utils.Lang 1656 1657**返回值:** 1658 1659| 类型 | 说明 | 1660| -------- | -------- | 1661| boolean | 如果当前对象的值为0,则返回true;否则返回false。 | 1662 1663**示例:** 1664 1665```ts 1666let rationalNumber = new util.RationalNumber(1,2); 1667let result = rationalNumber.isZero(); 1668console.info("result = " + result); 1669// 输出结果:result = false 1670``` 1671API 9及以上建议使用以下写法: 1672```ts 1673let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1674let result = rationalNumber.isZero(); 1675console.info("result = " + result); 1676// 输出结果:result = false 1677``` 1678 1679### isNaN<sup>8+</sup> 1680 1681isNaN(): boolean 1682 1683检查当前RationalNumber对象是否表示非数字(NaN)值。 1684 1685**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1686 1687**系统能力:** SystemCapability.Utils.Lang 1688 1689**返回值:** 1690 1691| 类型 | 说明 | 1692| -------- | -------- | 1693| boolean | 如果分母和分子都为0,则返回true;否则返回false。 | 1694 1695**示例:** 1696 1697```ts 1698let rationalNumber = new util.RationalNumber(1,2); 1699let result = rationalNumber.isNaN(); 1700console.info("result = " + result); 1701// 输出结果:result = false 1702``` 1703API 9及以上建议使用以下写法: 1704```ts 1705let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1706let result = rationalNumber.isNaN(); 1707console.info("result = " + result); 1708// 输出结果:result = false 1709``` 1710 1711### isFinite<sup>8+</sup> 1712 1713isFinite():boolean 1714 1715检查RationalNumber对象是否表示一个有限值。 1716 1717**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1718 1719**系统能力:** SystemCapability.Utils.Lang 1720 1721**返回值:** 1722 1723| 类型 | 说明 | 1724| -------- | -------- | 1725| boolean | 如果分母不为0,则返回true;否则返回false。 | 1726 1727**示例:** 1728 1729```ts 1730let rationalNumber = new util.RationalNumber(1,2); 1731let result = rationalNumber.isFinite(); 1732console.info("result = " + result); 1733// 输出结果:result = true 1734``` 1735API 9及以上建议使用以下写法: 1736```ts 1737let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1738let result = rationalNumber.isFinite(); 1739console.info("result = " + result); 1740// 输出结果:result = true 1741``` 1742 1743### toString<sup>8+</sup> 1744 1745toString(): string 1746 1747获取RationalNumber对象的字符串表示形式。 1748 1749**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1750 1751**系统能力:** SystemCapability.Utils.Lang 1752 1753**返回值:** 1754 1755| 类型 | 说明 | 1756| -------- | -------- | 1757| string | 返回Numerator/Denominator格式的字符串,例如3/5,如果分子为0,则返回0/1。如果分母为0,则返回Infinity。如果分子和分母都为0,则返回NaN。| 1758 1759**示例:** 1760 1761```ts 1762let rationalNumber = new util.RationalNumber(1,2); 1763let result = rationalNumber.toString(); 1764console.info("result = " + result); 1765// 输出结果:result = 1/2 1766``` 1767API 9及以上建议使用以下写法: 1768```ts 1769let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1770let result = rationalNumber.toString(); 1771console.info("result = " + result); 1772// 输出结果:result = 1/2 1773``` 1774 1775### constructor<sup>(deprecated)</sup> 1776 1777constructor(numerator: number,denominator: number) 1778 1779RationalNumber的构造函数。 1780 1781> **说明:** 1782> 1783> 从API version 8开始支持,从API version 9开始废弃,建议使用[parseRationalNumber<sup>9+</sup>](#parserationalnumber9)替代。 1784 1785**系统能力:** SystemCapability.Utils.Lang 1786 1787**参数:** 1788 1789| 参数名 | 类型 | 必填 | 说明 | 1790| -------- | -------- | -------- | -------- | 1791| numerator | number | 是 | 分子,整数类型。 | 1792| denominator | number | 是 | 分母,整数类型。 | 1793 1794**示例:** 1795 1796```ts 1797let rationalNumber = new util.RationalNumber(1,2); 1798``` 1799 1800### compareTo<sup>(deprecated)</sup> 1801 1802compareTo(another: RationalNumber): number 1803 1804比较当前的RationalNumber对象与给定的对象。 1805 1806> **说明:** 1807> 1808> 从API version 8开始支持,从API version 9开始废弃,建议使用[compare<sup>9+</sup>](#compare9)替代。 1809 1810**系统能力:** SystemCapability.Utils.Lang 1811 1812**参数:** 1813 1814| 参数名 | 类型 | 必填 | 说明 | 1815| -------- | -------- | -------- | -------- | 1816| another | RationalNumber | 是 | 其他的有理数对象。 | 1817 1818**返回值:** 1819 1820| 类型 | 说明 | 1821| -------- | -------- | 1822| number | 如果两个对象相等,则返回0;如果给定对象小于当前对象,则返回1;如果给定对象大于当前对象,则返回-1。 | 1823 1824**示例:** 1825 1826```ts 1827let rationalNumber = new util.RationalNumber(1,2); 1828let rational = util.RationalNumber.createRationalFromString("3/4"); 1829let result = rationalNumber.compareTo(rational); 1830console.info("result = " + result); 1831// 输出结果:result = -1 1832``` 1833 1834### getCommonDivisor<sup>(deprecated)</sup> 1835 1836static getCommonDivisor(number1: number,number2: number): number 1837 1838获取两个指定整数的最大公约数。 1839 1840> **说明:** 1841> 1842> 从API version 8开始支持,从API version 9开始废弃,建议使用[getCommonFactor<sup>9+</sup>](#getcommonfactor9)替代。 1843 1844**系统能力:** SystemCapability.Utils.Lang 1845 1846**参数:** 1847 1848| 参数名 | 类型 | 必填 | 说明 | 1849| -------- | -------- | -------- | -------- | 1850| number1 | number | 是 | 整数类型。 | 1851| number2 | number | 是 | 整数类型。 | 1852 1853**返回值:** 1854 1855| 类型 | 说明 | 1856| -------- | -------- | 1857| number | 返回两个给定数字的最大公约数。 | 1858 1859 1860 1861## LRUCache<sup>9+</sup> 1862 1863LRUCache用于在缓存空间不足时,将近期最少使用的数据替换为新数据。此设计基于资源访问的考虑:近期访问的数据,可能在不久的将来会再次访问。因此最少访问的数据被认为价值最低,应当优先从缓存中移除。 1864 1865### 属性 1866 1867**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1868 1869**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang。 1870 1871| 名称 | 类型 | 可读 | 可写 | 说明 | 1872| ------ | ------ | ---- | ---- | ---------------------- | 1873| length | number | 是 | 否 | 当前缓冲区中值的总数。 | 1874 1875**示例:** 1876 1877```ts 1878let pro = new util.LRUCache<number, number>(); 1879pro.put(2, 10); 1880pro.put(1, 8); 1881let result = pro.length; 1882console.info('result = ' + result); 1883// 输出结果:result = 2 1884``` 1885 1886### constructor<sup>9+</sup> 1887 1888constructor(capacity?: number) 1889 1890默认构造函数用于创建一个新的LRUCache实例,默认容量为64。 1891 1892**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1893 1894**系统能力:** SystemCapability.Utils.Lang 1895 1896**参数:** 1897 1898| 参数名 | 类型 | 必填 | 说明 | 1899| -------- | ------ | ---- | ---------------------------- | 1900| capacity | number | 否 | 指示要为缓冲区自定义的容量,不传默认值为64,最大值不能超过2147483647。 | 1901 1902**错误码:** 1903 1904以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1905 1906| 错误码ID | 错误信息 | 1907| -------- | -------- | 1908| 401 | Parameter error. Possible causes: 1.Incorrect parameter types. | 1909 1910**示例:** 1911 1912```ts 1913let pro = new util.LRUCache<number, number>(); 1914``` 1915 1916 1917### updateCapacity<sup>9+</sup> 1918 1919updateCapacity(newCapacity: number): void 1920 1921更新缓冲区容量为指定值,如果newCapacity小于或等于0,则抛出异常。当缓冲区中值的总数大于指定容量时,会执行删除操作。 1922 1923**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1924 1925**系统能力:** SystemCapability.Utils.Lang 1926 1927**参数:** 1928 1929| 参数名 | 类型 | 必填 | 说明 | 1930| ----------- | ------ | ---- | ---------------------------- | 1931| newCapacity | number | 是 | 指示要为缓冲区自定义的容量,最大值不能超过2147483647。 | 1932 1933**错误码:** 1934 1935以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1936 1937| 错误码ID | 错误信息 | 1938| -------- | -------- | 1939| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. | 1940 1941**示例:** 1942 1943```ts 1944let pro = new util.LRUCache<number, number>(); 1945pro.updateCapacity(100); 1946``` 1947 1948### toString<sup>9+</sup> 1949 1950toString(): string 1951 1952返回对象的字符串表示。 1953 1954**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1955 1956**系统能力:** SystemCapability.Utils.Lang 1957 1958**返回值:** 1959 1960| 类型 | 说明 | 1961| ------ | -------------------------- | 1962| string | 返回对象的字符串表示形式。 | 1963 1964**示例:** 1965 1966```ts 1967let pro = new util.LRUCache<number, number>(); 1968pro.put(2, 10); 1969pro.get(2); 1970pro.get(3); 1971console.info(pro.toString()); 1972// 输出结果:LRUCache[ maxSize = 64, hits = 1, misses = 1, hitRate = 50% ] 1973// maxSize: 缓存区最大值 hits: 查询值匹配成功的次数 misses: 查询值匹配失败的次数 hitRate: 查询值匹配率 1974``` 1975 1976### getCapacity<sup>9+</sup> 1977 1978getCapacity(): number 1979 1980获取当前缓冲区的容量。 1981 1982**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1983 1984**系统能力:** SystemCapability.Utils.Lang 1985 1986**返回值:** 1987 1988| 类型 | 说明 | 1989| ------ | ---------------------- | 1990| number | 返回当前缓冲区的容量。 | 1991 1992**示例:** 1993 1994```ts 1995let pro = new util.LRUCache<number, number>(); 1996let result = pro.getCapacity(); 1997console.info('result = ' + result); 1998// 输出结果:result = 64 1999``` 2000 2001### clear<sup>9+</sup> 2002 2003clear(): void 2004 2005清除当前缓冲区中的键值对。 2006 2007**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2008 2009**系统能力:** SystemCapability.Utils.Lang 2010 2011**示例:** 2012 2013```ts 2014let pro = new util.LRUCache<number, number>(); 2015pro.put(2, 10); 2016let result = pro.length; 2017pro.clear(); 2018let res = pro.length; 2019console.info('result = ' + result); 2020console.info('res = ' + res); 2021// 输出结果:result = 1 2022// 输出结果:res = 0 2023``` 2024 2025### getCreateCount<sup>9+</sup> 2026 2027getCreateCount(): number 2028 2029获取对象创建的次数。 2030 2031**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2032 2033**系统能力:** SystemCapability.Utils.Lang 2034 2035**返回值:** 2036 2037| 类型 | 说明 | 2038| ------ | -------------------| 2039| number | 返回创建对象的次数。 | 2040 2041**示例:** 2042 2043```ts 2044// 创建新类ChildLRUCache继承LRUCache,重写createDefault方法,返回一个非undefined的值。 2045class ChildLRUCache extends util.LRUCache<number, number> { 2046 constructor() { 2047 super(); 2048 } 2049 2050 createDefault(key: number): number { 2051 return key; 2052 } 2053} 2054let lru = new ChildLRUCache(); 2055lru.put(2, 10); 2056lru.get(3); 2057lru.get(5); 2058let res = lru.getCreateCount(); 2059console.info('res = ' + res); 2060// 输出结果:res = 2 2061``` 2062 2063### getMissCount<sup>9+</sup> 2064 2065getMissCount(): number 2066 2067获取查询值不匹配的次数。 2068 2069**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2070 2071**系统能力:** SystemCapability.Utils.Lang 2072 2073**返回值:** 2074 2075| 类型 | 说明 | 2076| ------ | ------------------------ | 2077| number | 返回查询值不匹配的次数。 | 2078 2079**示例:** 2080 2081```ts 2082let pro = new util.LRUCache<number, number>(); 2083pro.put(2, 10); 2084pro.get(2); 2085let result = pro.getMissCount(); 2086console.info('result = ' + result); 2087// 输出结果:result = 0 2088``` 2089 2090### getRemovalCount<sup>9+</sup> 2091 2092getRemovalCount(): number 2093 2094获取缓冲区键值对的回收次数。 2095 2096**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2097 2098**系统能力:** SystemCapability.Utils.Lang 2099 2100**返回值:** 2101 2102| 类型 | 说明 | 2103| ------ | -------------------------- | 2104| number | 返回缓冲区键值对回收的次数。 | 2105 2106**示例:** 2107 2108```ts 2109let pro = new util.LRUCache<number, number>(); 2110pro.put(2, 10); 2111pro.updateCapacity(2); 2112pro.put(50, 22); 2113let result = pro.getRemovalCount(); 2114console.info('result = ' + result); 2115// 输出结果:result = 0 2116``` 2117 2118### getMatchCount<sup>9+</sup> 2119 2120getMatchCount(): number 2121 2122获取查询值匹配成功的次数。 2123 2124**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2125 2126**系统能力:** SystemCapability.Utils.Lang 2127 2128**返回值:** 2129 2130| 类型 | 说明 | 2131| ------ | -------------------------- | 2132| number | 返回查询值匹配成功的次数。 | 2133 2134**示例:** 2135 2136 ```ts 2137 let pro = new util.LRUCache<number, number>(); 2138 pro.put(2, 10); 2139 pro.get(2); 2140 let result = pro.getMatchCount(); 2141 console.info('result = ' + result); 2142 // 输出结果:result = 1 2143 ``` 2144 2145### getPutCount<sup>9+</sup> 2146 2147getPutCount(): number 2148 2149获取将值添加到缓冲区的次数。 2150 2151**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2152 2153**系统能力:** SystemCapability.Utils.Lang 2154 2155**返回值:** 2156 2157| 类型 | 说明 | 2158| ------ | ---------------------------- | 2159| number | 返回将值添加到缓冲区的次数。 | 2160 2161**示例:** 2162 2163```ts 2164let pro = new util.LRUCache<number, number>(); 2165pro.put(2, 10); 2166let result = pro.getPutCount(); 2167console.info('result = ' + result); 2168// 输出结果:result = 1 2169``` 2170 2171### isEmpty<sup>9+</sup> 2172 2173isEmpty(): boolean 2174 2175检查缓冲区是否为空。 2176 2177**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2178 2179**系统能力:** SystemCapability.Utils.Lang 2180 2181**返回值:** 2182 2183| 类型 | 说明 | 2184| ------- | ---------------------------------------- | 2185| boolean | 如果缓冲区不包含任何值,则返回true。 | 2186 2187**示例:** 2188 2189```ts 2190let pro = new util.LRUCache<number, number>(); 2191pro.put(2, 10); 2192let result = pro.isEmpty(); 2193console.info('result = ' + result); 2194// 输出结果:result = false 2195``` 2196 2197### get<sup>9+</sup> 2198 2199get(key: K): V | undefined 2200 2201返回键对应的值。当键不在缓冲区中时,通过[createDefault<sup>9+</sup>](#createdefault9)接口创建,若createDefault创建的值不为undefined时,此时会调用[afterRemoval<sup>9+</sup>](#afterremoval9)接口,返回createDefault创建的值。 2202 2203**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2204 2205**系统能力:** SystemCapability.Utils.Lang 2206 2207**参数:** 2208 2209| 参数名 | 类型 | 必填 | 说明 | 2210| ------ | ---- | ---- | ------------ | 2211| key | K | 是 | 要查询的键。 | 2212 2213**返回值:** 2214 2215| 类型 | 说明 | 2216| ------------------------ | ------------------------------------------------------------ | 2217| V \| undefined | 如果指定的键存在于缓冲区中,则返回与键关联的值;否则返回createDefault创建的值。 | 2218 2219**错误码:** 2220 2221以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2222 2223| 错误码ID | 错误信息 | 2224| -------- | -------- | 2225| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2226 2227**示例:** 2228 2229```ts 2230let pro = new util.LRUCache<number, number>(); 2231pro.put(2, 10); 2232let result = pro.get(2); 2233console.info('result = ' + result); 2234// 输出结果:result = 10 2235``` 2236 2237### put<sup>9+</sup> 2238 2239put(key: K,value: V): V 2240 2241将键值对添加到缓冲区,返回与添加的键关联的值。当缓冲区中值的总数超过容量时,执行删除操作。 2242 2243**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2244 2245**系统能力:** SystemCapability.Utils.Lang 2246 2247**参数:** 2248 2249| 参数名 | 类型 | 必填 | 说明 | 2250| ------ | ---- | ---- | -------------------------- | 2251| key | K | 是 | 要添加的键。 | 2252| value | V | 是 | 指示与要添加的键关联的值。 | 2253 2254**返回值:** 2255 2256| 类型 | 说明 | 2257| ---- | ------------------------------------------------------------ | 2258| V | 返回与添加的键关联的值。如果键或值为空,则抛出此异常。 | 2259 2260**错误码:** 2261 2262以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2263 2264| 错误码ID | 错误信息 | 2265| -------- | -------- | 2266| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2267 2268**示例:** 2269 2270```ts 2271let pro = new util.LRUCache<number, number>(); 2272let result = pro.put(2, 10); 2273console.info('result = ' + result); 2274// 输出结果:result = 10 2275``` 2276 2277### values<sup>9+</sup> 2278 2279values(): V[] 2280 2281获取当前缓冲区中所有值,从最近访问到最近最少访问的顺序列表。 2282 2283**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2284 2285**系统能力:** SystemCapability.Utils.Lang 2286 2287**返回值:** 2288 2289| 类型 | 说明 | 2290| --------- | ------------------------------------------------------------ | 2291| V[] | 按从最近访问到最近最少访问的顺序返回当前缓冲区中所有值的列表。 | 2292 2293**示例:** 2294 2295```ts 2296let pro = new util.LRUCache<number|string,number|string>(); 2297pro.put(2, 10); 2298pro.put(2, "anhu"); 2299pro.put("afaf", "grfb"); 2300let result = pro.values(); 2301console.info('result = ' + result); 2302// 输出结果:result = anhu,grfb 2303``` 2304 2305### keys<sup>9+</sup> 2306 2307keys(): K[] 2308 2309获取当前缓冲区中所有键从最近访问到最近最少访问的升序列表。 2310 2311**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2312 2313**系统能力:** SystemCapability.Utils.Lang 2314 2315**返回值:** 2316 2317| 类型 | 说明 | 2318| --------- | ------------------------------------------------------------ | 2319| K [] | 按升序返回当前缓冲区中所有键的列表,从最近访问到最近最少访问。 | 2320 2321**示例:** 2322 2323```ts 2324let pro = new util.LRUCache<number, number>(); 2325pro.put(2, 10); 2326pro.put(3, 1); 2327let result = pro.keys(); 2328console.info('result = ' + result); 2329// 输出结果:result = 2,3 2330``` 2331 2332### remove<sup>9+</sup> 2333 2334remove(key: K): V | undefined 2335 2336从当前缓冲区中删除指定的键及其关联的值,并返回键关联的值。如果指定的键不存在,则返回undefined。 2337 2338**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2339 2340**系统能力:** SystemCapability.Utils.Lang 2341 2342**参数:** 2343 2344| 参数名 | 类型 | 必填 | 说明 | 2345| ------ | ---- | ---- | -------------- | 2346| key | K | 是 | 要删除的键值。 | 2347 2348**返回值:** 2349 2350| 类型 | 说明 | 2351| ------------------------ | ------------------------------------------------------------ | 2352| V \| undefined | 返回一个包含已删除键值对的Optional对象;如果key不存在,则返回undefined,如果key为null,则抛出异常。 | 2353 2354**错误码:** 2355 2356以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2357 2358| 错误码ID | 错误信息 | 2359| -------- | -------- | 2360| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2361 2362**示例:** 2363 2364```ts 2365let pro = new util.LRUCache<number, number>(); 2366pro.put(2, 10); 2367let result = pro.remove(20); 2368console.info('result = ' + result); 2369// 输出结果:result = undefined 2370``` 2371 2372### afterRemoval<sup>9+</sup> 2373 2374afterRemoval(isEvict: boolean, key: K, value: V, newValue: V): void 2375 2376删除值后执行后续操作,这些操作由开发者自行实现。本接口会在删除操作时被调用,如[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)接口。 2377 2378**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2379 2380**系统能力:** SystemCapability.Utils.Lang 2381 2382**参数:** 2383 2384| 参数名 | 类型 | 必填 | 说明 | 2385| -------- | ------- | ---- | ------------------------------------------------------------ | 2386| isEvict | boolean | 是 | 当因容量不足而调用该方法时,参数值设置为true,其他情况设置为false。 | 2387| key | K | 是 | 表示删除的键。 | 2388| value | V | 是 | 表示删除的值。 | 2389| newValue | V | 是 | 如果已调用put方法并且要添加的键已经存在,则参数值是关联的新值。其他情况下参数值为空。 | 2390 2391**错误码:** 2392 2393以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2394 2395| 错误码ID | 错误信息 | 2396| -------- | -------- | 2397| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2398 2399**示例:** 2400 2401```ts 2402class ChildLRUCache<K, V> extends util.LRUCache<K, V> { 2403 constructor(capacity?: number) { 2404 super(capacity); 2405 } 2406 2407 afterRemoval(isEvict: boolean, key: K, value: V, newValue: V): void { 2408 if (isEvict === true) { 2409 console.info('key = ' + key); 2410 // 输出结果:key = 1 2411 console.info('value = ' + value); 2412 // 输出结果:value = 1 2413 console.info('newValue = ' + newValue); 2414 // 输出结果:newValue = null 2415 } 2416 } 2417} 2418let lru = new ChildLRUCache<number, number>(2); 2419lru.put(1, 1); 2420lru.put(2, 2); 2421lru.put(3, 3); 2422``` 2423 2424### contains<sup>9+</sup> 2425 2426contains(key: K): boolean 2427 2428检查当前缓冲区是否包含指定的键。 2429 2430**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2431 2432**系统能力:** SystemCapability.Utils.Lang 2433 2434**参数:** 2435 2436| 参数名 | 类型 | 必填 | 说明 | 2437| ------ | ------ | ---- | ---------------- | 2438| key | K | 是 | 要检查的键。 | 2439 2440**返回值:** 2441 2442| 类型 | 说明 | 2443| ------- | ------------------------------------------ | 2444| boolean | 如果缓冲区包含指定的键,则返回 true。 | 2445 2446**错误码:** 2447 2448以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2449 2450| 错误码ID | 错误信息 | 2451| -------- | -------- | 2452| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2453 2454**示例:** 2455 2456```ts 2457let pro = new util.LRUCache<number, number>(); 2458pro.put(2, 10); 2459let result = pro.contains(2); 2460console.info('result = ' + result); 2461// 输出结果:result = true 2462``` 2463 2464### createDefault<sup>9+</sup> 2465 2466createDefault(key: K): V 2467 2468如果在缓冲区未匹配到键,则执行后续操作,参数表示未匹配的键,返回与键关联的值,默认返回undefined。 2469 2470**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2471 2472**系统能力:** SystemCapability.Utils.Lang 2473 2474**参数:** 2475 2476| 参数名 | 类型 | 必填 | 说明 | 2477| ------ | ---- | ---- | -------------- | 2478| key | K | 是 | 表示未匹配的键。 | 2479 2480**返回值:** 2481 2482| 类型 | 说明 | 2483| ---- | ------------------ | 2484| V | 返回与键关联的值。 | 2485 2486**错误码:** 2487 2488以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2489 2490| 错误码ID | 错误信息 | 2491| -------- | -------- | 2492| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2493 2494**示例:** 2495 2496```ts 2497let pro = new util.LRUCache<number, number>(); 2498let result = pro.createDefault(50); 2499console.info('result = ' + result); 2500// 输出结果:result = undefined 2501``` 2502 2503### entries<sup>9+</sup> 2504 2505entries(): IterableIterator<[K, V]> 2506 2507迭代此对象中的所有键值对。 2508 2509**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2510 2511**系统能力:** SystemCapability.Utils.Lang 2512 2513**返回值:** 2514 2515| 类型 | 说明 | 2516| ----------- | -------------------- | 2517| IterableIterator<[K, V]> | 返回一个可迭代数组。 | 2518 2519**示例:** 2520 2521```ts 2522let pro = new util.LRUCache<number, number>(); 2523pro.put(2, 10); 2524pro.put(3, 15); 2525let pair:Iterable<Object[]> = pro.entries(); 2526let arrayValue = Array.from(pair); 2527for (let value of arrayValue) { 2528 console.info(value[0]+ ', '+ value[1]); 2529 // 输出结果: 2530 // 2, 10 2531 // 3, 15 2532} 2533``` 2534 2535### [Symbol.iterator]<sup>9+</sup> 2536 2537[Symbol.iterator]\(): IterableIterator<[K, V]> 2538 2539返回键值对形式的二维数组。 2540 2541**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2542 2543**系统能力:** SystemCapability.Utils.Lang 2544 2545**返回值:** 2546 2547| 类型 | 说明 | 2548| ----------- | ------------------------------ | 2549| IterableIterator<[K, V]> | 返回一个键值对形式的二维数组。 | 2550 2551**示例:** 2552 2553```ts 2554let pro = new util.LRUCache<number, number>(); 2555pro.put(2, 10); 2556pro.put(3, 15); 2557let pair:Iterable<Object[]> = pro[Symbol.iterator](); 2558let arrayValue = Array.from(pair); 2559for (let value of arrayValue) { 2560 console.info(value[0]+ ', '+ value[1]); 2561 // 输出结果: 2562 // 2, 10 2563 // 3, 15 2564} 2565``` 2566 2567## ScopeComparable<sup>8+</sup> 2568 2569ScopeComparable类型的值需要实现compareTo方法,确保传入的数据具有可比性。 2570 2571**系统能力:** SystemCapability.Utils.Lang 2572 2573### compareTo<sup>8+</sup> 2574 2575compareTo(other: ScopeComparable): boolean 2576 2577比较两个值的大小,返回一个布尔值。 2578 2579**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2580 2581**系统能力:** SystemCapability.Utils.Lang 2582 2583**参数:** 2584 2585| 参数名 | 类型 | 必填 | 说明 | 2586| ------ | ---- | ---- | -------------- | 2587| other | [ScopeComparable](#scopecomparable8) | 是 | 表示要比较的值。 | 2588 2589**返回值:** 2590 2591| 类型 | 说明 | 2592| ---- | ------------------ | 2593| boolean | 调用compareTo的值大于等于传入的值返回true,否则返回false。| 2594 2595**示例:** 2596 2597构造新类,实现compareTo方法。后续示例代码中,均以此Temperature类为例。 2598 2599```ts 2600class Temperature implements util.ScopeComparable { 2601 private readonly _temp: number; 2602 2603 constructor(value: number) { 2604 this._temp = value; 2605 } 2606 2607 compareTo(value: Temperature) { 2608 return this._temp >= value.getTemp(); 2609 } 2610 2611 getTemp() { 2612 return this._temp; 2613 } 2614 2615 toString(): string { 2616 return this._temp.toString(); 2617 } 2618} 2619``` 2620 2621## ScopeType<sup>8+</sup> 2622 2623type ScopeType = ScopeComparable | number 2624 2625表示范围中的值的类型。 2626 2627**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2628 2629**系统能力:** SystemCapability.Utils.Lang 2630 2631| 类型 | 说明 | 2632| -------- | -------- | 2633| number | 表示值的类型为数字。 | 2634| [ScopeComparable](#scopecomparable8) | 表示值的类型为ScopeComparable。| 2635 2636## ScopeHelper<sup>9+</sup> 2637 2638ScopeHelper接口用于描述一个字段的有效范围。构造函数用于创建具有指定下限和上限的对象,并要求这些对象必须具有可比性。 2639 2640### constructor<sup>9+</sup> 2641 2642constructor(lowerObj: ScopeType, upperObj: ScopeType) 2643 2644创建指定下限和上限的作用域实例,返回一个ScopeHelper对象。 2645 2646**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2647 2648**系统能力:** SystemCapability.Utils.Lang 2649 2650**参数:** 2651 2652| 参数名 | 类型 | 必填 | 说明 | 2653| -------- | ------------------------ | ---- | ---------------------- | 2654| lowerObj | [ScopeType](#scopetype8) | 是 | 指定作用域实例的下限。 | 2655| upperObj | [ScopeType](#scopetype8) | 是 | 指定作用域实例的上限。 | 2656 2657**错误码:** 2658 2659以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2660 2661| 错误码ID | 错误信息 | 2662| -------- | -------- | 2663| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2664 2665**示例:** 2666 2667```ts 2668class Temperature implements util.ScopeComparable { 2669 private readonly _temp: number; 2670 2671 constructor(value: number) { 2672 this._temp = value; 2673 } 2674 2675 compareTo(value: Temperature) { 2676 return this._temp >= value.getTemp(); 2677 } 2678 2679 getTemp() { 2680 return this._temp; 2681 } 2682 2683 toString(): string { 2684 return this._temp.toString(); 2685 } 2686} 2687let tempLower = new Temperature(30); 2688let tempUpper = new Temperature(40); 2689let range = new util.ScopeHelper(tempLower, tempUpper); 2690console.info("range = " + range); 2691// 输出结果:range = [30, 40] 2692``` 2693 2694### toString<sup>9+</sup> 2695 2696toString(): string 2697 2698该字符串化方法返回当前范围的字符串表示形式。 2699 2700**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2701 2702**系统能力:** SystemCapability.Utils.Lang 2703 2704**返回值:** 2705 2706| 类型 | 说明 | 2707| ------ | -------------------------------------- | 2708| string | 返回当前范围的字符串表示形式。 | 2709 2710**示例:** 2711 2712```ts 2713class Temperature implements util.ScopeComparable { 2714 private readonly _temp: number; 2715 2716 constructor(value: number) { 2717 this._temp = value; 2718 } 2719 2720 compareTo(value: Temperature) { 2721 return this._temp >= value.getTemp(); 2722 } 2723 2724 getTemp() { 2725 return this._temp; 2726 } 2727 2728 toString(): string { 2729 return this._temp.toString(); 2730 } 2731} 2732 2733let tempLower = new Temperature(30); 2734let tempUpper = new Temperature(40); 2735let range = new util.ScopeHelper(tempLower, tempUpper); 2736let result = range.toString(); 2737console.info("result = " + result); 2738// 输出结果:result = [30, 40] 2739``` 2740 2741### intersect<sup>9+</sup> 2742 2743intersect(range: ScopeHelper): ScopeHelper 2744 2745获取给定范围和当前范围的交集。当交集为空集时,抛出异常。 2746 2747**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2748 2749**系统能力:** SystemCapability.Utils.Lang 2750 2751**参数:** 2752 2753| 参数名 | 类型 | 必填 | 说明 | 2754| ------ | ---------------------------- | ---- | ------------------ | 2755| range | [ScopeHelper](#scopehelper9) | 是 | 传入给定范围。 | 2756 2757**返回值:** 2758 2759| 类型 | 说明 | 2760| ------------------------------ | ------------------------------ | 2761| [ScopeHelper](#scopehelper9) | 返回给定范围和当前范围的交集。 | 2762 2763**错误码:** 2764 2765以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2766 2767| 错误码ID | 错误信息 | 2768| -------- | -------- | 2769| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2770 2771**示例:** 2772 2773```ts 2774class Temperature implements util.ScopeComparable { 2775 private readonly _temp: number; 2776 2777 constructor(value: number) { 2778 this._temp = value; 2779 } 2780 2781 compareTo(value: Temperature) { 2782 return this._temp >= value.getTemp(); 2783 } 2784 2785 getTemp() { 2786 return this._temp; 2787 } 2788 2789 toString(): string { 2790 return this._temp.toString(); 2791 } 2792} 2793 2794let tempLower = new Temperature(30); 2795let tempUpper = new Temperature(40); 2796let range = new util.ScopeHelper(tempLower, tempUpper); 2797let tempMiDF = new Temperature(35); 2798let tempMidS = new Temperature(39); 2799let rangeFir = new util.ScopeHelper(tempMiDF, tempMidS); 2800range.intersect(rangeFir); 2801``` 2802 2803### intersect<sup>9+</sup> 2804 2805intersect(lowerObj:ScopeType,upperObj:ScopeType):ScopeHelper 2806 2807获取当前范围与指定下限和上限范围的交集。当交集为空集时,抛出异常。 2808 2809**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2810 2811**系统能力:** SystemCapability.Utils.Lang 2812 2813**参数:** 2814 2815| 参数名 | 类型 | 必填 | 说明 | 2816| -------- | ------------------------ | ---- | ---------------- | 2817| lowerObj | [ScopeType](#scopetype8) | 是 | 给定范围的下限。 | 2818| upperObj | [ScopeType](#scopetype8) | 是 | 给定范围的上限。 | 2819 2820**返回值:** 2821 2822| 类型 | 说明 | 2823| ---------------------------- | ---------------------------------------- | 2824| [ScopeHelper](#scopehelper9) | 返回当前范围与给定下限和上限范围的交集。 | 2825 2826**错误码:** 2827 2828以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2829 2830| 错误码ID | 错误信息 | 2831| -------- | -------- | 2832| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2833 2834**示例:** 2835 2836```ts 2837class Temperature implements util.ScopeComparable { 2838 private readonly _temp: number; 2839 2840 constructor(value: number) { 2841 this._temp = value; 2842 } 2843 2844 compareTo(value: Temperature) { 2845 return this._temp >= value.getTemp(); 2846 } 2847 2848 getTemp() { 2849 return this._temp; 2850 } 2851 2852 toString(): string { 2853 return this._temp.toString(); 2854 } 2855} 2856 2857let tempLower = new Temperature(30); 2858let tempUpper = new Temperature(40); 2859let tempMiDF = new Temperature(35); 2860let tempMidS = new Temperature(39); 2861let range = new util.ScopeHelper(tempLower, tempUpper); 2862let result = range.intersect(tempMiDF, tempMidS); 2863console.info("result = " + result); 2864// 输出结果:result = [35, 39] 2865``` 2866 2867### getUpper<sup>9+</sup> 2868 2869getUpper(): ScopeType 2870 2871获取当前范围的上限。 2872 2873**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2874 2875**系统能力:** SystemCapability.Utils.Lang 2876 2877**返回值:** 2878 2879| 类型 | 说明 | 2880| ------------------------ | ---------------------- | 2881| [ScopeType](#scopetype8) | 返回当前范围的上限值。 | 2882 2883**示例:** 2884 2885```ts 2886class Temperature implements util.ScopeComparable { 2887 private readonly _temp: number; 2888 2889 constructor(value: number) { 2890 this._temp = value; 2891 } 2892 2893 compareTo(value: Temperature) { 2894 return this._temp >= value.getTemp(); 2895 } 2896 2897 getTemp() { 2898 return this._temp; 2899 } 2900 2901 toString(): string { 2902 return this._temp.toString(); 2903 } 2904} 2905 2906let tempLower = new Temperature(30); 2907let tempUpper = new Temperature(40); 2908let range = new util.ScopeHelper(tempLower, tempUpper); 2909let result = range.getUpper(); 2910console.info("result = " + result); 2911// 输出结果:result = 40 2912``` 2913 2914### getLower<sup>9+</sup> 2915 2916getLower(): ScopeType 2917 2918获取当前范围的下限。 2919 2920**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2921 2922**系统能力:** SystemCapability.Utils.Lang 2923 2924**返回值:** 2925 2926| 类型 | 说明 | 2927| ------------------------ | ---------------------- | 2928| [ScopeType](#scopetype8) | 返回当前范围的下限值。 | 2929 2930**示例:** 2931 2932```ts 2933class Temperature implements util.ScopeComparable { 2934 private readonly _temp: number; 2935 2936 constructor(value: number) { 2937 this._temp = value; 2938 } 2939 2940 compareTo(value: Temperature) { 2941 return this._temp >= value.getTemp(); 2942 } 2943 2944 getTemp() { 2945 return this._temp; 2946 } 2947 2948 toString(): string { 2949 return this._temp.toString(); 2950 } 2951} 2952 2953let tempLower = new Temperature(30); 2954let tempUpper = new Temperature(40); 2955let range = new util.ScopeHelper(tempLower, tempUpper); 2956let result = range.getLower(); 2957console.info("result = " + result); 2958// 输出结果:result = 30 2959``` 2960 2961### expand<sup>9+</sup> 2962 2963expand(lowerObj: ScopeType,upperObj: ScopeType): ScopeHelper 2964 2965创建并返回当前范围与给定下限和上限的并集。 2966 2967**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2968 2969**系统能力:** SystemCapability.Utils.Lang 2970 2971**参数:** 2972 2973| 参数名 | 类型 | 必填 | 说明 | 2974| -------- | ------------------------ | ---- | ---------------- | 2975| lowerObj | [ScopeType](#scopetype8) | 是 | 给定范围的下限。 | 2976| upperObj | [ScopeType](#scopetype8) | 是 | 给定范围的上限。 | 2977 2978**返回值:** 2979 2980| 类型 | 说明 | 2981| ---------------------------- | ------------------------------------ | 2982| [ScopeHelper](#scopehelper9) | 返回当前范围和给定下限和上限的并集。 | 2983 2984**错误码:** 2985 2986以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 2987 2988| 错误码ID | 错误信息 | 2989| -------- | -------- | 2990| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 2991 2992**示例:** 2993 2994```ts 2995class Temperature implements util.ScopeComparable { 2996 private readonly _temp: number; 2997 2998 constructor(value: number) { 2999 this._temp = value; 3000 } 3001 3002 compareTo(value: Temperature) { 3003 return this._temp >= value.getTemp(); 3004 } 3005 3006 getTemp() { 3007 return this._temp; 3008 } 3009 3010 toString(): string { 3011 return this._temp.toString(); 3012 } 3013} 3014 3015let tempLower = new Temperature(30); 3016let tempUpper = new Temperature(40); 3017let tempMiDF = new Temperature(35); 3018let tempMidS = new Temperature(39); 3019let range = new util.ScopeHelper(tempLower, tempUpper); 3020let result = range.expand(tempMiDF, tempMidS); 3021console.info("result = " + result); 3022// 输出结果:result = [30, 40] 3023``` 3024 3025### expand<sup>9+</sup> 3026 3027expand(range: ScopeHelper): ScopeHelper 3028 3029创建并返回当前范围和给定范围的并集。 3030 3031**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3032 3033**系统能力:** SystemCapability.Utils.Lang 3034 3035**参数:** 3036 3037| 参数名 | 类型 | 必填 | 说明 | 3038| ------ | ---------------------------- | ---- | ------------------ | 3039| range | [ScopeHelper](#scopehelper9) | 是 | 传入一个给定范围。 | 3040 3041**返回值:** 3042 3043| 类型 | 说明 | 3044| ---------------------------- | ---------------------------------- | 3045| [ScopeHelper](#scopehelper9) | 返回包括当前范围和给定范围的并集。 | 3046 3047**错误码:** 3048 3049以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3050 3051| 错误码ID | 错误信息 | 3052| -------- | -------- | 3053| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3054 3055**示例:** 3056 3057```ts 3058class Temperature implements util.ScopeComparable { 3059 private readonly _temp: number; 3060 3061 constructor(value: number) { 3062 this._temp = value; 3063 } 3064 3065 compareTo(value: Temperature) { 3066 return this._temp >= value.getTemp(); 3067 } 3068 3069 getTemp() { 3070 return this._temp; 3071 } 3072 3073 toString(): string { 3074 return this._temp.toString(); 3075 } 3076} 3077 3078let tempLower = new Temperature(30); 3079let tempUpper = new Temperature(40); 3080let tempMiDF = new Temperature(35); 3081let tempMidS = new Temperature(39); 3082let range = new util.ScopeHelper(tempLower, tempUpper); 3083let rangeFir = new util.ScopeHelper(tempMiDF, tempMidS); 3084let result = range.expand(rangeFir); 3085console.info("result = " + result); 3086// 输出结果:result = [30, 40] 3087``` 3088 3089### expand<sup>9+</sup> 3090 3091expand(value: ScopeType): ScopeHelper 3092 3093创建并返回当前范围和给定值的并集。 3094 3095**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3096 3097**系统能力:** SystemCapability.Utils.Lang 3098 3099**参数:** 3100 3101| 参数名 | 类型 | 必填 | 说明 | 3102| ------ | ------------------------ | ---- | ---------------- | 3103| value | [ScopeType](#scopetype8) | 是 | 传入一个给定值。 | 3104 3105**返回值:** 3106 3107| 类型 | 说明 | 3108| ---------------------------- | -------------------------------- | 3109| [ScopeHelper](#scopehelper9) | 返回包括当前范围和给定值的并集。 | 3110 3111**错误码:** 3112 3113以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3114 3115| 错误码ID | 错误信息 | 3116| -------- | -------- | 3117| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3118 3119**示例:** 3120 3121```ts 3122class Temperature implements util.ScopeComparable { 3123 private readonly _temp: number; 3124 3125 constructor(value: number) { 3126 this._temp = value; 3127 } 3128 3129 compareTo(value: Temperature) { 3130 return this._temp >= value.getTemp(); 3131 } 3132 3133 getTemp() { 3134 return this._temp; 3135 } 3136 3137 toString(): string { 3138 return this._temp.toString(); 3139 } 3140} 3141 3142let tempLower = new Temperature(30); 3143let tempUpper = new Temperature(40); 3144let tempMiDF = new Temperature(35); 3145let range = new util.ScopeHelper(tempLower, tempUpper); 3146let result = range.expand(tempMiDF); 3147console.info("result = " + result); 3148// 输出结果:result = [30, 40] 3149``` 3150 3151### contains<sup>9+</sup> 3152 3153contains(value: ScopeType): boolean 3154 3155检查给定value是否在当前范围内。 3156 3157**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3158 3159**系统能力:** SystemCapability.Utils.Lang 3160 3161**参数:** 3162 3163| 参数名 | 类型 | 必填 | 说明 | 3164| ------ | ------------------------ | ---- | ---------------- | 3165| value | [ScopeType](#scopetype8) | 是 | 传入一个给定值。 | 3166 3167**返回值:** 3168 3169| 类型 | 说明 | 3170| ------- | --------------------------------------------------- | 3171| boolean | 如果给定值包含在当前范围内返回true,否则返回false。 | 3172 3173**错误码:** 3174 3175以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3176 3177| 错误码ID | 错误信息 | 3178| -------- | -------- | 3179| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3180 3181**示例:** 3182 3183```ts 3184class Temperature implements util.ScopeComparable { 3185 private readonly _temp: number; 3186 3187 constructor(value: number) { 3188 this._temp = value; 3189 } 3190 3191 compareTo(value: Temperature) { 3192 return this._temp >= value.getTemp(); 3193 } 3194 3195 getTemp() { 3196 return this._temp; 3197 } 3198 3199 toString(): string { 3200 return this._temp.toString(); 3201 } 3202} 3203 3204let tempLower = new Temperature(30); 3205let tempUpper = new Temperature(40); 3206let tempMiDF = new Temperature(35); 3207let range = new util.ScopeHelper(tempLower, tempUpper); 3208let result = range.contains(tempMiDF); 3209console.info("result = " + result); 3210// 输出结果:result = true 3211``` 3212 3213### contains<sup>9+</sup> 3214 3215contains(range: ScopeHelper): boolean 3216 3217检查给定range是否在当前范围内。 3218 3219**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3220 3221**系统能力:** SystemCapability.Utils.Lang 3222 3223**参数:** 3224 3225| 参数名 | 类型 | 必填 | 说明 | 3226| ------ | ---------------------------- | ---- | ------------------ | 3227| range | [ScopeHelper](#scopehelper9) | 是 | 传入一个给定范围。 | 3228 3229**返回值:** 3230 3231| 类型 | 说明 | 3232| ------- | ----------------------------------------------------- | 3233| boolean | 如果给定范围在当前范围内则返回true,否则返回false。 | 3234 3235**错误码:** 3236 3237以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3238 3239| 错误码ID | 错误信息 | 3240| -------- | -------- | 3241| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3242 3243**示例:** 3244 3245```ts 3246class Temperature implements util.ScopeComparable { 3247 private readonly _temp: number; 3248 3249 constructor(value: number) { 3250 this._temp = value; 3251 } 3252 3253 compareTo(value: Temperature) { 3254 return this._temp >= value.getTemp(); 3255 } 3256 3257 getTemp() { 3258 return this._temp; 3259 } 3260 3261 toString(): string { 3262 return this._temp.toString(); 3263 } 3264} 3265 3266let tempLower = new Temperature(30); 3267let tempUpper = new Temperature(40); 3268let range = new util.ScopeHelper(tempLower, tempUpper); 3269let tempLess = new Temperature(20); 3270let tempMore = new Temperature(45); 3271let rangeSec = new util.ScopeHelper(tempLess, tempMore); 3272let result = range.contains(rangeSec); 3273console.info("result = " + result); 3274// 输出结果:result = false 3275``` 3276 3277### clamp<sup>9+</sup> 3278 3279clamp(value: ScopeType): ScopeType 3280 3281将值限定到当前范围内。 3282 3283**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3284 3285**系统能力:** SystemCapability.Utils.Lang 3286 3287**参数:** 3288 3289| 参数名 | 类型 | 必填 | 说明 | 3290| ------ | ------------------------ | ---- | -------------- | 3291| value | [ScopeType](#scopetype8) | 是 | 传入的给定值。 | 3292 3293**返回值:** 3294 3295| 类型 | 说明 | 3296| ------------------------ | ------------------------------------------------------------ | 3297| [ScopeType](#scopetype8) | 如果传入的value小于下限,返回lowerObj;如果大于上限值,返回upperObj;如果在当前范围内,返回value。 | 3298 3299**错误码:** 3300 3301以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3302 3303| 错误码ID | 错误信息 | 3304| -------- | -------- | 3305| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3306 3307**示例:** 3308 3309```ts 3310class Temperature implements util.ScopeComparable { 3311 private readonly _temp: number; 3312 3313 constructor(value: number) { 3314 this._temp = value; 3315 } 3316 3317 compareTo(value: Temperature) { 3318 return this._temp >= value.getTemp(); 3319 } 3320 3321 getTemp() { 3322 return this._temp; 3323 } 3324 3325 toString(): string { 3326 return this._temp.toString(); 3327 } 3328} 3329 3330let tempLower = new Temperature(30); 3331let tempUpper = new Temperature(40); 3332let tempMiDF = new Temperature(35); 3333let range = new util.ScopeHelper(tempLower, tempUpper); 3334let result = range.clamp(tempMiDF); 3335console.info("result = " + result); 3336// 输出结果:result = 35 3337``` 3338 3339## Base64Helper<sup>9+</sup> 3340 3341Base64Helper类提供Base64编解码和Base64URL编解码功能。Base64编码表包含A-Z、a-z、0-9这62个字符,以及"+"和"/"这两个特殊字符。在编码时,将原始数据按3个字节一组进行划分,得到若干个6位的数字,然后使用Base64编码表中对应的字符来表示这些数字。如果最后剩余1或2个字节,则需要使用"="字符进行补齐。Base64URL编码表包含A-Z、a-z、0-9以及"-"和"_"64个字符,Base64URL编码结果不含"="。 3342 3343### constructor<sup>9+</sup> 3344 3345constructor() 3346 3347Base64Helper的构造函数。 3348 3349**系统能力:** SystemCapability.Utils.Lang 3350 3351**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3352 3353**示例:** 3354 3355 ```ts 3356 let base64 = new util.Base64Helper(); 3357 ``` 3358 3359### encodeSync<sup>9+</sup> 3360 3361encodeSync(src: Uint8Array, options?: Type): Uint8Array 3362 3363通过输入参数编码后输出Uint8Array对象。 3364 3365**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3366 3367**系统能力:** SystemCapability.Utils.Lang 3368 3369**参数:** 3370 3371| 参数名 | 类型 | 必填 | 说明 | 3372| ------ | ---------- | ---- | ------------------- | 3373| src | Uint8Array | 是 | 待编码的Uint8Array对象。 | 3374| 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编码。 | 3375 3376**返回值:** 3377 3378| 类型 | 说明 | 3379| ---------- | ----------------------------- | 3380| Uint8Array | 返回编码后的Uint8Array对象。 | 3381 3382**错误码:** 3383 3384以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3385 3386| 错误码ID | 错误信息 | 3387| -------- | -------- | 3388| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3389 3390**示例:** 3391 3392 ```ts 3393 let base64Helper = new util.Base64Helper(); 3394 let array = new Uint8Array([115,49,51]); 3395 let result = base64Helper.encodeSync(array); 3396 console.info("result = " + result); 3397 // 输出结果:result = 99,122,69,122 3398 ``` 3399 3400 3401### encodeToStringSync<sup>9+</sup> 3402 3403encodeToStringSync(src: Uint8Array, options?: Type): string 3404 3405通过输入参数编码后输出对应文本。 3406 3407**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3408 3409**系统能力:** SystemCapability.Utils.Lang 3410 3411**参数:** 3412 3413| 参数名 | 类型 | 必填 | 说明 | 3414| ------ | ---------- | ---- | ------------------- | 3415| src | Uint8Array | 是 | 待编码Uint8Array对象。 | 3416| 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个字符,则会在每76个字符处进行换行,并以'\r\n'结束每行。如果返回值少于76个字符,则会抛出异常。<br/>- 当参数取值为util.Type.BASIC_URL_SAFE,表示Base64URL编码,返回值没有回车符、换行符。<br/>- 当参数取值为util.Type.MIME_URL_SAFE,表示Base64URL编码,返回值每一行不超过76个字符,而且每行以'\r\n'符结束。 | 3417 3418**返回值:** 3419 3420| 类型 | 说明 | 3421| ------ | -------------------- | 3422| string | 返回编码后的字符串。 | 3423 3424**错误码:** 3425 3426以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3427 3428| 错误码ID | 错误信息 | 3429| -------- | -------- | 3430| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3431 3432**示例:** 3433 3434 ```ts 3435 let base64Helper = new util.Base64Helper(); 3436 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]); 3437 let result = base64Helper.encodeToStringSync(array, util.Type.MIME); 3438 console.info("result = " + result); 3439 /* 3440 输出结果:result = TWFuaXNkaXN0aW5ndWlzaGVkbm90b25seWJ5aGlzcmVhc29uYnV0Ynl0aGlzc2luZ3VsYXJwYXNz 3441 aW9uZnJvbW90aGVyYW5pbWFsc3doaWNoaXNhbHVzdG9mdGhlbWluZGV4Y2VlZHN0aGVzaG9ydHZl 3442 aGVtZW5jZW9mYW55Y2FybmFscGxlYXN1cmU= 3443 */ 3444 ``` 3445 3446 3447### decodeSync<sup>9+</sup> 3448 3449decodeSync(src: Uint8Array | string, options?: Type): Uint8Array 3450 3451将输入参数解码后输出对应Uint8Array对象。 3452 3453**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3454 3455**系统能力:** SystemCapability.Utils.Lang 3456 3457**参数:** 3458 3459| 参数名 | 类型 | 必填 | 说明 | 3460| ------ | ------------------------------ | ---- | ----------------------------- | 3461| src | Uint8Array \| string | 是 | 待解码的Uint8Array对象或者字符串。 | 3462| 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入参包含回车符、换行符。 | 3463 3464**返回值:** 3465 3466| 类型 | 说明 | 3467| ---------- | ----------------------------- | 3468| Uint8Array | 返回解码后新分配的Uint8Array对象。 | 3469 3470**错误码:** 3471 3472以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3473 3474| 错误码ID | 错误信息 | 3475| -------- | -------- | 3476| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3477 3478**示例:** 3479 3480 ```ts 3481 let base64Helper = new util.Base64Helper(); 3482 let buff = 'TWFuaXNkaXN0aW5ndWlzaGVkbm90b25seWJ5aGlzcmVhc29uYnV0Ynl0aGlzc2luZ3VsYXJwYXNz\r\naW9uZnJvbW90aGVyYW5pbWFsc3doaWNoaXNhbHVzdG9mdGhlbWluZGV4Y2VlZHN0aGVzaG9ydHZl\r\naGVtZW5jZW9mYW55Y2FybmFscGxlYXN1cmU=\r\n'; 3483 let result = base64Helper.decodeSync(buff, util.Type.MIME); 3484 console.info("result = " + result); 3485 /* 3486 输出结果: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 3487 */ 3488 ``` 3489 3490 3491### encode<sup>9+</sup> 3492 3493encode(src: Uint8Array, options?: Type): Promise<Uint8Array> 3494 3495将输入参数异步编码后输出对应Uint8Array对象。 3496 3497**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 3498 3499**系统能力:** SystemCapability.Utils.Lang 3500 3501**参数:** 3502 3503| 参数名 | 类型 | 必填 | 说明 | 3504| ------ | ---------- | ---- | ----------------------- | 3505| src | Uint8Array | 是 | 待编码Uint8Array对象。 | 3506| 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编码。 | 3507 3508**返回值:** 3509 3510| 类型 | 说明 | 3511| ------------------------- | --------------------------------- | 3512| Promise<Uint8Array> | 返回异步编码后新分配的Uint8Array对象。 | 3513 3514**错误码:** 3515 3516以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3517 3518| 错误码ID | 错误信息 | 3519| -------- | -------- | 3520| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3521 3522**示例:** 3523 3524 ```ts 3525 let base64Helper = new util.Base64Helper(); 3526 let array = new Uint8Array([115,49,51]); 3527 base64Helper.encode(array).then((val) => { 3528 console.info(val.toString()); 3529 // 输出结果:99,122,69,122 3530 }) 3531 ``` 3532 3533 3534### encodeToString<sup>9+</sup> 3535 3536encodeToString(src: Uint8Array, options?: Type): Promise<string> 3537 3538将输入参数异步编码后输出对应文本。 3539 3540**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3541 3542**系统能力:** SystemCapability.Utils.Lang 3543 3544**参数:** 3545 3546| 参数名 | 类型 | 必填 | 说明 | 3547| ------ | ---------- | ---- | ----------------------- | 3548| src | Uint8Array | 是 | 异步编码输入Uint8Array对象。 | 3549| 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'符结束。 | 3550 3551**返回值:** 3552 3553| 类型 | 说明 | 3554| --------------------- | ------------------------ | 3555| Promise<string> | 返回异步编码后的字符串。 | 3556 3557**错误码:** 3558 3559以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3560 3561| 错误码ID | 错误信息 | 3562| -------- | -------- | 3563| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3564 3565**示例:** 3566 3567 ```ts 3568 let base64Helper = new util.Base64Helper(); 3569 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]); 3570 base64Helper.encodeToString(array, util.Type.MIME).then((val) => { 3571 console.info(val); 3572 /* 3573 输出结果:TWFuaXNkaXN0aW5ndWlzaGVkbm90b25seWJ5aGlzcmVhc29uYnV0Ynl0aGlzc2luZ3VsYXJwYXNz 3574 aW9uZnJvbW90aGVyYW5pbWFsc3doaWNoaXNhbHVzdG9mdGhlbWluZGV4Y2VlZHN0aGVzaG9ydHZl 3575 aGVtZW5jZW9mYW55Y2FybmFscGxlYXN1cmU= 3576 */ 3577 3578 }) 3579 ``` 3580 3581 3582### decode<sup>9+</sup> 3583 3584decode(src: Uint8Array | string, options?: Type): Promise<Uint8Array> 3585 3586将输入参数异步解码后输出对应Uint8Array对象。 3587 3588**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3589 3590**系统能力:** SystemCapability.Utils.Lang 3591 3592**参数:** 3593 3594| 参数名 | 类型 | 必填 | 说明 | 3595| ------ | ------------------------------ | ---- | --------------------------------- | 3596| src | Uint8Array \| string | 是 | 异步解码输入Uint8Array对象或者字符串。 | 3597| 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入参包含回车符、换行符。 | 3598 3599**返回值:** 3600 3601| 类型 | 说明 | 3602| ------------------------- | --------------------------------- | 3603| Promise<Uint8Array> | 返回异步解码后新分配的Uint8Array对象。 | 3604 3605**错误码:** 3606 3607以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3608 3609| 错误码ID | 错误信息 | 3610| -------- | -------- | 3611| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3612 3613**示例:** 3614 3615 ```ts 3616 let base64Helper = new util.Base64Helper(); 3617 let array = 'TWFuaXNkaXN0aW5ndWlzaGVkbm90b25seWJ5aGlzcmVhc29uYnV0Ynl0aGlzc2luZ3VsYXJwYXNz\r\naW9uZnJvbW90aGVyYW5pbWFsc3doaWNoaXNhbHVzdG9mdGhlbWluZGV4Y2VlZHN0aGVzaG9ydHZl\r\naGVtZW5jZW9mYW55Y2FybmFscGxlYXN1cmU=\r\n'; 3618 base64Helper.decode(array, util.Type.MIME).then((val) => { 3619 console.info(val.toString()); 3620 /* 3621 输出结果: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 3622 */ 3623 }) 3624 ``` 3625 3626## StringDecoder<sup>12+</sup> 3627 3628提供将二进制流解码为字符串的能力。支持的编码类型包括:utf-8、iso-8859-2、koi8-r、macintosh、windows-1250、windows-1251、gbk、gb18030、big5、utf-16be、utf-16le等。 3629 3630### constructor<sup>12+</sup> 3631 3632constructor(encoding?: string) 3633 3634StringDecoder的构造函数。 3635 3636**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3637 3638**系统能力:** SystemCapability.Utils.Lang 3639 3640**参数:** 3641 3642| 参数名 | 类型 | 必填 | 说明 | 3643| ------ | ------------------------------ | ---- | --------------------------------- | 3644| encoding | string | 否 | 输入数据的编码类型。默认值:'utf-8'。 | 3645 3646**错误码:** 3647 3648以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3649 3650| 错误码ID | 错误信息 | 3651| -------- | -------- | 3652| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3653 3654**示例:** 3655 3656 ```ts 3657 let decoder = new util.StringDecoder(); 3658 ``` 3659 3660### write<sup>12+</sup> 3661 3662write(chunk: string | Uint8Array): string 3663 3664返回一个解码后的字符串,确保Uint8Array末尾的任何不完整的多字节字符从返回的字符串中被过滤,并保存在一个内部的buffer中用于下次调用。 3665 3666**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3667 3668**系统能力:** SystemCapability.Utils.Lang 3669 3670**参数:** 3671 3672| 参数名 | 类型 | 必填 | 说明 | 3673| ------ | ---------- | ---- | ------------------- | 3674| chunk | string \| Uint8Array | 是 | 需要解码的字符串。会根据输入的编码类型进行解码,参数为Uint8Array时正常解码,参数为string时会将参数直接返回。 | 3675 3676**返回值:** 3677 3678| 类型 | 说明 | 3679| ---------- | ----------------------------- | 3680| string | 返回解码后的字符串。 | 3681 3682**错误码:** 3683 3684以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3685 3686| 错误码ID | 错误信息 | 3687| -------- | -------- | 3688| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3689 3690**示例:** 3691 3692 ```ts 3693 let decoder = new util.StringDecoder('utf-8'); 3694 let input = new Uint8Array([0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD]); 3695 const decoded = decoder.write(input); 3696 console.info("decoded:", decoded); 3697 // 输出结果:decoded: 你好 3698 ``` 3699 3700### end<sup>12+</sup> 3701 3702end(chunk?: string | Uint8Array): string 3703 3704结束解码过程,以字符串形式返回存储在内部缓冲区中的所有剩余输入。 3705 3706**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 3707 3708**系统能力:** SystemCapability.Utils.Lang 3709 3710**参数:** 3711 3712| 参数名 | 类型 | 必填 | 说明 | 3713| ------ | ---------- | ---- | ------------------- | 3714| chunk | string \| Uint8Array | 否 | 需要解码的字符串。默认为undefined。 | 3715 3716**返回值:** 3717 3718| 类型 | 说明 | 3719| ---------- | ----------------------------- | 3720| string | 返回解码后的字符串。 | 3721 3722**错误码:** 3723 3724以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 3725 3726| 错误码ID | 错误信息 | 3727| -------- | -------- | 3728| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 3729 3730**示例:** 3731 3732 ```ts 3733 let decoder = new util.StringDecoder('utf-8'); 3734 let input = new Uint8Array([0xE4, 0xBD, 0xA0, 0xE5, 0xA5, 0xBD]); 3735 const writeString = decoder.write(input.slice(0, 5)); 3736 const endString = decoder.end(input.slice(5)); 3737 console.info("writeString:", writeString); 3738 // 输出结果:writeString: 你 3739 console.info("endString:", endString); 3740 // 输出结果:endString: 好 3741 ``` 3742 3743## Type<sup>10+</sup> 3744 3745Base64编码格式枚举。 3746 3747**系统能力:** SystemCapability.Utils.Lang 3748 3749 3750| 名称 |值| 说明 | 3751| ----- |---| ----------------- | 3752| BASIC | 0 | 表示BASIC编码格式。**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。| 3753| MIME | 1 | 表示MIME编码格式。**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。| 3754| BASIC_URL_SAFE<sup>12+</sup> | 2 | 表示BASIC_URL_SAFE编码格式。<br/>从API version 12开始支持此枚举。**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。| 3755| MIME_URL_SAFE<sup>12+</sup> | 3 | 表示MIME_URL_SAFE编码格式。<br/>从API version 12开始支持此枚举。**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 | 3756 3757 3758## types<sup>8+</sup> 3759 3760types为不同类型的内置对象提供类型检查,可以避免由于类型错误导致的异常。该模块包含了多个工具函数,用于判断JS对象是否属于各种类型,例如:ArrayBuffer、Map、Set等。 3761 3762### constructor<sup>8+</sup> 3763 3764constructor() 3765 3766Types的构造函数。 3767 3768**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3769 3770**系统能力:** SystemCapability.Utils.Lang 3771 3772**示例:** 3773 3774 ```ts 3775 let type = new util.types(); 3776 ``` 3777 3778 3779### isAnyArrayBuffer<sup>8+</sup> 3780 3781isAnyArrayBuffer(value: Object): boolean 3782 3783检查value是否为ArrayBuffer或SharedArrayBuffer类型。 3784 3785**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3786 3787**系统能力:** SystemCapability.Utils.Lang 3788 3789**参数:** 3790 3791| 参数名 | 类型 | 必填 | 说明 | 3792| -------- | -------- | -------- | -------- | 3793| value | Object | 是 | 待检测对象。 | 3794 3795**返回值:** 3796 3797| 类型 | 说明 | 3798| -------- | -------- | 3799| boolean | 如果是ArrayBuffer或SharedArrayBuffer类型则返回true,否则返回false。 | 3800 3801**示例:** 3802 3803 ```ts 3804 let type = new util.types(); 3805 let result = type.isAnyArrayBuffer(new ArrayBuffer(0)); 3806 console.info("result = " + result); 3807 // 输出结果:result = true 3808 ``` 3809 3810 3811### isArrayBufferView<sup>8+</sup> 3812 3813isArrayBufferView(value: Object): boolean 3814 3815检查value是否为ArrayBufferView类型。 3816 3817ArrayBufferView类型包括:Int8Array、Int16Array、Int32Array、Uint8Array、Uint8ClampedArray、Uint32Array、Float32Array、Float64Array、DataView。 3818 3819**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3820 3821**系统能力:** SystemCapability.Utils.Lang 3822 3823**参数:** 3824 3825| 参数名 | 类型 | 必填 | 说明 | 3826| -------- | -------- | -------- | -------- | 3827| value | Object | 是 | 待检测对象。 | 3828 3829**返回值:** 3830 3831| 类型 | 说明 | 3832| -------- | -------- | 3833| boolean | 如果是ArrayBufferView类型则返回true,否则返回false。 | 3834 3835**示例:** 3836 3837 ```ts 3838 let type = new util.types(); 3839 let result = type.isArrayBufferView(new Int8Array([])); 3840 console.info("result = " + result); 3841 // 输出结果:result = true 3842 ``` 3843 3844 3845### isArgumentsObject<sup>8+</sup> 3846 3847isArgumentsObject(value: Object): boolean 3848 3849检查value是否为arguments对象。 3850 3851**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3852 3853**系统能力:** SystemCapability.Utils.Lang 3854 3855**参数:** 3856 3857| 参数名 | 类型 | 必填 | 说明 | 3858| -------- | -------- | -------- | -------- | 3859| value | Object | 是 | 待检测对象。 | 3860 3861**返回值:** 3862 3863| 类型 | 说明 | 3864| -------- | -------- | 3865| boolean | 如果是arguments对象则返回true,否则返回false。 | 3866 3867**示例:** 3868 3869 ```ts 3870 let type = new util.types(); 3871 function foo() { 3872 let result = type.isArgumentsObject(arguments); 3873 console.info("result = " + result); 3874 } 3875 let f = foo(); 3876 // 输出结果:result = true 3877 ``` 3878 3879 3880### isArrayBuffer<sup>8+</sup> 3881 3882isArrayBuffer(value: Object): boolean 3883 3884检查value是否为ArrayBuffer类型。 3885 3886**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3887 3888**系统能力:** SystemCapability.Utils.Lang 3889 3890**参数:** 3891 3892| 参数名 | 类型 | 必填 | 说明 | 3893| -------- | -------- | -------- | -------- | 3894| value | Object | 是 | 待检测对象。 | 3895 3896**返回值:** 3897 3898| 类型 | 说明 | 3899| -------- | -------- | 3900| boolean | 如果是ArrayBuffer类型则返回true,否则返回false。 | 3901 3902**示例:** 3903 3904 ```ts 3905 let type = new util.types(); 3906 let result = type.isArrayBuffer(new ArrayBuffer(0)); 3907 console.info("result = " + result); 3908 // 输出结果:result = true 3909 ``` 3910 3911 3912### isAsyncFunction<sup>8+</sup> 3913 3914isAsyncFunction(value: Object): boolean 3915 3916检查value是否为异步函数类型。 3917 3918**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3919 3920**系统能力:** SystemCapability.Utils.Lang 3921 3922**参数:** 3923 3924| 参数名 | 类型 | 必填 | 说明 | 3925| -------- | -------- | -------- | -------- | 3926| value | Object | 是 | 待检测对象。 | 3927 3928**返回值:** 3929 3930| 类型 | 说明 | 3931| -------- | -------- | 3932| boolean | 如果是异步函数则返回true,否则返回false。 | 3933 3934**示例:** 3935 3936 ```ts 3937 let type = new util.types(); 3938 let result = type.isAsyncFunction(async () => {}); 3939 console.info("result = " + result); 3940 // 输出结果:result = true 3941 ``` 3942 3943 3944### isBooleanObject<sup>(deprecated)</sup> 3945 3946isBooleanObject(value: Object): boolean 3947 3948检查value是否为Boolean对象。 3949 3950> **说明:** 3951> 3952> 从API version 8开始支持,从API version 14开始废弃,没有替代接口。 3953 3954**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3955 3956**系统能力:** SystemCapability.Utils.Lang 3957 3958**参数:** 3959 3960| 参数名 | 类型 | 必填 | 说明 | 3961| -------- | -------- | -------- | -------- | 3962| value | Object | 是 | 待检测对象。 | 3963 3964**返回值:** 3965 3966| 类型 | 说明 | 3967| -------- | -------- | 3968| boolean | 如果是Boolean对象则返回true,否则返回false。 | 3969 3970**示例:** 3971 3972 ```ts 3973 let type = new util.types(); 3974 let result = type.isBooleanObject(new Boolean(true)); 3975 console.info("result = " + result); 3976 // 输出结果:result = true 3977 ``` 3978 3979 3980### isBoxedPrimitive<sup>(deprecated)</sup> 3981 3982isBoxedPrimitive(value: Object): boolean 3983 3984检查value是否为Boolean、Number、String或Symbol对象类型。 3985 3986> **说明:** 3987> 3988> 从API version 8开始支持,从API version 14开始废弃,没有替代接口。 3989 3990**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3991 3992**系统能力:** SystemCapability.Utils.Lang 3993 3994**参数:** 3995 3996| 参数名 | 类型 | 必填 | 说明 | 3997| -------- | -------- | -------- | -------- | 3998| value | Object | 是 | 待检测对象。 | 3999 4000**返回值:** 4001 4002| 类型 | 说明 | 4003| -------- | -------- | 4004| boolean | 如果是Boolean、Number、String或Symbol对象则返回true,否则返回false。 | 4005 4006**示例:** 4007 4008 ```ts 4009 let type = new util.types(); 4010 let result = type.isBoxedPrimitive(new Boolean(false)); 4011 console.info("result = " + result); 4012 // 输出结果:result = true 4013 ``` 4014 4015 4016### isDataView<sup>8+</sup> 4017 4018isDataView(value: Object): boolean 4019 4020检查value是否为DataView类型。 4021 4022**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4023 4024**系统能力:** SystemCapability.Utils.Lang 4025 4026**参数:** 4027 4028| 参数名 | 类型 | 必填 | 说明 | 4029| -------- | -------- | -------- | -------- | 4030| value | Object | 是 | 待检测对象。 | 4031 4032**返回值:** 4033 4034| 类型 | 说明 | 4035| -------- | -------- | 4036| boolean | 如果是DataView类型则返回true,否则返回false。 | 4037 4038**示例:** 4039 4040 ```ts 4041 let type = new util.types(); 4042 const ab = new ArrayBuffer(20); 4043 let result = type.isDataView(new DataView(ab)); 4044 console.info("result = " + result); 4045 // 输出结果:result = true 4046 ``` 4047 4048 4049### isDate<sup>8+</sup> 4050 4051isDate(value: Object): boolean 4052 4053检查value是否为Date类型。 4054 4055**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4056 4057**系统能力:** SystemCapability.Utils.Lang 4058 4059**参数:** 4060 4061| 参数名 | 类型 | 必填 | 说明 | 4062| -------- | -------- | -------- | -------- | 4063| value | Object | 是 | 待检测对象。 | 4064 4065**返回值:** 4066 4067| 类型 | 说明 | 4068| -------- | -------- | 4069| boolean | 如果是Date类型则返回true,否则返回false。 | 4070 4071**示例:** 4072 4073 ```ts 4074 let type = new util.types(); 4075 let result = type.isDate(new Date()); 4076 console.info("result = " + result); 4077 // 输出结果:result = true 4078 ``` 4079 4080 4081### isExternal<sup>8+</sup> 4082 4083isExternal(value: Object): boolean 4084 4085检查value是否为native External类型。 4086 4087**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4088 4089**系统能力:** SystemCapability.Utils.Lang 4090 4091**参数:** 4092 4093| 参数名 | 类型 | 必填 | 说明 | 4094| -------- | -------- | -------- | -------- | 4095| value | Object | 是 | 待检测对象。 | 4096 4097**返回值:** 4098 4099| 类型 | 说明 | 4100| -------- | -------- | 4101| boolean | 如果是native External类型则返回true,否则返回false。 | 4102 4103**示例:** 4104 4105 ```cpp 4106 // /entry/src/main/cpp/napi_init.cpp 4107 #include "napi/native_api.h" 4108 #include <js_native_api.h> 4109 #include <stdlib.h> 4110 4111 napi_value result; 4112 static napi_value Testexternal(napi_env env, napi_callback_info info) { 4113 int* raw = (int*) malloc(1024); 4114 napi_status status = napi_create_external(env, (void*) raw, NULL, NULL, &result); 4115 if (status != napi_ok) { 4116 napi_throw_error(env, NULL, "create external failed"); 4117 return NULL; 4118 } 4119 return result; 4120 } 4121 4122 EXTERN_C_START 4123 static napi_value Init(napi_env env, napi_value exports) 4124 { 4125 napi_property_descriptor desc[] = { 4126 {"testexternal", nullptr, Testexternal, nullptr, nullptr, nullptr, napi_default, nullptr}, 4127 }; 4128 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 4129 return exports; 4130 } 4131 EXTERN_C_END 4132 // 此处已省略模块注册的代码, 你可能需要自行注册Testexternal方法 4133 ... 4134 4135 ``` 4136 4137 <!--code_no_check--> 4138 ```ts 4139 import testNapi from 'libentry.so'; 4140 4141 let type = new util.types(); 4142 const data = testNapi.testexternal(); 4143 let result = type.isExternal(data); 4144 4145 let result01 = type.isExternal(true); 4146 console.info("result = " + result); 4147 console.info("result01 = " + result01); 4148 // 输出结果:result = true 4149 // 输出结果:result01 = false 4150 ``` 4151 4152 4153### isFloat32Array<sup>8+</sup> 4154 4155isFloat32Array(value: Object): boolean 4156 4157检查value是否为Float32Array数组类型。 4158 4159**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4160 4161**系统能力:** SystemCapability.Utils.Lang 4162 4163**参数:** 4164 4165| 参数名 | 类型 | 必填 | 说明 | 4166| -------- | -------- | -------- | -------- | 4167| value | Object | 是 | 待检测对象。 | 4168 4169**返回值:** 4170 4171| 类型 | 说明 | 4172| -------- | -------- | 4173| boolean | 如果是Float32Array数组类型则返回true,否则返回false。 | 4174 4175**示例:** 4176 4177 ```ts 4178 let type = new util.types(); 4179 let result = type.isFloat32Array(new Float32Array()); 4180 console.info("result = " + result); 4181 // 输出结果:result = true 4182 ``` 4183 4184 4185### isFloat64Array<sup>8+</sup> 4186 4187isFloat64Array(value: Object): boolean 4188 4189检查value是否为Float64Array数组类型。 4190 4191**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4192 4193**系统能力:** SystemCapability.Utils.Lang 4194 4195**参数:** 4196 4197| 参数名 | 类型 | 必填 | 说明 | 4198| -------- | -------- | -------- | -------- | 4199| value | Object | 是 | 待检测对象。 | 4200 4201**返回值:** 4202 4203| 类型 | 说明 | 4204| -------- | -------- | 4205| boolean | 如果是Float64Array数组类型则返回true,否则返回false。 | 4206 4207**示例:** 4208 4209 ```ts 4210 let type = new util.types(); 4211 let result = type.isFloat64Array(new Float64Array()); 4212 console.info("result = " + result); 4213 // 输出结果:result = true 4214 ``` 4215 4216 4217### isGeneratorFunction<sup>8+</sup> 4218 4219isGeneratorFunction(value: Object): boolean 4220 4221检查value是否是为generator函数类型。 4222 4223**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4224 4225**系统能力:** SystemCapability.Utils.Lang 4226 4227**参数:** 4228 4229| 参数名 | 类型 | 必填 | 说明 | 4230| -------- | -------- | -------- | -------- | 4231| value | Object | 是 | 待检测对象。 | 4232 4233**返回值:** 4234 4235| 类型 | 说明 | 4236| -------- | -------- | 4237| boolean | 如果是generator函数类型则返回true,否则返回false。 | 4238 4239**示例:** 4240<!--code_no_check--> 4241 ```ts 4242 // /entry/src/main/ets/pages/test.ts 4243 export function* foo() {} 4244 ``` 4245 4246 <!--code_no_check--> 4247 ```ts 4248 import { foo } from './test' 4249 4250 let type = new util.types(); 4251 let result = type.isGeneratorFunction(foo); 4252 console.info("result = " + result); 4253 // 输出结果:result = true 4254 ``` 4255 4256 4257### isGeneratorObject<sup>8+</sup> 4258 4259isGeneratorObject(value: Object): boolean 4260 4261检查value是否为generator对象类型。 4262 4263**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4264 4265**系统能力:** SystemCapability.Utils.Lang 4266 4267**参数:** 4268 4269| 参数名 | 类型 | 必填 | 说明 | 4270| -------- | -------- | -------- | -------- | 4271| value | Object | 是 | 待检测对象。 | 4272 4273**返回值:** 4274 4275| 类型 | 说明 | 4276| -------- | -------- | 4277| boolean | 如果是generator对象则返回true,否则返回false。 | 4278 4279**示例:** 4280<!--code_no_check--> 4281 ```ts 4282 // /entry/src/main/ets/pages/test.ts 4283 function* foo() {} 4284 export const generator = foo(); 4285 ``` 4286 4287 <!--code_no_check--> 4288 ```ts 4289 import { generator } from './test' 4290 4291 let type = new util.types(); 4292 let result = type.isGeneratorObject(generator); 4293 console.info("result = " + result); 4294 // 输出结果:result = true 4295 ``` 4296 4297 4298### isInt8Array<sup>8+</sup> 4299 4300isInt8Array(value: Object): boolean 4301 4302检查value是否为Int8Array数组类型。 4303 4304**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4305 4306**系统能力:** SystemCapability.Utils.Lang 4307 4308**参数:** 4309 4310| 参数名 | 类型 | 必填 | 说明 | 4311| -------- | -------- | -------- | -------- | 4312| value | Object | 是 | 待检测对象。 | 4313 4314**返回值:** 4315 4316| 类型 | 说明 | 4317| -------- | -------- | 4318| boolean | 如果是Int8Array数组类型则返回true,否则返回false。 | 4319 4320**示例:** 4321 4322 ```ts 4323 let type = new util.types(); 4324 let result = type.isInt8Array(new Int8Array([])); 4325 console.info("result = " + result); 4326 // 输出结果:result = true 4327 ``` 4328 4329 4330### isInt16Array<sup>8+</sup> 4331 4332isInt16Array(value: Object): boolean 4333 4334检查value是否为Int16Array数组类型。 4335 4336**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4337 4338**系统能力:** SystemCapability.Utils.Lang 4339 4340**参数:** 4341 4342| 参数名 | 类型 | 必填 | 说明 | 4343| -------- | -------- | -------- | -------- | 4344| value | Object | 是 | 待检测对象。 | 4345 4346**返回值:** 4347 4348| 类型 | 说明 | 4349| -------- | -------- | 4350| boolean | 如果是Int16Array数组类型则返回true,否则返回false。 | 4351 4352**示例:** 4353 4354 ```ts 4355 let type = new util.types(); 4356 let result = type.isInt16Array(new Int16Array([])); 4357 console.info("result = " + result); 4358 // 输出结果:result = true 4359 ``` 4360 4361 4362### isInt32Array<sup>8+</sup> 4363 4364isInt32Array(value: Object): boolean 4365 4366检查value是否为Int32Array数组类型。 4367 4368**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4369 4370**系统能力:** SystemCapability.Utils.Lang 4371 4372**参数:** 4373 4374| 参数名 | 类型 | 必填 | 说明 | 4375| -------- | -------- | -------- | -------- | 4376| value | Object | 是 | 待检测对象。 | 4377 4378**返回值:** 4379 4380| 类型 | 说明 | 4381| -------- | -------- | 4382| boolean | 如果是Int32Array数组类型则返回true,否则返回false。 | 4383 4384**示例:** 4385 4386 ```ts 4387 let type = new util.types(); 4388 let result = type.isInt32Array(new Int32Array([])); 4389 console.info("result = " + result); 4390 // 输出结果:result = true 4391 ``` 4392 4393 4394### isMap<sup>8+</sup> 4395 4396isMap(value: Object): boolean 4397 4398检查value是否为Map类型。 4399 4400**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4401 4402**系统能力:** SystemCapability.Utils.Lang 4403 4404**参数:** 4405 4406| 参数名 | 类型 | 必填 | 说明 | 4407| -------- | -------- | -------- | -------- | 4408| value | Object | 是 | 待检测对象。 | 4409 4410**返回值:** 4411 4412| 类型 | 说明 | 4413| -------- | -------- | 4414| boolean | 如果是Map类型返回则返回true,否则返回false。 | 4415 4416**示例:** 4417 4418 ```ts 4419 let type = new util.types(); 4420 let result = type.isMap(new Map()); 4421 console.info("result = " + result); 4422 // 输出结果:result = true 4423 ``` 4424 4425 4426### isMapIterator<sup>8+</sup> 4427 4428isMapIterator(value: Object): boolean 4429 4430检查value是否为Map的Iterator类型。 4431 4432**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4433 4434**系统能力:** SystemCapability.Utils.Lang 4435 4436**参数:** 4437 4438 4439| 参数名 | 类型 | 必填 | 说明 | 4440| -------- | -------- | -------- | -------- | 4441| value | Object | 是 | 待检测对象。 | 4442 4443**返回值:** 4444 4445| 类型 | 说明 | 4446| -------- | -------- | 4447| boolean | 如果是Map的Iterator类型则返回true,否则返回false。 | 4448 4449**示例:** 4450 4451 ```ts 4452 let type = new util.types(); 4453 const map : Map<number,number> = new Map(); 4454 let result = type.isMapIterator(map.keys()); 4455 console.info("result = " + result); 4456 // 输出结果:result = true 4457 ``` 4458 4459 4460### isNativeError<sup>8+</sup> 4461 4462isNativeError(value: Object): boolean 4463 4464检查value是否为Error类型。 4465 4466**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4467 4468**系统能力:** SystemCapability.Utils.Lang 4469 4470**参数:** 4471 4472| 参数名 | 类型 | 必填 | 说明 | 4473| -------- | -------- | -------- | -------- | 4474| value | Object | 是 | 待检测对象。 | 4475 4476**返回值:** 4477 4478| 类型 | 说明 | 4479| -------- | -------- | 4480| boolean | 如果是Error类型则返回true,否则返回false。 | 4481 4482**示例:** 4483 4484 ```ts 4485 let type = new util.types(); 4486 let result = type.isNativeError(new TypeError()); 4487 console.info("result = " + result); 4488 // 输出结果:result = true 4489 ``` 4490 4491 4492### isNumberObject<sup>(deprecated)</sup> 4493 4494isNumberObject(value: Object): boolean 4495 4496检查value是否为Number对象类型。 4497 4498> **说明:** 4499> 4500> 从API version 8开始支持,从API version 14开始废弃,没有替代接口。 4501 4502**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4503 4504**系统能力:** SystemCapability.Utils.Lang 4505 4506**参数:** 4507 4508| 参数名 | 类型 | 必填 | 说明 | 4509| -------- | -------- | -------- | -------- | 4510| value | Object | 是 | 待检测对象。 | 4511 4512**返回值:** 4513 4514| 类型 | 说明 | 4515| -------- | -------- | 4516| boolean | 如果是Number对象类型则返回true,否则返回false。 | 4517 4518**示例:** 4519 4520 ```ts 4521 let type = new util.types(); 4522 let result = type.isNumberObject(new Number(0)); 4523 console.info("result = " + result); 4524 // 输出结果:result = true 4525 ``` 4526 4527 4528### isPromise<sup>8+</sup> 4529 4530isPromise(value: Object): boolean 4531 4532检查value是否为Promise类型。 4533 4534**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4535 4536**系统能力:** SystemCapability.Utils.Lang 4537 4538**参数:** 4539 4540| 参数名 | 类型 | 必填 | 说明 | 4541| -------- | -------- | -------- | -------- | 4542| value | Object | 是 | 待检测对象。 | 4543 4544**返回值:** 4545 4546| 类型 | 说明 | 4547| -------- | -------- | 4548| boolean | 如果是Promise类型则返回true,否则返回false。 | 4549 4550**示例:** 4551 4552 ```ts 4553 let type = new util.types(); 4554 let result = type.isPromise(Promise.resolve(1)); 4555 console.info("result = " + result); 4556 // 输出结果:result = true 4557 ``` 4558 4559 4560### isProxy<sup>8+</sup> 4561 4562isProxy(value: Object): boolean 4563 4564检查value是否为Proxy类型。 4565 4566**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4567 4568**系统能力:** SystemCapability.Utils.Lang 4569 4570**参数:** 4571 4572| 参数名 | 类型 | 必填 | 说明 | 4573| -------- | -------- | -------- | -------- | 4574| value | Object | 是 | 待检测对象。 | 4575 4576**返回值:** 4577 4578| 类型 | 说明 | 4579| -------- | -------- | 4580| boolean | 如果是Proxy类型则返回true,否则返回false。 | 4581 4582**示例:** 4583 4584 ```ts 4585 class Target{ 4586 } 4587 let type = new util.types(); 4588 const target : Target = {}; 4589 const proxy = new Proxy(target, target); 4590 let result = type.isProxy(proxy); 4591 console.info("result = " + result); 4592 // 输出结果:result = true 4593 ``` 4594 4595 4596### isRegExp<sup>8+</sup> 4597 4598isRegExp(value: Object): boolean 4599 4600检查value是否为RegExp类型。 4601 4602**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4603 4604**系统能力:** SystemCapability.Utils.Lang 4605 4606**参数:** 4607 4608| 参数名 | 类型 | 必填 | 说明 | 4609| -------- | -------- | -------- | -------- | 4610| value | Object | 是 | 待检测对象。 | 4611 4612**返回值:** 4613 4614| 类型 | 说明 | 4615| -------- | -------- | 4616| boolean | 如果是RegExp类型则返回true,否则返回false。 | 4617 4618**示例:** 4619 4620 ```ts 4621 let type = new util.types(); 4622 let result = type.isRegExp(new RegExp('abc')); 4623 console.info("result = " + result); 4624 // 输出结果:result = true 4625 ``` 4626 4627 4628### isSet<sup>8+</sup> 4629 4630isSet(value: Object): boolean 4631 4632检查value是否为Set类型。 4633 4634**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4635 4636**系统能力:** SystemCapability.Utils.Lang 4637 4638**参数:** 4639 4640| 参数名 | 类型 | 必填 | 说明 | 4641| -------- | -------- | -------- | -------- | 4642| value | Object | 是 | 待检测对象。 | 4643 4644**返回值:** 4645 4646| 类型 | 说明 | 4647| -------- | -------- | 4648| boolean | 如果是Set类型则返回true,否则返回false。 | 4649 4650**示例:** 4651 4652 ```ts 4653 let type = new util.types(); 4654 let set : Set<number> = new Set(); 4655 let result = type.isSet(set); 4656 console.info("result = " + result); 4657 // 输出结果:result = true 4658 ``` 4659 4660 4661### isSetIterator<sup>8+</sup> 4662 4663isSetIterator(value: Object): boolean 4664 4665检查value是否为Set的Iterator类型。 4666 4667**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4668 4669**系统能力:** SystemCapability.Utils.Lang 4670 4671**参数:** 4672 4673| 参数名 | 类型 | 必填 | 说明 | 4674| -------- | -------- | -------- | -------- | 4675| value | Object | 是 | 待检测对象。 | 4676 4677**返回值:** 4678 4679| 类型 | 说明 | 4680| -------- | -------- | 4681| boolean | 如果是Set的Iterator类型则返回true,否则返回false。 | 4682 4683**示例:** 4684 4685 ```ts 4686 let type = new util.types(); 4687 const set : Set<number> = new Set(); 4688 let result = type.isSetIterator(set.keys()); 4689 console.info("result = " + result); 4690 // 输出结果:result = true 4691 ``` 4692 4693 4694### isStringObject<sup>(deprecated)</sup> 4695 4696isStringObject(value: Object): boolean 4697 4698检查value是否为String对象类型。 4699 4700> **说明:** 4701> 4702> 从API version 8开始支持,从API version 14开始废弃,没有替代接口。 4703 4704**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4705 4706**系统能力:** SystemCapability.Utils.Lang 4707 4708**参数:** 4709 4710| 参数名 | 类型 | 必填 | 说明 | 4711| -------- | -------- | -------- | -------- | 4712| value | Object | 是 | 待检测对象。 | 4713 4714**返回值:** 4715 4716| 类型 | 说明 | 4717| -------- | -------- | 4718| boolean | 如果是String对象类型则返回true,否则返回false。 | 4719 4720**示例:** 4721 4722 ```ts 4723 let type = new util.types(); 4724 let result = type.isStringObject(new String('foo')); 4725 console.info("result = " + result); 4726 // 输出结果:result = true 4727 ``` 4728 4729 4730### isSymbolObject<sup>(deprecated)</sup> 4731 4732isSymbolObject(value: Object): boolean 4733 4734检查value是否为Symbol对象类型。 4735 4736> **说明:** 4737> 4738> 从API version 8开始支持,从API version 14开始废弃,没有替代接口。 4739 4740**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4741 4742**系统能力:** SystemCapability.Utils.Lang 4743 4744**参数:** 4745 4746| 参数名 | 类型 | 必填 | 说明 | 4747| -------- | -------- | -------- | -------- | 4748| value | Object | 是 | 待检测对象。 | 4749 4750**返回值:** 4751 4752| 类型 | 说明 | 4753| -------- | -------- | 4754| boolean | 如果是Symbol对象类型为则返回true,否则返回false。 | 4755 4756**示例:** 4757<!--code_no_check--> 4758 ```ts 4759 // /entry/src/main/ets/pages/test.ts 4760 export const symbols = Symbol('foo'); 4761 ``` 4762 4763 <!--code_no_check--> 4764 ```ts 4765 import { symbols } from './test' 4766 4767 let type = new util.types(); 4768 let result = type.isSymbolObject(Object(symbols)); 4769 console.info("result = " + result); 4770 // 输出结果:result = true 4771 ``` 4772 4773 4774### isTypedArray<sup>8+</sup> 4775 4776isTypedArray(value: Object): boolean 4777 4778检查value是否为TypedArray类型。 4779 4780TypedArray类型,包括Int8Array、Int16Array、Int32Array、Uint8Array、Uint8ClampedArray、Uint16Array、Uint32Array、Float32Array、Float64Array、BigInt64Array、BigUint64Array。 4781 4782**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4783 4784**系统能力:** SystemCapability.Utils.Lang 4785 4786**参数:** 4787 4788| 参数名 | 类型 | 必填 | 说明 | 4789| -------- | -------- | -------- | -------- | 4790| value | Object | 是 | 待检测对象。 | 4791 4792**返回值:** 4793 4794| 类型 | 说明 | 4795| -------- | -------- | 4796| boolean | 如果是TypedArray包含的类型则返回true,否则返回false。 | 4797 4798**示例:** 4799 4800 ```ts 4801 let type = new util.types(); 4802 let result = type.isTypedArray(new Float64Array([])); 4803 console.info("result = " + result); 4804 // 输出结果:result = true 4805 ``` 4806 4807 4808### isUint8Array<sup>8+</sup> 4809 4810isUint8Array(value: Object): boolean 4811 4812检查value是否为Uint8Array数组类型。 4813 4814**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4815 4816**系统能力:** SystemCapability.Utils.Lang 4817 4818**参数:** 4819 4820| 参数名 | 类型 | 必填 | 说明 | 4821| -------- | -------- | -------- | -------- | 4822| value | Object | 是 | 待检测对象。 | 4823 4824**返回值:** 4825 4826| 类型 | 说明 | 4827| -------- | -------- | 4828| boolean | 如果是Uint8Array数组类型则返回true,否则返回false。 | 4829 4830**示例:** 4831 4832 ```ts 4833 let type = new util.types(); 4834 let result = type.isUint8Array(new Uint8Array([])); 4835 console.info("result = " + result); 4836 // 输出结果:result = true 4837 ``` 4838 4839 4840### isUint8ClampedArray<sup>8+</sup> 4841 4842isUint8ClampedArray(value: Object): boolean 4843 4844检查value是否为Uint8ClampedArray数组类型。 4845 4846**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4847 4848**系统能力:** SystemCapability.Utils.Lang 4849 4850**参数:** 4851 4852| 参数名 | 类型 | 必填 | 说明 | 4853| -------- | -------- | -------- | -------- | 4854| value | Object | 是 | 待检测对象。 | 4855 4856**返回值:** 4857 4858| 类型 | 说明 | 4859| -------- | -------- | 4860| boolean | 如果是Uint8ClampedArray数组类型则返回true,否则返回false。 | 4861 4862**示例:** 4863 4864 ```ts 4865 let type = new util.types(); 4866 let result = type.isUint8ClampedArray(new Uint8ClampedArray([])); 4867 console.info("result = " + result); 4868 // 输出结果:result = true 4869 ``` 4870 4871 4872### isUint16Array<sup>8+</sup> 4873 4874isUint16Array(value: Object): boolean 4875 4876检查value是否为Uint16Array数组类型。 4877 4878**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4879 4880**系统能力:** SystemCapability.Utils.Lang 4881 4882**参数:** 4883 4884| 参数名 | 类型 | 必填 | 说明 | 4885| -------- | -------- | -------- | -------- | 4886| value | Object | 是 | 待检测对象。 | 4887 4888**返回值:** 4889 4890| 类型 | 说明 | 4891| -------- | -------- | 4892| boolean | 如果是Uint16Array数组类型则返回true,否则返回false。 | 4893 4894**示例:** 4895 4896 ```ts 4897 let type = new util.types(); 4898 let result = type.isUint16Array(new Uint16Array([])); 4899 console.info("result = " + result); 4900 // 输出结果:result = true 4901 ``` 4902 4903 4904### isUint32Array<sup>8+</sup> 4905 4906isUint32Array(value: Object): boolean 4907 4908检查value是否为Uint32Array数组类型。 4909 4910**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4911 4912**系统能力:** SystemCapability.Utils.Lang 4913 4914**参数:** 4915 4916| 参数名 | 类型 | 必填 | 说明 | 4917| -------- | -------- | -------- | -------- | 4918| value | Object | 是 | 待检测对象。 | 4919 4920**返回值:** 4921 4922| 类型 | 说明 | 4923| -------- | -------- | 4924| boolean | 如果是Uint32Array数组类型则返回true,否则返回false。 | 4925 4926**示例:** 4927 4928 ```ts 4929 let type = new util.types(); 4930 let result = type.isUint32Array(new Uint32Array([])); 4931 console.info("result = " + result); 4932 // 输出结果:result = true 4933 ``` 4934 4935 4936### isWeakMap<sup>8+</sup> 4937 4938isWeakMap(value: Object): boolean 4939 4940检查value是否为WeakMap类型。 4941 4942**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4943 4944**系统能力:** SystemCapability.Utils.Lang 4945 4946**参数:** 4947 4948| 参数名 | 类型 | 必填 | 说明 | 4949| -------- | -------- | -------- | -------- | 4950| value | Object | 是 | 待检测对象。 | 4951 4952**返回值:** 4953 4954| 类型 | 说明 | 4955| -------- | -------- | 4956| boolean | 如果是WeakMap类型则返回true,否则返回false。 | 4957 4958**示例:** 4959 4960 ```ts 4961 let type = new util.types(); 4962 let value : WeakMap<object, number> = new WeakMap(); 4963 let result = type.isWeakMap(value); 4964 console.info("result = " + result); 4965 // 输出结果:result = true 4966 ``` 4967 4968 4969### isWeakSet<sup>8+</sup> 4970 4971isWeakSet(value: Object): boolean 4972 4973检查value是否为WeakSet类型。 4974 4975**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4976 4977**系统能力:** SystemCapability.Utils.Lang 4978 4979**参数:** 4980 4981| 参数名 | 类型 | 必填 | 说明 | 4982| -------- | -------- | -------- | -------- | 4983| value | Object | 是 | 待检测对象。 | 4984 4985**返回值:** 4986 4987| 类型 | 说明 | 4988| -------- | -------- | 4989| boolean | 如果是WeakSet类型则返回true,否则返回false。 | 4990 4991**示例:** 4992 4993 ```ts 4994 let type = new util.types(); 4995 let result = type.isWeakSet(new WeakSet()); 4996 console.info("result = " + result); 4997 // 输出结果:result = true 4998 ``` 4999 5000 5001### isBigInt64Array<sup>8+</sup> 5002 5003isBigInt64Array(value: Object): boolean 5004 5005检查value是否为BigInt64Array类型。 5006 5007**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5008 5009**系统能力:** SystemCapability.Utils.Lang 5010 5011**参数:** 5012 5013| 参数名 | 类型 | 必填 | 说明 | 5014| -------- | -------- | -------- | -------- | 5015| value | Object | 是 | 待检测对象。 | 5016 5017**返回值:** 5018 5019| 类型 | 说明 | 5020| -------- | -------- | 5021| boolean | 如果是BigInt64Array类型则返回true,否则返回false。 | 5022 5023**示例:** 5024 5025 ```ts 5026 let type = new util.types(); 5027 let result = type.isBigInt64Array(new BigInt64Array([])); 5028 console.info("result = " + result); 5029 // 输出结果:result = true 5030 ``` 5031 5032 5033### isBigUint64Array<sup>8+</sup> 5034 5035isBigUint64Array(value: Object): boolean 5036 5037检查value是否为BigUint64Array类型。 5038 5039**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5040 5041**系统能力:** SystemCapability.Utils.Lang 5042 5043**参数:** 5044 5045| 参数名 | 类型 | 必填 | 说明 | 5046| -------- | -------- | -------- | -------- | 5047| value | Object | 是 | 待检测对象。 | 5048 5049**返回值:** 5050 5051| 类型 | 说明 | 5052| -------- | -------- | 5053| boolean | 如果是BigUint64Array类型则返回true,否则返回false。 | 5054 5055**示例:** 5056 5057 ```ts 5058 let type = new util.types(); 5059 let result = type.isBigUint64Array(new BigUint64Array([])); 5060 console.info("result = " + result); 5061 // 输出结果:result = true 5062 ``` 5063 5064 5065### isModuleNamespaceObject<sup>8+</sup> 5066 5067isModuleNamespaceObject(value: Object): boolean 5068 5069检查value是否为Module Namespace Object类型。 5070 5071**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5072 5073**系统能力:** SystemCapability.Utils.Lang 5074 5075**参数:** 5076 5077| 参数名 | 类型 | 必填 | 说明 | 5078| -------- | -------- | -------- | -------- | 5079| value | Object | 是 | 待检测对象。 | 5080 5081**返回值:** 5082 5083| 类型 | 说明 | 5084| -------- | -------- | 5085| boolean | 如果是Module Namespace Object类型则返回true,否则返回false。 | 5086 5087**示例:** 5088 5089 ```ts 5090 // /entry/src/main/ets/pages/test.ts 5091 export function func() { 5092 console.info("hello world"); 5093 } 5094 ``` 5095 5096 <!--code_no_check--> 5097 ```ts 5098 import * as nameSpace from './test'; 5099 5100 let type = new util.types(); 5101 let result = type.isModuleNamespaceObject(nameSpace); 5102 console.info("result = " + result); 5103 // 输出结果:result = true 5104 ``` 5105 5106 5107### isSharedArrayBuffer<sup>8+</sup> 5108 5109isSharedArrayBuffer(value: Object): boolean 5110 5111检查value是否为SharedArrayBuffer类型。 5112 5113**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 5114 5115**系统能力:** SystemCapability.Utils.Lang 5116 5117**参数:** 5118 5119| 参数名 | 类型 | 必填 | 说明 | 5120| -------- | -------- | -------- | -------- | 5121| value | Object | 是 | 待检测对象。 | 5122 5123**返回值:** 5124 5125| 类型 | 说明 | 5126| -------- | -------- | 5127| boolean | 如果是SharedArrayBuffer类型则返回true,否则返回false。 | 5128 5129**示例:** 5130 5131 ```ts 5132 let type = new util.types(); 5133 let result = type.isSharedArrayBuffer(new SharedArrayBuffer(0)); 5134 console.info("result = " + result); 5135 // 输出结果:result = true 5136 ``` 5137 5138## LruBuffer<sup>(deprecated)</sup> 5139 5140> **说明:** 5141> 5142> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache<sup>9+</sup>](#lrucache9)替代。 5143 5144### 属性 5145 5146**系统能力:** 以下各项对应的系统能力均为SystemCapability.Utils.Lang。 5147 5148| 名称 | 类型 | 可读 | 可写 | 说明 | 5149| -------- | -------- | -------- | -------- | -------- | 5150| length | number | 是 | 否 | 当前缓冲区中值的总数。 | 5151 5152**示例:** 5153 5154 ```ts 5155 let pro : util.LruBuffer<number,number>= new util.LruBuffer(); 5156 pro.put(2,10); 5157 pro.put(1,8); 5158 let result = pro.length; 5159 console.info("result = " + result); 5160 // 输出结果:result = 2 5161 ``` 5162 5163### constructor<sup>(deprecated)</sup> 5164 5165constructor(capacity?: number) 5166 5167构造函数用于创建一个新的LruBuffer实例,默认容量为64。 5168 5169> **说明:** 5170> 5171> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.constructor<sup>9+</sup>](#constructor9-3)替代。 5172 5173**系统能力:** SystemCapability.Utils.Lang 5174 5175**参数:** 5176 5177| 参数名 | 类型 | 必填 | 说明 | 5178| -------- | -------- | -------- | -------- | 5179| capacity | number | 否 | 指示要为缓冲区自定义的容量,默认值为64。 | 5180 5181**示例:** 5182 5183 ```ts 5184 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5185 ``` 5186 5187### updateCapacity<sup>(deprecated)</sup> 5188 5189updateCapacity(newCapacity: number): void 5190 5191将缓冲区容量更新为指定容量,如果newCapacity小于或等于0,则抛出异常。 5192 5193> **说明:** 5194> 5195> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.updateCapacity<sup>9+</sup>](#updatecapacity9)替代。 5196 5197**系统能力:** SystemCapability.Utils.Lang 5198 5199**参数:** 5200 5201| 参数名 | 类型 | 必填 | 说明 | 5202| -------- | -------- | -------- | -------- | 5203| newCapacity | number | 是 | 指示要为缓冲区自定义的容量。 | 5204 5205**示例:** 5206 5207 ```ts 5208 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5209 pro.updateCapacity(100); 5210 ``` 5211 5212### toString<sup>(deprecated)</sup> 5213 5214toString(): string 5215 5216返回对象的字符串表示形式。 5217 5218> **说明:** 5219> 5220> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.toString<sup>9+</sup>](#tostring9)替代。 5221 5222**系统能力:** SystemCapability.Utils.Lang 5223 5224**返回值:** 5225 5226| 类型 | 说明 | 5227| -------- | -------- | 5228| string | 返回对象的字符串表示形式。 | 5229 5230**示例:** 5231 5232 ```ts 5233 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5234 pro.put(2,10); 5235 pro.get(2); 5236 pro.remove(20); 5237 let result = pro.toString(); 5238 console.info("result = " + result); 5239 // 输出结果:result = Lrubuffer[ maxSize = 64, hits = 1, misses = 0, hitRate = 100% ] 5240 ``` 5241 5242### getCapacity<sup>(deprecated)</sup> 5243 5244getCapacity(): number 5245 5246获取当前缓冲区的容量。 5247 5248> **说明:** 5249> 5250> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getCapacity<sup>9+</sup>](#getcapacity9)替代。 5251 5252**系统能力:** SystemCapability.Utils.Lang 5253 5254**返回值:** 5255 5256| 类型 | 说明 | 5257| -------- | -------- | 5258| number | 返回当前缓冲区的容量。 | 5259 5260**示例:** 5261 5262 ```ts 5263 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5264 let result = pro.getCapacity(); 5265 console.info("result = " + result); 5266 // 输出结果:result = 64 5267 ``` 5268 5269### clear<sup>(deprecated)</sup> 5270 5271clear(): void 5272 5273清除当前缓冲区中的键值对,后续调用afterRemoval()方法执行操作。 5274 5275> **说明:** 5276> 5277> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.clear<sup>9+</sup>](#clear9)替代。 5278 5279**系统能力:** SystemCapability.Utils.Lang 5280 5281**示例:** 5282 5283 ```ts 5284 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5285 pro.put(2,10); 5286 let result = pro.length; 5287 pro.clear(); 5288 ``` 5289 5290### getCreateCount<sup>(deprecated)</sup> 5291 5292getCreateCount(): number 5293 5294获取createDefault()返回值的次数。 5295 5296> **说明:** 5297> 5298> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getCreateCount<sup>9+</sup>](#getcreatecount9)替代。 5299 5300**系统能力:** SystemCapability.Utils.Lang 5301 5302**返回值:** 5303 5304| 类型 | 说明 | 5305| -------- | -------- | 5306| number | 返回createDefault()返回值的次数。 | 5307 5308**示例:** 5309 5310 ```ts 5311 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5312 pro.put(1,8); 5313 let result = pro.getCreateCount(); 5314 console.info("result = " + result); 5315 // 输出结果:result = 0 5316 ``` 5317 5318### getMissCount<sup>(deprecated)</sup> 5319 5320getMissCount(): number 5321 5322获取查询值不匹配的次数。 5323 5324> **说明:** 5325> 5326> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getMissCount<sup>9+</sup>](#getmisscount9)替代。 5327 5328**系统能力:** SystemCapability.Utils.Lang 5329 5330**返回值:** 5331 5332| 类型 | 说明 | 5333| -------- | -------- | 5334| number | 返回查询值不匹配的次数。 | 5335 5336**示例:** 5337 5338 ```ts 5339 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5340 pro.put(2,10); 5341 pro.get(2); 5342 let result = pro.getMissCount(); 5343 console.info("result = " + result); 5344 // 输出结果:result = 0 5345 ``` 5346 5347### getRemovalCount<sup>(deprecated)</sup> 5348 5349getRemovalCount(): number 5350 5351获取从缓冲区中逐出值的次数。 5352 5353> **说明:** 5354> 5355> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getRemovalCount<sup>9+</sup>](#getremovalcount9)替代。 5356 5357**系统能力:** SystemCapability.Utils.Lang 5358 5359**返回值:** 5360 5361| 类型 | 说明 | 5362| -------- | -------- | 5363| number | 返回从缓冲区中驱逐的次数。 | 5364 5365**示例:** 5366 5367 ```ts 5368 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5369 pro.put(2,10); 5370 pro.updateCapacity(2); 5371 pro.put(50,22); 5372 let result = pro.getRemovalCount(); 5373 console.info("result = " + result); 5374 // 输出结果:result = 0 5375 ``` 5376 5377### getMatchCount<sup>(deprecated)</sup> 5378 5379getMatchCount(): number 5380 5381获取查询值匹配成功的次数。 5382 5383> **说明:** 5384> 5385> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getMatchCount<sup>9+</sup>](#getmatchcount9)替代。 5386 5387**系统能力:** SystemCapability.Utils.Lang 5388 5389**返回值:** 5390 5391| 类型 | 说明 | 5392| -------- | -------- | 5393| number | 返回查询值匹配成功的次数。 | 5394 5395**示例:** 5396 5397 ```ts 5398 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5399 pro.put(2,10); 5400 pro.get(2); 5401 let result = pro.getMatchCount(); 5402 console.info("result = " + result); 5403 // 输出结果:result = 1 5404 ``` 5405 5406### getPutCount<sup>(deprecated)</sup> 5407 5408getPutCount(): number 5409 5410获取将值添加到缓冲区的次数。 5411 5412> **说明:** 5413> 5414> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.getPutCount<sup>9+</sup>](#getputcount9)替代。 5415 5416**系统能力:** SystemCapability.Utils.Lang 5417 5418**返回值:** 5419 5420| 类型 | 说明 | 5421| -------- | -------- | 5422| number | 返回将值添加到缓冲区的次数。 | 5423 5424**示例:** 5425 5426 ```ts 5427 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5428 pro.put(2,10); 5429 let result = pro.getPutCount(); 5430 console.info("result = " + result); 5431 // 输出结果:result = 1 5432 ``` 5433 5434### isEmpty<sup>(deprecated)</sup> 5435 5436isEmpty(): boolean 5437 5438检查当前缓冲区是否为空。 5439 5440> **说明:** 5441> 5442> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.isEmpty<sup>9+</sup>](#isempty9)替代。 5443 5444**系统能力:** SystemCapability.Utils.Lang 5445 5446**返回值:** 5447 5448| 类型 | 说明 | 5449| -------- | -------- | 5450| boolean | 如果当前缓冲区不包含任何值,则返回true。 | 5451 5452**示例:** 5453 5454 ```ts 5455 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5456 pro.put(2,10); 5457 let result = pro.isEmpty(); 5458 console.info("result = " + result); 5459 // 输出结果:result = false 5460 ``` 5461 5462### get<sup>(deprecated)</sup> 5463 5464get(key: K): V | undefined 5465 5466表示要查询的键。 5467 5468> **说明:** 5469> 5470> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.get<sup>9+</sup>](#get9)替代。 5471 5472**系统能力:** SystemCapability.Utils.Lang 5473 5474**参数:** 5475 5476| 参数名 | 类型 | 必填 | 说明 | 5477| -------- | -------- | -------- | -------- | 5478| key | K | 是 | 要查询的键。 | 5479 5480**返回值:** 5481 5482| 类型 | 说明 | 5483| -------- | -------- | 5484| V \| undefined | 如果指定的键存在于缓冲区中,则返回与键关联的值;否则返回undefined。 | 5485 5486**示例:** 5487 5488 ```ts 5489 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5490 pro.put(2,10); 5491 let result = pro.get(2); 5492 console.info("result = " + result); 5493 // 输出结果:result = 10 5494 ``` 5495 5496### put<sup>(deprecated)</sup> 5497 5498put(key: K,value: V): V 5499 5500将键值对添加到缓冲区。 5501 5502> **说明:** 5503> 5504> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.put<sup>9+</sup>](#put9)替代。 5505 5506**系统能力:** SystemCapability.Utils.Lang 5507 5508**参数:** 5509 5510| 参数名 | 类型 | 必填 | 说明 | 5511| -------- | -------- | -------- | -------- | 5512| key | K | 是 | 要添加的密钥。 | 5513| value | V | 是 | 指示与要添加的键关联的值。 | 5514 5515**返回值:** 5516 5517| 类型 | 说明 | 5518| -------- | -------- | 5519| V | 返回与添加的键关联的值;如果要添加的键已经存在,则返回原始值,如果键或值为空,则抛出此异常。 | 5520 5521**示例:** 5522 5523 ```ts 5524 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5525 let result = pro.put(2,10); 5526 console.info("result = " + result); 5527 // 输出结果:result = 10 5528 ``` 5529 5530### values<sup>(deprecated)</sup> 5531 5532values(): V[] 5533 5534获取当前缓冲区中所有值从最近访问到最近最少访问的顺序列表。 5535 5536> **说明:** 5537> 5538> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.values<sup>9+</sup>](#values9)替代。 5539 5540**系统能力:** SystemCapability.Utils.Lang 5541 5542**返回值:** 5543 5544| 类型 | 说明 | 5545| -------- | -------- | 5546| V [] | 按从最近访问到最近最少访问的顺序返回当前缓冲区中所有值的列表。 | 5547 5548**示例:** 5549 5550 ```ts 5551 let pro : util.LruBuffer<number|string,number|string> = new util.LruBuffer(); 5552 pro.put(2,10); 5553 pro.put(2,"anhu"); 5554 pro.put("afaf","grfb"); 5555 let result = pro.values(); 5556 console.info("result = " + result); 5557 // 输出结果:result = anhu,grfb 5558 ``` 5559 5560### keys<sup>(deprecated)</sup> 5561 5562keys(): K[] 5563 5564获取当前缓冲区中所有键从最近访问到最近最少访问的升序列表。 5565 5566> **说明:** 5567> 5568> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.keys<sup>9+</sup>](#keys9)替代。 5569 5570**系统能力:** SystemCapability.Utils.Lang 5571 5572**返回值:** 5573 5574| 类型 | 说明 | 5575| -------- | -------- | 5576| K [] | 按升序返回当前缓冲区中所有键的列表,从最近访问到最近最少访问。 | 5577 5578**示例:** 5579 5580 ```ts 5581 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5582 pro.put(2,10); 5583 let result = pro.keys(); 5584 console.info("result = " + result); 5585 // 输出结果:result = 2 5586 ``` 5587 5588### remove<sup>(deprecated)</sup> 5589 5590remove(key: K): V | undefined 5591 5592删除当前缓冲区中指定的键及其关联的值。 5593 5594> **说明:** 5595> 5596> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.remove<sup>9+</sup>](#remove9)替代。 5597 5598**系统能力:** SystemCapability.Utils.Lang 5599 5600**参数:** 5601 5602| 参数名 | 类型 | 必填 | 说明 | 5603| -------- | -------- | -------- | -------- | 5604| key | K | 是 | 要删除的密钥。 | 5605 5606**返回值:** 5607 5608| 类型 | 说明 | 5609| -------- | -------- | 5610| V \| undefined | 返回一个包含已删除键值对的Optional对象;如果key不存在,则返回一个空的Optional对象,如果key为null,则抛出异常。 | 5611 5612**示例:** 5613 5614 ```ts 5615 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5616 pro.put(2,10); 5617 let result = pro.remove(20); 5618 console.info("result = " + result); 5619 // 输出结果:result = undefined 5620 ``` 5621 5622### afterRemoval<sup>(deprecated)</sup> 5623 5624afterRemoval(isEvict: boolean,key: K,value: V,newValue: V): void 5625 5626删除值后执行后续操作。 5627 5628> **说明:** 5629> 5630> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.afterRemoval<sup>9+</sup>](#afterremoval9)替代。 5631 5632**系统能力:** SystemCapability.Utils.Lang 5633 5634**参数:** 5635 5636| 参数名 | 类型 | 必填 | 说明 | 5637| -------- | -------- | -------- | -------- | 5638| isEvict | boolean | 是 | 因容量不足而调用该方法时,参数值为true,其他情况为false。 | 5639| key | K | 是 | 表示删除的键。 | 5640| value | V | 是 | 表示删除的值。 | 5641| newValue | V | 是 | 如果已调用put方法并且要添加的键已经存在,则参数值是关联的新值。其他情况下参数值为空。 | 5642 5643**示例:** 5644 5645```ts 5646class ChildLruBuffer<K, V> extends util.LruBuffer<K, V> { 5647 constructor(capacity?: number) { 5648 super(capacity); 5649 } 5650 5651 afterRemoval(isEvict: boolean, key: K, value: V, newValue: V): void { 5652 if (isEvict === true) { 5653 console.info('key: ' + key); 5654 // 输出结果:key: 11 5655 console.info('value: ' + value); 5656 // 输出结果:value: 1 5657 console.info('newValue: ' + newValue); 5658 // 输出结果:newValue: null 5659 } 5660 } 5661} 5662let lru: ChildLruBuffer<number, number> = new ChildLruBuffer(2); 5663lru.put(11, 1); 5664lru.put(22, 2); 5665lru.put(33, 3); 5666``` 5667 5668### contains<sup>(deprecated)</sup> 5669 5670contains(key: K): boolean 5671 5672检查当前缓冲区是否包含指定的键。 5673 5674 5675> **说明:** 5676> 5677> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.contains<sup>9+</sup>](#contains9)替代。 5678 5679**系统能力:** SystemCapability.Utils.Lang 5680 5681**参数:** 5682 5683| 参数名 | 类型 | 必填 | 说明 | 5684| -------- | -------- | -------- | -------- | 5685| key | K | 是 | 表示要检查的键。 | 5686 5687**返回值:** 5688 5689| 类型 | 说明 | 5690| -------- | -------- | 5691| boolean | 如果缓冲区包含指定的键,则返回 true。 | 5692 5693**示例:** 5694 5695 ```ts 5696 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5697 pro.put(2,10); 5698 let result = pro.contains(20); 5699 console.info('result = ' + result); 5700 // 输出结果:result = false 5701 ``` 5702 5703### createDefault<sup>(deprecated)</sup> 5704 5705createDefault(key: K): V 5706 5707如果未计算特定键的值,则执行后续操作,参数表示丢失的键,返回与键关联的值。 5708 5709> **说明:** 5710> 5711> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.createDefault<sup>9+</sup>](#createdefault9)替代。 5712 5713**系统能力:** SystemCapability.Utils.Lang 5714 5715**参数:** 5716 5717| 参数名 | 类型 | 必填 | 说明 | 5718| -------- | -------- | -------- | -------- | 5719| key | K | 是 | 表示丢失的键。 | 5720 5721**返回值:** 5722 5723| 类型 | 说明 | 5724| -------- | -------- | 5725| V | 返回与键关联的值。 | 5726 5727**示例:** 5728 5729 ```ts 5730 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5731 let result = pro.createDefault(50); 5732 ``` 5733 5734### entries<sup>(deprecated)</sup> 5735 5736entries(): IterableIterator<[K, V]> 5737 5738允许迭代包含在这个对象中的所有键值对。 5739 5740> **说明:** 5741> 5742> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.entries<sup>9+</sup>](#entries9)替代。 5743 5744**系统能力:** SystemCapability.Utils.Lang 5745 5746**返回值:** 5747 5748| 类型 | 说明 | 5749| -------- | -------- | 5750| IterableIterator<[K, V]> | 返回一个可迭代数组。 | 5751 5752**示例:** 5753 5754 ```ts 5755 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5756 pro.put(2,10); 5757 let result = pro.entries(); 5758 ``` 5759 5760### [Symbol.iterator]<sup>(deprecated)</sup> 5761 5762[Symbol.iterator]\(): IterableIterator<[K, V]> 5763 5764返回一个键值对形式的二维数组。 5765 5766> **说明:** 5767> 5768> 从API version 8开始支持,从API version 9开始废弃,建议使用[LRUCache.Symbol.iterator<sup>9+</sup>](#symboliterator9)替代。 5769 5770**系统能力:** SystemCapability.Utils.Lang 5771 5772**返回值:** 5773 5774| 类型 | 说明 | 5775| -------- | -------- | 5776| IterableIterator<[K, V]> | 返回一个键值对形式的二维数组。 | 5777 5778**示例:** 5779 5780 ```ts 5781 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 5782 pro.put(2,10); 5783 let result = pro[Symbol.iterator](); 5784 ``` 5785 5786## Scope<sup>(deprecated)</sup> 5787 5788> **说明:** 5789> 5790> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper<sup>9+</sup>](#scopehelper9)替代。 5791 5792### constructor<sup>(deprecated)</sup> 5793 5794constructor(lowerObj: ScopeType, upperObj: ScopeType) 5795 5796创建指定下限和上限的作用域实例,并返回一个Scope对象。 5797 5798> **说明:** 5799> 5800> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.constructor<sup>9+</sup>](#constructor9-4)替代。 5801 5802 5803**系统能力:** SystemCapability.Utils.Lang 5804 5805**参数:** 5806 5807| 参数名 | 类型 | 必填 | 说明 | 5808| -------- | -------- | -------- | -------- | 5809| lowerObj | [ScopeType](#scopetype8) | 是 | 指定作用域实例的下限。 | 5810| upperObj | [ScopeType](#scopetype8) | 是 | 指定作用域实例的上限。 | 5811 5812**示例:** 5813```ts 5814class Temperature implements util.ScopeComparable { 5815 private readonly _temp: number; 5816 5817 constructor(value: number) { 5818 this._temp = value; 5819 } 5820 5821 compareTo(value: Temperature) { 5822 return this._temp >= value.getTemp(); 5823 } 5824 5825 getTemp() { 5826 return this._temp; 5827 } 5828 5829 toString(): string { 5830 return this._temp.toString(); 5831 } 5832} 5833 5834let tempLower = new Temperature(30); 5835let tempUpper = new Temperature(40); 5836let range = new util.Scope(tempLower, tempUpper); 5837console.info("range = " + range); 5838// 输出结果:range = [30, 40] 5839``` 5840 5841### toString<sup>(deprecated)</sup> 5842 5843toString(): string 5844 5845该字符串化方法返回一个包含当前范围的字符串表示形式。 5846 5847> **说明:** 5848> 5849> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.toString<sup>9+</sup>](#tostring9-1)替代。 5850 5851**系统能力:** SystemCapability.Utils.Lang 5852 5853**返回值:** 5854 5855| 类型 | 说明 | 5856| -------- | -------- | 5857| string | 返回包含当前范围对象的字符串表示形式。 | 5858 5859**示例:** 5860 5861```ts 5862class Temperature implements util.ScopeComparable { 5863 private readonly _temp: number; 5864 5865 constructor(value: number) { 5866 this._temp = value; 5867 } 5868 5869 compareTo(value: Temperature) { 5870 return this._temp >= value.getTemp(); 5871 } 5872 5873 getTemp() { 5874 return this._temp; 5875 } 5876 5877 toString(): string { 5878 return this._temp.toString(); 5879 } 5880} 5881 5882let tempLower = new Temperature(30); 5883let tempUpper = new Temperature(40); 5884let range = new util.Scope(tempLower, tempUpper); 5885let result = range.toString(); 5886console.info("result = " + result); 5887// 输出结果:result = [30, 40] 5888``` 5889 5890### intersect<sup>(deprecated)</sup> 5891 5892intersect(range: Scope): Scope 5893 5894获取给定范围和当前范围的交集。 5895 5896> **说明:** 5897> 5898> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.intersect<sup>9+</sup>](#intersect9)替代。 5899 5900**系统能力:** SystemCapability.Utils.Lang 5901 5902**参数:** 5903 5904| 参数名 | 类型 | 必填 | 说明 | 5905| -------- | -------- | -------- | -------- | 5906| range | [Scope](#scopedeprecated) | 是 | 传入一个给定范围。 | 5907 5908**返回值:** 5909 5910| 类型 | 说明 | 5911| -------- | -------- | 5912| [Scope](#scopedeprecated) | 返回给定范围和当前范围的交集。 | 5913 5914**示例:** 5915 5916```ts 5917class Temperature implements util.ScopeComparable { 5918 private readonly _temp: number; 5919 5920 constructor(value: number) { 5921 this._temp = value; 5922 } 5923 5924 compareTo(value: Temperature) { 5925 return this._temp >= value.getTemp(); 5926 } 5927 5928 getTemp() { 5929 return this._temp; 5930 } 5931 5932 toString(): string { 5933 return this._temp.toString(); 5934 } 5935} 5936 5937let tempLower = new Temperature(30); 5938let tempUpper = new Temperature(40); 5939let range = new util.Scope(tempLower, tempUpper); 5940let tempMiDF = new Temperature(35); 5941let tempMidS = new Temperature(39); 5942let rangeFir = new util.Scope(tempMiDF, tempMidS); 5943let result = range.intersect(rangeFir ); 5944console.info("result = " + result); 5945 // 输出结果:result = [35, 39] 5946 ``` 5947 5948### intersect<sup>(deprecated)</sup> 5949 5950intersect(lowerObj:ScopeType,upperObj:ScopeType):Scope 5951 5952获取当前范围与给定下限和上限范围的交集。 5953 5954> **说明:** 5955> 5956> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.intersect<sup>9+</sup>](#intersect9-1)替代。 5957 5958**系统能力:** SystemCapability.Utils.Lang 5959 5960**参数:** 5961 5962| 参数名 | 类型 | 必填 | 说明 | 5963| -------- | -------- | -------- | -------- | 5964| lowerObj | [ScopeType](#scopetype8) | 是 | 给定范围的下限。 | 5965| upperObj | [ScopeType](#scopetype8) | 是 | 给定范围的上限。 | 5966 5967**返回值:** 5968 5969| 类型 | 说明 | 5970| -------- | -------- | 5971| [Scope](#scopedeprecated) | 返回当前范围与给定下限和上限范围的交集。 | 5972 5973**示例:** 5974 5975```ts 5976class Temperature implements util.ScopeComparable { 5977 private readonly _temp: number; 5978 5979 constructor(value: number) { 5980 this._temp = value; 5981 } 5982 5983 compareTo(value: Temperature) { 5984 return this._temp >= value.getTemp(); 5985 } 5986 5987 getTemp() { 5988 return this._temp; 5989 } 5990 5991 toString(): string { 5992 return this._temp.toString(); 5993 } 5994} 5995 5996let tempLower = new Temperature(30); 5997let tempUpper = new Temperature(40); 5998let tempMiDF = new Temperature(35); 5999let tempMidS = new Temperature(39); 6000let range = new util.Scope(tempLower, tempUpper); 6001let result = range.intersect(tempMiDF, tempMidS); 6002console.info("result = " + result); 6003// 输出结果:result = [35, 39] 6004``` 6005 6006### getUpper<sup>(deprecated)</sup> 6007 6008getUpper(): ScopeType 6009 6010获取当前范围的上限。 6011 6012> **说明:** 6013> 6014> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.getUpper<sup>9+</sup>](#getupper9)替代。 6015 6016**系统能力:** SystemCapability.Utils.Lang 6017 6018**返回值:** 6019 6020| 类型 | 说明 | 6021| -------- | -------- | 6022| [ScopeType](#scopetype8) | 返回当前范围的上限值。 | 6023 6024**示例:** 6025 6026```ts 6027class Temperature implements util.ScopeComparable { 6028 private readonly _temp: number; 6029 6030 constructor(value: number) { 6031 this._temp = value; 6032 } 6033 6034 compareTo(value: Temperature) { 6035 return this._temp >= value.getTemp(); 6036 } 6037 6038 getTemp() { 6039 return this._temp; 6040 } 6041 6042 toString(): string { 6043 return this._temp.toString(); 6044 } 6045} 6046 6047let tempLower = new Temperature(30); 6048let tempUpper = new Temperature(40); 6049let range = new util.Scope(tempLower, tempUpper); 6050let result = range.getUpper(); 6051console.info("result = " + result); 6052// 输出结果:result = 40 6053``` 6054 6055### getLower<sup>(deprecated)</sup> 6056 6057getLower(): ScopeType 6058 6059获取当前范围的下限。 6060 6061> **说明:** 6062> 6063> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.getLower<sup>9+</sup>](#getlower9)替代。 6064 6065**系统能力:** SystemCapability.Utils.Lang 6066 6067**返回值:** 6068 6069| 类型 | 说明 | 6070| -------- | -------- | 6071| [ScopeType](#scopetype8) | 返回当前范围的下限值。 | 6072 6073**示例:** 6074 6075```ts 6076class Temperature implements util.ScopeComparable { 6077 private readonly _temp: number; 6078 6079 constructor(value: number) { 6080 this._temp = value; 6081 } 6082 6083 compareTo(value: Temperature) { 6084 return this._temp >= value.getTemp(); 6085 } 6086 6087 getTemp() { 6088 return this._temp; 6089 } 6090 6091 toString(): string { 6092 return this._temp.toString(); 6093 } 6094} 6095 6096let tempLower = new Temperature(30); 6097let tempUpper = new Temperature(40); 6098let range = new util.Scope(tempLower, tempUpper); 6099let result = range.getLower(); 6100console.info("result = " + result); 6101// 输出结果:result = 30 6102``` 6103 6104### expand<sup>(deprecated)</sup> 6105 6106expand(lowerObj: ScopeType,upperObj: ScopeType): Scope 6107 6108创建并返回包括当前范围和给定下限和上限的并集。 6109 6110> **说明:** 6111> 6112> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.expand<sup>9+</sup>](#expand9)替代。 6113 6114**系统能力:** SystemCapability.Utils.Lang 6115 6116**参数:** 6117 6118| 参数名 | 类型 | 必填 | 说明 | 6119| -------- | -------- | -------- | -------- | 6120| lowerObj | [ScopeType](#scopetype8) | 是 | 给定范围的下限。 | 6121| upperObj | [ScopeType](#scopetype8) | 是 | 给定范围的上限。 | 6122 6123**返回值:** 6124 6125| 类型 | 说明 | 6126| -------- | -------- | 6127| [Scope](#scopedeprecated) | 返回当前范围和给定下限和上限的并集。 | 6128 6129**示例:** 6130 6131```ts 6132class Temperature implements util.ScopeComparable { 6133 private readonly _temp: number; 6134 6135 constructor(value: number) { 6136 this._temp = value; 6137 } 6138 6139 compareTo(value: Temperature) { 6140 return this._temp >= value.getTemp(); 6141 } 6142 6143 getTemp() { 6144 return this._temp; 6145 } 6146 6147 toString(): string { 6148 return this._temp.toString(); 6149 } 6150} 6151 6152let tempLower = new Temperature(30); 6153let tempUpper = new Temperature(40); 6154let tempMiDF = new Temperature(35); 6155let tempMidS = new Temperature(39); 6156let range = new util.Scope(tempLower, tempUpper); 6157let result = range.expand(tempMiDF, tempMidS); 6158console.info("result = " + result); 6159// 输出结果:result = [30, 40] 6160``` 6161 6162### expand<sup>(deprecated)</sup> 6163 6164expand(range: Scope): Scope 6165 6166创建并返回包括当前范围和给定范围的并集。 6167 6168> **说明:** 6169> 6170> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.expand<sup>9+</sup>](#expand9-1)替代。 6171 6172**系统能力:** SystemCapability.Utils.Lang 6173 6174**参数:** 6175 6176| 参数名 | 类型 | 必填 | 说明 | 6177| -------- | -------- | -------- | -------- | 6178| range | [Scope](#scopedeprecated) | 是 | 传入一个给定范围。 | 6179 6180**返回值:** 6181 6182| 类型 | 说明 | 6183| -------- | -------- | 6184| [Scope](#scopedeprecated) | 返回包括当前范围和给定范围的并集。 | 6185 6186**示例:** 6187 6188```ts 6189class Temperature implements util.ScopeComparable { 6190 private readonly _temp: number; 6191 6192 constructor(value: number) { 6193 this._temp = value; 6194 } 6195 6196 compareTo(value: Temperature) { 6197 return this._temp >= value.getTemp(); 6198 } 6199 6200 getTemp() { 6201 return this._temp; 6202 } 6203 6204 toString(): string { 6205 return this._temp.toString(); 6206 } 6207} 6208 6209let tempLower = new Temperature(30); 6210let tempUpper = new Temperature(40); 6211let tempMiDF = new Temperature(35); 6212let tempMidS = new Temperature(39); 6213let range = new util.Scope(tempLower, tempUpper); 6214let rangeFir = new util.Scope(tempMiDF, tempMidS); 6215let result = range.expand(rangeFir); 6216console.info("result = " + result); 6217// 输出结果:result = [30, 40] 6218``` 6219 6220### expand<sup>(deprecated)</sup> 6221 6222expand(value: ScopeType): Scope 6223 6224创建并返回包括当前范围和给定值的并集。 6225 6226> **说明:** 6227> 6228> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.expand<sup>9+</sup>](#expand9-2)替代。 6229 6230**系统能力:** SystemCapability.Utils.Lang 6231 6232**参数:** 6233 6234| 参数名 | 类型 | 必填 | 说明 | 6235| -------- | -------- | -------- | -------- | 6236| value | [ScopeType](#scopetype8) | 是 | 传入一个给定值。 | 6237 6238**返回值:** 6239 6240| 类型 | 说明 | 6241| -------- | -------- | 6242| [Scope](#scopedeprecated) | 返回包括当前范围和给定值的并集。 | 6243 6244**示例:** 6245 6246```ts 6247class Temperature implements util.ScopeComparable { 6248 private readonly _temp: number; 6249 6250 constructor(value: number) { 6251 this._temp = value; 6252 } 6253 6254 compareTo(value: Temperature) { 6255 return this._temp >= value.getTemp(); 6256 } 6257 6258 getTemp() { 6259 return this._temp; 6260 } 6261 6262 toString(): string { 6263 return this._temp.toString(); 6264 } 6265} 6266 6267let tempLower = new Temperature(30); 6268let tempUpper = new Temperature(40); 6269let tempMiDF = new Temperature(35); 6270let range = new util.Scope(tempLower, tempUpper); 6271let result = range.expand(tempMiDF); 6272console.info("result = " + result); 6273// 输出结果:result = [30, 40] 6274``` 6275 6276### contains<sup>(deprecated)</sup> 6277 6278contains(value: ScopeType): boolean 6279 6280检查给定value是否包含在当前范围内。 6281 6282> **说明:** 6283> 6284> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.contains<sup>9+</sup>](#contains9-1)替代。 6285 6286**系统能力:** SystemCapability.Utils.Lang 6287 6288**参数:** 6289 6290| 参数名 | 类型 | 必填 | 说明 | 6291| -------- | -------- | -------- | -------- | 6292| value | [ScopeType](#scopetype8) | 是 | 传入一个给定值。 | 6293 6294**返回值:** 6295 6296| 类型 | 说明 | 6297| -------- | -------- | 6298| boolean | 如果给定值包含在当前范围内返回true,否则返回false。 | 6299 6300**示例:** 6301 6302```ts 6303class Temperature implements util.ScopeComparable { 6304 private readonly _temp: number; 6305 6306 constructor(value: number) { 6307 this._temp = value; 6308 } 6309 6310 compareTo(value: Temperature) { 6311 return this._temp >= value.getTemp(); 6312 } 6313 6314 getTemp() { 6315 return this._temp; 6316 } 6317 6318 toString(): string { 6319 return this._temp.toString(); 6320 } 6321} 6322 6323let tempLower = new Temperature(30); 6324let tempUpper = new Temperature(40); 6325let tempMiDF = new Temperature(35); 6326let range = new util.Scope(tempLower, tempUpper); 6327let result = range.contains(tempMiDF); 6328console.info("result = " + result); 6329// 输出结果:result = true 6330``` 6331 6332### contains<sup>(deprecated)</sup> 6333 6334contains(range: Scope): boolean 6335 6336检查给定range是否在当前范围内。 6337 6338> **说明:** 6339> 6340> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.contains<sup>9+</sup>](#contains9-2)替代。 6341 6342**系统能力:** SystemCapability.Utils.Lang 6343 6344**参数:** 6345 6346| 参数名 | 类型 | 必填 | 说明 | 6347| -------- | -------- | -------- | -------- | 6348| range | [Scope](#scopedeprecated) | 是 | 传入一个给定范围。 | 6349 6350**返回值:** 6351 6352| 类型 | 说明 | 6353| -------- | -------- | 6354| boolean | 如果给定范围包含在当前范围内返回true,否则返回false。 | 6355 6356**示例:** 6357 6358```ts 6359class Temperature implements util.ScopeComparable { 6360 private readonly _temp: number; 6361 6362 constructor(value: number) { 6363 this._temp = value; 6364 } 6365 6366 compareTo(value: Temperature) { 6367 return this._temp >= value.getTemp(); 6368 } 6369 6370 getTemp() { 6371 return this._temp; 6372 } 6373 6374 toString(): string { 6375 return this._temp.toString(); 6376 } 6377} 6378 6379let tempLower = new Temperature(30); 6380let tempUpper = new Temperature(40); 6381let range = new util.Scope(tempLower, tempUpper); 6382let tempLess = new Temperature(20); 6383let tempMore = new Temperature(45); 6384let rangeSec = new util.Scope(tempLess, tempMore); 6385let result = range.contains(rangeSec); 6386console.info("result = " + result); 6387// 输出结果:result = false 6388``` 6389 6390### clamp<sup>(deprecated)</sup> 6391 6392 6393clamp(value: ScopeType): ScopeType 6394 6395将给定值限定到当前范围内。 6396 6397> **说明:** 6398> 6399> 从API version 8开始支持,从API version 9开始废弃,建议使用[ScopeHelper.clamp<sup>9+</sup>](#clamp9)替代。 6400 6401**系统能力:** SystemCapability.Utils.Lang 6402 6403**参数:** 6404 6405| 参数名 | 类型 | 必填 | 说明 | 6406| -------- | -------- | -------- | -------- | 6407| value | [ScopeType](#scopetype8) | 是 | 传入的给定值。 | 6408 6409**返回值:** 6410 6411| 类型 | 说明 | 6412| -------- | -------- | 6413| [ScopeType](#scopetype8) | 如果传入的value小于下限,则返回lowerObj;如果大于上限值则返回upperObj;如果在当前范围内,则返回value。 | 6414 6415**示例:** 6416 6417```ts 6418class Temperature implements util.ScopeComparable { 6419 private readonly _temp: number; 6420 6421 constructor(value: number) { 6422 this._temp = value; 6423 } 6424 6425 compareTo(value: Temperature) { 6426 return this._temp >= value.getTemp(); 6427 } 6428 6429 getTemp() { 6430 return this._temp; 6431 } 6432 6433 toString(): string { 6434 return this._temp.toString(); 6435 } 6436} 6437 6438let tempLower = new Temperature(30); 6439let tempUpper = new Temperature(40); 6440let tempMiDF = new Temperature(35); 6441let range = new util.Scope(tempLower, tempUpper); 6442let result = range.clamp(tempMiDF); 6443console.info("result = " + result); 6444// 输出结果:result = 35 6445``` 6446 6447 6448## Base64<sup>(deprecated)</sup> 6449 6450> **说明:** 6451> 6452> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper<sup>9+</sup>](#base64helper9)替代。 6453 6454### constructor<sup>(deprecated)</sup> 6455 6456constructor() 6457 6458Base64的构造函数。 6459 6460> **说明:** 6461> 6462> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.constructor<sup>9+</sup>](#constructor9-5)替代。 6463 6464**系统能力:** SystemCapability.Utils.Lang 6465 6466**示例:** 6467 6468 ```ts 6469 let base64 = new util.Base64(); 6470 ``` 6471 6472### encodeSync<sup>(deprecated)</sup> 6473 6474encodeSync(src: Uint8Array): Uint8Array 6475 6476通过输入参数编码后输出对应文本。 6477 6478> **说明:** 6479> 6480> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.encodeSync<sup>9+</sup>](#encodesync9)替代。 6481 6482**系统能力:** SystemCapability.Utils.Lang 6483 6484**参数:** 6485 6486| 参数名 | 类型 | 必填 | 说明 | 6487| -------- | -------- | -------- | -------- | 6488| src | Uint8Array | 是 | 编码输入Uint8数组。 | 6489 6490**返回值:** 6491 6492| 类型 | 说明 | 6493| -------- | -------- | 6494| Uint8Array | 返回编码后新分配的Uint8数组。 | 6495 6496**示例:** 6497 6498 ```ts 6499 let base64 = new util.Base64(); 6500 let array = new Uint8Array([115,49,51]); 6501 let result = base64.encodeSync(array); 6502 console.info("result = " + result); 6503 // 输出结果:result = 99,122,69,122 6504 ``` 6505 6506### encodeToStringSync<sup>(deprecated)</sup> 6507 6508encodeToStringSync(src: Uint8Array): string 6509 6510通过输入参数编码后输出对应文本。 6511 6512> **说明:** 6513> 6514> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.encodeToStringSync<sup>9+</sup>](#encodetostringsync9)替代。 6515 6516**系统能力:** SystemCapability.Utils.Lang 6517 6518**参数:** 6519 6520| 参数名 | 类型 | 必填 | 说明 | 6521| -------- | -------- | -------- | -------- | 6522| src | Uint8Array | 是 | 编码输入Uint8数组。 | 6523 6524**返回值:** 6525 6526| 类型 | 说明 | 6527| -------- | -------- | 6528| string | 返回编码后的字符串。 | 6529 6530**示例:** 6531 6532 ```ts 6533 let base64 = new util.Base64(); 6534 let array = new Uint8Array([115,49,51]); 6535 let result = base64.encodeToStringSync(array); 6536 console.info("result = " + result); 6537 // 输出结果:result = czEz 6538 ``` 6539 6540### decodeSync<sup>(deprecated)</sup> 6541 6542decodeSync(src: Uint8Array | string): Uint8Array 6543 6544通过输入参数解码后输出对应文本。 6545 6546> **说明:** 6547> 6548> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.decodeSync<sup>9+</sup>](#decodesync9)替代。 6549 6550**系统能力:** SystemCapability.Utils.Lang 6551 6552**参数:** 6553 6554| 参数名 | 类型 | 必填 | 说明 | 6555| -------- | -------- | -------- | -------- | 6556| src | Uint8Array \| string | 是 | 解码输入Uint8数组或者字符串。 | 6557 6558**返回值:** 6559 6560| 类型 | 说明 | 6561| -------- | -------- | 6562| Uint8Array | 返回解码后新分配的Uint8数组。 | 6563 6564**示例:** 6565 6566 ```ts 6567 let base64 = new util.Base64(); 6568 let buff = 'czEz'; 6569 let result = base64.decodeSync(buff); 6570 console.info("result = " + result); 6571 // 输出结果:result = 115,49,51 6572 ``` 6573 6574### encode<sup>(deprecated)</sup> 6575 6576encode(src: Uint8Array): Promise<Uint8Array> 6577 6578通过输入参数异步编码后输出对应文本。 6579 6580> **说明:** 6581> 6582> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.encode<sup>9+</sup>](#encode9)替代。 6583 6584**系统能力:** SystemCapability.Utils.Lang 6585 6586**参数:** 6587 6588| 参数名 | 类型 | 必填 | 说明 | 6589| -------- | -------- | -------- | -------- | 6590| src | Uint8Array | 是 | 异步编码输入Uint8数组。 | 6591 6592**返回值:** 6593 6594| 类型 | 说明 | 6595| -------- | -------- | 6596| Promise<Uint8Array> | 返回异步编码后新分配的Uint8数组。 | 6597 6598**示例:** 6599 6600 ```ts 6601 let base64 = new util.Base64(); 6602 let array = new Uint8Array([115,49,51]); 6603 base64.encode(array).then((val) => { 6604 console.info(val.toString()); 6605 // 输出结果:99,122,69,122 6606 }) 6607 ``` 6608 6609### encodeToString<sup>(deprecated)</sup> 6610 6611encodeToString(src: Uint8Array): Promise<string> 6612 6613通过输入参数异步编码后输出对应文本。 6614 6615> **说明:** 6616> 6617> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.encodeToString<sup>9+</sup>](#encodetostring9)替代。 6618 6619**系统能力:** SystemCapability.Utils.Lang 6620 6621**参数:** 6622 6623| 参数名 | 类型 | 必填 | 说明 | 6624| -------- | -------- | -------- | -------- | 6625| src | Uint8Array | 是 | 异步编码输入Uint8数组。 | 6626 6627**返回值:** 6628 6629| 类型 | 说明 | 6630| -------- | -------- | 6631| Promise<string> | 返回异步编码后的字符串。 | 6632 6633**示例:** 6634 6635 ```ts 6636 let base64 = new util.Base64(); 6637 let array = new Uint8Array([115,49,51]); 6638 base64.encodeToString(array).then((val) => { 6639 console.info(val); 6640 // 输出结果:czEz 6641 }) 6642 ``` 6643 6644### decode<sup>(deprecated)</sup> 6645 6646 6647decode(src: Uint8Array | string): Promise<Uint8Array> 6648 6649将输入参数异步解码后输出对应文本。 6650 6651> **说明:** 6652> 6653> 从API version 8开始支持,从API version 9开始废弃,建议使用[Base64Helper.decode<sup>9+</sup>](#decode9)替代。 6654 6655**系统能力:** SystemCapability.Utils.Lang 6656 6657**参数:** 6658 6659| 参数名 | 类型 | 必填 | 说明 | 6660| -------- | -------- | -------- | -------- | 6661| src | Uint8Array \| string | 是 | 异步解码输入Uint8数组或者字符串。 | 6662 6663**返回值:** 6664 6665| 类型 | 说明 | 6666| -------- | -------- | 6667| Promise<Uint8Array> | 返回异步解码后新分配的Uint8数组。 | 6668 6669**示例:** 6670 6671 ```ts 6672 let base64 = new util.Base64(); 6673 let array = new Uint8Array([99,122,69,122]); 6674 base64.decode(array).then((val) => { 6675 console.info(val.toString()); 6676 // 输出结果:115,49,51 6677 }) 6678 ``` 6679