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