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