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