# @ohos.util (util) The **util** module provides common utility functions, such as **TextEncoder** and **TextDecoder** for string encoding and decoding, **RationalNumber** for rational number operations, **LruBuffer** for buffer management, **Scope** for range determination, **Base64** for Base64 encoding and decoding, and **Types** for checks of built-in object types. > **NOTE** > > 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. ## Modules to Import ```js import util from '@ohos.util'; ``` ## util.format9+ format(format: string, ...args: Object[]): string Formats the specified values and inserts them into the string by replacing the wildcard in the string. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | ------- | -------- | ---- | -------------- | | format | string | Yes | String.| | ...args | Object[] | No | Values to format. The formatted values will be replaced the wildcard in the string. | **Return value** | Type | Description | | ------ | ---------------------------- | | string | String containing the formatted values.| **Example** ```js let res = util.format("%s", "hello world!"); console.log(res); ``` ## util.errnoToString9+ errnoToString(errno: number): string Obtains detailed information about a system error code. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | -------------------------- | | errno | number | Yes | Error code generated.| **Return value** | Type | Description | | ------ | ---------------------- | | string | Detailed information about the error code.| **Example** ```js let errnum = -1; // -1 is a system error code. let result = util.errnoToString(errnum); console.log("result = " + result); ``` ## util.callbackWrapper callbackWrapper(original: Function): (err: Object, value: Object )=>void Calls 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. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | original | Function | Yes| Asynchronous function.| **Return value** | Type| Description| | -------- | -------- | | Function | Callback, in which 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.| **Example** ```js async function promiseFn() { return Promise.reject('value'); } let err = "type err"; let cb = util.callbackWrapper(promiseFn); cb((err, ret) => { console.log(err); console.log(ret); }, err) ``` ## util.promisify9+ promisify(original: (err: Object, value: Object) => void): Function Processes an asynchronous function and returns a promise. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | original | Function | Yes| Asynchronous function.| **Return value** | Type| Description| | -------- | -------- | | Function | Function in the error-first style (that is, **(err, value) =>...** is called as the last parameter) and the promise.| **Example** ```js function aysnFun(str1, str2) { if (typeof str1 === 'object' && typeof str2 === 'object') { return str2 } else { return str1 } } let newPromiseObj = util.promisify(aysnFun); newPromiseObj({ err: "type error" }, {value:'HelloWorld'}).then(res => { console.log(res); }) ``` ## util.randomUUID9+ randomUUID(entropyCache?: boolean): string Uses a secure random number generator to generate a random universally unique identifier (UUID) of RFC 4122 version 4. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | entropyCache | boolean | No| Whether a cached UUID can be used. The default value is **true**.| **Return value** | Type| Description| | -------- | -------- | | string | A string representing the UUID generated.| **Example** ```js let uuid = util.randomUUID(true); console.log("RFC 4122 Version 4 UUID:" + uuid); // Output: // RFC 4122 Version 4 UUID:88368f2a-d5db-47d8-a05f-534fab0a0045 ``` ## util.randomBinaryUUID9+ randomBinaryUUID(entropyCache?: boolean): Uint8Array Uses a secure random number generator to generate a random binary UUID of RFC 4122 version 4. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | entropyCache | boolean | No| Whether a cached UUID can be used. The default value is **true**.| **Return value** | Type| Description| | -------- | -------- | | Uint8Array | A Uint8Array value representing the UUID generated.| **Example** ```js let uuid = util.randomBinaryUUID(true); console.log(JSON.stringify(uuid)); // Output: // 138,188,43,243,62,254,70,119,130,20,235,222,199,164,140,150 ``` ## util.parseUUID9+ parseUUID(uuid: string): Uint8Array Parses a UUID from a string, as described in RFC 4122 version 4. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | uuid | string | Yes| A string representing the UUID.| **Return value** | Type| Description| | -------- | -------- | | Uint8Array | A Uint8Array value representing the UUID parsed. If the parsing fails, **SyntaxError** is thrown.| **Example** ```js let uuid = util.parseUUID("84bdf796-66cc-4655-9b89-d6218d100f9c"); console.log(JSON.stringify(uuid)); // Output: // 132,189,247,150,102,204,70,85,155,137,214,33,141,16,15,156 ``` ## util.printf(deprecated) printf(format: string, ...args: Object[]): string Formats the specified values and inserts them into the string by replacing the wildcard in the string. > **NOTE** > > This API is supported since API version 7 and deprecated since API version 9. You are advised to use [util.format9+](#utilformat9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | format | string | Yes| String.| | ...args | Object[] | No| Values to format. The formatted values will be replaced the wildcard in the string.| **Return value** | Type| Description| | -------- | -------- | | string | String containing the formatted values.| **Example** ```js let res = util.printf("%s", "hello world!"); console.log(res); ``` ## util.getErrorString(deprecated) getErrorString(errno: number): string Obtains detailed information about a system error code. > **NOTE** > > This API is supported since API version 7 and deprecated since API version 9. You are advised to use [util.errnoToString9+](#utilerrnotostring9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | errno | number | Yes| Error code generated.| **Return value** | Type| Description| | -------- | -------- | | string | Detailed information about the error code.| **Example** ```js let errnum = -1; // -1 is a system error code. let result = util.getErrorString(errnum); console.log("result = " + result); ``` ## util.promiseWrapper(deprecated) promiseWrapper(original: (err: Object, value: Object) => void): Object Processes an asynchronous function and returns a promise. > **NOTE** > > This API is unavailable. You are advised to use [util.promisify9+](#utilpromisify9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | original | Function | Yes| Asynchronous function.| **Return value** | Type| Description| | -------- | -------- | | Function | Function in the error-first style (that is, **(err, value) =>...** is called as the last parameter) and the promise.| ## TextDecoder ### Attributes **System capability**: SystemCapability.Utils.Lang | Name| Type| Readable| Writable| Description| | -------- | -------- | -------- | -------- | -------- | | encoding | string | Yes| No| Encoding format.
- 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| | fatal | boolean | Yes| No| Whether to display fatal errors.| | 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.| ### constructor9+ constructor() A constructor used to create a **TextDecoder** object. **System capability**: SystemCapability.Utils.Lang ### create9+ create(encoding?: string,options?: { fatal?: boolean; ignoreBOM?: boolean }): TextDecoder; Creates a **TextDecoder** object. It provides the same function as the deprecated argument constructor. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | -------- | ------ | ---- | ------------------------------------------------ | | encoding | string | No | Encoding format. | | options | Object | No | Encoding-related options, which include **fatal** and **ignoreBOM**.| **Table 1.1** options | Name | Type| Mandatory| Description | | --------- | -------- | ---- | ------------------ | | fatal | boolean | No | Whether to display fatal errors.| | ignoreBOM | boolean | No | Whether to ignore the BOM. | **Example** ```js let textDecoder = new util.TextDecoder() textDecoder.create('utf-8', { ignoreBOM : true }); ``` ### decodeWithStream9+ decodeWithStream(input: Uint8Array, options?: { stream?: boolean }): string Decodes the input content. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | input | Uint8Array | Yes| Uint8Array to decode.| | options | Object | No| Options related to decoding.| **Table 2** options | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | 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**.| **Return value** | Type| Description| | -------- | -------- | | string | Data decoded.| **Example** ```js let textDecoder = new util.TextDecoder("utf-8",{ignoreBOM: true}); let result = new Uint8Array(6); result[0] = 0xEF; result[1] = 0xBB; result[2] = 0xBF; result[3] = 0x61; result[4] = 0x62; result[5] = 0x63; console.log("input num:"); let retStr = textDecoder.decodeWithStream( result , {stream: false}); console.log("retStr = " + retStr); ``` ### constructor(deprecated) constructor(encoding?: string, options?: { fatal?: boolean; ignoreBOM?: boolean }) A constructor used to create a **TextDecoder** object. > **NOTE** > > This API is supported since API version 7 and deprecated since API version 9. You are advised to use [create9+](#create9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | encoding | string | No| Encoding format.| | options | Object | No| Encoding-related options, which include **fatal** and **ignoreBOM**.| **Table 1** options | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | fatal | boolean | No| Whether to display fatal errors.| | ignoreBOM | boolean | No| Whether to ignore the BOM.| **Example** ```js let textDecoder = new util.TextDecoder("utf-8",{ignoreBOM: true}); ``` ### decode(deprecated) decode(input: Uint8Array, options?: { stream?: false }): string Decodes the input content. > **NOTE** > > This API is supported since API version 7 and deprecated since API version 9. You are advised to use [decodeWithStream9+](#decodewithstream9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | input | Uint8Array | Yes| Uint8Array to decode.| | options | Object | No| Options related to decoding.| **Table 2** options | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | 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**.| **Return value** | Type| Description| | -------- | -------- | | string | Data decoded.| **Example** ```js let textDecoder = new util.TextDecoder("utf-8",{ignoreBOM: true}); let result = new Uint8Array(6); result[0] = 0xEF; result[1] = 0xBB; result[2] = 0xBF; result[3] = 0x61; result[4] = 0x62; result[5] = 0x63; console.log("input num:"); let retStr = textDecoder.decode( result , {stream: false}); console.log("retStr = " + retStr); ``` ## TextEncoder ### Attributes **System capability**: SystemCapability.Utils.Lang | Name| Type| Readable| Writable| Description| | -------- | -------- | -------- | -------- | -------- | | encoding | string | Yes| No| Encoding format. The default format is **utf-8**.| ### constructor constructor() A constructor used to create a **TextEncoder** object. **System capability**: SystemCapability.Utils.Lang **Example** ```js let textEncoder = new util.TextEncoder(); ``` ### constructor9+ constructor(encoding?: string) A constructor used to create a **TextEncoder** object. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | ----- | ---- | ---- | ---- | | encoding | string | No| Encoding format.| **Example** ```js let textEncoder = new util.TextEncoder("utf-8"); ``` ### encodeInto9+ encodeInto(input?: string): Uint8Array Encodes the input content. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------ | | input | string | No | String to encode.| **Return value** | Type | Description | | ---------- | ------------------ | | Uint8Array | Encoded text.| **Example** ```js let textEncoder = new util.TextEncoder(); let buffer = new ArrayBuffer(20); let result = new Uint8Array(buffer); result = textEncoder.encodeInto("\uD800¥¥"); ``` ### encodeIntoUint8Array9+ encodeIntoUint8Array(input: string, dest: Uint8Array, ): { read: number; written: number } Stores the UTF-8 encoded text. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ---------- | ---- | ------------------------------------------------------- | | input | string | Yes | String to encode. | | dest | Uint8Array | Yes | **Uint8Array** instance used to store the UTF-8 encoded text.| **Return value** | Type | Description | | ---------- | ------------------ | | Uint8Array | Encoded text.| **Example** ```js let that = new util.TextEncoder() let buffer = new ArrayBuffer(4) let dest = new Uint8Array(buffer) let result = new Object() result = that.encodeIntoUint8Array('abcd', dest) ``` ### encodeInto(deprecated) encodeInto(input: string, dest: Uint8Array, ): { read: number; written: number } Stores the UTF-8 encoded text. > **NOTE** > > This API is supported since API version 7 and deprecated since API version 9. You are advised to use [encodeIntoUint8Array9+](#encodeintouint8array9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | input | string | Yes| String to encode.| | dest | Uint8Array | Yes| **Uint8Array** instance used to store the UTF-8 encoded text.| **Return value** | Type| Description| | -------- | -------- | | Uint8Array | Encoded text.| **Example** ```js let that = new util.TextEncoder() let buffer = new ArrayBuffer(4) let dest = new Uint8Array(buffer) let result = new Object() result = that.encodeInto('abcd', dest) ``` ### encode(deprecated) encode(input?: string): Uint8Array Encodes the input content. > **NOTE** > > This API is supported since API version 7 and deprecated since API version 9. You are advised to use [encodeInto9+](#encodeinto9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | input | string | No| String to encode.| **Return value** | Type| Description| | -------- | -------- | | Uint8Array | Encoded text.| **Example** ```js let textEncoder = new util.TextEncoder(); let buffer = new ArrayBuffer(20); let result = new Uint8Array(buffer); result = textEncoder.encode("\uD800¥¥"); ``` ## RationalNumber8+ ### constructor9+ constructor() A constructor used to create a **RationalNumber** object. **System capability**: SystemCapability.Utils.Lang **Example** ```js let rationalNumber = new util.RationalNumber(); ``` ### parseRationalNumber9+ parseRationalNumber(numerator: number,denominator: number): RationalNumber Parses a rational number. Previously, this processing is an internal action of the deprecated constructor. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | ----------- | ------ | ---- | ---------------- | | numerator | number | Yes | Numerator, which is an integer.| | denominator | number | Yes | Denominator, which is an integer.| **Example** ```js let rationalNumber = util.RationalNumber.parseRationalNumber(1,2) ``` ### createRationalFromString8+ static createRationalFromString​(rationalString: string): RationalNumber​ Creates a **RationalNumber** object based on the given string. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | rationalString | string | Yes| String used to create the **RationalNumber** object.| **Return value** | Type| Description| | -------- | -------- | | object | **RationalNumber** object created.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let rational = util.RationalNumber.createRationalFromString("3/4"); ``` ### compare9+ compare​(another: RationalNumber): number​ Compares this **RationalNumber** object with a given object. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | ------- | -------------- | ---- | ------------------ | | another | RationalNumber | Yes | Object used to compare with this **RationalNumber** object.| **Return value** | Type | Description | | ------ | ------------------------------------------------------------ | | 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.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let rational = util.RationalNumber.createRationalFromString("3/4"); let result = rationalNumber.compare(rational); ``` ### valueOf8+ valueOf(): number Obtains the value of this **RationalNumber** object as an integer or a floating-point number. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | number | An integer or a floating-point number.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let result = rationalNumber.valueOf(); ``` ### equals8+ equals​(obj: Object): boolean Checks whether this **RationalNumber** object equals the given object. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | object | Object | Yes| Object used to compare with this **RationalNumber** object.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the two objects are equal; returns **false** otherwise.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let rational = util.RationalNumber.createRationalFromString("3/4"); let result = rationalNumber.equals(rational); ``` ### getCommonFactor9+ getCommonFactor(number1: number,number2: number): number Obtains the greatest common divisor of two specified integers. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | ------- | ------ | ---- | ---------- | | number1 | number | Yes | The first integer used to get the greatest common divisor.| | number2 | number | Yes | The second integer used to get the greatest common divisor.| **Return value** | Type | Description | | ------ | ------------------------------ | | number | Greatest common divisor obtained.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let result = util.RationalNumber.getCommonFactor(4,6); ``` ### getNumerator8+ getNumerator​(): number Obtains the numerator of this **RationalNumber** object. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | number | Numerator of this **RationalNumber** object.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let result = rationalNumber.getNumerator(); ``` ### getDenominator8+ getDenominator​(): number Obtains the denominator of this **RationalNumber** object. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | number | Denominator of this **RationalNumber** object.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let result = rationalNumber.getDenominator(); ``` ### isZero8+ isZero​():boolean Checks whether this **RationalNumber** object is **0**. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the value of this **RationalNumber** object is **0**; returns **false** otherwise.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let result = rationalNumber.isZero(); ``` ### isNaN8+ isNaN​(): boolean Checks whether this **RationalNumber** object is a Not a Number (NaN). **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if this **RationalNumber** object is a NaN (the denominator and numerator are both **0**); returns **false** otherwise.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let result = rationalNumber.isNaN(); ``` ### isFinite8+ isFinite​():boolean Checks whether this **RationalNumber** object represents a finite value. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if this **RationalNumber** object represents a finite value (the denominator is not **0**); returns **false** otherwise.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let result = rationalNumber.isFinite(); ``` ### toString8+ toString​(): string Obtains the string representation of this **RationalNumber** object. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | string | Returns **NaN** if the numerator and denominator of this object are both **0**; returns a string in Numerator/Denominator format otherwise, for example, **3/5**.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let result = rationalNumber.toString(); ``` ### constructor(deprecated) constructor(numerator: number,denominator: number) A constructor used to create a **RationalNumber** object. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [constructor9+](#constructor9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | numerator | number | Yes| Numerator, which is an integer.| | denominator | number | Yes| Denominator, which is an integer.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); ``` ### compareTo(deprecated) compareTo​(another: RationalNumber): number​ Compares this **RationalNumber** object with a given object. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [compare9+](#compare9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | another | RationalNumber | Yes| Object used to compare with this **RationalNumber** object.| **Return value** | Type| Description| | -------- | -------- | | 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.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let rational = util.RationalNumber.createRationalFromString("3/4"); let result = rationalNumber.compareTo(rational); ``` ### getCommonDivisor(deprecated) static getCommonDivisor​(number1: number,number2: number): number Obtains the greatest common divisor of two specified integers. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getCommonFactor9+](#getcommonfactor9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | number1 | number | Yes| The first integer used to get the greatest common divisor.| | number2 | number | Yes| The second integer used to get the greatest common divisor.| **Return value** | Type| Description| | -------- | -------- | | number | Greatest common divisor obtained.| **Example** ```js let rationalNumber = new util.RationalNumber(1,2); let result = util.RationalNumber.getCommonDivisor(4,6); ``` ## LRUCache9+ ### Attributes **System capability**: SystemCapability.Utils.Lang | Name | Type | Readable| Writable| Description | | ------ | ------ | ---- | ---- | ---------------------- | | length | number | Yes | No | Total number of values in this cache.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); pro.put(1,8); let result = pro.length; ``` ### constructor9+ constructor(capacity?: number) A constructor used to create a **LruCache** instance. The default capacity of the cache is 64. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | -------- | ------ | ---- | ---------------------------- | | capacity | number | No | Capacity of the **LruCache** to create.| **Example** ```js let lrubuffer= new util.LRUCache(); ``` ### updateCapacity9+ updateCapacity(newCapacity: number): void Changes the **LruCache** capacity. If the new capacity is less than or equal to **0**, an exception will be thrown. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | ----------- | ------ | ---- | ---------------------------- | | newCapacity | number | Yes | New capacity of the **LruCache**.| **Example** ```js let pro = new util.LRUCache(); let result = pro.updateCapacity(100); ``` ### toString9+ toString(): string Obtains the string representation of this **LruCache** object. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | -------------------------- | | string | String representation of this **LruCache** object.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); pro.get(2); pro.remove(20); let result = pro.toString(); ``` ### getCapacity9+ getCapacity(): number Obtains the capacity of this cache. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | ---------------------- | | number | Capacity of this cache.| **Example** ```js let pro = new util.LRUCache(); let result = pro.getCapacity(); ``` ### clear9+ clear(): void Clears key-value pairs from this cache. The **afterRemoval()** method will be called to perform subsequent operations. **System capability**: SystemCapability.Utils.Lang **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); let result = pro.length; pro.clear(); ``` ### getCreateCount9+ getCreateCount(): number Obtains the number of return values for **createDefault()**. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | --------------------------------- | | number | Number of return values for **createDefault()**.| **Example** ```js let pro = new util.LRUCache(); pro.put(1,8); let result = pro.getCreateCount(); ``` ### getMissCount9+ getMissCount(): number Obtains the number of times that the queried values are mismatched. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | ------------------------ | | number | Number of times that the queried values are mismatched.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); pro.get(2); let result = pro.getMissCount(); ``` ### getRemovalCount9+ getRemovalCount(): number Obtains the number of removals from this cache. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | -------------------------- | | number | Number of removals from the cache.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); pro.updateCapacity(2); pro.put(50,22); let result = pro.getRemovalCount(); ``` ### getMatchCount9+ getMatchCount(): number Obtains the number of times that the queried values are matched. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | -------------------------- | | number | Number of times that the queried values are matched.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); pro.get(2); let result = pro.getMatchCount(); ``` ### getPutCount9+ getPutCount(): number Obtains the number of additions to this cache. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | ---------------------------- | | number | Number of additions to the cache.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); let result = pro.getPutCount(); ``` ### isEmpty9+ isEmpty(): boolean Checks whether this cache is empty. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------- | ---------------------------------------- | | boolean | Returns **true** if the cache does not contain any value.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); let result = pro.isEmpty(); ``` ### get9+ get(key: K): V | undefined Obtains the value of the specified key. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description | | ------ | ---- | ---- | ------------ | | key | K | Yes | Key based on which the value is queried.| **Return value** | Type | Description | | ------------------------ | ------------------------------------------------------------ | | V \| undefined | Returns the value of the key if a match is found in the cache; returns **undefined** otherwise.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); let result = pro.get(2); ``` ### put9+ put(key: K,value: V): V Adds a key-value pair to this cache. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description | | ------ | ---- | ---- | -------------------------- | | key | K | Yes | Key of the key-value pair to add. | | value | V | Yes | Value of the key-value pair to add.| **Return value** | Type| Description | | ---- | ------------------------------------------------------------ | | V | Returns the existing value if the key already exists; returns the value added otherwise. If the key or value is null, an exception will be thrown. | **Example** ```js let pro = new util.LRUCache(); let result = pro.put(2,10); ``` ### values9+ values(): V[] Obtains all values in this cache, listed from the most to the least recently accessed. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | --------- | ------------------------------------------------------------ | | V [] | All values in the cache, listed from the most to the least recently accessed.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); pro.put(2,"anhu"); pro.put("afaf","grfb"); let result = pro.values(); ``` ### keys9+ keys(): K[] Obtains all keys in this cache, listed from the most to the least recently accessed. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | --------- | ------------------------------------------------------------ | | K [] | All keys in the cache, listed from the most to the least recently accessed.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); let result = pro.keys(); ``` ### remove9+ remove(key: K): V | undefined Removes the specified key and its value from this cache. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description | | ------ | ---- | ---- | -------------- | | key | K | Yes | Key to remove.| **Return value** | Type | Description | | ------------------------ | ------------------------------------------------------------ | | 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. If the key is null, an exception will be thrown.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); let result = pro.remove(20); ``` ### afterRemoval9+ afterRemoval(isEvict: boolean,key: K,value: V,newValue: V): void Performs subsequent operations after a value is removed. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | -------- | ------- | ---- | ------------------------------------------------------------ | | isEvict | boolean | Yes | Whether the cache capacity is insufficient. If the value is **true**, this API is called due to insufficient capacity. | | key | K | Yes | Key removed. | | value | V | Yes | Value removed. | | 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.| **Example** ```js let arr = []; class ChildLruBuffer extends util.LRUCache { constructor() { super(); } afterRemoval(isEvict, key, value, newValue) { if (isEvict === false) { arr = [key, value, newValue]; } } } let lru = new ChildLruBuffer(); lru.afterRemoval(false,10,30,null); ``` ### contains9+ contains(key: K): boolean Checks whether this cache contains the specified key. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ---------------- | | key | K | Yes | Key to check.| **Return value** | Type | Description | | ------- | ------------------------------------------ | | boolean | Returns **true** if the cache contains the specified key; returns **false** otherwise.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); let obj = {1:"key"}; let result = pro.contains(obj); ``` ### createDefault9+ createDefault(key: K): V Creates a value if the value of the specified key is not available. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description | | ------ | ---- | ---- | -------------- | | key | K | Yes | Key of which the value is missing.| **Return value** | Type| Description | | ---- | ------------------ | | V | Value of the key.| **Example** ```js let pro = new util.LRUCache(); let result = pro.createDefault(50); ``` ### entries9+ entries(): IterableIterator<[K,V]> Obtains a new iterator object that contains all key-value pairs in this object. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ----------- | -------------------- | | [K, V] | Iterable array.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); let result = pro.entries(); ``` ### [Symbol.iterator]9+ [Symbol.iterator]\(): IterableIterator<[K, V]> Obtains a two-dimensional array in key-value pairs. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ----------- | ------------------------------ | | [K, V] | Two-dimensional array in key-value pairs.| **Example** ```js let pro = new util.LRUCache(); pro.put(2,10); let result = pro[Symbol.iterator](); ``` ## ScopeComparable8+ The values of the **ScopeComparable** type are used to implement the **compareTo** method. Therefore, ensure that the input parameters are comparable. **System capability**: SystemCapability.Utils.Lang ### compareTo8+ compareTo(other: ScopeComparable): boolean; Compares two values and returns a Boolean value. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description | | ------ | ---- | ---- | -------------- | | other | [ScopeComparable](#scopecomparable8) | Yes | The other value to be compared with the current value.| **Return value** | Type| Description | | ---- | ------------------ | | boolean | If the current value is greater than or equal to the input value, **true** is returned. Otherwise, **false** is returned.| **Example** Create a class to implement the **compareTo** method. The **Temperature** class is used as an example in the following sample code. ```js class Temperature{ constructor(value){ // If TS is used for development, add the following code: // private readonly _temp: Temperature; this._temp = value; } compareTo(value){ return this._temp >= value.getTemp(); } getTemp(){ return this._temp; } toString(){ return this._temp.toString(); } } ``` ## ScopeType8+ Defines the type of values in a **Scope** object. **System capability**: SystemCapability.Utils.Lang | Type| Description| | -------- | -------- | | number | The value type is a number.| | [ScopeComparable](#scopecomparable8) | The value type is ScopeComparable.| ## ScopeHelper9+ ### constructor9+ constructor(lowerObj: ScopeType, upperObj: ScopeType) A constructor used to create a **ScopeHelper** object with the specified upper and lower limits. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------ | ---- | ---------------------- | | lowerObj | [ScopeType](#scopetype8) | Yes | Lower limit of the **Scope** object.| | upperObj | [ScopeType](#scopetype8) | Yes | Upper limit of the **Scope** object.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let range = new util.ScopeHelper(tempLower, tempUpper); ``` ### toString9+ toString(): string Obtains a string representation that contains this **Scope**. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------ | -------------------------------------- | | string | String representation containing the **Scope**.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let range = new util.ScopeHelper(tempLower, tempUpper); let result = range.toString(); ``` ### intersect9+ intersect(range: ScopeHelper): ScopeHelper Obtains the intersection of this **Scope** and the given **Scope**. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ---------------------------- | ---- | ------------------ | | range | [ScopeHelper](#scopehelper9) | Yes | **Scope** specified.| **Return value** | Type | Description | | ------------------------------ | ------------------------------ | | [ScopeHelper9+](#scopehelper9) | Intersection of this **Scope** and the given **Scope**.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let range = new util.ScopeHelper(tempLower, tempUpper); let tempMiDF = new Temperature(35); let tempMidS = new Temperature(39); let rangeFir = new util.ScopeHelper(tempMiDF, tempMidS); range.intersect(rangeFir); ``` ### intersect9+ intersect(lowerObj:ScopeType,upperObj:ScopeType):ScopeHelper Obtains the intersection of this **Scope** and the given lower and upper limits. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------ | ---- | ---------------- | | lowerObj | [ScopeType](#scopetype8) | Yes | Lower limit.| | upperObj | [ScopeType](#scopetype8) | Yes | Upper limit.| **Return value** | Type | Description | | ---------------------------- | ---------------------------------------- | | [ScopeHelper](#scopehelper9) | Intersection of this **Scope** and the given lower and upper limits.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let tempMiDF = new Temperature(35); let tempMidS = new Temperature(39); let range = new util.ScopeHelper(tempLower, tempUpper); let result = range.intersect(tempMiDF, tempMidS); ``` ### getUpper9+ getUpper(): ScopeType Obtains the upper limit of this **Scope**. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------------------------ | ---------------------- | | [ScopeType](#scopetype8) | Upper limit of this **Scope**.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let range = new util.ScopeHelper(tempLower, tempUpper); let result = range.getUpper(); ``` ### getLower9+ getLower(): ScopeType Obtains the lower limit of this **Scope**. **System capability**: SystemCapability.Utils.Lang **Return value** | Type | Description | | ------------------------ | ---------------------- | | [ScopeType](#scopetype8) | Lower limit of this **Scope**.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let range = new util.ScopeHelper(tempLower, tempUpper); let result = range.getLower(); ``` ### expand9+ expand(lowerObj: ScopeType,upperObj: ScopeType): ScopeHelper Obtains the union set of this **Scope** and the given lower and upper limits. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------ | ---- | ---------------- | | lowerObj | [ScopeType](#scopetype8) | Yes | Lower limit.| | upperObj | [ScopeType](#scopetype8) | Yes | Upper limit.| **Return value** | Type | Description | | ---------------------------- | ------------------------------------ | | [ScopeHelper](#scopehelper9) | Union set of this **Scope** and the given lower and upper limits.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let tempMiDF = new Temperature(35); let tempMidS = new Temperature(39); let range = new util.ScopeHelper(tempLower, tempUpper); let result = range.expand(tempMiDF, tempMidS); ``` ### expand9+ expand(range: ScopeHelper): ScopeHelper Obtains the union set of this **Scope** and the given **Scope**. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ---------------------------- | ---- | ------------------ | | range | [ScopeHelper](#scopehelper9) | Yes | **Scope** specified.| **Return value** | Type | Description | | ---------------------------- | ---------------------------------- | | [ScopeHelper](#scopehelper9) | Union set of this **Scope** and the given **Scope**.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let tempMiDF = new Temperature(35); let tempMidS = new Temperature(39); let range = new util.ScopeHelper(tempLower, tempUpper); let rangeFir = new util.ScopeHelper(tempMiDF, tempMidS); let result = range.expand(rangeFir); ``` ### expand9+ expand(value: ScopeType): ScopeHelper Obtains the union set of this **Scope** and the given value. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------------------------ | ---- | ---------------- | | value | [ScopeType](#scopetype8) | Yes | Value specified.| **Return value** | Type | Description | | ---------------------------- | -------------------------------- | | [ScopeHelper](#scopehelper9) | Union set of this **Scope** and the given value.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let tempMiDF = new Temperature(35); let range = new util.ScopeHelper(tempLower, tempUpper); let result = range.expand(tempMiDF); ``` ### contains9+ contains(value: ScopeType): boolean Checks whether a value is within this **Scope**. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------------------------ | ---- | ---------------- | | value | [ScopeType](#scopetype8) | Yes | Value specified.| **Return value** | Type | Description | | ------- | --------------------------------------------------- | | boolean | Returns **true** if the value is within this **Scope**; returns **false** otherwise.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let tempMiDF = new Temperature(35); let range = new util.ScopeHelper(tempLower, tempUpper); range.contains(tempMiDF); ``` ### contains9+ contains(range: ScopeHelper): boolean Checks whether a range is within this **Scope**. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ---------------------------- | ---- | ------------------ | | range | [ScopeHelper](#scopehelper9) | Yes | **Scope** specified.| **Return value** | Type | Description | | ------- | ----------------------------------------------------- | | boolean | Returns **true** if the range is within this **Scope**; returns **false** otherwise.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let range = new util.ScopeHelper(tempLower, tempUpper); let tempLess = new Temperature(20); let tempMore = new Temperature(45); let rangeSec = new util.ScopeHelper(tempLess, tempMore); let result = range.contains(rangeSec); ``` ### clamp9+ clamp(value: ScopeType): ScopeType Limits a value to this **Scope**. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------------------------ | ---- | -------------- | | value | [ScopeType](#scopetype8) | Yes | Value specified.| **Return value** | Type | Description | | ------------------------ | ------------------------------------------------------------ | | [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**.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let tempMiDF = new Temperature(35); let range = new util.ScopeHelper(tempLower, tempUpper); let result = range.clamp(tempMiDF); ``` ## Base64Helper9+ ### constructor9+ constructor() A constructor used to create a **Base64Helper** instance. **System capability**: SystemCapability.Utils.Lang **Example** ```js let base64 = new util.Base64Helper(); ``` ### encodeSync9+ encodeSync(src: Uint8Array): Uint8Array Encodes the input content. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ---------- | ---- | ------------------- | | src | Uint8Array | Yes | Uint8Array to encode.| **Return value** | Type | Description | | ---------- | ----------------------------- | | Uint8Array | Uint8Array encoded.| **Example** ```js let that = new util.Base64Helper(); let array = new Uint8Array([115,49,51]); let result = that.encodeSync(array); ``` ### encodeToStringSync9+ encodeToStringSync(src: Uint8Array): string Encodes the input content. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ---------- | ---- | ------------------- | | src | Uint8Array | Yes | Uint8Array to encode.| **Return value** | Type | Description | | ------ | -------------------- | | string | String encoded from the Uint8Array.| **Example** ```js let that = new util.Base64Helper(); let array = new Uint8Array([115,49,51]); let result = that.encodeToStringSync(array); ``` ### decodeSync9+ decodeSync(src: Uint8Array | string): Uint8Array Decodes the input content. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------------------------------ | ---- | ----------------------------- | | src | Uint8Array \| string | Yes | Uint8Array or string to decode.| **Return value** | Type | Description | | ---------- | ----------------------------- | | Uint8Array | Uint8Array decoded.| **Example** ```js let that = new util.Base64Helper(); let buff = 'czEz'; let result = that.decodeSync(buff); ``` ### encode9+ encode(src: Uint8Array): Promise<Uint8Array> Encodes the input content asynchronously. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ---------- | ---- | ----------------------- | | src | Uint8Array | Yes | Uint8Array to encode asynchronously.| **Return value** | Type | Description | | ------------------------- | --------------------------------- | | Promise<Uint8Array> | Uint8Array obtained after asynchronous encoding.| **Example** ```js let that = new util.Base64Helper(); let array = new Uint8Array([115,49,51]); let rarray = new Uint8Array([99,122,69,122]); that.encode(array).then(val=>{ for (var i = 0; i < rarray.length; i++) { console.log(val[i].toString()) } }) ``` ### encodeToString9+ encodeToString(src: Uint8Array): Promise<string> Encodes the input content asynchronously. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ---------- | ---- | ----------------------- | | src | Uint8Array | Yes | Uint8Array to encode asynchronously.| **Return value** | Type | Description | | --------------------- | ------------------------ | | Promise<string> | String obtained after asynchronous encoding.| **Example** ```js let that = new util.Base64Helper(); let array = new Uint8Array([115,49,51]); that.encodeToString(array).then(val=>{ console.log(val) }) ``` ### decode9+ decode(src: Uint8Array | string): Promise<Uint8Array> Decodes the input content asynchronously. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type | Mandatory| Description | | ------ | ------------------------------ | ---- | --------------------------------- | | src | Uint8Array \| string | Yes | Uint8Array or string to decode asynchronously.| **Return value** | Type | Description | | ------------------------- | --------------------------------- | | Promise<Uint8Array> | Uint8Array obtained after asynchronous decoding.| **Example** ```js let that = new util.Base64Helper(); let array = new Uint8Array([99,122,69,122]); let rarray = new Uint8Array([115,49,51]); that.decode(array).then(val=>{ for (var i = 0; i < rarray.length; i++) { console.log(val[i].toString()) } }) ``` ## types8+ ### constructor8+ constructor() A constructor used to create a **Types** object. **System capability**: SystemCapability.Utils.Lang **Example** ```js let type = new util.types(); ``` ### isAnyArrayBuffer8+ isAnyArrayBuffer(value: Object): boolean Checks whether the input value is of the **ArrayBuffer** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **ArrayBuffer** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isAnyArrayBuffer(new ArrayBuffer(0)); ``` ### isArrayBufferView8+ isArrayBufferView(value: Object): boolean Checks whether the input value is of the **ArrayBufferView** type. **ArrayBufferView** is a helper type representing any of the following: **Int8Array**, **Int16Array**, **Int32Array**, **Uint8Array**, **Uint8ClampedArray**, **Uint32Array**, **Float32Array**, **Float64Array**, and **DataView**. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **ArrayBufferView** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isArrayBufferView(new Int8Array([])); ``` ### isArgumentsObject8+ isArgumentsObject(value: Object): boolean Checks whether the input value is of the **arguments** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **arguments** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); function foo() { var result = that.isArgumentsObject(arguments); } let f = foo(); ``` ### isArrayBuffer8+ isArrayBuffer(value: Object): boolean Checks whether the input value is of the **ArrayBuffer** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **ArrayBuffer** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isArrayBuffer(new ArrayBuffer(0)); ``` ### isAsyncFunction8+ isAsyncFunction(value: Object): boolean Checks whether the input value is an asynchronous function. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is an asynchronous function; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isAsyncFunction(async function foo() {}); ``` ### isBooleanObject8+ isBooleanObject(value: Object): boolean Checks whether the input value is of the **Boolean** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Boolean** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isBooleanObject(new Boolean(true)); ``` ### isBoxedPrimitive8+ isBoxedPrimitive(value: Object): boolean Checks whether the input value is of the **Boolean**, **Number**, **String**, or **Symbol** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Boolean**, **Number**, **String**, or **Symbol** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isBoxedPrimitive(new Boolean(false)); ``` ### isDataView8+ isDataView(value: Object): boolean Checks whether the input value is of the **DataView** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **DataView** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); const ab = new ArrayBuffer(20); let result = that.isDataView(new DataView(ab)); ``` ### isDate8+ isDate(value: Object): boolean Checks whether the input value is of the **Date** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Date** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isDate(new Date()); ``` ### isExternal8+ isExternal(value: Object): boolean Checks whether the input value is of the **native external** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **native external** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isExternal(true); ``` ### isFloat32Array8+ isFloat32Array(value: Object): boolean Checks whether the input value is of the **Float32Array** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Float32Array** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isFloat32Array(new Float32Array()); ``` ### isFloat64Array8+ isFloat64Array(value: Object): boolean Checks whether the input value is of the **Float64Array** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Float64Array** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isFloat64Array(new Float64Array()); ``` ### isGeneratorFunction8+ isGeneratorFunction(value: Object): boolean Checks whether the input value is a generator function. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is a generator function; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isGeneratorFunction(function* foo() {}); ``` ### isGeneratorObject8+ isGeneratorObject(value: Object): boolean Checks whether the input value is a generator object. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is a generator object; returns **false** otherwise.| **Example** ```js let that = new util.types(); function* foo() {} const generator = foo(); let result = that.isGeneratorObject(generator); ``` ### isInt8Array8+ isInt8Array(value: Object): boolean Checks whether the input value is of the **Int8Array** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Int8Array** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isInt8Array(new Int8Array([])); ``` ### isInt16Array8+ isInt16Array(value: Object): boolean Checks whether the input value is of the **Int16Array** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Int16Array** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isInt16Array(new Int16Array([])); ``` ### isInt32Array8+ isInt32Array(value: Object): boolean Checks whether the input value is of the **Int32Array** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Int32Array** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isInt32Array(new Int32Array([])); ``` ### isMap8+ isMap(value: Object): boolean Checks whether the input value is of the **Map** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Map** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isMap(new Map()); ``` ### isMapIterator8+ isMapIterator(value: Object): boolean Checks whether the input value is of the **MapIterator** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **MapIterator** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); const map = new Map(); let result = that.isMapIterator(map.keys()); ``` ### isNativeError8+ isNativeError(value: Object): boolean Checks whether the input value is of the **Error** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Error** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isNativeError(new TypeError()); ``` ### isNumberObject8+ isNumberObject(value: Object): boolean Checks whether the input value is a number object. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is a number object; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isNumberObject(new Number(0)); ``` ### isPromise8+ isPromise(value: Object): boolean Checks whether the input value is a promise. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is a promise; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isPromise(Promise.resolve(1)); ``` ### isProxy8+ isProxy(value: Object): boolean Checks whether the input value is a proxy. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is a proxy; returns **false** otherwise.| **Example** ```js let that = new util.types(); const target = {}; const proxy = new Proxy(target, {}); let result = that.isProxy(proxy); ``` ### isRegExp8+ isRegExp(value: Object): boolean Checks whether the input value is of the **RegExp** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **RegExp** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isRegExp(new RegExp('abc')); ``` ### isSet8+ isSet(value: Object): boolean Checks whether the input value is of the **Set** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Set** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isSet(new Set()); ``` ### isSetIterator8+ isSetIterator(value: Object): boolean Checks whether the input value is of the **SetIterator** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **SetIterator** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); const set = new Set(); let result = that.isSetIterator(set.keys()); ``` ### isStringObject8+ isStringObject(value: Object): boolean Checks whether the input value is a string object. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is a string object; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isStringObject(new String('foo')); ``` ### isSymbolObjec8+ isSymbolObject(value: Object): boolean Checks whether the input value is a symbol object. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is a symbol object; returns **false** otherwise.| **Example** ```js let that = new util.types(); const symbols = Symbol('foo'); let result = that.isSymbolObject(Object(symbols)); ``` ### isTypedArray8+ isTypedArray(value: Object): boolean Checks whether the input value is of the **TypedArray** type. **TypedArray** is a helper type representing any of the following: **Int8Array**, **Int16Array**, **Int32Array**, **Uint8Array**, **Uint8ClampedArray**, **Uint16Array**, **Uint32Array**, **Float32Array**, **Float64Array**, and **DataView**. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **TypedArray** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isTypedArray(new Float64Array([])); ``` ### isUint8Array8+ isUint8Array(value: Object): boolean Checks whether the input value is of the **Uint8Array** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Uint8Array** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isUint8Array(new Uint8Array([])); ``` ### isUint8ClampedArray8+ isUint8ClampedArray(value: Object): boolean Checks whether the input value is of the **Uint8ClampedArray** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Uint8ClampedArray** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isUint8ClampedArray(new Uint8ClampedArray([])); ``` ### isUint16Array8+ isUint16Array(value: Object): boolean Checks whether the input value is of the **Uint16Array** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Uint16Array** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isUint16Array(new Uint16Array([])); ``` ### isUint32Array8+ isUint32Array(value: Object): boolean Checks whether the input value is of the **Uint32Array** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **Uint32Array** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isUint32Array(new Uint32Array([])); ``` ### isWeakMap8+ isWeakMap(value: Object): boolean Checks whether the input value is of the **WeakMap** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **WeakMap** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isWeakMap(new WeakMap()); ``` ### isWeakSet8+ isWeakSet(value: Object): boolean Checks whether the input value is of the **WeakSet** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **WeakSet** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isWeakSet(new WeakSet()); ``` ### isBigInt64Array8+ isBigInt64Array(value: Object): boolean Checks whether the input value is of the **BigInt64Array** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **BigInt64Array** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isBigInt64Array(new BigInt64Array([])); ``` ### isBigUint64Array8+ isBigUint64Array(value: Object): boolean Checks whether the input value is of the **BigUint64Array** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **BigUint64Array** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isBigUint64Array(new BigUint64Array([])); ``` ### isModuleNamespaceObject8+ isModuleNamespaceObject(value: Object): boolean Checks whether the input value is a module namespace object. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is a module namespace object; returns **false** otherwise.| **Example** ```js import url from '@ohos.url' let that = new util.types(); let result = that.isModuleNamespaceObject(url); ``` ### isSharedArrayBuffer8+ isSharedArrayBuffer(value: Object): boolean Checks whether the input value is of the **SharedArrayBuffer** type. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | Object | Yes| Object to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the input value is of the **SharedArrayBuffer** type; returns **false** otherwise.| **Example** ```js let that = new util.types(); let result = that.isSharedArrayBuffer(new SharedArrayBuffer(0)); ``` ## LruBuffer(deprecated) > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache9+](#lrucache9) instead. ### Attributes **System capability**: SystemCapability.Utils.Lang | Name| Type| Readable| Writable| Description| | -------- | -------- | -------- | -------- | -------- | | length | number | Yes| No| Total number of values in this buffer.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); pro.put(1,8); let result = pro.length; ``` ### constructor(deprecated) constructor(capacity?: number) A constructor used to create a **LruBuffer** instance. The default capacity of the buffer is 64. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.constructor9+](#constructor9-3) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | capacity | number | No| Capacity of the **LruBuffer** to create.| **Example** ```js let lrubuffer= new util.LruBuffer(); ``` ### updateCapacity(deprecated) updateCapacity(newCapacity: number): void Changes the **LruBuffer** capacity. If the new capacity is less than or equal to **0**, an exception will be thrown. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.updateCapacity9+](#updatecapacity9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | newCapacity | number | Yes| New capacity of the **LruBuffer**.| **Example** ```js let pro = new util.LruBuffer(); let result = pro.updateCapacity(100); ``` ### toString(deprecated) toString(): string Obtains the string representation of this **LruBuffer** object. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.toString9+](#tostring9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | string | String representation of this **LruBuffer** object.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); pro.get(2); pro.remove(20); let result = pro.toString(); ``` ### getCapacity(deprecated) getCapacity(): number Obtains the capacity of this buffer. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.getCapacity9+](#getcapacity9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | number | Capacity of this buffer.| **Example** ```js let pro = new util.LruBuffer(); let result = pro.getCapacity(); ``` ### clear(deprecated) clear(): void Clears key-value pairs from this buffer. The **afterRemoval()** method will be called to perform subsequent operations. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.clear9+](#clear9) instead. **System capability**: SystemCapability.Utils.Lang **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); let result = pro.length; pro.clear(); ``` ### getCreateCount(deprecated) getCreateCount(): number Obtains the number of return values for **createDefault()**. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.getCreateCount9+](#getcreatecount9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | number | Number of return values for **createDefault()**.| **Example** ```js let pro = new util.LruBuffer(); pro.put(1,8); let result = pro.getCreateCount(); ``` ### getMissCount(deprecated) getMissCount(): number Obtains the number of times that the queried values are mismatched. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.getMissCount9+](#getmisscount9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | number | Number of times that the queried values are mismatched.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); pro.get(2); let result = pro.getMissCount(); ``` ### getRemovalCount(deprecated) getRemovalCount(): number Obtains the number of removals from this buffer. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.getRemovalCount9+](#getremovalcount9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | number | Number of removals from the buffer.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); pro.updateCapacity(2); pro.put(50,22); let result = pro.getRemovalCount(); ``` ### getMatchCount(deprecated) getMatchCount(): number Obtains the number of times that the queried values are matched. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.getMatchCount9+](#getmatchcount9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | number | Number of times that the queried values are matched.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); pro.get(2); let result = pro.getMatchCount(); ``` ### getPutCount(deprecated) getPutCount(): number Obtains the number of additions to this buffer. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.getPutCount9+](#getputcount9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | number | Number of additions to the buffer.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); let result = pro.getPutCount(); ``` ### isEmpty(deprecated) isEmpty(): boolean Checks whether this buffer is empty. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.isEmpty9+](#isempty9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the buffer does not contain any value.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); let result = pro.isEmpty(); ``` ### get(deprecated) get(key: K): V | undefined Obtains the value of the specified key. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.get9+](#get9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | key | K | Yes| Key based on which the value is queried.| **Return value** | Type| Description| | -------- | -------- | | V \| undefined | Returns the value of the key if a match is found in the buffer; returns **undefined** otherwise.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); let result = pro.get(2); ``` ### put(deprecated) put(key: K,value: V): V Adds a key-value pair to this buffer. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.put9+](#put9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | key | K | Yes| Key of the key-value pair to add.| | value | V | Yes| Value of the key-value pair to add.| **Return value** | Type| Description| | -------- | -------- | | V | Returns the existing value if the key already exists; returns the value added otherwise. If the key or value is null, an exception will be thrown. | **Example** ```js let pro = new util.LruBuffer(); let result = pro.put(2,10); ``` ### values(deprecated) values(): V[] Obtains all values in this buffer, listed from the most to the least recently accessed. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.values9+](#values9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | V [] | All values in the buffer, listed from the most to the least recently accessed.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); pro.put(2,"anhu"); pro.put("afaf","grfb"); let result = pro.values(); ``` ### keys(deprecated) keys(): K[] Obtains all keys in this buffer, listed from the most to the least recently accessed. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.keys9+](#keys9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | K [] | All keys in the buffer, listed from the most to the least recently accessed.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); let result = pro.keys(); ``` ### remove(deprecated) remove(key: K): V | undefined Removes the specified key and its value from this buffer. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.remove9+](#remove9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | key | K | Yes| Key to remove.| **Return value** | Type| Description| | -------- | -------- | | V \| undefined | Returns an **Optional** object containing the removed key-value pair if the key exists in the buffer; returns an empty **Optional** object otherwise. If the key is null, an exception will be thrown.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); let result = pro.remove(20); ``` ### afterRemoval(deprecated) afterRemoval(isEvict: boolean,key: K,value: V,newValue: V): void Performs subsequent operations after a value is removed. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.afterRemoval9+](#afterremoval9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | isEvict | boolean | Yes| Whether the buffer capacity is insufficient. If the value is **true**, this API is called due to insufficient capacity.| | key | K | Yes| Key removed.| | value | V | Yes| Value removed.| | 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.| **Example** ```js let arr = []; class ChildLruBuffer extends util.LruBuffer { constructor() { super(); } afterRemoval(isEvict, key, value, newValue) { if (isEvict === false) { arr = [key, value, newValue]; } } } let lru = new ChildLruBuffer(); lru.afterRemoval(false,10,30,null); ``` ### contains(deprecated) contains(key: K): boolean Checks whether this buffer contains the specified key. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.contains9+](#contains9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | key | K | Yes| Key to check.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the buffer contains the specified key; returns **false** otherwise.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); let result = pro.contains(20); ``` ### createDefault(deprecated) createDefault(key: K): V Creates a value if the value of the specified key is not available. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.createDefault9+](#createdefault9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | key | K | Yes| Key of which the value is missing.| **Return value** | Type| Description| | -------- | -------- | | V | Value of the key.| **Example** ```js let pro = new util.LruBuffer(); let result = pro.createDefault(50); ``` ### entries(deprecated) entries(): IterableIterator<[K,V]> Obtains a new iterator object that contains all key-value pairs in this object. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.entries9+](#entries9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | [K, V] | Iterable array.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); let result = pro.entries(); ``` ### [Symbol.iterator](deprecated) [Symbol.iterator]\(): IterableIterator<[K, V]> Obtains a two-dimensional array in key-value pairs. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [LRUCache.Symbol.iterator9+](#symboliterator9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | [K, V] | Two-dimensional array in key-value pairs.| **Example** ```js let pro = new util.LruBuffer(); pro.put(2,10); let result = pro[Symbol.iterator](); ``` ## Scope(deprecated) > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper9+](#scopehelper9) instead. ### constructor(deprecated) constructor(lowerObj: ScopeType, upperObj: ScopeType) A constructor used to create a **Scope** object with the specified upper and lower limits. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper.constructor9+](#constructor9-4) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | lowerObj | [ScopeType](#scopetype8) | Yes| Lower limit of the **Scope** object.| | upperObj | [ScopeType](#scopetype8) | Yes| Upper limit of the **Scope** object.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let range = new util.Scope(tempLower, tempUpper); ``` ### toString(deprecated) toString(): string Obtains a string representation that contains this **Scope**. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper.toString9+](#tostring9-1) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | string | String representation containing the **Scope**.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let range = new util.Scope(tempLower, tempUpper); let result = range.toString(); ``` ### intersect(deprecated) intersect(range: Scope): Scope Obtains the intersection of this **Scope** and the given **Scope**. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper.intersect9+](#intersect9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | range | [Scope](#scopedeprecated) | Yes| **Scope** specified.| **Return value** | Type| Description| | -------- | -------- | | [Scope](#scopedeprecated) | Intersection of this **Scope** and the given **Scope**.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let range = new util.Scope(tempLower, tempUpper); let tempMiDF = new Temperature(35); let tempMidS = new Temperature(39); let rangeFir = new util.Scope(tempMiDF, tempMidS); range.intersect(rangeFir ); ``` ### intersect(deprecated) intersect(lowerObj:ScopeType,upperObj:ScopeType):Scope Obtains the intersection of this **Scope** and the given lower and upper limits. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper.intersect9+](#intersect9-1) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | lowerObj | [ScopeType](#scopetype8) | Yes| Lower limit.| | upperObj | [ScopeType](#scopetype8) | Yes| Upper limit.| **Return value** | Type| Description| | -------- | -------- | | [Scope](#scopedeprecated) | Intersection of this **Scope** and the given lower and upper limits.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let tempMiDF = new Temperature(35); let tempMidS = new Temperature(39); let range = new util.Scope(tempLower, tempUpper); let result = range.intersect(tempMiDF, tempMidS); ``` ### getUpper(deprecated) getUpper(): ScopeType Obtains the upper limit of this **Scope**. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper.getUpper9+](#getupper9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | [ScopeType](#scopetype8) | Upper limit of this **Scope**.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let range = new util.Scope(tempLower, tempUpper); let result = range.getUpper(); ``` ### getLower(deprecated) getLower(): ScopeType Obtains the lower limit of this **Scope**. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper.getLower9+](#getlower9) instead. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | [ScopeType](#scopetype8) | Lower limit of this **Scope**.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let range = new util.Scope(tempLower, tempUpper); let result = range.getLower(); ``` ### expand(deprecated) expand(lowerObj: ScopeType,upperObj: ScopeType): Scope Obtains the union set of this **Scope** and the given lower and upper limits. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper.expand9+](#expand9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | lowerObj | [ScopeType](#scopetype8) | Yes| Lower limit.| | upperObj | [ScopeType](#scopetype8) | Yes| Upper limit.| **Return value** | Type| Description| | -------- | -------- | | [Scope](#scopedeprecated) | Union set of this **Scope** and the given lower and upper limits.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let tempMiDF = new Temperature(35); let tempMidS = new Temperature(39); let range = new util.Scope(tempLower, tempUpper); let result = range.expand(tempMiDF, tempMidS); ``` ### expand(deprecated) expand(range: Scope): Scope Obtains the union set of this **Scope** and the given **Scope**. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper.expand9+](#expand9-1) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | range | [Scope](#scopedeprecated) | Yes| **Scope** specified.| **Return value** | Type| Description| | -------- | -------- | | [Scope](#scopedeprecated) | Union set of this **Scope** and the given **Scope**.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let tempMiDF = new Temperature(35); let tempMidS = new Temperature(39); let range = new util.Scope(tempLower, tempUpper); let rangeFir = new util.Scope(tempMiDF, tempMidS); let result = range.expand(rangeFir); ``` ### expand(deprecated) expand(value: ScopeType): Scope Obtains the union set of this **Scope** and the given value. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper.expand9+](#expand9-2) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | [ScopeType](#scopetype8) | Yes| Value specified.| **Return value** | Type| Description| | -------- | -------- | | [Scope](#scopedeprecated) | Union set of this **Scope** and the given value.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let tempMiDF = new Temperature(35); let range = new util.Scope(tempLower, tempUpper); let result = range.expand(tempMiDF); ``` ### contains(deprecated) contains(value: ScopeType): boolean Checks whether a value is within this **Scope**. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper.contains9+](#contains9-1) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | [ScopeType](#scopetype8) | Yes| Value specified.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the value is within this **Scope**; returns **false** otherwise.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let tempMiDF = new Temperature(35); let range = new util.Scope(tempLower, tempUpper); range.contains(tempMiDF); ``` ### contains(deprecated) contains(range: Scope): boolean Checks whether a range is within this **Scope**. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper.contains9+](#contains9-2) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | range | [Scope](#scopedeprecated) | Yes| **Scope** specified.| **Return value** | Type| Description| | -------- | -------- | | boolean | Returns **true** if the range is within this **Scope**; returns **false** otherwise.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let range = new util.Scope(tempLower, tempUpper); let tempLess = new Temperature(20); let tempMore = new Temperature(45); let rangeSec = new util.Scope(tempLess, tempMore); let result = range.contains(rangeSec); ``` ### clamp(deprecated) clamp(value: ScopeType): ScopeType Limits a value to this **Scope**. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [ScopeHelper.clamp9+](#clamp9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | [ScopeType](#scopetype8) | Yes| Value specified.| **Return value** | Type| Description| | -------- | -------- | | [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**.| **Example** ```js let tempLower = new Temperature(30); let tempUpper = new Temperature(40); let tempMiDF = new Temperature(35); let range = new util.Scope(tempLower, tempUpper); let result = range.clamp(tempMiDF); ``` ## Base64(deprecated) > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [Base64Helper9+](#base64helper9) instead. ### constructor(deprecated) constructor() A constructor used to create a **Base64** object. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [Base64Helper.constructor9+](#constructor9-5) instead. **System capability**: SystemCapability.Utils.Lang **Example** ```js let base64 = new util.Base64(); ``` ### encodeSync(deprecated) encodeSync(src: Uint8Array): Uint8Array Encodes the input content. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [Base64Helper.encodeSync9+](#encodesync9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | src | Uint8Array | Yes| Uint8Array to encode.| **Return value** | Type| Description| | -------- | -------- | | Uint8Array | Uint8Array encoded.| **Example** ```js let that = new util.Base64(); let array = new Uint8Array([115,49,51]); let result = that.encodeSync(array); ``` ### encodeToStringSync(deprecated) encodeToStringSync(src: Uint8Array): string Encodes the input content. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [Base64Helper.encodeToStringSync9+](#encodetostringsync9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | src | Uint8Array | Yes| Uint8Array to encode.| **Return value** | Type| Description| | -------- | -------- | | string | String encoded from the Uint8Array.| **Example** ```js let that = new util.Base64(); let array = new Uint8Array([115,49,51]); let result = that.encodeToStringSync(array); ``` ### decodeSync(deprecated) decodeSync(src: Uint8Array | string): Uint8Array Decodes the input content. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [Base64Helper.decodeSync9+](#decodesync9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | src | Uint8Array \| string | Yes| Uint8Array or string to decode.| **Return value** | Type| Description| | -------- | -------- | | Uint8Array | Uint8Array decoded.| **Example** ```js let that = new util.Base64(); let buff = 'czEz'; let result = that.decodeSync(buff); ``` ### encode(deprecated) encode(src: Uint8Array): Promise<Uint8Array> Encodes the input content asynchronously. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [Base64Helper.encode9+](#encode9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | src | Uint8Array | Yes| Uint8Array to encode asynchronously.| **Return value** | Type| Description| | -------- | -------- | | Promise<Uint8Array> | Uint8Array obtained after asynchronous encoding.| **Example** ```js let that = new util.Base64(); let array = new Uint8Array([115,49,51]); let rarray = new Uint8Array([99,122,69,122]); that.encode(array).then(val=>{ for (var i = 0; i < rarray.length; i++) { console.log(val[i].toString()) } }) ``` ### encodeToString(deprecated) encodeToString(src: Uint8Array): Promise<string> Encodes the input content asynchronously. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [Base64Helper.encodeToString9+](#encodetostring9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | src | Uint8Array | Yes| Uint8Array to encode asynchronously.| **Return value** | Type| Description| | -------- | -------- | | Promise<string> | String obtained after asynchronous encoding.| **Example** ```js let that = new util.Base64(); let array = new Uint8Array([115,49,51]); that.encodeToString(array).then(val=>{ console.log(val) }) ``` ### decode(deprecated) decode(src: Uint8Array | string): Promise<Uint8Array> Decodes the input content asynchronously. > **NOTE** > > This API is supported since API version 8 and deprecated since API version 9. You are advised to use [Base64Helper.decode9+](#decode9) instead. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | src | Uint8Array \| string | Yes| Uint8Array or string to decode asynchronously.| **Return value** | Type| Description| | -------- | -------- | | Promise<Uint8Array> | Uint8Array obtained after asynchronous decoding.| **Example** ```js let that = new util.Base64(); let array = new Uint8Array([99,122,69,122]); let rarray = new Uint8Array([115,49,51]); that.decode(array).then(val=>{ for (var i = 0; i < rarray.length; i++) { console.log(val[i].toString()) } }) ```