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