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