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, and [types<sup>8+</sup>](#types8) for built-in object type check. 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 '@ohos.util'; 14``` 15 16## util.format<sup>9+</sup> 17 18format(format: string, ...args: Object[]): string 19 20Formats a string by replacing the placeholders in it. 21 22**System capability**: SystemCapability.Utils.Lang 23 24**Parameters** 25 26| Name | Type | Mandatory| Description | 27| ------- | -------- | ---- | -------------- | 28| format | string | Yes | Format string. This string contains zero or more placeholders, which specify the position and format of the arguments to be inserted.| 29| ...args | Object[] | No | Data used to replace the placeholders in **format**. If **null** is passed in, the first argument is returned by default.| 30 31**Return value** 32 33| Type | Description | 34| ------ | -----------------| 35| string | Formatted string.| 36 37 38**Format Specifiers** 39 40| Specifier| Description | 41| ------ | -------------------------------- | 42| %s | Converts a parameter into a string for all values except **Object**, **BigInt**, and **-0**.| 43| %d | Converts a parameter into a decimal integer for all values except **Symbol** and **BigInt**.| 44| %i | Converts a string into a decimal integer for all values except **Symbol** and **BigInt**.| 45| %f | Converts a string into a floating point number for all values except **Symbol** and **BigInt**.| 46| %j | Converts a JavaScript object into a JSON string.| 47| %o | Converts a JavaScript object into a string, without containing the prototype chain information of the object.| 48| %O | Converts a JavaScript object into a string.| 49| %c | Valid only in the browser. It is ignored in other environments.| 50| %% | Placeholder for escaping the percent sign.| 51 52**Example** 53 54```ts 55let name = 'John'; 56let age = 20; 57let formattedString = util.format('My name is %s and I am %s years old', name, age); 58console.log(formattedString); 59// Output: My name is John and I am 20 years old 60let num = 10.5; 61formattedString = util.format('The number is %d', num); 62console.log(formattedString); 63// Output: The number is 10.5. 64num = 100.5; 65formattedString = util.format('The number is %i', num); 66console.log(formattedString); 67// Output: The number is 100. 68const pi = 3.141592653; 69formattedString = util.format('The value of pi is %f', pi); 70console.log(formattedString); 71// Output: The value of pi is 3.141592653 72const obj = { name: 'John', age: 20 }; 73formattedString = util.format('The object is %j', obj); 74console.log(formattedString); 75// Output: The object is {"name":"John","age":20}. 76const person = { 77 name: 'John', 78 age: 20, 79 address: { 80 city: 'New York', 81 country: 'USA' 82 } 83}; 84console.log(util.format('Formatted object using %%O: %O', person)); 85console.log(util.format('Formatted object using %%o: %o', person)); 86/* 87Output: 88Formatted object using %O: { name: 'John', 89 age: 20, 90 address: 91 { city: 'New York', 92 country: 'USA' } } 93Formatted object using %o: { name: 'John', 94 age: 20, 95 address: 96 { city: 'New York', 97 country: 'USA' } } 98*/ 99const percentage = 80; 100let arg = 'homework'; 101formattedString = util.format('John finished %d%% of the %s', percentage, arg); 102console.log(formattedString); 103// Output: John finished 80% of the homework 104``` 105 106## util.errnoToString<sup>9+</sup> 107 108errnoToString(errno: number): string 109 110Obtains detailed information about a system error code. 111 112**System capability**: SystemCapability.Utils.Lang 113 114**Parameters** 115 116| Name| Type | Mandatory| Description | 117| ------ | ------ | ---- | -------------------------- | 118| errno | number | Yes | Error code generated.| 119 120**Return value** 121 122| Type | Description | 123| ------ | ---------------------- | 124| string | Detailed information about the error code.| 125 126**Example** 127 128```ts 129let errnum = -1; // -1 is a system error code. 130let result = util.errnoToString(errnum); 131console.log("result = " + result); 132``` 133 134**Some error code and message examples** 135 136| Error Code| Message | 137| ------ | -------------------------------- | 138| -1 | operation not permitted | 139| -2 | no such file or directory | 140| -3 | no such process | 141| -4 | interrupted system call | 142| -5 | i/o error | 143| -11 | resource temporarily unavailable | 144| -12 | not enough memory | 145| -13 | permission denied | 146| -100 | network is down | 147 148## util.callbackWrapper 149 150callbackWrapper(original: Function): (err: Object, value: Object )=>void 151 152Calls 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. 153 154**System capability**: SystemCapability.Utils.Lang 155 156**Parameters** 157 158| Name| Type| Mandatory| Description| 159| -------- | -------- | -------- | -------- | 160| original | Function | Yes| Asynchronous function.| 161 162**Return value** 163 164| Type| Description| 165| -------- | -------- | 166| 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.| 167 168**Example** 169 170```ts 171async function fn() { 172 return 'hello world'; 173} 174let cb = util.callbackWrapper(fn); 175cb(1, (err : Object, ret : string) => { 176 if (err) throw new Error; 177 console.log(ret); 178}); 179``` 180 181## util.promisify<sup>9+</sup> 182 183promisify(original: (err: Object, value: Object) => void): Function 184 185Processes an asynchronous function and returns a promise. 186 187**System capability**: SystemCapability.Utils.Lang 188 189**Parameters** 190 191| Name| Type| Mandatory| Description| 192| -------- | -------- | -------- | -------- | 193| 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. | 194 195**Return value** 196 197| Type| Description| 198| -------- | -------- | 199| Function | Promise function.| 200 201**Example** 202 203```ts 204async function fn() { 205 return 'hello world'; 206} 207const addCall = util.promisify(util.callbackWrapper(fn)); 208(async () => { 209 try { 210 let res: string = await addCall(); 211 console.log(res); 212 } catch (err) { 213 console.log(err); 214 } 215})(); 216``` 217 218## util.generateRandomUUID<sup>9+</sup> 219 220generateRandomUUID(entropyCache?: boolean): string 221 222Uses a secure random number generator to generate a random universally unique identifier (UUID) of the string type in RFC 4122 version 4. 223 224**System capability**: SystemCapability.Utils.Lang 225 226**Parameters** 227 228| Name| Type| Mandatory| Description| 229| -------- | -------- | -------- | -------- | 230| entropyCache | boolean | No| Whether a cached UUID can be used. The default value is **true**.| 231 232**Return value** 233 234| Type| Description| 235| -------- | -------- | 236| string | A string representing the UUID generated.| 237 238**Example** 239 240```ts 241let uuid = util.generateRandomUUID(true); 242console.log("RFC 4122 Version 4 UUID:" + uuid); 243// Output a random UUID. 244``` 245 246## util.generateRandomBinaryUUID<sup>9+</sup> 247 248generateRandomBinaryUUID(entropyCache?: boolean): Uint8Array 249 250Uses a secure random number generator to generate a random UUID of the Uint8Array type in RFC 4122 version 4. 251 252**System capability**: SystemCapability.Utils.Lang 253 254**Parameters** 255 256| Name| Type| Mandatory| Description| 257| -------- | -------- | -------- | -------- | 258| entropyCache | boolean | No| Whether a cached UUID can be used. The default value is **true**.| 259 260**Return value** 261 262| Type| Description| 263| -------- | -------- | 264| Uint8Array | A Uint8Array value representing the UUID generated.| 265 266**Example** 267 268```ts 269let uuid = util.generateRandomBinaryUUID(true); 270console.log(JSON.stringify(uuid)); 271// Output: 272// 138,188,43,243,62,254,70,119,130,20,235,222,199,164,140,150 273``` 274 275## util.parseUUID<sup>9+</sup> 276 277parseUUID(uuid: string): Uint8Array 278 279Converts 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. 280 281**System capability**: SystemCapability.Utils.Lang 282 283**Parameters** 284 285| Name| Type| Mandatory| Description| 286| -------- | -------- | -------- | -------- | 287| uuid | string | Yes| A string representing the UUID.| 288 289**Return value** 290 291| Type| Description| 292| -------- | -------- | 293| Uint8Array | A Uint8Array value representing the UUID parsed. If the parsing fails, **SyntaxError** is thrown.| 294 295**Example** 296 297```ts 298let uuid = util.parseUUID("84bdf796-66cc-4655-9b89-d6218d100f9c"); 299console.log(JSON.stringify(uuid)); 300// Output: 301// 132,189,247,150,102,204,70,85,155,137,214,33,141,16,15,156 302``` 303 304## util.printf<sup>(deprecated)</sup> 305 306printf(format: string, ...args: Object[]): string 307 308Formats a string by replacing the placeholders in it. 309 310> **NOTE** 311> 312> 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. 313 314**System capability**: SystemCapability.Utils.Lang 315 316**Parameters** 317 318| Name| Type| Mandatory| Description| 319| -------- | -------- | -------- | -------- | 320| format | string | Yes| Format string.| 321| ...args | Object[] | No| Data used to replace the placeholders in **format**. If **null** is passed in, the first argument is returned by default.| 322 323**Return value** 324 325| Type| Description| 326| -------- | -------- | 327| string | String containing the formatted values.| 328 329**Example** 330 331```ts 332let res = util.printf("%s", "hello world!"); 333console.log(res); 334``` 335 336 337## util.getErrorString<sup>(deprecated)</sup> 338 339getErrorString(errno: number): string 340 341Obtains detailed information about a system error code. 342 343> **NOTE** 344> 345> 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. 346 347**System capability**: SystemCapability.Utils.Lang 348 349**Parameters** 350 351| Name| Type| Mandatory| Description| 352| -------- | -------- | -------- | -------- | 353| errno | number | Yes| Error code generated.| 354 355**Return value** 356 357| Type| Description| 358| -------- | -------- | 359| string | Detailed information about the error code.| 360 361**Example** 362 363```ts 364let errnum = -1; // -1 is a system error code. 365let result = util.getErrorString(errnum); 366console.log("result = " + result); 367``` 368 369## util.promiseWrapper<sup>(deprecated)</sup> 370 371promiseWrapper(original: (err: Object, value: Object) => void): Object 372 373Processes an asynchronous function and returns a promise. 374 375> **NOTE** 376> 377> This API is unavailable. You are advised to use [util.promisify<sup>9+</sup>](#utilpromisify9) instead. 378 379**System capability**: SystemCapability.Utils.Lang 380 381**Parameters** 382 383| Name| Type| Mandatory| Description| 384| -------- | -------- | -------- | -------- | 385| original | Function | Yes| Asynchronous function.| 386 387**Return value** 388 389| Type| Description| 390| -------- | -------- | 391| Function | Function in the error-first style (that is, **(err, value) =>...** is called as the last parameter) and the promise.| 392 393 394## TextDecoder 395 396Provides APIs to decode byte arrays into strings. It supports multiple formats, including UTF-8, UTF-16LE, UTF-16BE, ISO-8859, and Windows-1251. 397 398### Attributes 399 400**System capability**: SystemCapability.Utils.Lang 401 402| Name| Type| Readable| Writable| Description| 403| -------- | -------- | -------- | -------- | -------- | 404| encoding | string | Yes| No| Encoding format.<br>- Supported formats: 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-cyrilli, gbk, gb18030, big5, euc-jp, iso-2022-jp, shift_jis, euc-kr, utf-16be, utf-16le| 405| fatal | boolean | Yes| No| Whether to display fatal errors.| 406| 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.| 407 408### constructor<sup>9+</sup> 409 410constructor() 411 412A constructor used to create a **TextDecoder** object. 413 414**System capability**: SystemCapability.Utils.Lang 415 416**Example** 417 418```ts 419let result = new util.TextDecoder(); 420let retStr = result.encoding; 421``` 422### create<sup>9+</sup> 423 424create(encoding?: string, options?: { fatal?: boolean; ignoreBOM?: boolean }): TextDecoder 425 426Creates a **TextDecoder** object. It provides the same function as the deprecated argument constructor. 427 428**System capability**: SystemCapability.Utils.Lang 429 430**Parameters** 431 432| Name | Type | Mandatory| Description | 433| -------- | ------ | ---- | ------------------------------------------------ | 434| encoding | string | No | Encoding format. The default format is **'utf-8'**. | 435| options | object | No | Decoding-related options, which include **fatal** and **ignoreBOM**.| 436 437**Table 1.1** options 438 439| Name | Type| Mandatory| Description | 440| --------- | -------- | ---- | ------------------ | 441| fatal | boolean | No | Whether to display fatal errors. The default value is **false**.| 442| ignoreBOM | boolean | No | Whether to ignore the BOM. The default value is **false**. | 443 444**Example** 445 446```ts 447let result = util.TextDecoder.create('utf-8', { ignoreBOM : true }); 448let retStr = result.encoding; 449``` 450 451### decodeWithStream<sup>9+</sup> 452 453decodeWithStream(input: Uint8Array, options?: { stream?: boolean }): string 454 455Decodes the input content into a string. 456 457**System capability**: SystemCapability.Utils.Lang 458 459**Parameters** 460 461| Name| Type| Mandatory| Description| 462| -------- | -------- | -------- | -------- | 463| input | Uint8Array | Yes| Uint8Array object to decode.| 464| options | object | No| Decoding-related options.| 465 466**Table 2** options 467 468| Name| Type| Mandatory| Description| 469| -------- | -------- | -------- | -------- | 470| 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**.| 471 472**Return value** 473 474| Type| Description| 475| -------- | -------- | 476| string | String obtained.| 477 478**Example** 479 480```ts 481let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM : true }); 482let result = new Uint8Array(6); 483result[0] = 0xEF; 484result[1] = 0xBB; 485result[2] = 0xBF; 486result[3] = 0x61; 487result[4] = 0x62; 488result[5] = 0x63; 489console.log("input num:"); 490let retStr = textDecoder.decodeWithStream( result , {stream: false}); 491console.log("retStr = " + retStr); 492``` 493 494### constructor<sup>(deprecated)</sup> 495 496constructor(encoding?: string, options?: { fatal?: boolean; ignoreBOM?: boolean }) 497 498A constructor used to create a **TextDecoder** object. 499 500> **NOTE** 501> 502> 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. 503 504**System capability**: SystemCapability.Utils.Lang 505 506**Parameters** 507 508| Name| Type| Mandatory| Description| 509| -------- | -------- | -------- | -------- | 510| encoding | string | No| Encoding format. The default format is **'utf-8'**.| 511| options | object | No| Decoding-related options, which include **fatal** and **ignoreBOM**.| 512 513 **Table 1** options 514 515| Name| Type| Mandatory| Description| 516| -------- | -------- | -------- | -------- | 517| fatal | boolean | No| Whether to display fatal errors. The default value is **false**.| 518| ignoreBOM | boolean | No| Whether to ignore the BOM. The default value is **false**.| 519 520**Example** 521 522```ts 523let textDecoder = new util.TextDecoder("utf-8",{ignoreBOM: true}); 524``` 525 526### decode<sup>(deprecated)</sup> 527 528decode(input: Uint8Array, options?: { stream?: false }): string 529 530Decodes the input content into a string. 531 532> **NOTE** 533> 534> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [decodeWithStream<sup>9+</sup>](#decodewithstream9) instead. 535 536**System capability**: SystemCapability.Utils.Lang 537 538**Parameters** 539 540| Name| Type| Mandatory| Description| 541| -------- | -------- | -------- | -------- | 542| input | Uint8Array | Yes| Uint8Array object to decode.| 543| options | object | No| Decoding-related options.| 544 545**Table 2** options 546 547| Name| Type| Mandatory| Description| 548| -------- | -------- | -------- | -------- | 549| 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**.| 550 551**Return value** 552 553| Type| Description| 554| -------- | -------- | 555| string | String obtained.| 556 557**Example** 558 559```ts 560let textDecoder = new util.TextDecoder("utf-8",{ignoreBOM: true}); 561let result = new Uint8Array(6); 562result[0] = 0xEF; 563result[1] = 0xBB; 564result[2] = 0xBF; 565result[3] = 0x61; 566result[4] = 0x62; 567result[5] = 0x63; 568console.log("input num:"); 569let retStr = textDecoder.decode( result , {stream: false}); 570console.log("retStr = " + retStr); 571``` 572 573## TextEncoder 574 575Provides APIs to encode strings into byte arrays. It supports multiple formats, including UTF-8, UTF-16LE, and UTF-16BE. When **TextEncoder** is used for encoding, the number of bytes occupied by a character varies according to the encoding format. For example, a Chinese character usually occupies three bytes in UTF-8 encoding format but two bytes in UTF-16LE or UTF-16BE encoding format. Therefore, when using **TextEncoder**, you must explicitly specify the encoding format to obtain the required encoding result. 576 577### Attributes 578 579**System capability**: SystemCapability.Utils.Lang 580 581| Name| Type| Readable| Writable| Description| 582| -------- | -------- | -------- | -------- | -------- | 583| encoding | string | Yes| No| Encoding format. The default format is **'utf-8'**.| 584 585 586### constructor 587 588constructor() 589 590A constructor used to create a **TextEncoder** object. 591 592**System capability**: SystemCapability.Utils.Lang 593 594**Example** 595 596```ts 597let textEncoder = new util.TextEncoder(); 598``` 599 600### constructor<sup>9+</sup> 601 602constructor(encoding?: string) 603 604A constructor used to create a **TextEncoder** object. 605 606**System capability**: SystemCapability.Utils.Lang 607 608**Parameters** 609 610| Name| Type| Mandatory| Description| 611| ----- | ---- | ---- | ---- | 612| encoding | string | No| Encoding format. The default format is **'utf-8'**.| 613 614**Example** 615 616```ts 617let textEncoder = new util.TextEncoder("utf-8"); 618``` 619 620### encodeInto<sup>9+</sup> 621 622encodeInto(input?: string): Uint8Array 623 624Encodes the input content into a Uint8Array object. 625 626**System capability**: SystemCapability.Utils.Lang 627 628**Parameters** 629 630| Name| Type | Mandatory| Description | 631| ------ | ------ | ---- | ------------------ | 632| input | string | No | String to encode. The default value is an empty string.| 633 634**Return value** 635 636| Type | Description | 637| ---------- | ------------------ | 638| Uint8Array | Uint8Array object obtained.| 639 640**Example** 641 642```ts 643let textEncoder = new util.TextEncoder(); 644let buffer = new ArrayBuffer(20); 645let result = new Uint8Array(buffer); 646result = textEncoder.encodeInto("\uD800¥¥"); 647``` 648 649### encodeIntoUint8Array<sup>9+</sup> 650 651encodeIntoUint8Array(input: string, dest: Uint8Array): { read: number; written: number } 652 653Encodes the input content into a Uint8Array object. 654 655**System capability**: SystemCapability.Utils.Lang 656 657**Parameters** 658 659| Name| Type | Mandatory| Description | 660| ------ | ---------- | ---- | ------------------------------------------------------- | 661| input | string | Yes | String to encode. | 662| dest | Uint8Array | Yes | Uint8Array object used to store the UTF-8 encoded text.| 663 664**Return value** 665 666| Type | Description | 667| --------- | ------------------ | 668| object | Object obtained. **read** indicates the number of encoded characters, and **write** indicates the number of bytes in the encoded characters.| 669 670**Example** 671 672```ts 673let that = new util.TextEncoder(); 674let buffer = new ArrayBuffer(4); 675let dest = new Uint8Array(buffer); 676let result = new Object(); 677result = that.encodeIntoUint8Array('abcd', dest); 678``` 679 680### encodeInto<sup>(deprecated)</sup> 681 682encodeInto(input: string, dest: Uint8Array): { read: number; written: number } 683 684Stores the UTF-8 encoded text. 685 686> **NOTE** 687> 688> 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. 689 690**System capability**: SystemCapability.Utils.Lang 691 692**Parameters** 693 694| Name| Type| Mandatory| Description| 695| -------- | -------- | -------- | -------- | 696| input | string | Yes| String to encode.| 697| dest | Uint8Array | Yes| Uint8Array object used to store the UTF-8 encoded text.| 698 699**Return value** 700 701| Type| Description| 702| -------- | -------- | 703| Uint8Array | Uint8Array object obtained.| 704 705**Example** 706 707```ts 708let that = new util.TextEncoder(); 709let buffer = new ArrayBuffer(4); 710let dest = new Uint8Array(buffer); 711let result = new Object(); 712result = that.encodeInto('abcd', dest); 713``` 714 715### encode<sup>(deprecated)</sup> 716 717encode(input?: string): Uint8Array 718 719Encodes the input content in to a Uint8Array object. 720 721> **NOTE** 722> 723> 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. 724 725**System capability**: SystemCapability.Utils.Lang 726 727**Parameters** 728 729| Name| Type| Mandatory| Description| 730| -------- | -------- | -------- | -------- | 731| input | string | No| String to encode. The default value is an empty string.| 732 733**Return value** 734 735| Type| Description| 736| -------- | -------- | 737| Uint8Array | Uint8Array object obtained.| 738 739**Example** 740 741```ts 742let textEncoder = new util.TextEncoder(); 743let buffer = new ArrayBuffer(20); 744let result = new Uint8Array(buffer); 745result = textEncoder.encode("\uD800¥¥"); 746``` 747 748## RationalNumber<sup>8+</sup> 749 750Provides 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. 751 752### constructor<sup>9+</sup> 753 754constructor() 755 756A constructor used to create a **RationalNumber** object. 757 758**System capability**: SystemCapability.Utils.Lang 759 760**Example** 761 762```ts 763let rationalNumber = new util.RationalNumber(); 764``` 765 766### parseRationalNumber<sup>9+</sup> 767 768parseRationalNumber(numerator: number,denominator: number): RationalNumber 769 770Create a **RationalNumber** instance with a given numerator and denominator. 771 772**System capability**: SystemCapability.Utils.Lang 773 774**Parameters** 775 776| Name | Type | Mandatory| Description | 777| ----------- | ------ | ---- | ---------------- | 778| numerator | number | Yes | Numerator, which is an integer.| 779| denominator | number | Yes | Denominator, which is an integer.| 780 781**Example** 782 783```ts 784let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 785``` 786 787### createRationalFromString<sup>8+</sup> 788 789static createRationalFromString(rationalString: string): RationalNumber 790 791Creates a **RationalNumber** object based on the given string. 792 793**System capability**: SystemCapability.Utils.Lang 794 795**Parameters** 796 797| Name| Type| Mandatory| Description| 798| -------- | -------- | -------- | -------- | 799| rationalString | string | Yes| String used to create the **RationalNumber** object.| 800 801**Return value** 802 803| Type| Description| 804| -------- | -------- | 805| Object | **RationalNumber** object obtained.| 806 807**Example** 808 809```ts 810let rational = util.RationalNumber.createRationalFromString("3/4"); 811``` 812 813### compare<sup>9+</sup> 814 815compare(another: RationalNumber): number 816 817Compares this **RationalNumber** object with another **RationalNumber** object. 818 819**System capability**: SystemCapability.Utils.Lang 820 821**Parameters** 822 823| Name | Type | Mandatory| Description | 824| ------- | -------------- | ---- | ------------------ | 825| another | [RationalNumber](#rationalnumber8) | Yes | Object used to compare with this **RationalNumber** object.| 826 827**Return value** 828 829| Type | Description | 830| ------ | ------------------------------------------------------------ | 831| 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.| 832 833**Example** 834 835```ts 836let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 837let rational = util.RationalNumber.createRationalFromString("3/4"); 838let result = rationalNumber.compare(rational); 839console.log("result = " + result); 840// Output: result = -1 841``` 842 843### valueOf<sup>8+</sup> 844 845valueOf(): number 846 847Obtains the value of this **RationalNumber** object as an integer or a floating-point number. 848 849**System capability**: SystemCapability.Utils.Lang 850 851**Return value** 852 853| Type| Description| 854| -------- | -------- | 855| number | An integer or a floating-point number.| 856 857**Example** 858 859```ts 860let rationalNumber = new util.RationalNumber(1,2); 861let result = rationalNumber.valueOf(); 862console.log("result = " + result); 863// Output: result = 0.5 864``` 865You are advised to use the following code snippet for API version 9 and later versions: 866```ts 867let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 868let result = rationalNumber.valueOf(); 869console.log("result = " + result); 870// Output: result = 0.5 871``` 872 873### equals<sup>8+</sup> 874 875equals(obj: Object): boolean 876 877Checks whether this **RationalNumber** object equals the given object. 878 879**System capability**: SystemCapability.Utils.Lang 880 881**Parameters** 882 883| Name| Type| Mandatory| Description| 884| -------- | -------- | -------- | -------- | 885| object | Object | Yes| Object used to compare with this **RationalNumber** object.| 886 887**Return value** 888 889| Type| Description| 890| -------- | -------- | 891| boolean | Returns **true** if the two objects are equal; returns **false** otherwise.| 892 893**Example** 894 895```ts 896let rationalNumber = new util.RationalNumber(1,2); 897let rational = util.RationalNumber.createRationalFromString("3/4"); 898let result = rationalNumber.equals(rational); 899console.log("result = " + result); 900// Output: result = false 901``` 902You are advised to use the following code snippet for API version 9 and later versions: 903```ts 904let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 905let rational = util.RationalNumber.createRationalFromString("3/4"); 906let result = rationalNumber.equals(rational); 907console.log("result = " + result); 908// Output: result = false 909``` 910 911### getCommonFactor<sup>9+</sup> 912 913getCommonFactor(number1: number,number2: number): number 914 915Obtains the greatest common divisor of two specified integers. 916 917**System capability**: SystemCapability.Utils.Lang 918 919**Parameters** 920 921| Name | Type | Mandatory| Description | 922| ------- | ------ | ---- | ---------- | 923| number1 | number | Yes | The first integer used to get the greatest common divisor.| 924| number2 | number | Yes | The second integer used to get the greatest common divisor.| 925 926**Return value** 927 928| Type | Description | 929| ------ | ------------------------------ | 930| number | Greatest common divisor obtained.| 931 932**Example** 933 934```ts 935let result = util.RationalNumber.getCommonFactor(4,6); 936console.log("result = " + result); 937// Output: result = 2 938``` 939 940### getNumerator<sup>8+</sup> 941 942getNumerator(): number 943 944Obtains the numerator of this **RationalNumber** object. 945 946**System capability**: SystemCapability.Utils.Lang 947 948**Return value** 949 950| Type| Description| 951| -------- | -------- | 952| number | Numerator of this **RationalNumber** object.| 953 954**Example** 955 956```ts 957let rationalNumber = new util.RationalNumber(1,2); 958let result = rationalNumber.getNumerator(); 959console.log("result = " + result); 960// Output: result = 1 961``` 962You are advised to use the following code snippet for API version 9 and later versions: 963```ts 964let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 965let result = rationalNumber.getNumerator(); 966console.log("result = " + result); 967// Output: result = 1 968``` 969 970### getDenominator<sup>8+</sup> 971 972getDenominator(): number 973 974Obtains the denominator of this **RationalNumber** object. 975 976**System capability**: SystemCapability.Utils.Lang 977 978**Return value** 979 980| Type| Description| 981| -------- | -------- | 982| number | Denominator of this **RationalNumber** object.| 983 984**Example** 985 986```ts 987let rationalNumber = new util.RationalNumber(1,2); 988let result = rationalNumber.getDenominator(); 989console.log("result = " + result); 990// Output: result = 2 991``` 992You are advised to use the following code snippet for API version 9 and later versions: 993```ts 994let rationalNumber = util.RationalNumber.parseRationalNumber(1,2) 995let result = rationalNumber.getDenominator(); 996console.log("result = " + result); 997// Output: result = 2 998``` 999 1000### isZero<sup>8+</sup> 1001 1002isZero():boolean 1003 1004Checks whether this **RationalNumber** object is **0**. 1005 1006**System capability**: SystemCapability.Utils.Lang 1007 1008**Return value** 1009 1010| Type| Description| 1011| -------- | -------- | 1012| boolean | Returns **true** if the value of this **RationalNumber** object is **0**; returns **false** otherwise.| 1013 1014**Example** 1015 1016```ts 1017let rationalNumber = new util.RationalNumber(1,2); 1018let result = rationalNumber.isZero(); 1019console.log("result = " + result); 1020// Output: result = false 1021``` 1022You are advised to use the following code snippet for API version 9 and later versions: 1023```ts 1024let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1025let result = rationalNumber.isZero(); 1026console.log("result = " + result); 1027// Output: result = false 1028``` 1029 1030### isNaN<sup>8+</sup> 1031 1032isNaN(): boolean 1033 1034Checks whether this **RationalNumber** object is a Not a Number (NaN). 1035 1036**System capability**: SystemCapability.Utils.Lang 1037 1038**Return value** 1039 1040| Type| Description| 1041| -------- | -------- | 1042| boolean | Returns **true** if this **RationalNumber** object is a NaN (the denominator and numerator are both **0**); returns **false** otherwise.| 1043 1044**Example** 1045 1046```ts 1047let rationalNumber = new util.RationalNumber(1,2); 1048let result = rationalNumber.isNaN(); 1049console.log("result = " + result); 1050// Output: result = false 1051``` 1052You are advised to use the following code snippet for API version 9 and later versions: 1053```ts 1054let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1055let result = rationalNumber.isNaN(); 1056console.log("result = " + result); 1057// Output: result = false 1058``` 1059 1060### isFinite<sup>8+</sup> 1061 1062isFinite():boolean 1063 1064Checks whether this **RationalNumber** object represents a finite value. 1065 1066**System capability**: SystemCapability.Utils.Lang 1067 1068**Return value** 1069 1070| Type| Description| 1071| -------- | -------- | 1072| boolean | Returns **true** if this **RationalNumber** object represents a finite value (the denominator is not **0**); returns **false** otherwise.| 1073 1074**Example** 1075 1076```ts 1077let rationalNumber = new util.RationalNumber(1,2); 1078let result = rationalNumber.isFinite(); 1079console.log("result = " + result); 1080// Output: result = true 1081``` 1082You are advised to use the following code snippet for API version 9 and later versions: 1083```ts 1084let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1085let result = rationalNumber.isFinite(); 1086console.log("result = " + result); 1087// Output: result = true 1088``` 1089 1090### toString<sup>8+</sup> 1091 1092toString(): string 1093 1094Obtains the string representation of this **RationalNumber** object. 1095 1096**System capability**: SystemCapability.Utils.Lang 1097 1098**Return value** 1099 1100| Type| Description| 1101| -------- | -------- | 1102| 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**.| 1103 1104**Example** 1105 1106```ts 1107let rationalNumber = new util.RationalNumber(1,2); 1108let result = rationalNumber.toString(); 1109console.log("result = " + result); 1110// Output: result = 1/2 1111``` 1112You are advised to use the following code snippet for API version 9 and later versions: 1113```ts 1114let rationalNumber = util.RationalNumber.parseRationalNumber(1,2); 1115let result = rationalNumber.toString(); 1116console.log("result = " + result); 1117// Output: result = 1/2 1118``` 1119 1120### constructor<sup>(deprecated)</sup> 1121 1122constructor(numerator: number,denominator: number) 1123 1124A constructor used to create a **RationalNumber** object. 1125 1126> **NOTE** 1127> 1128> 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. 1129 1130**System capability**: SystemCapability.Utils.Lang 1131 1132**Parameters** 1133 1134| Name| Type| Mandatory| Description| 1135| -------- | -------- | -------- | -------- | 1136| numerator | number | Yes| Numerator, which is an integer.| 1137| denominator | number | Yes| Denominator, which is an integer.| 1138 1139**Example** 1140 1141```ts 1142let rationalNumber = new util.RationalNumber(1,2); 1143``` 1144 1145### compareTo<sup>(deprecated)</sup> 1146 1147compareTo(another: RationalNumber): number 1148 1149Compares this **RationalNumber** object with a given object. 1150 1151> **NOTE** 1152> 1153> 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. 1154 1155**System capability**: SystemCapability.Utils.Lang 1156 1157**Parameters** 1158 1159| Name| Type| Mandatory| Description| 1160| -------- | -------- | -------- | -------- | 1161| another | RationalNumber | Yes| Object used to compare with this **RationalNumber** object.| 1162 1163**Return value** 1164 1165| Type| Description| 1166| -------- | -------- | 1167| 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.| 1168 1169**Example** 1170 1171```ts 1172let rationalNumber = new util.RationalNumber(1,2); 1173let rational = util.RationalNumber.createRationalFromString("3/4"); 1174let result = rationalNumber.compareTo(rational); 1175``` 1176 1177### getCommonDivisor<sup>(deprecated)</sup> 1178 1179static getCommonDivisor(number1: number,number2: number): number 1180 1181Obtains the greatest common divisor of two specified integers. 1182 1183> **NOTE** 1184> 1185> 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. 1186 1187**System capability**: SystemCapability.Utils.Lang 1188 1189**Parameters** 1190 1191| Name| Type| Mandatory| Description| 1192| -------- | -------- | -------- | -------- | 1193| number1 | number | Yes| The first integer used to get the greatest common divisor.| 1194| number2 | number | Yes| The second integer used to get the greatest common divisor.| 1195 1196**Return value** 1197 1198| Type| Description| 1199| -------- | -------- | 1200| number | Greatest common divisor obtained.| 1201 1202**Example** 1203 1204```ts 1205let rationalNumber = new util.RationalNumber(1,2); 1206let result = util.RationalNumber.getCommonDivisor(4,6); 1207``` 1208 1209## LRUCache<sup>9+</sup> 1210 1211Provides 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. 1212 1213### Attributes 1214 1215**System capability**: SystemCapability.Utils.Lang 1216 1217| Name | Type | Readable| Writable| Description | 1218| ------ | ------ | ---- | ---- | ---------------------- | 1219| length | number | Yes | No | Total number of values in this cache.| 1220 1221**Example** 1222 1223```ts 1224let pro : util.LRUCache<number, number> = new util.LRUCache(); 1225pro.put(2,10); 1226pro.put(1,8); 1227let result = pro.length; 1228``` 1229 1230### constructor<sup>9+</sup> 1231 1232constructor(capacity?: number) 1233 1234A constructor used to create a **LruCache** instance. The default capacity of the cache is 64. 1235 1236**System capability**: SystemCapability.Utils.Lang 1237 1238**Parameters** 1239 1240| Name | Type | Mandatory| Description | 1241| -------- | ------ | ---- | ---------------------------- | 1242| capacity | number | No | Capacity of the cache to create. The default value is **64**.| 1243 1244**Example** 1245 1246```ts 1247let lrubuffer : util.LRUCache<number, number> = new util.LRUCache(); 1248``` 1249 1250 1251### updateCapacity<sup>9+</sup> 1252 1253updateCapacity(newCapacity: number): void 1254 1255Changes the cache capacity. If the new capacity is less than or equal to **0**, an exception will be thrown. 1256 1257**System capability**: SystemCapability.Utils.Lang 1258 1259**Parameters** 1260 1261| Name | Type | Mandatory| Description | 1262| ----------- | ------ | ---- | ---------------------------- | 1263| newCapacity | number | Yes | New capacity of the cache.| 1264 1265**Example** 1266 1267```ts 1268let pro: util.LRUCache<number, number> = new util.LRUCache(); 1269pro.updateCapacity(100); 1270``` 1271 1272### toString<sup>9+</sup> 1273 1274toString(): string 1275 1276Obtains the string representation of this cache. 1277 1278**System capability**: SystemCapability.Utils.Lang 1279 1280**Return value** 1281 1282| Type | Description | 1283| ------ | -------------------------- | 1284| string | String representation of this cache.| 1285 1286**Example** 1287 1288```ts 1289let pro: util.LRUCache<number, number> = new util.LRUCache(); 1290pro.put(2,10); 1291pro.get(2); 1292pro.get(3); 1293console.log(pro.toString()); 1294// Output: LRUCache[ maxSize = 64, hits = 1, misses = 1, hitRate = 50% ] 1295// maxSize: maximum size of the cache. hits: number of matched queries. misses: number of mismatched queries. hitRate: matching rate. 1296``` 1297 1298### getCapacity<sup>9+</sup> 1299 1300getCapacity(): number 1301 1302Obtains the capacity of this cache. 1303 1304**System capability**: SystemCapability.Utils.Lang 1305 1306**Return value** 1307 1308| Type | Description | 1309| ------ | ---------------------- | 1310| number | Capacity of the cache.| 1311 1312**Example** 1313 1314```ts 1315let pro: util.LRUCache<number, number> = new util.LRUCache(); 1316let result = pro.getCapacity(); 1317``` 1318 1319### clear<sup>9+</sup> 1320 1321clear(): void 1322 1323Clears key-value pairs from this cache. The **afterRemoval()** method will be called to perform subsequent operations. 1324 1325**System capability**: SystemCapability.Utils.Lang 1326 1327**Example** 1328 1329```ts 1330let pro: util.LRUCache<number, number> = new util.LRUCache(); 1331pro.put(2,10); 1332let result = pro.length; 1333pro.clear(); 1334``` 1335 1336### getCreateCount<sup>9+</sup> 1337 1338getCreateCount(): number 1339 1340Obtains the number of times that an object is created. 1341 1342**System capability**: SystemCapability.Utils.Lang 1343 1344**Return value** 1345 1346| Type | Description | 1347| ------ | -------------------| 1348| number | Number of times that objects are created.| 1349 1350**Example** 1351 1352```ts 1353// Create the ChildLRUCache class that inherits LRUCache, and override createDefault() to return a non-undefined value. 1354class ChildLRUCache extends util.LRUCache<number, number> { 1355 constructor() { 1356 super(); 1357 } 1358 1359 createDefault(key: number): number { 1360 return key; 1361 } 1362} 1363let lru = new ChildLRUCache(); 1364lru.put(2,10); 1365lru.get(3); 1366lru.get(5); 1367let res = lru.getCreateCount(); 1368``` 1369 1370### getMissCount<sup>9+</sup> 1371 1372getMissCount(): number 1373 1374Obtains the number of times that the queried values are mismatched. 1375 1376**System capability**: SystemCapability.Utils.Lang 1377 1378**Return value** 1379 1380| Type | Description | 1381| ------ | ------------------------ | 1382| number | Number of times that the queried values are mismatched.| 1383 1384**Example** 1385 1386```ts 1387let pro: util.LRUCache<number, number> = new util.LRUCache(); 1388pro.put(2,10); 1389pro.get(2); 1390let result = pro.getMissCount(); 1391``` 1392 1393### getRemovalCount<sup>9+</sup> 1394 1395getRemovalCount(): number 1396 1397Obtains the number of times that key-value pairs in the cache are recycled. 1398 1399**System capability**: SystemCapability.Utils.Lang 1400 1401**Return value** 1402 1403| Type | Description | 1404| ------ | -------------------------- | 1405| number | Number of times that key-value pairs in the cache are recycled.| 1406 1407**Example** 1408 1409```ts 1410let pro: util.LRUCache<number, number> = new util.LRUCache(); 1411pro.put(2,10); 1412pro.updateCapacity(2); 1413pro.put(50,22); 1414let result = pro.getRemovalCount(); 1415``` 1416 1417### getMatchCount<sup>9+</sup> 1418 1419getMatchCount(): number 1420 1421Obtains the number of times that the queried values are matched. 1422 1423**System capability**: SystemCapability.Utils.Lang 1424 1425**Return value** 1426 1427| Type | Description | 1428| ------ | -------------------------- | 1429| number | Number of times that the queried values are matched.| 1430 1431**Example** 1432 1433 ```ts 1434 let pro: util.LRUCache<number, number> = new util.LRUCache(); 1435 pro.put(2,10); 1436 pro.get(2); 1437 let result = pro.getMatchCount(); 1438 ``` 1439 1440### getPutCount<sup>9+</sup> 1441 1442getPutCount(): number 1443 1444Obtains the number of additions to this cache. 1445 1446**System capability**: SystemCapability.Utils.Lang 1447 1448**Return value** 1449 1450| Type | Description | 1451| ------ | ---------------------------- | 1452| number | Number of additions to the cache.| 1453 1454**Example** 1455 1456```ts 1457let pro: util.LRUCache<number, number> = new util.LRUCache(); 1458pro.put(2,10); 1459let result = pro.getPutCount(); 1460``` 1461 1462### isEmpty<sup>9+</sup> 1463 1464isEmpty(): boolean 1465 1466Checks whether this cache is empty. 1467 1468**System capability**: SystemCapability.Utils.Lang 1469 1470**Return value** 1471 1472| Type | Description | 1473| ------- | ---------------------------------------- | 1474| boolean | Returns **true** if the cache does not contain any value.| 1475 1476**Example** 1477 1478```ts 1479let pro: util.LRUCache<number, number> = new util.LRUCache(); 1480pro.put(2,10); 1481let result = pro.isEmpty(); 1482``` 1483 1484### get<sup>9+</sup> 1485 1486get(key: K): V | undefined 1487 1488Obtains the value of the specified key. 1489 1490**System capability**: SystemCapability.Utils.Lang 1491 1492**Parameters** 1493 1494| Name| Type| Mandatory| Description | 1495| ------ | ---- | ---- | ------------ | 1496| key | K | Yes | Key based on which the value is queried.| 1497 1498**Return value** 1499 1500| Type | Description | 1501| ------------------------ | ------------------------------------------------------------ | 1502| V \| undefined | Returns the value of the key if a match is found in the cache; returns **undefined** otherwise.| 1503 1504**Example** 1505 1506```ts 1507let pro: util.LRUCache<number, number> = new util.LRUCache(); 1508pro.put(2,10); 1509let result = pro.get(2); 1510``` 1511 1512### put<sup>9+</sup> 1513 1514put(key: K,value: V): V 1515 1516Adds a key-value pair to this cache. 1517 1518**System capability**: SystemCapability.Utils.Lang 1519 1520**Parameters** 1521 1522| Name| Type| Mandatory| Description | 1523| ------ | ---- | ---- | -------------------------- | 1524| key | K | Yes | Key of the key-value pair to add. | 1525| value | V | Yes | Value of the key-value pair to add.| 1526 1527**Return value** 1528 1529| Type| Description | 1530| ---- | ------------------------------------------------------------ | 1531| 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**.| 1532 1533**Example** 1534 1535```ts 1536let pro: util.LRUCache<number, number> = new util.LRUCache(); 1537let result = pro.put(2,10); 1538``` 1539 1540### values<sup>9+</sup> 1541 1542values(): V[] 1543 1544Obtains all values in this cache, listed from the most to the least recently accessed. 1545 1546**System capability**: SystemCapability.Utils.Lang 1547 1548**Return value** 1549 1550| Type | Description | 1551| --------- | ------------------------------------------------------------ | 1552| V [] | All values in the cache, listed from the most to the least recently accessed.| 1553 1554**Example** 1555 1556```ts 1557let pro: util.LRUCache<number|string,number|string> = new util.LRUCache(); 1558pro.put(2,10); 1559pro.put(2,"anhu"); 1560pro.put("afaf","grfb"); 1561let result = pro.values(); 1562``` 1563 1564### keys<sup>9+</sup> 1565 1566keys(): K[] 1567 1568Obtains all keys in this cache, listed from the most to the least recently accessed. 1569 1570**System capability**: SystemCapability.Utils.Lang 1571 1572**Return value** 1573 1574| Type | Description | 1575| --------- | ------------------------------------------------------------ | 1576| K [] | All keys in the cache, listed from the most to the least recently accessed.| 1577 1578**Example** 1579 1580```ts 1581let pro: util.LRUCache<number, number> = new util.LRUCache(); 1582pro.put(2,10); 1583let result = pro.keys(); 1584``` 1585 1586### remove<sup>9+</sup> 1587 1588remove(key: K): V | undefined 1589 1590Removes the specified key and its value from this cache. 1591 1592**System capability**: SystemCapability.Utils.Lang 1593 1594**Parameters** 1595 1596| Name| Type| Mandatory| Description | 1597| ------ | ---- | ---- | -------------- | 1598| key | K | Yes | Key to remove.| 1599 1600**Return value** 1601 1602| Type | Description | 1603| ------------------------ | ------------------------------------------------------------ | 1604| 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**.| 1605 1606**Example** 1607 1608```ts 1609let pro: util.LRUCache<number, number> = new util.LRUCache(); 1610pro.put(2,10); 1611let result = pro.remove(20); 1612``` 1613 1614### afterRemoval<sup>9+</sup> 1615 1616afterRemoval(isEvict: boolean,key: K,value: V,newValue: V): void 1617 1618Performs subsequent operations after a value is removed. 1619 1620**System capability**: SystemCapability.Utils.Lang 1621 1622**Parameters** 1623 1624| Name | Type | Mandatory| Description | 1625| -------- | ------- | ---- | ------------------------------------------------------------ | 1626| isEvict | boolean | Yes | Whether the capacity is insufficient. If the value is **true**, this API is called due to insufficient capacity. | 1627| key | K | Yes | Key removed. | 1628| value | V | Yes | Value removed. | 1629| 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.| 1630 1631**Example** 1632 1633```ts 1634class ChildLRUCache<K, V> extends util.LRUCache<K, V> { 1635 constructor(capacity?: number) { 1636 super(capacity); 1637 } 1638 1639 afterRemoval(isEvict: boolean, key: K, value: V, newValue: V): void { 1640 if (isEvict === true) { 1641 console.info('key: ' + key); 1642 console.info('value: ' + value); 1643 console.info('newValue: ' + newValue); 1644 } 1645 } 1646} 1647let lru: ChildLRUCache<number, number>= new ChildLRUCache(2); 1648lru.put(1, 1); 1649lru.put(2, 2); 1650lru.put(3, 3); 1651``` 1652 1653### contains<sup>9+</sup> 1654 1655contains(key: K): boolean 1656 1657Checks whether this cache contains the specified key. 1658 1659**System capability**: SystemCapability.Utils.Lang 1660 1661**Parameters** 1662 1663| Name| Type | Mandatory| Description | 1664| ------ | ------ | ---- | ---------------- | 1665| key | K | Yes | Key to check.| 1666 1667**Return value** 1668 1669| Type | Description | 1670| ------- | ------------------------------------------ | 1671| boolean | Returns **true** if the cache contains the specified key; returns **false** otherwise.| 1672 1673**Example** 1674 1675```ts 1676let pro : util.LRUCache<number | object, number> = new util.LRUCache(); 1677pro.put(2,10); 1678class Lru{ 1679s : string = ""; 1680} 1681let obj : Lru = {s : "key" }; 1682let result = pro.contains(obj); 1683``` 1684 1685### createDefault<sup>9+</sup> 1686 1687createDefault(key: K): V 1688 1689Creates a value if the value of the specified key is not available. 1690 1691**System capability**: SystemCapability.Utils.Lang 1692 1693**Parameters** 1694 1695| Name| Type| Mandatory| Description | 1696| ------ | ---- | ---- | -------------- | 1697| key | K | Yes | Key of which the value is missing.| 1698 1699**Return value** 1700 1701| Type| Description | 1702| ---- | ------------------ | 1703| V | Value of the key.| 1704 1705**Example** 1706 1707```ts 1708let pro: util.LRUCache<number, number> = new util.LRUCache(); 1709let result = pro.createDefault(50); 1710``` 1711 1712### entries<sup>9+</sup> 1713 1714entries(): IterableIterator<[K,V]> 1715 1716Obtains a new iterator object that contains all key-value pairs in this object. 1717 1718**System capability**: SystemCapability.Utils.Lang 1719 1720**Return value** 1721 1722| Type | Description | 1723| ----------- | -------------------- | 1724| [K, V] | Iterable array.| 1725 1726**Example** 1727 1728```ts 1729let pro: util.LRUCache<number, number> = new util.LRUCache(); 1730pro.put(2,10); 1731pro.put(3,15); 1732let pair:Iterable<Object[]> = pro.entries(); 1733let arrayValue = Array.from(pair); 1734for (let value of arrayValue) { 1735 console.log(value[0]+ ', '+ value[1]); 1736} 1737``` 1738 1739### [Symbol.iterator]<sup>9+</sup> 1740 1741[Symbol.iterator]\(): IterableIterator<[K, V]> 1742 1743Obtains a two-dimensional array in key-value pairs. 1744 1745> **NOTE** 1746> 1747> This API cannot be used in .ets files. 1748 1749**System capability**: SystemCapability.Utils.Lang 1750 1751**Return value** 1752 1753| Type | Description | 1754| ----------- | ------------------------------ | 1755| [K, V] | Two-dimensional array in key-value pairs.| 1756 1757**Example** 1758 1759```ts 1760let pro: util.LRUCache<number, number> = new util.LRUCache(); 1761pro.put(2,10); 1762pro.put(3,15); 1763let pair:Iterable<Object[]> = pro[Symbol.iterator](); 1764let arrayValue = Array.from(pair); 1765for (let value of arrayValue) { 1766 console.log(value[0]+ ', '+ value[1]); 1767} 1768``` 1769 1770## ScopeComparable<sup>8+</sup> 1771 1772The values of the **ScopeComparable** type are used to implement the **compareTo** method. Therefore, ensure that the input parameters are comparable. 1773 1774**System capability**: SystemCapability.Utils.Lang 1775 1776### compareTo<sup>8+</sup> 1777 1778compareTo(other: ScopeComparable): boolean 1779 1780Compares two values and returns a Boolean value. 1781 1782**System capability**: SystemCapability.Utils.Lang 1783 1784**Parameters** 1785 1786| Name| Type| Mandatory| Description | 1787| ------ | ---- | ---- | -------------- | 1788| other | [ScopeComparable](#scopecomparable8) | Yes | The other value to be compared with the current value.| 1789 1790**Return value** 1791 1792| Type| Description | 1793| ---- | ------------------ | 1794| boolean | If the current value is greater than or equal to the input value, **true** is returned. Otherwise, **false** is returned.| 1795 1796**Example** 1797 1798Create a class to implement the **compareTo** method. The **Temperature** class is used as an example in the following sample code. 1799 1800```ts 1801class Temperature{ 1802 private readonly _temp: number; 1803 constructor(value : number) { 1804 this._temp = value; 1805 } 1806 compareTo(value : Temperature ) { 1807 return this._temp >= value.getTemp(); 1808 } 1809 getTemp() { 1810 return this._temp; 1811 } 1812 toString() : string { 1813 return this._temp.toString(); 1814 } 1815} 1816``` 1817 1818## ScopeType<sup>8+</sup> 1819 1820Defines the type of values in a **Scope** object. 1821 1822**System capability**: SystemCapability.Utils.Lang 1823 1824| Type| Description| 1825| -------- | -------- | 1826| number | The value type is a number.| 1827| [ScopeComparable](#scopecomparable8) | The value type is ScopeComparable.| 1828 1829## ScopeHelper<sup>9+</sup> 1830 1831Provides APIs to define the valid range of a field. The constructor of this class creates comparable objects with lower and upper limits. 1832 1833### constructor<sup>9+</sup> 1834 1835constructor(lowerObj: ScopeType, upperObj: ScopeType) 1836 1837A constructor used to create a **ScopeHelper** object with the specified upper and lower limits. 1838 1839**System capability**: SystemCapability.Utils.Lang 1840 1841**Parameters** 1842 1843| Name | Type | Mandatory| Description | 1844| -------- | ------------------------ | ---- | ---------------------- | 1845| lowerObj | [ScopeType](#scopetype8) | Yes | Lower limit of the **Scope** object.| 1846| upperObj | [ScopeType](#scopetype8) | Yes | Upper limit of the **Scope** object.| 1847 1848**Example** 1849 1850```ts 1851class Temperature{ 1852 private readonly _temp: number; 1853 constructor(value : number) { 1854 this._temp = value; 1855 } 1856 compareTo(value : Temperature ) { 1857 return this._temp >= value.getTemp(); 1858 } 1859 getTemp() { 1860 return this._temp; 1861 } 1862 toString() : string { 1863 return this._temp.toString(); 1864 } 1865} 1866let tempLower = new Temperature(30); 1867let tempUpper = new Temperature(40); 1868let range = new util.ScopeHelper(tempLower, tempUpper); 1869``` 1870 1871### toString<sup>9+</sup> 1872 1873toString(): string 1874 1875Obtains a string representation that contains this **Scope**. 1876 1877**System capability**: SystemCapability.Utils.Lang 1878 1879**Return value** 1880 1881| Type | Description | 1882| ------ | -------------------------------------- | 1883| string | String representation containing the **Scope**.| 1884 1885**Example** 1886 1887```ts 1888class Temperature{ 1889 private readonly _temp: number; 1890 constructor(value : number) { 1891 this._temp = value; 1892 } 1893 compareTo(value : Temperature ) { 1894 return this._temp >= value.getTemp(); 1895 } 1896 getTemp() { 1897 return this._temp; 1898 } 1899 toString() : string { 1900 return this._temp.toString(); 1901 } 1902} 1903 1904let tempLower = new Temperature(30); 1905let tempUpper = new Temperature(40); 1906let range = new util.ScopeHelper(tempLower, tempUpper); 1907let result = range.toString(); 1908``` 1909 1910### intersect<sup>9+</sup> 1911 1912intersect(range: ScopeHelper): ScopeHelper 1913 1914Obtains the intersection of this **Scope** and the given **Scope**. 1915 1916**System capability**: SystemCapability.Utils.Lang 1917 1918**Parameters** 1919 1920| Name| Type | Mandatory| Description | 1921| ------ | ---------------------------- | ---- | ------------------ | 1922| range | [ScopeHelper](#scopehelper9) | Yes | **Scope** specified.| 1923 1924**Return value** 1925 1926| Type | Description | 1927| ------------------------------ | ------------------------------ | 1928| [ScopeHelper](#scopehelper9) | Intersection of this **Scope** and the given **Scope**.| 1929 1930**Example** 1931 1932```ts 1933class Temperature{ 1934 private readonly _temp: number; 1935 constructor(value : number) { 1936 this._temp = value; 1937 } 1938 compareTo(value : Temperature ) { 1939 return this._temp >= value.getTemp(); 1940 } 1941 getTemp() { 1942 return this._temp; 1943 } 1944 toString() : string { 1945 return this._temp.toString(); 1946 } 1947} 1948 1949let tempLower = new Temperature(30); 1950let tempUpper = new Temperature(40); 1951let range = new util.ScopeHelper(tempLower, tempUpper); 1952let tempMiDF = new Temperature(35); 1953let tempMidS = new Temperature(39); 1954let rangeFir = new util.ScopeHelper(tempMiDF, tempMidS); 1955range.intersect(rangeFir); 1956``` 1957 1958### intersect<sup>9+</sup> 1959 1960intersect(lowerObj:ScopeType,upperObj:ScopeType):ScopeHelper 1961 1962Obtains the intersection of this **Scope** and the given lower and upper limits. 1963 1964**System capability**: SystemCapability.Utils.Lang 1965 1966**Parameters** 1967 1968| Name | Type | Mandatory| Description | 1969| -------- | ------------------------ | ---- | ---------------- | 1970| lowerObj | [ScopeType](#scopetype8) | Yes | Lower limit.| 1971| upperObj | [ScopeType](#scopetype8) | Yes | Upper limit.| 1972 1973**Return value** 1974 1975| Type | Description | 1976| ---------------------------- | ---------------------------------------- | 1977| [ScopeHelper](#scopehelper9) | Intersection of this **Scope** and the given lower and upper limits.| 1978 1979**Example** 1980 1981```ts 1982class Temperature{ 1983 private readonly _temp: number; 1984 constructor(value : number) { 1985 this._temp = value; 1986 } 1987 compareTo(value : Temperature ) { 1988 return this._temp >= value.getTemp(); 1989 } 1990 getTemp() { 1991 return this._temp; 1992 } 1993 toString() : string { 1994 return this._temp.toString(); 1995 } 1996} 1997 1998let tempLower = new Temperature(30); 1999let tempUpper = new Temperature(40); 2000let tempMiDF = new Temperature(35); 2001let tempMidS = new Temperature(39); 2002let range = new util.ScopeHelper(tempLower, tempUpper); 2003let result = range.intersect(tempMiDF, tempMidS); 2004``` 2005 2006### getUpper<sup>9+</sup> 2007 2008getUpper(): ScopeType 2009 2010Obtains the upper limit of this **Scope**. 2011 2012**System capability**: SystemCapability.Utils.Lang 2013 2014**Return value** 2015 2016| Type | Description | 2017| ------------------------ | ---------------------- | 2018| [ScopeType](#scopetype8) | Upper limit of this **Scope**.| 2019 2020**Example** 2021 2022```ts 2023class Temperature{ 2024 private readonly _temp: number; 2025 constructor(value : number) { 2026 this._temp = value; 2027 } 2028 compareTo(value : Temperature ) { 2029 return this._temp >= value.getTemp(); 2030 } 2031 getTemp() { 2032 return this._temp; 2033 } 2034 toString() : string { 2035 return this._temp.toString(); 2036 } 2037} 2038 2039let tempLower = new Temperature(30); 2040let tempUpper = new Temperature(40); 2041let range = new util.ScopeHelper(tempLower, tempUpper); 2042let result = range.getUpper(); 2043``` 2044 2045### getLower<sup>9+</sup> 2046 2047getLower(): ScopeType 2048 2049Obtains the lower limit of this **Scope**. 2050 2051**System capability**: SystemCapability.Utils.Lang 2052 2053**Return value** 2054 2055| Type | Description | 2056| ------------------------ | ---------------------- | 2057| [ScopeType](#scopetype8) | Lower limit of this **Scope**.| 2058 2059**Example** 2060 2061```ts 2062class Temperature{ 2063 private readonly _temp: number; 2064 constructor(value : number) { 2065 this._temp = value; 2066 } 2067 compareTo(value : Temperature ) { 2068 return this._temp >= value.getTemp(); 2069 } 2070 getTemp() { 2071 return this._temp; 2072 } 2073 toString() : string { 2074 return this._temp.toString(); 2075 } 2076} 2077 2078let tempLower = new Temperature(30); 2079let tempUpper = new Temperature(40); 2080let range = new util.ScopeHelper(tempLower, tempUpper); 2081let result = range.getLower(); 2082``` 2083 2084### expand<sup>9+</sup> 2085 2086expand(lowerObj: ScopeType,upperObj: ScopeType): ScopeHelper 2087 2088Obtains the union set of this **Scope** and the given lower and upper limits. 2089 2090**System capability**: SystemCapability.Utils.Lang 2091 2092**Parameters** 2093 2094| Name | Type | Mandatory| Description | 2095| -------- | ------------------------ | ---- | ---------------- | 2096| lowerObj | [ScopeType](#scopetype8) | Yes | Lower limit.| 2097| upperObj | [ScopeType](#scopetype8) | Yes | Upper limit.| 2098 2099**Return value** 2100 2101| Type | Description | 2102| ---------------------------- | ------------------------------------ | 2103| [ScopeHelper](#scopehelper9) | Union set of this **Scope** and the given lower and upper limits.| 2104 2105**Example** 2106 2107```ts 2108class Temperature{ 2109 private readonly _temp: number; 2110 constructor(value : number) { 2111 this._temp = value; 2112 } 2113 compareTo(value : Temperature ) { 2114 return this._temp >= value.getTemp(); 2115 } 2116 getTemp() { 2117 return this._temp; 2118 } 2119 toString() : string { 2120 return this._temp.toString(); 2121 } 2122} 2123 2124let tempLower = new Temperature(30); 2125let tempUpper = new Temperature(40); 2126let tempMiDF = new Temperature(35); 2127let tempMidS = new Temperature(39); 2128let range = new util.ScopeHelper(tempLower, tempUpper); 2129let result = range.expand(tempMiDF, tempMidS); 2130``` 2131 2132### expand<sup>9+</sup> 2133 2134expand(range: ScopeHelper): ScopeHelper 2135 2136Obtains the union set of this **Scope** and the given **Scope**. 2137 2138**System capability**: SystemCapability.Utils.Lang 2139 2140**Parameters** 2141 2142| Name| Type | Mandatory| Description | 2143| ------ | ---------------------------- | ---- | ------------------ | 2144| range | [ScopeHelper](#scopehelper9) | Yes | **Scope** specified.| 2145 2146**Return value** 2147 2148| Type | Description | 2149| ---------------------------- | ---------------------------------- | 2150| [ScopeHelper](#scopehelper9) | Union set of this **Scope** and the given **Scope**.| 2151 2152**Example** 2153 2154```ts 2155class Temperature{ 2156 private readonly _temp: number; 2157 constructor(value : number) { 2158 this._temp = value; 2159 } 2160 compareTo(value : Temperature ) { 2161 return this._temp >= value.getTemp(); 2162 } 2163 getTemp() { 2164 return this._temp; 2165 } 2166 toString() : string { 2167 return this._temp.toString(); 2168 } 2169} 2170 2171let tempLower = new Temperature(30); 2172let tempUpper = new Temperature(40); 2173let tempMiDF = new Temperature(35); 2174let tempMidS = new Temperature(39); 2175let range = new util.ScopeHelper(tempLower, tempUpper); 2176let rangeFir = new util.ScopeHelper(tempMiDF, tempMidS); 2177let result = range.expand(rangeFir); 2178``` 2179 2180### expand<sup>9+</sup> 2181 2182expand(value: ScopeType): ScopeHelper 2183 2184Obtains the union set of this **Scope** and the given value. 2185 2186**System capability**: SystemCapability.Utils.Lang 2187 2188**Parameters** 2189 2190| Name| Type | Mandatory| Description | 2191| ------ | ------------------------ | ---- | ---------------- | 2192| value | [ScopeType](#scopetype8) | Yes | Value specified.| 2193 2194**Return value** 2195 2196| Type | Description | 2197| ---------------------------- | -------------------------------- | 2198| [ScopeHelper](#scopehelper9) | Union set of this **Scope** and the given value.| 2199 2200**Example** 2201 2202```ts 2203class Temperature{ 2204 private readonly _temp: number; 2205 constructor(value : number) { 2206 this._temp = value; 2207 } 2208 compareTo(value : Temperature ) { 2209 return this._temp >= value.getTemp(); 2210 } 2211 getTemp() { 2212 return this._temp; 2213 } 2214 toString() : string { 2215 return this._temp.toString(); 2216 } 2217} 2218 2219let tempLower = new Temperature(30); 2220let tempUpper = new Temperature(40); 2221let tempMiDF = new Temperature(35); 2222let range = new util.ScopeHelper(tempLower, tempUpper); 2223let result = range.expand(tempMiDF); 2224``` 2225 2226### contains<sup>9+</sup> 2227 2228contains(value: ScopeType): boolean 2229 2230Checks whether a value is within this **Scope**. 2231 2232**System capability**: SystemCapability.Utils.Lang 2233 2234**Parameters** 2235 2236| Name| Type | Mandatory| Description | 2237| ------ | ------------------------ | ---- | ---------------- | 2238| value | [ScopeType](#scopetype8) | Yes | Value specified.| 2239 2240**Return value** 2241 2242| Type | Description | 2243| ------- | --------------------------------------------------- | 2244| boolean | Returns **true** if the value is within this **Scope**; returns **false** otherwise.| 2245 2246**Example** 2247 2248```ts 2249class Temperature{ 2250 private readonly _temp: number; 2251 constructor(value : number) { 2252 this._temp = value; 2253 } 2254 compareTo(value : Temperature ) { 2255 return this._temp >= value.getTemp(); 2256 } 2257 getTemp() { 2258 return this._temp; 2259 } 2260 toString() : string { 2261 return this._temp.toString(); 2262 } 2263} 2264 2265let tempLower = new Temperature(30); 2266let tempUpper = new Temperature(40); 2267let tempMiDF = new Temperature(35); 2268let range = new util.ScopeHelper(tempLower, tempUpper); 2269let result = range.contains(tempMiDF); 2270``` 2271 2272### contains<sup>9+</sup> 2273 2274contains(range: ScopeHelper): boolean 2275 2276Checks whether a range is within this **Scope**. 2277 2278**System capability**: SystemCapability.Utils.Lang 2279 2280**Parameters** 2281 2282| Name| Type | Mandatory| Description | 2283| ------ | ---------------------------- | ---- | ------------------ | 2284| range | [ScopeHelper](#scopehelper9) | Yes | **Scope** specified.| 2285 2286**Return value** 2287 2288| Type | Description | 2289| ------- | ----------------------------------------------------- | 2290| boolean | Returns **true** if the range is within this **Scope**; returns **false** otherwise.| 2291 2292**Example** 2293 2294```ts 2295class Temperature{ 2296 private readonly _temp: number; 2297 constructor(value : number) { 2298 this._temp = value; 2299 } 2300 compareTo(value : Temperature ) { 2301 return this._temp >= value.getTemp(); 2302 } 2303 getTemp() { 2304 return this._temp; 2305 } 2306 toString() : string { 2307 return this._temp.toString(); 2308 } 2309} 2310 2311let tempLower = new Temperature(30); 2312let tempUpper = new Temperature(40); 2313let range = new util.ScopeHelper(tempLower, tempUpper); 2314let tempLess = new Temperature(20); 2315let tempMore = new Temperature(45); 2316let rangeSec = new util.ScopeHelper(tempLess, tempMore); 2317let result = range.contains(rangeSec); 2318``` 2319 2320### clamp<sup>9+</sup> 2321 2322clamp(value: ScopeType): ScopeType 2323 2324Limits a value to this **Scope**. 2325 2326**System capability**: SystemCapability.Utils.Lang 2327 2328**Parameters** 2329 2330| Name| Type | Mandatory| Description | 2331| ------ | ------------------------ | ---- | -------------- | 2332| value | [ScopeType](#scopetype8) | Yes | Value specified.| 2333 2334**Return value** 2335 2336| Type | Description | 2337| ------------------------ | ------------------------------------------------------------ | 2338| [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**.| 2339 2340**Example** 2341 2342```ts 2343class Temperature{ 2344 private readonly _temp: number; 2345 constructor(value : number) { 2346 this._temp = value; 2347 } 2348 compareTo(value : Temperature ) { 2349 return this._temp >= value.getTemp(); 2350 } 2351 getTemp() { 2352 return this._temp; 2353 } 2354 toString() : string { 2355 return this._temp.toString(); 2356 } 2357} 2358 2359let tempLower = new Temperature(30); 2360let tempUpper = new Temperature(40); 2361let tempMiDF = new Temperature(35); 2362let range = new util.ScopeHelper(tempLower, tempUpper); 2363let result = range.clamp(tempMiDF); 2364``` 2365 2366## Base64Helper<sup>9+</sup> 2367 2368The Base64 encoding table contains 62 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. 2369 2370### constructor<sup>9+</sup> 2371 2372constructor() 2373 2374A constructor used to create a **Base64Helper** instance. 2375 2376**System capability**: SystemCapability.Utils.Lang 2377 2378**Example** 2379 2380 ```ts 2381 let base64 = new util.Base64Helper(); 2382 ``` 2383 2384### encodeSync<sup>9+</sup> 2385 2386encodeSync(src: Uint8Array): Uint8Array 2387 2388Encodes the input content into a Uint8Array object. This API returns the result synchronously. 2389 2390**System capability**: SystemCapability.Utils.Lang 2391 2392**Parameters** 2393 2394| Name| Type | Mandatory| Description | 2395| ------ | ---------- | ---- | ------------------- | 2396| src | Uint8Array | Yes | Uint8Array object to encode.| 2397 2398**Return value** 2399 2400| Type | Description | 2401| ---------- | ----------------------------- | 2402| Uint8Array | Uint8Array object obtained.| 2403 2404**Example** 2405 2406 ```ts 2407 let that = new util.Base64Helper(); 2408 let array = new Uint8Array([115,49,51]); 2409 let result = that.encodeSync(array); 2410 ``` 2411 2412 2413### encodeToStringSync<sup>9+</sup> 2414 2415encodeToStringSync(src: Uint8Array, options?: Type): string 2416 2417Encodes the input content into a string. This API returns the result synchronously. 2418 2419**System capability**: SystemCapability.Utils.Lang 2420 2421**Parameters** 2422 2423| Name| Type | Mandatory| Description | 2424| ------ | ---------- | ---- | ------------------- | 2425| src | Uint8Array | Yes | Uint8Array object to encode.| 2426| options<sup>10+</sup> | [Type](#type10) | No | Encoding format.<br>The following values are available:<br>- **util.Type.BASIC** (default value): The output can contain 64 printable characters, which are the uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), and the special characters plus sign (+) and slash (/). There is no carriage return or line feed character.<br>- **util.Type.MIME**: The output can contain 64 printable characters, which are the uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), and the special characters plus sign (+) and slash (/). Each line of the output contains a maximum of 76 characters, ended with '\r\n'.| 2427 2428**Return value** 2429 2430| Type | Description | 2431| ------ | -------------------- | 2432| string | String obtained.| 2433 2434**Example** 2435 2436 ```ts 2437 let that = new util.Base64Helper(); 2438 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]); 2439 let result = that.encodeToStringSync(array, util.Type.MIME); 2440 ``` 2441 2442 2443### decodeSync<sup>9+</sup> 2444 2445decodeSync(src: Uint8Array | string, options?: Type): Uint8Array 2446 2447Decodes a string into a Uint8Array object. This API returns the result synchronously. 2448 2449**System capability**: SystemCapability.Utils.Lang 2450 2451**Parameters** 2452 2453| Name| Type | Mandatory| Description | 2454| ------ | ------------------------------ | ---- | ----------------------------- | 2455| src | Uint8Array \| string | Yes | Uint8Array object or string to decode.| 2456| options<sup>10+</sup> | [Type](#type10) | No | Encoding format.<br>The following values are available:<br>- **util.Type.BASIC** (default value): The input can contain 64 printable characters, which are the uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), and the special characters plus sign (+) and slash (/). There is no carriage return or line feed character.<br>- **util.Type.MIME**: The input can contain 64 printable characters, which are the uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), and the special characters plus sign (+) and slash (/). Each line of the input contains a maximum of 76 characters, ended with '\r\n'.| 2457 2458**Return value** 2459 2460| Type | Description | 2461| ---------- | ----------------------------- | 2462| Uint8Array | Uint8Array object obtained.| 2463 2464**Example** 2465 2466 ```ts 2467 let that = new util.Base64Helper(); 2468 let buff = 'TWFuaXNkaXN0aW5ndWlzaGVkbm90b25seWJ5aGlzcmVhc29uYnV0Ynl0aGlzc2luZ3VsYXJwYXNz\r\naW9uZnJvbW90aGVyYW5pbWFsc3doaWNoaXNhbHVzdG9mdGhlbWluZGV4Y2VlZHN0aGVzaG9ydHZl\r\naGVtZW5jZW9mYW55Y2FybmFscGxlYXN1cmU=\r\n'; 2469 let result = that.decodeSync(buff, util.Type.MIME); 2470 ``` 2471 2472 2473### encode<sup>9+</sup> 2474 2475encode(src: Uint8Array): Promise<Uint8Array> 2476 2477Encodes the input content into a Uint8Array object. This API uses a promise to return the result. 2478 2479**System capability**: SystemCapability.Utils.Lang 2480 2481**Parameters** 2482 2483| Name| Type | Mandatory| Description | 2484| ------ | ---------- | ---- | ----------------------- | 2485| src | Uint8Array | Yes | Uint8Array object to encode.| 2486 2487**Return value** 2488 2489| Type | Description | 2490| ------------------------- | --------------------------------- | 2491| Promise<Uint8Array> | Promise used to return the Uint8Array object obtained.| 2492 2493**Example** 2494 2495 ```ts 2496 let that = new util.Base64Helper(); 2497 let array = new Uint8Array([115,49,51]); 2498 let rarray = new Uint8Array([99,122,69,122]); 2499 that.encode(array).then(val=>{ 2500 for (let i = 0; i < rarray.length; i++) { 2501 console.log(val[i].toString()); 2502 } 2503 }) 2504 ``` 2505 2506 2507### encodeToString<sup>9+</sup> 2508 2509encodeToString(src: Uint8Array, options?: Type): Promise<string> 2510 2511Encodes the input content into a string. This API uses a promise to return the result. 2512 2513**System capability**: SystemCapability.Utils.Lang 2514 2515**Parameters** 2516 2517| Name| Type | Mandatory| Description | 2518| ------ | ---------- | ---- | ----------------------- | 2519| src | Uint8Array | Yes | Uint8Array object to encode.| 2520| options<sup>10+</sup> | [Type](#type10) | No | Encoding format.<br>The following values are available:<br>- **util.Type.BASIC** (default value): The output can contain 64 printable characters, which are the uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), and the special characters plus sign (+) and slash (/). There is no carriage return or line feed character.<br>- **util.Type.MIME**: The output can contain 64 printable characters, which are the uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), and the special characters plus sign (+) and slash (/). Each line of the output contains a maximum of 76 characters, ended with '\r\n'.| 2521 2522**Return value** 2523 2524| Type | Description | 2525| --------------------- | ------------------------ | 2526| Promise<string> | Promise used to return the string obtained.| 2527 2528**Example** 2529 2530 ```ts 2531 let that = new util.Base64Helper(); 2532 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]); 2533 that.encodeToString(array, util.Type.MIME).then(val=>{ 2534 // Add information as required. 2535 }) 2536 ``` 2537 2538 2539### decode<sup>9+</sup> 2540 2541decode(src: Uint8Array | string, options?: Type): Promise<Uint8Array> 2542 2543Decodes the input content into a Uint8Array object. This API uses a promise to return the result. 2544 2545**System capability**: SystemCapability.Utils.Lang 2546 2547**Parameters** 2548 2549| Name| Type | Mandatory| Description | 2550| ------ | ------------------------------ | ---- | --------------------------------- | 2551| src | Uint8Array \| string | Yes | Uint8Array object or string to decode.| 2552| options<sup>10+</sup> | [Type](#type10) | No | Encoding format.<br>The following values are available:<br>- **util.Type.BASIC** (default value): The input can contain 64 printable characters, which are the uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), and the special characters plus sign (+) and slash (/). There is no carriage return or line feed character.<br>- **util.Type.MIME**: The input can contain 64 printable characters, which are the uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), and the special characters plus sign (+) and slash (/). Each line of the input contains a maximum of 76 characters, ended with '\r\n'.| 2553 2554**Return value** 2555 2556| Type | Description | 2557| ------------------------- | --------------------------------- | 2558| Promise<Uint8Array> | Promise used to return the Uint8Array object obtained.| 2559 2560**Example** 2561 2562 ```ts 2563 let that = new util.Base64Helper(); 2564 let array = 'TWFuaXNkaXN0aW5ndWlzaGVkbm90b25seWJ5aGlzcmVhc29uYnV0Ynl0aGlzc2luZ3VsYXJwYXNz\r\naW9uZnJvbW90aGVyYW5pbWFsc3doaWNoaXNhbHVzdG9mdGhlbWluZGV4Y2VlZHN0aGVzaG9ydHZl\r\naGVtZW5jZW9mYW55Y2FybmFscGxlYXN1cmU=\r\n'; 2565 that.decode(array, util.Type.MIME).then(val=>{ 2566 // Add information as required. 2567 }) 2568 ``` 2569 2570 2571## Type<sup>10+</sup> 2572 2573Enumerates the Base64 encoding formats. 2574 2575**System capability**: SystemCapability.Utils.Lang 2576 2577| Name |Value| Description | 2578| ----- |---| ----------------- | 2579| BASIC | 0 | Basic format.| 2580| MIME | 1 | MIME format.| 2581 2582 2583## types<sup>8+</sup> 2584 2585Provides 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. 2586 2587### constructor<sup>8+</sup> 2588 2589constructor() 2590 2591A constructor used to create a **Types** object. 2592 2593**System capability**: SystemCapability.Utils.Lang 2594 2595**Example** 2596 2597 ```ts 2598 let type = new util.types(); 2599 ``` 2600 2601 2602### isAnyArrayBuffer<sup>8+</sup> 2603 2604isAnyArrayBuffer(value: Object): boolean 2605 2606Checks whether the input value is of the ArrayBuffer type. 2607 2608**System capability**: SystemCapability.Utils.Lang 2609 2610**Parameters** 2611 2612| Name| Type| Mandatory| Description| 2613| -------- | -------- | -------- | -------- | 2614| value | Object | Yes| Object to check.| 2615 2616**Return value** 2617 2618| Type| Description| 2619| -------- | -------- | 2620| boolean | Returns **true** if the input value is of the ArrayBuffer type; returns **false** otherwise.| 2621 2622**Example** 2623 2624 ```ts 2625 let that = new util.types(); 2626 let result = that.isAnyArrayBuffer(new ArrayBuffer(0)); 2627 ``` 2628 2629 2630### isArrayBufferView<sup>8+</sup> 2631 2632isArrayBufferView(value: Object): boolean 2633 2634Checks whether the input value is of the ArrayBufferView type. 2635 2636**ArrayBufferView** is a helper type representing any of the following: Int8Array, Int16Array, Int32Array, Uint8Array, Uint8ClampedArray, Uint32Array, Float32Array, **Float64Array**, and DataView. 2637 2638**System capability**: SystemCapability.Utils.Lang 2639 2640**Parameters** 2641 2642| Name| Type| Mandatory| Description| 2643| -------- | -------- | -------- | -------- | 2644| value | Object | Yes| Object to check.| 2645 2646**Return value** 2647 2648| Type| Description| 2649| -------- | -------- | 2650| boolean | Returns **true** if the input value is of the ArrayBufferView type; returns **false** otherwise.| 2651 2652**Example** 2653 2654 ```ts 2655 let that = new util.types(); 2656 let result = that.isArrayBufferView(new Int8Array([])); 2657 ``` 2658 2659 2660### isArgumentsObject<sup>8+</sup> 2661 2662isArgumentsObject(value: Object): boolean 2663 2664Checks whether the input value is of the arguments type. 2665 2666**System capability**: SystemCapability.Utils.Lang 2667 2668**Parameters** 2669 2670| Name| Type| Mandatory| Description| 2671| -------- | -------- | -------- | -------- | 2672| value | Object | Yes| Object to check.| 2673 2674**Return value** 2675 2676| Type| Description| 2677| -------- | -------- | 2678| boolean | Returns **true** if the input value is of the arguments type; returns **false** otherwise.| 2679 2680**Example** 2681 2682 ```ts 2683 let that = new util.types(); 2684 function foo() { 2685 let result = that.isArgumentsObject(arguments); 2686 } 2687 let f = foo(); 2688 ``` 2689 2690 2691### isArrayBuffer<sup>8+</sup> 2692 2693isArrayBuffer(value: Object): boolean 2694 2695Checks whether the input value is of the ArrayBuffer type. 2696 2697**System capability**: SystemCapability.Utils.Lang 2698 2699**Parameters** 2700 2701| Name| Type| Mandatory| Description| 2702| -------- | -------- | -------- | -------- | 2703| value | Object | Yes| Object to check.| 2704 2705**Return value** 2706 2707| Type| Description| 2708| -------- | -------- | 2709| boolean | Returns **true** if the input value is of the ArrayBuffer type; returns **false** otherwise.| 2710 2711**Example** 2712 2713 ```ts 2714 let that = new util.types(); 2715 let result = that.isArrayBuffer(new ArrayBuffer(0)); 2716 ``` 2717 2718 2719### isAsyncFunction<sup>8+</sup> 2720 2721isAsyncFunction(value: Object): boolean 2722 2723Checks whether the input value is an asynchronous function. 2724 2725**System capability**: SystemCapability.Utils.Lang 2726 2727**Parameters** 2728 2729| Name| Type| Mandatory| Description| 2730| -------- | -------- | -------- | -------- | 2731| value | Object | Yes| Object to check.| 2732 2733**Return value** 2734 2735| Type| Description| 2736| -------- | -------- | 2737| boolean | Returns **true** if the input value is an asynchronous function; returns **false** otherwise.| 2738 2739**Example** 2740 2741 ```ts 2742 let that = new util.types(); 2743 let result = that.isAsyncFunction(async () => {}); 2744 ``` 2745 2746 2747### isBooleanObject<sup>8+</sup> 2748 2749isBooleanObject(value: Object): boolean 2750 2751Checks whether the input value is of the Boolean type. 2752 2753**System capability**: SystemCapability.Utils.Lang 2754 2755**Parameters** 2756 2757| Name| Type| Mandatory| Description| 2758| -------- | -------- | -------- | -------- | 2759| value | Object | Yes| Object to check.| 2760 2761**Return value** 2762 2763| Type| Description| 2764| -------- | -------- | 2765| boolean | Returns **true** if the input value is of the Boolean type; returns **false** otherwise.| 2766 2767**Example** 2768 2769 ```ts 2770 let that = new util.types(); 2771 let result = that.isBooleanObject(new Boolean(true)); 2772 ``` 2773 2774 2775### isBoxedPrimitive<sup>8+</sup> 2776 2777isBoxedPrimitive(value: Object): boolean 2778 2779Checks whether the input value is of the Boolean, Number, String, or Symbol type. 2780 2781**System capability**: SystemCapability.Utils.Lang 2782 2783**Parameters** 2784 2785| Name| Type| Mandatory| Description| 2786| -------- | -------- | -------- | -------- | 2787| value | Object | Yes| Object to check.| 2788 2789**Return value** 2790 2791| Type| Description| 2792| -------- | -------- | 2793| boolean | Returns **true** if the input value is of the Boolean, Number, String, or Symbol type; returns **false** otherwise.| 2794 2795**Example** 2796 2797 ```ts 2798 let that = new util.types(); 2799 let result = that.isBoxedPrimitive(new Boolean(false)); 2800 ``` 2801 2802 2803### isDataView<sup>8+</sup> 2804 2805isDataView(value: Object): boolean 2806 2807Checks whether the input value is of the DataView type. 2808 2809**System capability**: SystemCapability.Utils.Lang 2810 2811**Parameters** 2812 2813| Name| Type| Mandatory| Description| 2814| -------- | -------- | -------- | -------- | 2815| value | Object | Yes| Object to check.| 2816 2817**Return value** 2818 2819| Type| Description| 2820| -------- | -------- | 2821| boolean | Returns **true** if the input value is of the DataView type; returns **false** otherwise.| 2822 2823**Example** 2824 2825 ```ts 2826 let that = new util.types(); 2827 const ab = new ArrayBuffer(20); 2828 let result = that.isDataView(new DataView(ab)); 2829 ``` 2830 2831 2832### isDate<sup>8+</sup> 2833 2834isDate(value: Object): boolean 2835 2836Checks whether the input value is of the Date type. 2837 2838**System capability**: SystemCapability.Utils.Lang 2839 2840**Parameters** 2841 2842| Name| Type| Mandatory| Description| 2843| -------- | -------- | -------- | -------- | 2844| value | Object | Yes| Object to check.| 2845 2846**Return value** 2847 2848| Type| Description| 2849| -------- | -------- | 2850| boolean | Returns **true** if the input value is of the Date type; returns **false** otherwise.| 2851 2852**Example** 2853 2854 ```ts 2855 let that = new util.types(); 2856 let result = that.isDate(new Date()); 2857 ``` 2858 2859 2860### isExternal<sup>8+</sup> 2861 2862isExternal(value: Object): boolean 2863 2864Checks whether the input value is of the native external type. 2865 2866**System capability**: SystemCapability.Utils.Lang 2867 2868**Parameters** 2869 2870| Name| Type| Mandatory| Description| 2871| -------- | -------- | -------- | -------- | 2872| value | Object | Yes| Object to check.| 2873 2874**Return value** 2875 2876| Type| Description| 2877| -------- | -------- | 2878| boolean | Returns **true** if the input value is of the native external type; returns **false** otherwise.| 2879 2880**Example** 2881 2882 ```ts 2883 let that = new util.types(); 2884 let result = that.isExternal(true); 2885 ``` 2886 2887 2888### isFloat32Array<sup>8+</sup> 2889 2890isFloat32Array(value: Object): boolean 2891 2892Checks whether the input value is of the Float32Array type. 2893 2894**System capability**: SystemCapability.Utils.Lang 2895 2896**Parameters** 2897 2898| Name| Type| Mandatory| Description| 2899| -------- | -------- | -------- | -------- | 2900| value | Object | Yes| Object to check.| 2901 2902**Return value** 2903 2904| Type| Description| 2905| -------- | -------- | 2906| boolean | Returns **true** if the input value is of the Float32Array type; returns **false** otherwise.| 2907 2908**Example** 2909 2910 ```ts 2911 let that = new util.types(); 2912 let result = that.isFloat32Array(new Float32Array()); 2913 ``` 2914 2915 2916### isFloat64Array<sup>8+</sup> 2917 2918isFloat64Array(value: Object): boolean 2919 2920Checks whether the input value is of the Float64Array type. 2921 2922**System capability**: SystemCapability.Utils.Lang 2923 2924**Parameters** 2925 2926| Name| Type| Mandatory| Description| 2927| -------- | -------- | -------- | -------- | 2928| value | Object | Yes| Object to check.| 2929 2930**Return value** 2931 2932| Type| Description| 2933| -------- | -------- | 2934| boolean | Returns **true** if the input value is of the Float64Array type; returns **false** otherwise.| 2935 2936**Example** 2937 2938 ```ts 2939 let that = new util.types(); 2940 let result = that.isFloat64Array(new Float64Array()); 2941 ``` 2942 2943 2944### isGeneratorFunction<sup>8+</sup> 2945 2946isGeneratorFunction(value: Object): boolean 2947 2948Checks whether the input value is a generator function. 2949 2950> **NOTE** 2951> 2952> This API cannot be used in .ets files. 2953 2954**System capability**: SystemCapability.Utils.Lang 2955 2956**Parameters** 2957 2958| Name| Type| Mandatory| Description| 2959| -------- | -------- | -------- | -------- | 2960| value | Object | Yes| Object to check.| 2961 2962**Return value** 2963 2964| Type| Description| 2965| -------- | -------- | 2966| boolean | Returns **true** if the input value is a generator function; returns **false** otherwise.| 2967 2968**Example** 2969 2970 ```ts 2971 let that = new util.types(); 2972 let result = that.isGeneratorFunction(function* foo() {}); 2973 ``` 2974 2975 2976### isGeneratorObject<sup>8+</sup> 2977 2978isGeneratorObject(value: Object): boolean 2979 2980Checks whether the input value is a generator object. 2981 2982> **NOTE** 2983> 2984> This API cannot be used in .ets files. 2985 2986**System capability**: SystemCapability.Utils.Lang 2987 2988**Parameters** 2989 2990| Name| Type| Mandatory| Description| 2991| -------- | -------- | -------- | -------- | 2992| value | Object | Yes| Object to check.| 2993 2994**Return value** 2995 2996| Type| Description| 2997| -------- | -------- | 2998| boolean | Returns **true** if the input value is a generator object; returns **false** otherwise.| 2999 3000**Example** 3001 3002 ```ts 3003 // This API cannot be used in .ets files. 3004 let that = new util.types(); 3005 function* foo() {}; 3006 const generator = foo(); 3007 let result = that.isGeneratorObject(generator); 3008 ``` 3009 3010 3011### isInt8Array<sup>8+</sup> 3012 3013isInt8Array(value: Object): boolean 3014 3015Checks whether the input value is of the Int8Array type. 3016 3017**System capability**: SystemCapability.Utils.Lang 3018 3019**Parameters** 3020 3021| Name| Type| Mandatory| Description| 3022| -------- | -------- | -------- | -------- | 3023| value | Object | Yes| Object to check.| 3024 3025**Return value** 3026 3027| Type| Description| 3028| -------- | -------- | 3029| boolean | Returns **true** if the input value is of the Int8Array type; returns **false** otherwise.| 3030 3031**Example** 3032 3033 ```ts 3034 let that = new util.types(); 3035 let result = that.isInt8Array(new Int8Array([])); 3036 ``` 3037 3038 3039### isInt16Array<sup>8+</sup> 3040 3041isInt16Array(value: Object): boolean 3042 3043Checks whether the input value is of the Int16Array type. 3044 3045**System capability**: SystemCapability.Utils.Lang 3046 3047**Parameters** 3048 3049| Name| Type| Mandatory| Description| 3050| -------- | -------- | -------- | -------- | 3051| value | Object | Yes| Object to check.| 3052 3053**Return value** 3054 3055| Type| Description| 3056| -------- | -------- | 3057| boolean | Returns **true** if the input value is of the Int16Array type; returns **false** otherwise.| 3058 3059**Example** 3060 3061 ```ts 3062 let that = new util.types(); 3063 let result = that.isInt16Array(new Int16Array([])); 3064 ``` 3065 3066 3067### isInt32Array<sup>8+</sup> 3068 3069isInt32Array(value: Object): boolean 3070 3071Checks whether the input value is of the Int32Array type. 3072 3073**System capability**: SystemCapability.Utils.Lang 3074 3075**Parameters** 3076 3077| Name| Type| Mandatory| Description| 3078| -------- | -------- | -------- | -------- | 3079| value | Object | Yes| Object to check.| 3080 3081**Return value** 3082 3083| Type| Description| 3084| -------- | -------- | 3085| boolean | Returns **true** if the input value is of the Int32Array type; returns **false** otherwise.| 3086 3087**Example** 3088 3089 ```ts 3090 let that = new util.types(); 3091 let result = that.isInt32Array(new Int32Array([])); 3092 ``` 3093 3094 3095### isMap<sup>8+</sup> 3096 3097isMap(value: Object): boolean 3098 3099Checks whether the input value is of the Map type. 3100 3101**System capability**: SystemCapability.Utils.Lang 3102 3103**Parameters** 3104 3105| Name| Type| Mandatory| Description| 3106| -------- | -------- | -------- | -------- | 3107| value | Object | Yes| Object to check.| 3108 3109**Return value** 3110 3111| Type| Description| 3112| -------- | -------- | 3113| boolean | Returns **true** if the input value is of the Map type; returns **false** otherwise.| 3114 3115**Example** 3116 3117 ```ts 3118 let that = new util.types(); 3119 let result = that.isMap(new Map()); 3120 ``` 3121 3122 3123### isMapIterator<sup>8+</sup> 3124 3125isMapIterator(value: Object): boolean 3126 3127Checks whether the input value is of the MapIterator type. 3128 3129**System capability**: SystemCapability.Utils.Lang 3130 3131**Parameters** 3132 3133 3134| Name| Type| Mandatory| Description| 3135| -------- | -------- | -------- | -------- | 3136| value | Object | Yes| Object to check.| 3137 3138**Return value** 3139 3140| Type| Description| 3141| -------- | -------- | 3142| boolean | Returns **true** if the input value is of the MapIterator type; returns **false** otherwise.| 3143 3144**Example** 3145 3146 ```ts 3147 let that = new util.types(); 3148 const map : Map<number,number> = new Map(); 3149 let result = that.isMapIterator(map.keys()); 3150 ``` 3151 3152 3153### isNativeError<sup>8+</sup> 3154 3155isNativeError(value: Object): boolean 3156 3157Checks whether the input value is of the Error type. 3158 3159**System capability**: SystemCapability.Utils.Lang 3160 3161**Parameters** 3162 3163| Name| Type| Mandatory| Description| 3164| -------- | -------- | -------- | -------- | 3165| value | Object | Yes| Object to check.| 3166 3167**Return value** 3168 3169| Type| Description| 3170| -------- | -------- | 3171| boolean | Returns **true** if the input value is of the Error type; returns **false** otherwise.| 3172 3173**Example** 3174 3175 ```ts 3176 let that = new util.types(); 3177 let result = that.isNativeError(new TypeError()); 3178 ``` 3179 3180 3181### isNumberObject<sup>8+</sup> 3182 3183isNumberObject(value: Object): boolean 3184 3185Checks whether the input value is a number object. 3186 3187**System capability**: SystemCapability.Utils.Lang 3188 3189**Parameters** 3190 3191| Name| Type| Mandatory| Description| 3192| -------- | -------- | -------- | -------- | 3193| value | Object | Yes| Object to check.| 3194 3195**Return value** 3196 3197| Type| Description| 3198| -------- | -------- | 3199| boolean | Returns **true** if the input value is a number object; returns **false** otherwise.| 3200 3201**Example** 3202 3203 ```ts 3204 let that = new util.types(); 3205 let result = that.isNumberObject(new Number(0)); 3206 ``` 3207 3208 3209### isPromise<sup>8+</sup> 3210 3211isPromise(value: Object): boolean 3212 3213Checks whether the input value is a promise. 3214 3215**System capability**: SystemCapability.Utils.Lang 3216 3217**Parameters** 3218 3219| Name| Type| Mandatory| Description| 3220| -------- | -------- | -------- | -------- | 3221| value | Object | Yes| Object to check.| 3222 3223**Return value** 3224 3225| Type| Description| 3226| -------- | -------- | 3227| boolean | Returns **true** if the input value is a promise; returns **false** otherwise.| 3228 3229**Example** 3230 3231 ```ts 3232 let that = new util.types(); 3233 let result = that.isPromise(Promise.resolve(1)); 3234 ``` 3235 3236 3237### isProxy<sup>8+</sup> 3238 3239isProxy(value: Object): boolean 3240 3241Checks whether the input value is a proxy. 3242 3243**System capability**: SystemCapability.Utils.Lang 3244 3245**Parameters** 3246 3247| Name| Type| Mandatory| Description| 3248| -------- | -------- | -------- | -------- | 3249| value | Object | Yes| Object to check.| 3250 3251**Return value** 3252 3253| Type| Description| 3254| -------- | -------- | 3255| boolean | Returns **true** if the input value is a proxy; returns **false** otherwise.| 3256 3257**Example** 3258 3259 ```ts 3260 class Target{ 3261 } 3262 let that = new util.types(); 3263 const target : Target = {}; 3264 const proxy = new Proxy(target, target); 3265 let result = that.isProxy(proxy); 3266 ``` 3267 3268 3269### isRegExp<sup>8+</sup> 3270 3271isRegExp(value: Object): boolean 3272 3273Checks whether the input value is of the RegExp type. 3274 3275**System capability**: SystemCapability.Utils.Lang 3276 3277**Parameters** 3278 3279| Name| Type| Mandatory| Description| 3280| -------- | -------- | -------- | -------- | 3281| value | Object | Yes| Object to check.| 3282 3283**Return value** 3284 3285| Type| Description| 3286| -------- | -------- | 3287| boolean | Returns **true** if the input value is of the RegExp type; returns **false** otherwise.| 3288 3289**Example** 3290 3291 ```ts 3292 let that = new util.types(); 3293 let result = that.isRegExp(new RegExp('abc')); 3294 ``` 3295 3296 3297### isSet<sup>8+</sup> 3298 3299isSet(value: Object): boolean 3300 3301Checks whether the input value is of the Set type. 3302 3303**System capability**: SystemCapability.Utils.Lang 3304 3305**Parameters** 3306 3307| Name| Type| Mandatory| Description| 3308| -------- | -------- | -------- | -------- | 3309| value | Object | Yes| Object to check.| 3310 3311**Return value** 3312 3313| Type| Description| 3314| -------- | -------- | 3315| boolean | Returns **true** if the input value is of the Set type; returns **false** otherwise.| 3316 3317**Example** 3318 3319 ```ts 3320 let that = new util.types(); 3321 let set : Set<number> = new Set(); 3322 let result = that.isSet(set); 3323 ``` 3324 3325 3326### isSetIterator<sup>8+</sup> 3327 3328isSetIterator(value: Object): boolean 3329 3330Checks whether the input value is of the SetIterator type. 3331 3332**System capability**: SystemCapability.Utils.Lang 3333 3334**Parameters** 3335 3336| Name| Type| Mandatory| Description| 3337| -------- | -------- | -------- | -------- | 3338| value | Object | Yes| Object to check.| 3339 3340**Return value** 3341 3342| Type| Description| 3343| -------- | -------- | 3344| boolean | Returns **true** if the input value is of the SetIterator type; returns **false** otherwise.| 3345 3346**Example** 3347 3348 ```ts 3349 let that = new util.types(); 3350 const set : Set<number> = new Set(); 3351 let result = that.isSetIterator(set.keys()); 3352 ``` 3353 3354 3355### isStringObject<sup>8+</sup> 3356 3357isStringObject(value: Object): boolean 3358 3359Checks whether the input value is a string object. 3360 3361**System capability**: SystemCapability.Utils.Lang 3362 3363**Parameters** 3364 3365| Name| Type| Mandatory| Description| 3366| -------- | -------- | -------- | -------- | 3367| value | Object | Yes| Object to check.| 3368 3369**Return value** 3370 3371| Type| Description| 3372| -------- | -------- | 3373| boolean | Returns **true** if the input value is a string object; returns **false** otherwise.| 3374 3375**Example** 3376 3377 ```ts 3378 let that = new util.types(); 3379 let result = that.isStringObject(new String('foo')); 3380 ``` 3381 3382 3383### isSymbolObjec<sup>8+</sup> 3384 3385isSymbolObject(value: Object): boolean 3386 3387Checks whether the input value is a symbol object. 3388 3389> **NOTE** 3390> 3391> This API cannot be used in .ets files. 3392 3393**System capability**: SystemCapability.Utils.Lang 3394 3395**Parameters** 3396 3397| Name| Type| Mandatory| Description| 3398| -------- | -------- | -------- | -------- | 3399| value | Object | Yes| Object to check.| 3400 3401**Return value** 3402 3403| Type| Description| 3404| -------- | -------- | 3405| boolean | Returns **true** if the input value is a symbol object; returns **false** otherwise.| 3406 3407**Example** 3408 3409 ```ts 3410 // This API cannot be used in .ets files. 3411 let that = new util.types(); 3412 const symbols = Symbol('foo'); 3413 let result = that.isSymbolObject(Object(symbols)); 3414 ``` 3415 3416 3417### isTypedArray<sup>8+</sup> 3418 3419isTypedArray(value: Object): boolean 3420 3421Checks whether the input value is of the TypedArray type. 3422 3423**TypedArray** is a helper type representing any of the following: Int8Array, Int16Array, Int32Array, Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, Float32Array, Float64Array, and DataView. 3424 3425**System capability**: SystemCapability.Utils.Lang 3426 3427**Parameters** 3428 3429| Name| Type| Mandatory| Description| 3430| -------- | -------- | -------- | -------- | 3431| value | Object | Yes| Object to check.| 3432 3433**Return value** 3434 3435| Type| Description| 3436| -------- | -------- | 3437| boolean | Returns **true** if the input value is of the TypedArray type; returns **false** otherwise.| 3438 3439**Example** 3440 3441 ```ts 3442 let that = new util.types(); 3443 let result = that.isTypedArray(new Float64Array([])); 3444 ``` 3445 3446 3447### isUint8Array<sup>8+</sup> 3448 3449isUint8Array(value: Object): boolean 3450 3451Checks whether the input value is of the Uint8Array type. 3452 3453**System capability**: SystemCapability.Utils.Lang 3454 3455**Parameters** 3456 3457| Name| Type| Mandatory| Description| 3458| -------- | -------- | -------- | -------- | 3459| value | Object | Yes| Object to check.| 3460 3461**Return value** 3462 3463| Type| Description| 3464| -------- | -------- | 3465| boolean | Returns **true** if the input value is of the Uint8Array type; returns **false** otherwise.| 3466 3467**Example** 3468 3469 ```ts 3470 let that = new util.types(); 3471 let result = that.isUint8Array(new Uint8Array([])); 3472 ``` 3473 3474 3475### isUint8ClampedArray<sup>8+</sup> 3476 3477isUint8ClampedArray(value: Object): boolean 3478 3479Checks whether the input value is of the Uint8ClampedArray type. 3480 3481**System capability**: SystemCapability.Utils.Lang 3482 3483**Parameters** 3484 3485| Name| Type| Mandatory| Description| 3486| -------- | -------- | -------- | -------- | 3487| value | Object | Yes| Object to check.| 3488 3489**Return value** 3490 3491| Type| Description| 3492| -------- | -------- | 3493| boolean | Returns **true** if the input value is of the Uint8ClampedArray type; returns **false** otherwise.| 3494 3495**Example** 3496 3497 ```ts 3498 let that = new util.types(); 3499 let result = that.isUint8ClampedArray(new Uint8ClampedArray([])); 3500 ``` 3501 3502 3503### isUint16Array<sup>8+</sup> 3504 3505isUint16Array(value: Object): boolean 3506 3507Checks whether the input value is of the Uint16Array type. 3508 3509**System capability**: SystemCapability.Utils.Lang 3510 3511**Parameters** 3512 3513| Name| Type| Mandatory| Description| 3514| -------- | -------- | -------- | -------- | 3515| value | Object | Yes| Object to check.| 3516 3517**Return value** 3518 3519| Type| Description| 3520| -------- | -------- | 3521| boolean | Returns **true** if the input value is of the Uint16Array type; returns **false** otherwise.| 3522 3523**Example** 3524 3525 ```ts 3526 let that = new util.types(); 3527 let result = that.isUint16Array(new Uint16Array([])); 3528 ``` 3529 3530 3531### isUint32Array<sup>8+</sup> 3532 3533isUint32Array(value: Object): boolean 3534 3535Checks whether the input value is of the Uint32Array type. 3536 3537**System capability**: SystemCapability.Utils.Lang 3538 3539**Parameters** 3540 3541| Name| Type| Mandatory| Description| 3542| -------- | -------- | -------- | -------- | 3543| value | Object | Yes| Object to check.| 3544 3545**Return value** 3546 3547| Type| Description| 3548| -------- | -------- | 3549| boolean | Returns **true** if the input value is of the Uint32Array type; returns **false** otherwise.| 3550 3551**Example** 3552 3553 ```ts 3554 let that = new util.types(); 3555 let result = that.isUint32Array(new Uint32Array([])); 3556 ``` 3557 3558 3559### isWeakMap<sup>8+</sup> 3560 3561isWeakMap(value: Object): boolean 3562 3563Checks whether the input value is of the WeakMap type. 3564 3565**System capability**: SystemCapability.Utils.Lang 3566 3567**Parameters** 3568 3569| Name| Type| Mandatory| Description| 3570| -------- | -------- | -------- | -------- | 3571| value | Object | Yes| Object to check.| 3572 3573**Return value** 3574 3575| Type| Description| 3576| -------- | -------- | 3577| boolean | Returns **true** if the input value is of the WeakMap type; returns **false** otherwise.| 3578 3579**Example** 3580 3581 ```ts 3582 let that = new util.types(); 3583 let value : WeakMap<object, number> = new WeakMap(); 3584 let result = that.isWeakMap(value); 3585 ``` 3586 3587 3588### isWeakSet<sup>8+</sup> 3589 3590isWeakSet(value: Object): boolean 3591 3592Checks whether the input value is of the WeakSet type. 3593 3594**System capability**: SystemCapability.Utils.Lang 3595 3596**Parameters** 3597 3598| Name| Type| Mandatory| Description| 3599| -------- | -------- | -------- | -------- | 3600| value | Object | Yes| Object to check.| 3601 3602**Return value** 3603 3604| Type| Description| 3605| -------- | -------- | 3606| boolean | Returns **true** if the input value is of the WeakSet type; returns **false** otherwise.| 3607 3608**Example** 3609 3610 ```ts 3611 let that = new util.types(); 3612 let result = that.isWeakSet(new WeakSet()); 3613 ``` 3614 3615 3616### isBigInt64Array<sup>8+</sup> 3617 3618isBigInt64Array(value: Object): boolean 3619 3620Checks whether the input value is of the BigInt64Array type. 3621 3622**System capability**: SystemCapability.Utils.Lang 3623 3624**Parameters** 3625 3626| Name| Type| Mandatory| Description| 3627| -------- | -------- | -------- | -------- | 3628| value | Object | Yes| Object to check.| 3629 3630**Return value** 3631 3632| Type| Description| 3633| -------- | -------- | 3634| boolean | Returns **true** if the input value is of the BigInt64Array type; returns **false** otherwise.| 3635 3636**Example** 3637 3638 ```ts 3639 let that = new util.types(); 3640 let result = that.isBigInt64Array(new BigInt64Array([])); 3641 ``` 3642 3643 3644### isBigUint64Array<sup>8+</sup> 3645 3646isBigUint64Array(value: Object): boolean 3647 3648Checks whether the input value is of the BigUint64Array type. 3649 3650**System capability**: SystemCapability.Utils.Lang 3651 3652**Parameters** 3653 3654| Name| Type| Mandatory| Description| 3655| -------- | -------- | -------- | -------- | 3656| value | Object | Yes| Object to check.| 3657 3658**Return value** 3659 3660| Type| Description| 3661| -------- | -------- | 3662| boolean | Returns **true** if the input value is of the BigUint64Array type; returns **false** otherwise.| 3663 3664**Example** 3665 3666 ```ts 3667 let that = new util.types(); 3668 let result = that.isBigUint64Array(new BigUint64Array([])); 3669 ``` 3670 3671 3672### isModuleNamespaceObject<sup>8+</sup> 3673 3674isModuleNamespaceObject(value: Object): boolean 3675 3676Checks whether the input value is a module namespace object. 3677 3678> **NOTE** 3679> 3680> This API cannot be used in .ets files. 3681 3682**System capability**: SystemCapability.Utils.Lang 3683 3684**Parameters** 3685 3686| Name| Type| Mandatory| Description| 3687| -------- | -------- | -------- | -------- | 3688| value | Object | Yes| Object to check.| 3689 3690**Return value** 3691 3692| Type| Description| 3693| -------- | -------- | 3694| boolean | Returns **true** if the input value is a module namespace object; returns **false** otherwise.| 3695 3696**Example** 3697 3698 ```ts 3699 // This API cannot be used in .ets files. 3700 import url from '@ohos.url' 3701 let that = new util.types(); 3702 let result = that.isModuleNamespaceObject(url); 3703 ``` 3704 3705 3706### isSharedArrayBuffer<sup>8+</sup> 3707 3708isSharedArrayBuffer(value: Object): boolean 3709 3710Checks whether the input value is of the SharedArrayBuffer type. 3711 3712**System capability**: SystemCapability.Utils.Lang 3713 3714**Parameters** 3715 3716| Name| Type| Mandatory| Description| 3717| -------- | -------- | -------- | -------- | 3718| value | Object | Yes| Object to check.| 3719 3720**Return value** 3721 3722| Type| Description| 3723| -------- | -------- | 3724| boolean | Returns **true** if the input value is of the SharedArrayBuffer type; returns **false** otherwise.| 3725 3726**Example** 3727 3728 ```ts 3729 let that = new util.types(); 3730 let result = that.isSharedArrayBuffer(new SharedArrayBuffer(0)); 3731 ``` 3732 3733## LruBuffer<sup>(deprecated)</sup> 3734 3735> **NOTE** 3736> 3737> 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. 3738 3739### Attributes 3740 3741**System capability**: SystemCapability.Utils.Lang 3742 3743| Name| Type| Readable| Writable| Description| 3744| -------- | -------- | -------- | -------- | -------- | 3745| length | number | Yes| No| Total number of values in this cache.| 3746 3747**Example** 3748 3749 ```ts 3750 let pro : util.LruBuffer<number,number>= new util.LruBuffer(); 3751 pro.put(2,10); 3752 pro.put(1,8); 3753 let result = pro.length; 3754 ``` 3755 3756### constructor<sup>(deprecated)</sup> 3757 3758constructor(capacity?: number) 3759 3760A constructor used to create a **LruBuffer** instance. The default capacity of the cache is 64. 3761 3762> **NOTE** 3763> 3764> 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. 3765 3766**System capability**: SystemCapability.Utils.Lang 3767 3768**Parameters** 3769 3770| Name| Type| Mandatory| Description| 3771| -------- | -------- | -------- | -------- | 3772| capacity | number | No| Capacity of the cache to create. The default value is **64**.| 3773 3774**Example** 3775 3776 ```ts 3777 let lrubuffer : util.LruBuffer<number,number> = new util.LruBuffer(); 3778 ``` 3779 3780### updateCapacity<sup>(deprecated)</sup> 3781 3782updateCapacity(newCapacity: number): void 3783 3784Changes the cache capacity. If the new capacity is less than or equal to **0**, an exception will be thrown. 3785 3786> **NOTE** 3787> 3788> 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. 3789 3790**System capability**: SystemCapability.Utils.Lang 3791 3792**Parameters** 3793 3794| Name| Type| Mandatory| Description| 3795| -------- | -------- | -------- | -------- | 3796| newCapacity | number | Yes| New capacity of the cache.| 3797 3798**Example** 3799 3800 ```ts 3801 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 3802 pro.updateCapacity(100); 3803 ``` 3804 3805### toString<sup>(deprecated)</sup> 3806 3807toString(): string 3808 3809Obtains the string representation of this cache. 3810 3811> **NOTE** 3812> 3813> 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. 3814 3815**System capability**: SystemCapability.Utils.Lang 3816 3817**Return value** 3818 3819| Type| Description| 3820| -------- | -------- | 3821| string | String representation of this cache.| 3822 3823**Example** 3824 3825 ```ts 3826 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 3827 pro.put(2,10); 3828 pro.get(2); 3829 pro.remove(20); 3830 let result = pro.toString(); 3831 ``` 3832 3833### getCapacity<sup>(deprecated)</sup> 3834 3835getCapacity(): number 3836 3837Obtains the capacity of this cache. 3838 3839> **NOTE** 3840> 3841> 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. 3842 3843**System capability**: SystemCapability.Utils.Lang 3844 3845**Return value** 3846 3847| Type| Description| 3848| -------- | -------- | 3849| number | Capacity of the cache.| 3850 3851**Example** 3852 3853 ```ts 3854 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 3855 let result = pro.getCapacity(); 3856 ``` 3857 3858### clear<sup>(deprecated)</sup> 3859 3860clear(): void 3861 3862Clears key-value pairs from this cache. The **afterRemoval()** method will be called to perform subsequent operations. 3863 3864> **NOTE** 3865> 3866> 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. 3867 3868**System capability**: SystemCapability.Utils.Lang 3869 3870**Example** 3871 3872 ```ts 3873 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 3874 pro.put(2,10); 3875 let result = pro.length; 3876 pro.clear(); 3877 ``` 3878 3879### getCreateCount<sup>(deprecated)</sup> 3880 3881getCreateCount(): number 3882 3883Obtains the number of return values for **createDefault()**. 3884 3885> **NOTE** 3886> 3887> 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. 3888 3889**System capability**: SystemCapability.Utils.Lang 3890 3891**Return value** 3892 3893| Type| Description| 3894| -------- | -------- | 3895| number | Number of return values for **createDefault()**.| 3896 3897**Example** 3898 3899 ```ts 3900 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 3901 pro.put(1,8); 3902 let result = pro.getCreateCount(); 3903 ``` 3904 3905### getMissCount<sup>(deprecated)</sup> 3906 3907getMissCount(): number 3908 3909Obtains the number of times that the queried values are mismatched. 3910 3911> **NOTE** 3912> 3913> 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. 3914 3915**System capability**: SystemCapability.Utils.Lang 3916 3917**Return value** 3918 3919| Type| Description| 3920| -------- | -------- | 3921| number | Number of times that the queried values are mismatched.| 3922 3923**Example** 3924 3925 ```ts 3926 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 3927 pro.put(2,10); 3928 pro.get(2); 3929 let result = pro.getMissCount(); 3930 ``` 3931 3932### getRemovalCount<sup>(deprecated)</sup> 3933 3934getRemovalCount(): number 3935 3936Obtains the number of removals from this cache. 3937 3938> **NOTE** 3939> 3940> 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. 3941 3942**System capability**: SystemCapability.Utils.Lang 3943 3944**Return value** 3945 3946| Type| Description| 3947| -------- | -------- | 3948| number | Number of removals from the cache.| 3949 3950**Example** 3951 3952 ```ts 3953 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 3954 pro.put(2,10); 3955 pro.updateCapacity(2); 3956 pro.put(50,22); 3957 let result = pro.getRemovalCount(); 3958 ``` 3959 3960### getMatchCount<sup>(deprecated)</sup> 3961 3962getMatchCount(): number 3963 3964Obtains the number of times that the queried values are matched. 3965 3966> **NOTE** 3967> 3968> 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. 3969 3970**System capability**: SystemCapability.Utils.Lang 3971 3972**Return value** 3973 3974| Type| Description| 3975| -------- | -------- | 3976| number | Number of times that the queried values are matched.| 3977 3978**Example** 3979 3980 ```ts 3981 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 3982 pro.put(2,10); 3983 pro.get(2); 3984 let result = pro.getMatchCount(); 3985 ``` 3986 3987### getPutCount<sup>(deprecated)</sup> 3988 3989getPutCount(): number 3990 3991Obtains the number of additions to this cache. 3992 3993> **NOTE** 3994> 3995> 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. 3996 3997**System capability**: SystemCapability.Utils.Lang 3998 3999**Return value** 4000 4001| Type| Description| 4002| -------- | -------- | 4003| number | Number of additions to the cache.| 4004 4005**Example** 4006 4007 ```ts 4008 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 4009 pro.put(2,10); 4010 let result = pro.getPutCount(); 4011 ``` 4012 4013### isEmpty<sup>(deprecated)</sup> 4014 4015isEmpty(): boolean 4016 4017Checks whether this cache is empty. 4018 4019> **NOTE** 4020> 4021> 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. 4022 4023**System capability**: SystemCapability.Utils.Lang 4024 4025**Return value** 4026 4027| Type| Description| 4028| -------- | -------- | 4029| boolean | Returns **true** if the cache does not contain any value.| 4030 4031**Example** 4032 4033 ```ts 4034 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 4035 pro.put(2,10); 4036 let result = pro.isEmpty(); 4037 ``` 4038 4039### get<sup>(deprecated)</sup> 4040 4041get(key: K): V | undefined 4042 4043Obtains the value of the specified key. 4044 4045> **NOTE** 4046> 4047> 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. 4048 4049**System capability**: SystemCapability.Utils.Lang 4050 4051**Parameters** 4052 4053| Name| Type| Mandatory| Description| 4054| -------- | -------- | -------- | -------- | 4055| key | K | Yes| Key based on which the value is queried.| 4056 4057**Return value** 4058 4059| Type| Description| 4060| -------- | -------- | 4061| V \| undefined | Returns the value of the key if a match is found in the cache; returns **undefined** otherwise.| 4062 4063**Example** 4064 4065 ```ts 4066 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 4067 pro.put(2,10); 4068 let result = pro.get(2); 4069 ``` 4070 4071### put<sup>(deprecated)</sup> 4072 4073put(key: K,value: V): V 4074 4075Adds a key-value pair to this cache. 4076 4077> **NOTE** 4078> 4079> 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. 4080 4081**System capability**: SystemCapability.Utils.Lang 4082 4083**Parameters** 4084 4085| Name| Type| Mandatory| Description| 4086| -------- | -------- | -------- | -------- | 4087| key | K | Yes| Key of the key-value pair to add.| 4088| value | V | Yes| Value of the key-value pair to add.| 4089 4090**Return value** 4091 4092| Type| Description| 4093| -------- | -------- | 4094| 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**.| 4095 4096**Example** 4097 4098 ```ts 4099 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 4100 let result = pro.put(2,10); 4101 ``` 4102 4103### values<sup>(deprecated)</sup> 4104 4105values(): V[] 4106 4107Obtains all values in this cache, listed from the most to the least recently accessed. 4108 4109> **NOTE** 4110> 4111> 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. 4112 4113**System capability**: SystemCapability.Utils.Lang 4114 4115**Return value** 4116 4117| Type| Description| 4118| -------- | -------- | 4119| V [] | All values in the cache, listed from the most to the least recently accessed.| 4120 4121**Example** 4122 4123 ```ts 4124 let pro : util.LruBuffer<number|string,number|string> = new util.LruBuffer(); 4125 pro.put(2,10); 4126 pro.put(2,"anhu"); 4127 pro.put("afaf","grfb"); 4128 let result = pro.values(); 4129 ``` 4130 4131### keys<sup>(deprecated)</sup> 4132 4133keys(): K[] 4134 4135Obtains all keys in this cache, listed from the most to the least recently accessed. 4136 4137> **NOTE** 4138> 4139> 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. 4140 4141**System capability**: SystemCapability.Utils.Lang 4142 4143**Return value** 4144 4145| Type| Description| 4146| -------- | -------- | 4147| K [] | All keys in the cache, listed from the most to the least recently accessed.| 4148 4149**Example** 4150 4151 ```ts 4152 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 4153 pro.put(2,10); 4154 let result = pro.keys(); 4155 ``` 4156 4157### remove<sup>(deprecated)</sup> 4158 4159remove(key: K): V | undefined 4160 4161Removes the specified key and its value from this cache. 4162 4163> **NOTE** 4164> 4165> 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. 4166 4167**System capability**: SystemCapability.Utils.Lang 4168 4169**Parameters** 4170 4171| Name| Type| Mandatory| Description| 4172| -------- | -------- | -------- | -------- | 4173| key | K | Yes| Key to remove.| 4174 4175**Return value** 4176 4177| Type| Description| 4178| -------- | -------- | 4179| 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**.| 4180 4181**Example** 4182 4183 ```ts 4184 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 4185 pro.put(2,10); 4186 let result = pro.remove(20); 4187 ``` 4188 4189### afterRemoval<sup>(deprecated)</sup> 4190 4191afterRemoval(isEvict: boolean,key: K,value: V,newValue: V): void 4192 4193Performs subsequent operations after a value is removed. 4194 4195> **NOTE** 4196> 4197> 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. 4198 4199**System capability**: SystemCapability.Utils.Lang 4200 4201**Parameters** 4202 4203| Name| Type| Mandatory| Description| 4204| -------- | -------- | -------- | -------- | 4205| isEvict | boolean | Yes| Whether the capacity is insufficient. If the value is **true**, this API is called due to insufficient capacity.| 4206| key | K | Yes| Key removed.| 4207| value | V | Yes| Value removed.| 4208| 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.| 4209 4210**Example** 4211 4212```ts 4213class ChildLruBuffer<K, V> extends util.LruBuffer<K, V> { 4214 constructor(capacity?: number) { 4215 super(capacity); 4216 } 4217 4218 afterRemoval(isEvict: boolean, key: K, value: V, newValue: V): void { 4219 if (isEvict === true) { 4220 console.info('key: ' + key); 4221 console.info('value: ' + value); 4222 console.info('newValue: ' + newValue); 4223 } 4224 } 4225} 4226let lru: ChildLruBuffer<number, number> = new ChildLruBuffer(2); 4227lru.put(11, 1); 4228lru.put(22, 2); 4229lru.put(33, 3); 4230``` 4231 4232### contains<sup>(deprecated)</sup> 4233 4234contains(key: K): boolean 4235 4236Checks whether this cache contains the specified key. 4237 4238 4239> **NOTE** 4240> 4241> 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. 4242 4243**System capability**: SystemCapability.Utils.Lang 4244 4245**Parameters** 4246 4247| Name| Type| Mandatory| Description| 4248| -------- | -------- | -------- | -------- | 4249| key | K | Yes| Key to check.| 4250 4251**Return value** 4252 4253| Type| Description| 4254| -------- | -------- | 4255| boolean | Returns **true** if the cache contains the specified key; returns **false** otherwise.| 4256 4257**Example** 4258 4259 ```ts 4260 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 4261 pro.put(2,10); 4262 let result = pro.contains(20); 4263 ``` 4264 4265### createDefault<sup>(deprecated)</sup> 4266 4267createDefault(key: K): V 4268 4269Creates a value if the value of the specified key is not available. 4270 4271> **NOTE** 4272> 4273> 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. 4274 4275**System capability**: SystemCapability.Utils.Lang 4276 4277**Parameters** 4278 4279| Name| Type| Mandatory| Description| 4280| -------- | -------- | -------- | -------- | 4281| key | K | Yes| Key of which the value is missing.| 4282 4283**Return value** 4284 4285| Type| Description| 4286| -------- | -------- | 4287| V | Value of the key.| 4288 4289**Example** 4290 4291 ```ts 4292 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 4293 let result = pro.createDefault(50); 4294 ``` 4295 4296### entries<sup>(deprecated)</sup> 4297 4298entries(): IterableIterator<[K,V]> 4299 4300Obtains a new iterator object that contains all key-value pairs in this object. 4301 4302> **NOTE** 4303> 4304> 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. 4305 4306**System capability**: SystemCapability.Utils.Lang 4307 4308**Return value** 4309 4310| Type| Description| 4311| -------- | -------- | 4312| [K, V] | Iterable array.| 4313 4314**Example** 4315 4316 ```ts 4317 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 4318 pro.put(2,10); 4319 let result = pro.entries(); 4320 ``` 4321 4322### [Symbol.iterator]<sup>(deprecated)</sup> 4323 4324[Symbol.iterator]\(): IterableIterator<[K, V]> 4325 4326Obtains a two-dimensional array in key-value pairs. 4327 4328> **NOTE** 4329> 4330> 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. 4331 4332**System capability**: SystemCapability.Utils.Lang 4333 4334**Return value** 4335 4336| Type| Description| 4337| -------- | -------- | 4338| [K, V] | Two-dimensional array in key-value pairs.| 4339 4340**Example** 4341 4342 ```ts 4343 let pro : util.LruBuffer<number,number> = new util.LruBuffer(); 4344 pro.put(2,10); 4345 let result = pro[Symbol.iterator](); 4346 ``` 4347 4348## Scope<sup>(deprecated)</sup> 4349 4350> **NOTE** 4351> 4352> 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. 4353 4354### constructor<sup>(deprecated)</sup> 4355 4356constructor(lowerObj: ScopeType, upperObj: ScopeType) 4357 4358A constructor used to create a **Scope** object with the specified upper and lower limits. 4359 4360> **NOTE** 4361> 4362> 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. 4363 4364 4365**System capability**: SystemCapability.Utils.Lang 4366 4367**Parameters** 4368 4369| Name| Type| Mandatory| Description| 4370| -------- | -------- | -------- | -------- | 4371| lowerObj | [ScopeType](#scopetype8) | Yes| Lower limit of the **Scope** object.| 4372| upperObj | [ScopeType](#scopetype8) | Yes| Upper limit of the **Scope** object.| 4373 4374**Example** 4375 ```ts 4376 class Temperature{ 4377 private readonly _temp: number; 4378 constructor(value : number) { 4379 this._temp = value; 4380 } 4381 compareTo(value : Temperature ) { 4382 return this._temp >= value.getTemp(); 4383 } 4384 getTemp() { 4385 return this._temp; 4386 } 4387 toString() : string { 4388 return this._temp.toString(); 4389 } 4390 } 4391 let tempLower = new Temperature(30); 4392 let tempUpper = new Temperature(40); 4393 let range = new util.Scope(tempLower, tempUpper); 4394 ``` 4395 4396### toString<sup>(deprecated)</sup> 4397 4398toString(): string 4399 4400Obtains a string representation that contains this **Scope**. 4401 4402> **NOTE** 4403> 4404> 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. 4405 4406**System capability**: SystemCapability.Utils.Lang 4407 4408**Return value** 4409 4410| Type| Description| 4411| -------- | -------- | 4412| string | String representation containing the **Scope**.| 4413 4414**Example** 4415 4416 ```ts 4417 class Temperature{ 4418 private readonly _temp: number; 4419 constructor(value : number) { 4420 this._temp = value; 4421 } 4422 compareTo(value : Temperature ) { 4423 return this._temp >= value.getTemp(); 4424 } 4425 getTemp() { 4426 return this._temp; 4427 } 4428 toString() : string { 4429 return this._temp.toString(); 4430 } 4431 } 4432 4433 let tempLower = new Temperature(30); 4434 let tempUpper = new Temperature(40); 4435 let range = new util.Scope(tempLower, tempUpper); 4436 let result = range.toString(); 4437 ``` 4438 4439### intersect<sup>(deprecated)</sup> 4440 4441intersect(range: Scope): Scope 4442 4443Obtains the intersection of this **Scope** and the given **Scope**. 4444 4445> **NOTE** 4446> 4447> 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. 4448 4449**System capability**: SystemCapability.Utils.Lang 4450 4451**Parameters** 4452 4453| Name| Type| Mandatory| Description| 4454| -------- | -------- | -------- | -------- | 4455| range | [Scope](#scopedeprecated) | Yes| **Scope** specified.| 4456 4457**Return value** 4458 4459| Type| Description| 4460| -------- | -------- | 4461| [Scope](#scopedeprecated) | Intersection of this **Scope** and the given **Scope**.| 4462 4463**Example** 4464 4465 ```ts 4466 class Temperature{ 4467 private readonly _temp: number; 4468 constructor(value : number) { 4469 this._temp = value; 4470 } 4471 compareTo(value : Temperature ) { 4472 return this._temp >= value.getTemp(); 4473 } 4474 getTemp() { 4475 return this._temp; 4476 } 4477 toString() : string { 4478 return this._temp.toString(); 4479 } 4480 } 4481 4482 let tempLower = new Temperature(30); 4483 let tempUpper = new Temperature(40); 4484 let range = new util.Scope(tempLower, tempUpper); 4485 let tempMiDF = new Temperature(35); 4486 let tempMidS = new Temperature(39); 4487 let rangeFir = new util.Scope(tempMiDF, tempMidS); 4488 let result = range.intersect(rangeFir ); 4489 ``` 4490 4491### intersect<sup>(deprecated)</sup> 4492 4493intersect(lowerObj:ScopeType,upperObj:ScopeType):Scope 4494 4495Obtains the intersection of this **Scope** and the given lower and upper limits. 4496 4497> **NOTE** 4498> 4499> 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. 4500 4501**System capability**: SystemCapability.Utils.Lang 4502 4503**Parameters** 4504 4505| Name| Type| Mandatory| Description| 4506| -------- | -------- | -------- | -------- | 4507| lowerObj | [ScopeType](#scopetype8) | Yes| Lower limit.| 4508| upperObj | [ScopeType](#scopetype8) | Yes| Upper limit.| 4509 4510**Return value** 4511 4512| Type| Description| 4513| -------- | -------- | 4514| [Scope](#scopedeprecated) | Intersection of this **Scope** and the given lower and upper limits.| 4515 4516**Example** 4517 4518 ```ts 4519 class Temperature{ 4520 private readonly _temp: number; 4521 constructor(value : number) { 4522 this._temp = value; 4523 } 4524 compareTo(value : Temperature ) { 4525 return this._temp >= value.getTemp(); 4526 } 4527 getTemp() { 4528 return this._temp; 4529 } 4530 toString() : string { 4531 return this._temp.toString(); 4532 } 4533 } 4534 4535 let tempLower = new Temperature(30); 4536 let tempUpper = new Temperature(40); 4537 let tempMiDF = new Temperature(35); 4538 let tempMidS = new Temperature(39); 4539 let range = new util.Scope(tempLower, tempUpper); 4540 let result = range.intersect(tempMiDF, tempMidS); 4541 ``` 4542 4543### getUpper<sup>(deprecated)</sup> 4544 4545getUpper(): ScopeType 4546 4547Obtains the upper limit of this **Scope**. 4548 4549> **NOTE** 4550> 4551> 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. 4552 4553**System capability**: SystemCapability.Utils.Lang 4554 4555**Return value** 4556 4557| Type| Description| 4558| -------- | -------- | 4559| [ScopeType](#scopetype8) | Upper limit of this **Scope**.| 4560 4561**Example** 4562 4563 ```ts 4564 class Temperature{ 4565 private readonly _temp: number; 4566 constructor(value : number) { 4567 this._temp = value; 4568 } 4569 compareTo(value : Temperature ) { 4570 return this._temp >= value.getTemp(); 4571 } 4572 getTemp() { 4573 return this._temp; 4574 } 4575 toString() : string { 4576 return this._temp.toString(); 4577 } 4578 } 4579 4580 let tempLower = new Temperature(30); 4581 let tempUpper = new Temperature(40); 4582 let range = new util.Scope(tempLower, tempUpper); 4583 let result = range.getUpper(); 4584 ``` 4585 4586### getLower<sup>(deprecated)</sup> 4587 4588getLower(): ScopeType 4589 4590Obtains the lower limit of this **Scope**. 4591 4592> **NOTE** 4593> 4594> 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. 4595 4596**System capability**: SystemCapability.Utils.Lang 4597 4598**Return value** 4599 4600| Type| Description| 4601| -------- | -------- | 4602| [ScopeType](#scopetype8) | Lower limit of this **Scope**.| 4603 4604**Example** 4605 4606 ```ts 4607 class Temperature{ 4608 private readonly _temp: number; 4609 constructor(value : number) { 4610 this._temp = value; 4611 } 4612 compareTo(value : Temperature ) { 4613 return this._temp >= value.getTemp(); 4614 } 4615 getTemp() { 4616 return this._temp; 4617 } 4618 toString() : string { 4619 return this._temp.toString(); 4620 } 4621 } 4622 4623 let tempLower = new Temperature(30); 4624 let tempUpper = new Temperature(40); 4625 let range = new util.Scope(tempLower, tempUpper); 4626 let result = range.getLower(); 4627 ``` 4628 4629### expand<sup>(deprecated)</sup> 4630 4631expand(lowerObj: ScopeType,upperObj: ScopeType): Scope 4632 4633Obtains the union set of this **Scope** and the given lower and upper limits. 4634 4635> **NOTE** 4636> 4637> 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. 4638 4639**System capability**: SystemCapability.Utils.Lang 4640 4641**Parameters** 4642 4643| Name| Type| Mandatory| Description| 4644| -------- | -------- | -------- | -------- | 4645| lowerObj | [ScopeType](#scopetype8) | Yes| Lower limit.| 4646| upperObj | [ScopeType](#scopetype8) | Yes| Upper limit.| 4647 4648**Return value** 4649 4650| Type| Description| 4651| -------- | -------- | 4652| [Scope](#scopedeprecated) | Union set of this **Scope** and the given lower and upper limits.| 4653 4654**Example** 4655 4656 ```ts 4657 class Temperature{ 4658 private readonly _temp: number; 4659 constructor(value : number) { 4660 this._temp = value; 4661 } 4662 compareTo(value : Temperature ) { 4663 return this._temp >= value.getTemp(); 4664 } 4665 getTemp() { 4666 return this._temp; 4667 } 4668 toString() : string { 4669 return this._temp.toString(); 4670 } 4671 } 4672 4673 let tempLower = new Temperature(30); 4674 let tempUpper = new Temperature(40); 4675 let tempMiDF = new Temperature(35); 4676 let tempMidS = new Temperature(39); 4677 let range = new util.Scope(tempLower, tempUpper); 4678 let result = range.expand(tempMiDF, tempMidS); 4679 ``` 4680 4681### expand<sup>(deprecated)</sup> 4682 4683expand(range: Scope): Scope 4684 4685Obtains the union set of this **Scope** and the given **Scope**. 4686 4687> **NOTE** 4688> 4689> 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. 4690 4691**System capability**: SystemCapability.Utils.Lang 4692 4693**Parameters** 4694 4695| Name| Type| Mandatory| Description| 4696| -------- | -------- | -------- | -------- | 4697| range | [Scope](#scopedeprecated) | Yes| **Scope** specified.| 4698 4699**Return value** 4700 4701| Type| Description| 4702| -------- | -------- | 4703| [Scope](#scopedeprecated) | Union set of this **Scope** and the given **Scope**.| 4704 4705**Example** 4706 4707 ```ts 4708 class Temperature{ 4709 private readonly _temp: number; 4710 constructor(value : number) { 4711 this._temp = value; 4712 } 4713 compareTo(value : Temperature ) { 4714 return this._temp >= value.getTemp(); 4715 } 4716 getTemp() { 4717 return this._temp; 4718 } 4719 toString() : string { 4720 return this._temp.toString(); 4721 } 4722 } 4723 4724 let tempLower = new Temperature(30); 4725 let tempUpper = new Temperature(40); 4726 let tempMiDF = new Temperature(35); 4727 let tempMidS = new Temperature(39); 4728 let range = new util.Scope(tempLower, tempUpper); 4729 let rangeFir = new util.Scope(tempMiDF, tempMidS); 4730 let result = range.expand(rangeFir); 4731 ``` 4732 4733### expand<sup>(deprecated)</sup> 4734 4735expand(value: ScopeType): Scope 4736 4737Obtains the union set of this **Scope** and the given value. 4738 4739> **NOTE** 4740> 4741> 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. 4742 4743**System capability**: SystemCapability.Utils.Lang 4744 4745**Parameters** 4746 4747| Name| Type| Mandatory| Description| 4748| -------- | -------- | -------- | -------- | 4749| value | [ScopeType](#scopetype8) | Yes| Value specified.| 4750 4751**Return value** 4752 4753| Type| Description| 4754| -------- | -------- | 4755| [Scope](#scopedeprecated) | Union set of this **Scope** and the given value.| 4756 4757**Example** 4758 4759 ```ts 4760 class Temperature{ 4761 private readonly _temp: number; 4762 constructor(value : number) { 4763 this._temp = value; 4764 } 4765 compareTo(value : Temperature ) { 4766 return this._temp >= value.getTemp(); 4767 } 4768 getTemp() { 4769 return this._temp; 4770 } 4771 toString() : string { 4772 return this._temp.toString(); 4773 } 4774 } 4775 4776 let tempLower = new Temperature(30); 4777 let tempUpper = new Temperature(40); 4778 let tempMiDF = new Temperature(35); 4779 let range = new util.Scope(tempLower, tempUpper); 4780 let result = range.expand(tempMiDF); 4781 ``` 4782 4783### contains<sup>(deprecated)</sup> 4784 4785contains(value: ScopeType): boolean 4786 4787Checks whether a value is within this **Scope**. 4788 4789> **NOTE** 4790> 4791> 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. 4792 4793**System capability**: SystemCapability.Utils.Lang 4794 4795**Parameters** 4796 4797| Name| Type| Mandatory| Description| 4798| -------- | -------- | -------- | -------- | 4799| value | [ScopeType](#scopetype8) | Yes| Value specified.| 4800 4801**Return value** 4802 4803| Type| Description| 4804| -------- | -------- | 4805| boolean | Returns **true** if the value is within this **Scope**; returns **false** otherwise.| 4806 4807**Example** 4808 4809 ```ts 4810 class Temperature{ 4811 private readonly _temp: number; 4812 constructor(value : number) { 4813 this._temp = value; 4814 } 4815 compareTo(value : Temperature ) { 4816 return this._temp >= value.getTemp(); 4817 } 4818 getTemp() { 4819 return this._temp; 4820 } 4821 toString() : string { 4822 return this._temp.toString(); 4823 } 4824 } 4825 4826 let tempLower = new Temperature(30); 4827 let tempUpper = new Temperature(40); 4828 let tempMiDF = new Temperature(35); 4829 let range = new util.Scope(tempLower, tempUpper); 4830 let result = range.contains(tempMiDF); 4831 ``` 4832 4833### contains<sup>(deprecated)</sup> 4834 4835contains(range: Scope): boolean 4836 4837Checks whether a range is within this **Scope**. 4838 4839> **NOTE** 4840> 4841> 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. 4842 4843**System capability**: SystemCapability.Utils.Lang 4844 4845**Parameters** 4846 4847| Name| Type| Mandatory| Description| 4848| -------- | -------- | -------- | -------- | 4849| range | [Scope](#scopedeprecated) | Yes| **Scope** specified.| 4850 4851**Return value** 4852 4853| Type| Description| 4854| -------- | -------- | 4855| boolean | Returns **true** if the range is within this **Scope**; returns **false** otherwise.| 4856 4857**Example** 4858 4859 ```ts 4860 class Temperature{ 4861 private readonly _temp: number; 4862 constructor(value : number) { 4863 this._temp = value; 4864 } 4865 compareTo(value : Temperature ) { 4866 return this._temp >= value.getTemp(); 4867 } 4868 getTemp() { 4869 return this._temp; 4870 } 4871 toString() : string { 4872 return this._temp.toString(); 4873 } 4874 } 4875 4876 let tempLower = new Temperature(30); 4877 let tempUpper = new Temperature(40); 4878 let range = new util.Scope(tempLower, tempUpper); 4879 let tempLess = new Temperature(20); 4880 let tempMore = new Temperature(45); 4881 let rangeSec = new util.Scope(tempLess, tempMore); 4882 let result = range.contains(rangeSec); 4883 ``` 4884 4885### clamp<sup>(deprecated)</sup> 4886 4887 4888clamp(value: ScopeType): ScopeType 4889 4890Limits a value to this **Scope**. 4891 4892> **NOTE** 4893> 4894> 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. 4895 4896**System capability**: SystemCapability.Utils.Lang 4897 4898**Parameters** 4899 4900| Name| Type| Mandatory| Description| 4901| -------- | -------- | -------- | -------- | 4902| value | [ScopeType](#scopetype8) | Yes| Value specified.| 4903 4904**Return value** 4905 4906| Type| Description| 4907| -------- | -------- | 4908| [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**.| 4909 4910**Example** 4911 4912 ```ts 4913 class Temperature{ 4914 private readonly _temp: number; 4915 constructor(value : number) { 4916 this._temp = value; 4917 } 4918 compareTo(value : Temperature ) { 4919 return this._temp >= value.getTemp(); 4920 } 4921 getTemp() { 4922 return this._temp; 4923 } 4924 toString() : string { 4925 return this._temp.toString(); 4926 } 4927 } 4928 4929 let tempLower = new Temperature(30); 4930 let tempUpper = new Temperature(40); 4931 let tempMiDF = new Temperature(35); 4932 let range = new util.Scope(tempLower, tempUpper); 4933 let result = range.clamp(tempMiDF); 4934 ``` 4935 4936 4937## Base64<sup>(deprecated)</sup> 4938 4939> **NOTE** 4940> 4941> 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. 4942 4943### constructor<sup>(deprecated)</sup> 4944 4945constructor() 4946 4947A constructor used to create a **Base64** object. 4948 4949> **NOTE** 4950> 4951> 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. 4952 4953**System capability**: SystemCapability.Utils.Lang 4954 4955**Example** 4956 4957 ```ts 4958 let base64 = new util.Base64(); 4959 ``` 4960 4961### encodeSync<sup>(deprecated)</sup> 4962 4963encodeSync(src: Uint8Array): Uint8Array 4964 4965Encodes the input content into a Uint8Array object. This API returns the result synchronously. 4966 4967> **NOTE** 4968> 4969> 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. 4970 4971**System capability**: SystemCapability.Utils.Lang 4972 4973**Parameters** 4974 4975| Name| Type| Mandatory| Description| 4976| -------- | -------- | -------- | -------- | 4977| src | Uint8Array | Yes| Uint8Array object to encode.| 4978 4979**Return value** 4980 4981| Type| Description| 4982| -------- | -------- | 4983| Uint8Array | Uint8Array object obtained.| 4984 4985**Example** 4986 4987 ```ts 4988 let that = new util.Base64(); 4989 let array = new Uint8Array([115,49,51]); 4990 let result = that.encodeSync(array); 4991 ``` 4992 4993### encodeToStringSync<sup>(deprecated)</sup> 4994 4995encodeToStringSync(src: Uint8Array): string 4996 4997Encodes the input content into a string. This API returns the result synchronously. 4998 4999> **NOTE** 5000> 5001> 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. 5002 5003**System capability**: SystemCapability.Utils.Lang 5004 5005**Parameters** 5006 5007| Name| Type| Mandatory| Description| 5008| -------- | -------- | -------- | -------- | 5009| src | Uint8Array | Yes| Uint8Array object to encode.| 5010 5011**Return value** 5012 5013| Type| Description| 5014| -------- | -------- | 5015| string | String obtained.| 5016 5017**Example** 5018 5019 ```ts 5020 let that = new util.Base64(); 5021 let array = new Uint8Array([115,49,51]); 5022 let result = that.encodeToStringSync(array); 5023 ``` 5024 5025### decodeSync<sup>(deprecated)</sup> 5026 5027decodeSync(src: Uint8Array | string): Uint8Array 5028 5029Decodes the input content into a Uint8Array object. 5030 5031> **NOTE** 5032> 5033> 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. 5034 5035**System capability**: SystemCapability.Utils.Lang 5036 5037**Parameters** 5038 5039| Name| Type| Mandatory| Description| 5040| -------- | -------- | -------- | -------- | 5041| src | Uint8Array \| string | Yes| Uint8Array object or string to decode.| 5042 5043**Return value** 5044 5045| Type| Description| 5046| -------- | -------- | 5047| Uint8Array | Uint8Array object obtained.| 5048 5049**Example** 5050 5051 ```ts 5052 let that = new util.Base64(); 5053 let buff = 'czEz'; 5054 let result = that.decodeSync(buff); 5055 ``` 5056 5057### encode<sup>(deprecated)</sup> 5058 5059encode(src: Uint8Array): Promise<Uint8Array> 5060 5061Encodes the input content into a Uint8Array object. This API uses a promise to return the result. 5062 5063> **NOTE** 5064> 5065> 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. 5066 5067**System capability**: SystemCapability.Utils.Lang 5068 5069**Parameters** 5070 5071| Name| Type| Mandatory| Description| 5072| -------- | -------- | -------- | -------- | 5073| src | Uint8Array | Yes| Uint8Array object to encode.| 5074 5075**Return value** 5076 5077| Type| Description| 5078| -------- | -------- | 5079| Promise<Uint8Array> | Promise used to return the Uint8Array object obtained.| 5080 5081**Example** 5082 5083 ```ts 5084 let that = new util.Base64(); 5085 let array = new Uint8Array([115,49,51]); 5086 let rarray = new Uint8Array([99,122,69,122]); 5087 that.encode(array).then(val=>{ 5088 for (let i = 0; i < rarray.length; i++) { 5089 console.log(val[i].toString()) 5090 } 5091 }) 5092 ``` 5093 5094### encodeToString<sup>(deprecated)</sup> 5095 5096encodeToString(src: Uint8Array): Promise<string> 5097 5098Encodes the input content into a string. This API uses a promise to return the result. 5099 5100> **NOTE** 5101> 5102> 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. 5103 5104**System capability**: SystemCapability.Utils.Lang 5105 5106**Parameters** 5107 5108| Name| Type| Mandatory| Description| 5109| -------- | -------- | -------- | -------- | 5110| src | Uint8Array | Yes| Uint8Array object to encode.| 5111 5112**Return value** 5113 5114| Type| Description| 5115| -------- | -------- | 5116| Promise<string> | Promise used to return the string obtained.| 5117 5118**Example** 5119 5120 ```ts 5121 let that = new util.Base64(); 5122 let array = new Uint8Array([115,49,51]); 5123 that.encodeToString(array).then(val=>{ 5124 console.log(val) 5125 }) 5126 ``` 5127 5128### decode<sup>(deprecated)</sup> 5129 5130 5131decode(src: Uint8Array | string): Promise<Uint8Array> 5132 5133Decodes the input content into a Uint8Array object. This API uses a promise to return the result. 5134 5135> **NOTE** 5136> 5137> 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. 5138 5139**System capability**: SystemCapability.Utils.Lang 5140 5141**Parameters** 5142 5143| Name| Type| Mandatory| Description| 5144| -------- | -------- | -------- | -------- | 5145| src | Uint8Array \| string | Yes| Uint8Array object or string to decode.| 5146 5147**Return value** 5148 5149| Type| Description| 5150| -------- | -------- | 5151| Promise<Uint8Array> | Promise used to return the Uint8Array object obtained.| 5152 5153**Example** 5154 5155 ```ts 5156 let that = new util.Base64(); 5157 let array = new Uint8Array([99,122,69,122]); 5158 let rarray = new Uint8Array([115,49,51]); 5159 that.decode(array).then(val=>{ 5160 for (let i = 0; i < rarray.length; i++) { 5161 console.log(val[i].toString()); 5162 } 5163 }) 5164 ``` 5165