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