# util > **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. This 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. ## Modules to Import ``` import util from '@ohos.util'; ``` ## util.printf printf(format: string, ...args: Object[]): string Prints the input content in a formatted string. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | format | string | Yes | Format of the string to print. | | ...args | Object[] | No | Data to format. | - Return values | Type | Description | | -------- | -------- | | string | String in the specified format. | - Example ``` var res = util.printf("%s", "hello world!"); console.log(res); ``` ## util.getErrorString getErrorString(errno: number): string Obtains detailed information about a system error code. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | errno | number | Yes | Error code generated. | - Return values | Type | Description | | -------- | -------- | | string | Detailed information about the error code. | - Example ``` var errnum = 10; // 10 is the system error code. var result = util.getErrorString(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. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | original | Function | Yes | Asynchronous function to process. | - Return values | 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 ``` async function promiseFn() { return Promise.reject('value'); } var cb = util.callbackWrapper(promiseFn); cb((err, ret) => { console.log(err); console.log(ret); }) ``` ## util.promiseWrapper promiseWrapper(original: (err: Object, value: Object) => void): Object Processes an asynchronous function and returns a promise version. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | original | Function | Yes | Asynchronous function to process. | - Return values | Type | Description | | -------- | -------- | | Function | Function in the error-first style (that is, **(err, value) =>...** is called as the last parameter) and the promise version. | - Example ``` function aysnFun(str1, str2, callback) { if (typeof str1 === 'string' && typeof str2 === 'string') { callback(null, str1 + str2); } else { callback('type err'); } } let newPromiseObj = util.promiseWrapper(aysnFun)("Hello", 'World'); newPromiseObj.then(res => { console.log(res); }) ``` ## TextDecoder ### Attributes | 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. | ### constructor constructor(encoding?:string, options?:{ fatal?:boolean;ignoreBOM?:boolean }) A constructor used to create a **TextDecoder** object. - 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 ``` var textDecoder = new util.TextDecoder("utf-8",{ignoreBOM:true}); ``` ### decode decode(input: Unit8Array, options?:{stream?:false}):string Decodes the input content. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | input | Unit8Array | 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 values | Type | Description | | -------- | -------- | | string | Data decoded. | - Example ``` var textDecoder = new util.TextDecoder("utf-8",{ignoreBOM:true}); var 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:"); for(var j= 0; j < 6; j++) { console.log(result[j]); } var retStr = textDecoder.decode( result , {stream:false}); console.log("retStr = " + retStr); ``` ## TextEncoder ### Attributes | 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. - Example ``` var textEncoder = new util.TextEncoder(); ``` ### encode encode(input?:string):Uint8Array Encodes the input content. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | input | string | Yes | String to encode. | - Return values | Type | Description | | -------- | -------- | | Uint8Array | Encoded text. | - Example ``` var textEncoder = new util.TextEncoder(); var result = new Uint8Array(buffer); result = textEncoder.encode("\uD800¥¥"); ``` ### encodeInto encodeInto(input:string, dest:Uint8Array, ):{ read:number; written:number } Stores the UTF-8 encoded text. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | input | string | Yes | String to encode. | | dest | Uint8Array | Yes | Uint8Array object used to hold the UTF-8 encoded text. | - Return values | Type | Description | | -------- | -------- | | Uint8Array | Encoded text. | - Example ``` var that = new util.TextEncoder(); var buffer = new ArrayBuffer(4); this.dest = new Uint8Array(buffer); var result = that.encodeInto("abcd", this.dest); ``` ## RationalNumber8+ ### constructor8+ constructor(numerator:number,denominator:number) A constructor used to create a **RationalNumber** object. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | numerator | number | Yes | Numerator, which is an integer. | | denominator | number | Yes | Denominator, which is an integer. | - Example ``` var rationalNumber = new util.RationalNumber(1,2); ``` ### createRationalFromString8+ static createRationalFromString​(rationalString:string):RationalNumber​ Creates a **RationalNumber** object using the given string. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | rationalString | string | Yes | String used to create the **RationalNumber** object. | - Return values | Type | Description | | -------- | -------- | | object | **RationalNumber** object created. | - Example ``` var rationalNumber = new util.RationalNumber(1,2); var rational = rationalNumer.creatRationalFromString("3/4"); ``` ### compareTo8+ compareTo​(another:RationalNumber):number​ Compares this **RationalNumber** object with a given object. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | another | RationalNumber | Yes | Object used to compare with this **RationalNumber** object. | - Return values | 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 ``` var rationalNumber = new util.RationalNumber(1,2); var rational = rationalNumer.creatRationalFromString("3/4"); var result = rationalNumber.compareTo(rational); ``` ### valueOf8+ valueOf():number Obtains the value of this **RationalNumber** object as an integer or a floating-point number. - Return values | Type | Description | | -------- | -------- | | number | An integer or a floating-point number. | - Example ``` var rationalNumber = new util.RationalNumber(1,2); var result = rationalNumber.valueOf(); ``` ### equals8+ equals​(obj:Object):boolean Checks whether this **RationalNumber** object equals the given object. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | object | Object | Yes | Object used to compare with this **RationalNumber** object. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the two objects are equal; returns **false** otherwise. | - Example ``` var rationalNumber = new util.RationalNumber(1,2); var rational = rationalNumer.creatRationalFromString("3/4"); var result = rationalNumber.equals(rational); ``` ### getCommonDivisor8+ static getCommonDivisor​(number1:number,number2:number):number Obtains the greatest common divisor of the two specified integers. - 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 values | Type | Description | | -------- | -------- | | number | Greatest common divisor obtained. | - Example ``` var rationalNumber = new util.RationalNumber(1,2); var result = rationalNumber.getCommonDivisor(4,6); ``` ### getNumerator8+ getNumerator​():number Obtains the numerator of this **RationalNumber** object. - Return values | Type | Description | | -------- | -------- | | number | Numerator of this **RationalNumber** object. | - Example ``` var rationalNumber = new util.RationalNumber(1,2); var result = rationalNumber.getNumerator(); ``` ### getDenominator8+ getDenominator​():number Obtains the denominator of this **RationalNumber** object. - Return values | Type | Description | | -------- | -------- | | number | Denominator of this **RationalNumber** object. | - Example ``` var rationalNumber = new util.RationalNumber(1,2); var result = rationalNumber.getDenominator(); ``` ### isZero8+ isZero​():boolean Checks whether this **RationalNumber** object is **0**. - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the value of this **RationalNumber** object is **0**; returns **false** otherwise. | - Example ``` var rationalNumber = new util.RationalNumber(1,2); var result = rationalNumber.isZero(); ``` ### isNaN8+ isNaN​():boolean Checks whether this **RationalNumber** object is a Not a Number (NaN). - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if this **RationalNumber** object is a NaN (the denominator and numerator are both **0**); returns **false** otherwise. | - Example ``` var rationalNumber = new util.RationalNumber(1,2); var result = rationalNumber.isNaN(); ``` ### isFinite8+ isFinite​():boolean Checks whether this **RationalNumber** object represents a finite value. - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if this **RationalNumber** object represents a finite value (the denominator is not **0**); returns **false** otherwise. | - Example ``` var rationalNumber = new util.RationalNumber(1,2); var result = rationalNumber.isFinite(); ``` ### toString8+ toString​():string Obtains the string representation of this **RationalNumber** object. - Return values | 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 ``` var rationalNumber = new util.RationalNumber(1,2); var result = rationalNumber.toString(); ``` ## LruBuffer8+ ### Attributes | Name | Type | Readable | Writable | Description | | -------- | -------- | -------- | -------- | -------- | | length | number | Yes | No | Total number of values in this buffer. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); pro.put(1,8); var result = pro.length; ``` ### constructor8+ constructor(capacity?:number) A constructor used to create an **LruBuffer** instance. The default capacity of the buffer is 64. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | capacity | number | No | Capacity of the **LruBuffer** to create. | - Example ``` var lrubuffer= new util.LruBuffer(); ``` ### updateCapacity8+ updateCapacity(newCapacity:number):void Changes the **LruBuffer** capacity. If the new capacity is less than or equal to **0**, an exception will be thrown. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | newCapacity | number | Yes | New capacity of the buffer. | - Example ``` var pro = new util.LruBuffer(); var result = pro.updateCapacity(100); ``` ### toString8+ toString():string Obtains the string representation of this **LruBuffer** object. - Return values | Type | Description | | -------- | -------- | | string | String representation of this object. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); pro.get(2); pro.remove(20); var result = pro.toString(); ``` ### getCapacity8+ getCapacity():number Obtains the capacity of this buffer. - Return values | Type | Description | | -------- | -------- | | number | Capacity of this buffer. | - Example ``` var pro = new util.LruBuffer(); var result = pro.getCapacity(); ``` ### clear8+ clear():void Clears key-value pairs from this buffer. The **afterRemoval()** method will be called to perform subsequent operations. - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); var result = pro.size(); pro.clear(); ``` ### getCreateCount8+ getCreateCount():number Obtains the number of return values for **createDefault()**. - Return values | Type | Description | | -------- | -------- | | number | Number of return values for **createDefault()**. | - Example ``` var pro = new util.LruBuffer(); pro.put(1,8); var result = pro.getCreateCount(); ``` ### getMissCount8+ getMissCount():number Obtains the number of times that the queried values are mismatched. - Return values | Type | Description | | -------- | -------- | | number | Number of times that the queried values are mismatched. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); pro.get(2); var result = pro.getMissCount(); ``` ### getRemovalCount8+ getRemovalCount():number Obtains the number of removals from this buffer. - Return values | Type | Description | | -------- | -------- | | number | Number of removals from the buffer. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); pro.updateCapacity(2); pro.put(50,22); var result = pro.getRemovalCount(); ``` ### getMatchCount8+ getMatchCount():number Obtains the number of times that the queried values are matched. - Return values | Type | Description | | -------- | -------- | | number | Number of times that the queried values are matched. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); pro.get(2); var result = pro.getMatchCount(); ``` ### getPutCount8+ getPutCount():number Obtains the number of additions to this buffer. - Return values | Type | Description | | -------- | -------- | | number | Number of additions to the buffer. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); var result = pro.getPutCount(); ``` ### isEmpty8+ isEmpty():boolean Checks whether this buffer is empty. - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the buffer does not contain any value. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); var result = pro.isEmpty(); ``` ### get8+ get(key:K):V | undefined Obtains the value of the specified key. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | key | K | Yes | Key based on which the value is queried. | - Return values | Type | Description | | -------- | -------- | | V \| undefind | Returns the value of the key if a match is found in the buffer; returns **undefined** otherwise. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); var result = pro.get(2); ``` ### put8+ put(key:K,value:V):V Adds a key-value pair to this buffer. - 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 values | 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 ``` var pro = new util.LruBuffer(); var result = pro.put(2,10); ``` ### values8+ values():V[] Obtains all values in this buffer, listed from the most to the least recently accessed. - Return values | Type | Description | | -------- | -------- | | V [] | All values in the buffer, listed from the most to the least recently accessed. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); pro.put(2,"anhu"); pro.put("afaf","grfb"); var result = pro.values(); ``` ### keys8+ keys():K[] Obtains all keys in this buffer, listed from the most to the least recently accessed. - Return values | Type | Description | | -------- | -------- | | K [] | All keys in the buffer, listed from the most to the least recently accessed. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); var result = pro.keys(); ``` ### remove8+ remove(key:K):V | undefined Deletes the specified key and its value from this buffer. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | key | K | Yes | Key to delete. | - Return values | Type | Description | | -------- | -------- | | V \| undefind | Returns an **Optional** object containing the deleted 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 ``` var pro = new util.LruBuffer(); pro.put(2,10); var result = pro.remove(20); ``` ### afterRemoval8+ afterRemoval(isEvict:boolean,key:K,value:V,newValue:V):void Performs subsequent operations after a value is deleted. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | isEvict | boolean | No | Whether the buffer capacity is insufficient. If the value is **true**, this method is called due to insufficient capacity. | | key | K | Yes | Key deleted. | | value | V | Yes | Value deleted. | | newValue | V | No | 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 ``` var arr = []; class ChildLruBuffer extends util.LruBuffer { constructor() { super(); } static getInstance() { if(this.instance == null) { this.instance = new ChildLruBuffer(); } return this.instance; } afterRemoval(isEvict, key, value, newValue) { if (isEvict === false) { arr = [key, value, newValue]; } } } ChildLruBuffer.getInstance().afterRemoval(false,10,30,null); ``` ### contains8+ contains(key:K):boolean Checks whether this buffer contains the specified key. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | key | K | Yes | Key to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the buffer contains the specified key; returns **false** otherwise. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); var result = pro.contains(20); ``` ### createDefault8+ createDefault(key:K):V Creates a value if the value of the specified key is not available. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | key | K | Yes | Key of which the value is missing. | - Return values | Type | Description | | -------- | -------- | | V | Value of the key. | - Example ``` var pro = new util.LruBuffer(); var result = pro.createDefault(50); ``` ### entries8+ entries():IterableIterator<[K,V]> Obtains a new iterator object that contains all key-value pairs in this object. - Return values | Type | Description | | -------- | -------- | | [K, V] | Returns an iterable array. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); var result = pro.entries(); ``` ### [Symbol.iterator]8+ [Symbol.iterator](): IterableIterator<[K, V]> Obtains a two-dimensional array in key-value pairs. - Return values | Type | Description | | -------- | -------- | | [K, V] | A two-dimensional array in key-value pairs. | - Example ``` var pro = new util.LruBuffer(); pro.put(2,10); var result = pro[symbol.iterator](); ``` ## Scope8+ ### ScopeType8+ Defines the type of values in a **Scope** object. The value type can be **ScopeComparable** or **number**. The values of the **ScopeComparable** type are used to implement the **compareTo** method. Therefore, ensure that the input parameters are comparable. ``` interface ScopeComparable{ compareTo(other:ScopeComparable):boolean; } type ScopeType = ScopeComparable | number; ``` Create a class to implement the **compareTo** method. In the subsequent sample code, **Temperature** is used as an example of the [ScopeType](#scopetype8) object. Example ``` class Temperature{ constructor(value){ this._temp = value; } comapreTo(value){ return this._temp >= value.getTemp(); } getTemp(){ return this._temp; } toString(){ return this._temp.toString(); } } ``` ### constructor8+ constructor(lowerObj:ScopeType,upperObje:ScopeType) A constructor used to create a **Scope** object with the specified upper and lower limits. - 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 ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new util.Scope(tempLower, tempUpper); ``` ### toString8+ toString():string Obtains a string representation that contains this **Scope**. - Return values | Type | Description | | -------- | -------- | | string | String representation containing the **Scope**. | - Example ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new util.Scope(tempLower, tempUpper); var result = range.toString(); ``` ### intersect8+ intersect(range:Scope):Scope Obtains the intersection of this **Scope** and the given **Scope**. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | range | [Scope](#scope8) | Yes | **Scope** specified. | - Return values | Type | Description | | -------- | -------- | | [Scope](#scope8) | Intersection of this **Scope** and the given **Scope**. | - Example ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new util.Scope(tempLower, tempUpper); var tempMiDF = new Temperature(35); var tempMidS = new Temperature(39); var rangeFir = new util.Scope(tempMiDF, tempMidS); range.intersect(rangeFir ); ``` ### intersect8+ intersect(lowerObj:ScopeType,upperObj:ScopeType):Scope Obtains the intersection of this **Scope** and the given lower and upper limits. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | lowerObj | [ScopeType](#scopetype8) | Yes | Lower limit. | | upperObj | [ScopeType](#scopetype8) | Yes | Upper limit. | - Return values | Type | Description | | -------- | -------- | | [Scope](#scope8) | Intersection of this **Scope** and the given lower and upper limits. | - Example ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var tempMiDF = new Temperature(35); var tempMidS = new Temperature(39); var range = new util.Scope(tempLower, tempUpper); var result = range.intersect(tempMiDF, tempMidS); ``` ### getUpper8+ getUpper():ScopeType Obtains the upper limit of this **Scope**. - Return values | Type | Description | | -------- | -------- | | [ScopeType](#scopetype8) | Upper limit of this **Scope**. | - Example ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new util.Scope(tempLower, tempUpper); var result = range.getUpper(); ``` ### getLower8+ getLower():ScopeType Obtains the lower limit of this **Scope**. - Return values | Type | Description | | -------- | -------- | | [ScopeType](#scopetype8) | Lower limit of this **Scope**. | - Example ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new util.Scope(tempLower, tempUpper); var result = range.getLower(); ``` ### expand8+ expand(lowerObj:ScopeType,upperObj:ScopeType):Scope Obtains the union set of this **Scope** and the given lower and upper limits. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | lowerObj | [ScopeType](#scopetype8) | Yes | Lower limit. | | upperObj | [ScopeType](#scopetype8) | Yes | Upper limit. | - Return values | Type | Description | | -------- | -------- | | [Scope](#scope8) | Union set of this **Scope** and the given lower and upper limits. | - Example ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var tempMiDF = new Temperature(35); var tempMidS = new Temperature(39); var range = new util.Scope(tempLower, tempUpper); var result = range.expand(tempMiDF, tempMidS); ``` ### expand8+ expand(range:Scope):Scope Obtains the union set of this **Scope** and the given **Scope**. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | range | [Scope](#scope8) | Yes | **Scope** specified. | - Return values | Type | Description | | -------- | -------- | | [Scope](#scope8) | Union set of this **Scope** and the given **Scope**. | - Example ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var tempMiDF = new Temperature(35); var tempMidS = new Temperature(39); var range = new util.Scope(tempLower, tempUpper); var rangeFir = new util.Scope(tempMiDF, tempMidS); var result = range.expand(rangeFir); ``` ### expand8+ expand(value:ScopeType):Scope Obtains the union set of this **Scope** and the given value. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | [ScopeType](#scopetype8) | Yes | Value specified. | - Return values | Type | Description | | -------- | -------- | | [Scope](#scope8) | Union set of this **Scope** and the given value. | - Example ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var tempMiDF = new Temperature(35); var range = new util.Scope(tempLower, tempUpper); var result = range.expand(tempMiDF); ``` ### contains8+ contains(value:ScopeType):boolean Checks whether a value is within this **Scope**. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | [ScopeType](#scopetype8) | Yes | Value specified. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the value is within this **Scope**; returns **false** otherwise. | - Example ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var tempMiDF = new Temperature(35); var range = new util.Scope(tempLower, tempUpper); range.contains(tempMiDF); ``` ### contains8+ contains(range:Scope):boolean Checks whether a range is within this **Scope**. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | range | [Scope](#scope8) | Yes | Range specified. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the range is within this **Scope**; returns **false** otherwise. | - Example ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var range = new util.Scope(tempLower, tempUpper); var tempLess = new Temperature(20); var tempMore = new Temperature(45); var rangeSec = new util.Scope(tempLess, tempMore); var result = range.contains(rangeSec); ``` ### clamp8+ clamp(value:ScopeType):ScopeType Limits a value to this **Scope**. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | [ScopeType](#scopetype8) | Yes | Value specified. | - Return values | 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 ``` var tempLower = new Temperature(30); var tempUpper = new Temperature(40); var tempMiDF = new Temperature(35); var range = new util.Scope(tempLower, tempUpper); var result = range.clamp(tempMiDF); ``` ## Base648+ ### constructor8+ constructor() A constructor used to create a **Base64** object. - Example ``` var base64 = new util.Base64(); ``` ### encodeSync8+ encodeSync(src:Uint8Array):Uint8Array Encodes the input content. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | src | Uint8Array | Yes | Uint8Array to encode. | - Return values | Type | Description | | -------- | -------- | | Uint8Array | Uint8Array encoded. | - Example ``` var that = new util.Base64(); var array = new Uint8Array([115,49,51]); var result = that.encodeSync(array); ``` ### encodeToStringSync8+ encodeToStringSync(src:Uint8Array):string Encodes the input content into a string. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | src | Uint8Array | Yes | Uint8Array to encode. | - Return values | Type | Description | | -------- | -------- | | string | String encoded from the Uint8Array. | - Example ``` var that = new util.Base64(); var array = new Uint8Array([115,49,51]); var result = that.encodeToStringSync(array); ``` ### decodeSync8+ decodeSync(src:Uint8Array | string):Uint8Array Decodes the input content. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | src | Uint8Array \| string | Yes | Uint8Array or string to decode. | - Return values | Type | Description | | -------- | -------- | | Uint8Array | Uint8Array decoded. | - Example ``` var that = new util.Base64(); var buff = 'czEz'; var result = that.decodeSync(buff); ``` ### encode8+ encode(src:Uint8Array):Promise<Uint8Array> Encodes the input content asynchronously. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | src | Uint8Array | Yes | Uint8Array to encode asynchronously. | - Return values | Type | Description | | -------- | -------- | | Promise<Uint8Array> | Uint8Array obtained after asynchronous encoding. | - Example ``` var that = new util.Base64(); var array = new Uint8Array([115,49,51]); var rarray = new Uint8Array([99,122,69,122]); that.encode(array).then(val=>{ for (var i = 0; i < rarray.length; i++) { console.log(val[i]); } }) ``` ### encodeToString8+ encodeToString(src:Uint8Array):Promise<string> Encodes the input content asynchronously into a string. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | src | Uint8Array | Yes | Uint8Array to encode asynchronously. | - Return values | Type | Description | | -------- | -------- | | Promise<string> | String obtained after asynchronous encoding. | - Example ``` var that = new util.Base64(); var array = new Uint8Array([115,49,51]); that.encodeToString(array).then(val=>{ console.log(val); }) ``` ### decode8+ decode(src:Uint8Array | string):Promise<Uint8Array> Decodes the input content asynchronously. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | src | Uint8Array \| string | Yes | Uint8Array or string to decode asynchronously. | - Return values | Type | Description | | -------- | -------- | | Promise<Uint8Array> | Uint8Array obtained after asynchronous decoding. | - Example ``` var that = new util.Base64(); var array = new Uint8Array([99,122,69,122]); var rarray = new Uint8Array([115,49,51]); that.decode(array).then(val=>{ for (var i = 0; i < rarray.length; i++) { console.log(val[i]); } }) ``` ## Types8+ ### constructor8+ constructor() A constructor used to create a **Types** object. - Example ``` var type = new util.Types(); ``` ### isAnyArrayBuffer8+ isAnyArrayBuffer(value: Object):boolean Checks whether the input value is of the **ArrayBuffer** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **ArrayBuffer** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isAnyArrayBuffer(new ArrayBuffer([])); ``` ### 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**. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **ArrayBufferView** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isArrayBufferView(new Int8Array([])); ``` ### isArgumentsObject8+ isArgumentsObject(value: Object):boolean Checks whether the input value is of the **arguments** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **arguments** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); function foo() { var result = that.isArgumentsObject(arguments); } var f = foo(); ``` ### isArrayBuffer8+ isArrayBuffer(value: Object):boolean Checks whether the input value is of the **ArrayBuffer** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **ArrayBuffer** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isArrayBuffer(new ArrayBuffer([])); ``` ### isAsyncFunction8+ isAsyncFunction(value: Object):boolean Checks whether the input value is an asynchronous function. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is an asynchronous function; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isAsyncFunction(async function foo() {}); ``` ### isBooleanObject8+ isBooleanObject(value: Object):boolean Checks whether the input value is of the **Boolean** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Boolean** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var 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. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Boolean**, **Number**, **String**, or **Symbol** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isBoxedPrimitive(new Boolean(false)); ``` ### isDataView8+ isDataView(value: Object):boolean Checks whether the input value is of the **DataView** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **DataView** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); const ab = new ArrayBuffer(20); var result = that.isDataView(new DataView(ab)); ``` ### isDate8+ isDate(value: Object):boolean Checks whether the input value is of the **Date** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Data** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isDate(new Date()); ``` ### isExternal8+ isExternal(value: Object):boolean Checks whether the input value is of the **native external** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **native external** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); const data = util.createExternalType(); var result = that.isExternal(data); ``` ### isFloat32Array8+ isFloat32Array(value: Object):boolean Checks whether the input value is of the **Float32Array** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Float32Array** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isFloat32Array(new Float32Array()); ``` ### isFloat64Array8+ isFloat64Array(value: Object):boolean Checks whether the input value is of the **Float64Array** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Float64Array** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isFloat64Array(new Float64Array()); ``` ### isGeneratorFunction8+ isGeneratorFunction(value: Object):boolean Checks whether the input value is a generator function. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is a generator function; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isGeneratorFunction(function* foo() {}); ``` ### isGeneratorObject8+ isGeneratorObject(value: Object):boolean Checks whether the input value is a generator object. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is a generator object; returns **false** otherwise. | - Example ``` var that = new util.Types(); function* foo() {} const generator = foo(); var result = that.isGeneratorObject(generator); ``` ### isInt8Array8+ isInt8Array(value: Object):boolean Checks whether the input value is of the **Int8Array** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Int8Array** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isInt8Array(new Int8Array([])); ``` ### isInt16Array8+ isInt16Array(value: Object):boolean Checks whether the input value is of the **Int16Array** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Int16Array** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isInt16Array(new Int16Array([])); ``` ### isInt32Array8+ isInt32Array(value: Object):boolean Checks whether the input value is of the **Int32Array** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Int32Array** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isInt32Array(new Int32Array([])); ``` ### isMap8+ isMap(value: Object):boolean Checks whether the input value is of the **Map** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Map** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isMap(new Map()); ``` ### isMapIterator8+ isMapIterator(value: Object):boolean Checks whether the input value is of the **MapIterator** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **MapIterator** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); const map = new Map(); var result = that.isMapIterator(map.keys()); ``` ### isNativeError8+ isNativeError(value: Object):boolean Checks whether the input value is of the **Error** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Error** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isNativeError(new TypeError()); ``` ### isNumberObject8+ isNumberObject(value: Object):boolean Checks whether the input value is a number object. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is a number object; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isNumberObject(new Number(0)); ``` ### isPromise8+ isPromise(value: Object):boolean Checks whether the input value is a promise. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is a promise; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isPromise(Promise.resolve(1)); ``` ### isProxy8+ isProxy(value: Object):boolean Checks whether the input value is a proxy. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is a proxy; returns **false** otherwise. | - Example ``` var that = new util.Types(); const target = {}; const proxy = new Proxy(target, {}); var result = that.isProxy(proxy); ``` ### isRegExp8+ isRegExp(value: Object):boolean Checks whether the input value is of the **RegExp** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **RegExp** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isRegExp(new RegExp('abc')); ``` ### isSet8+ isSet(value: Object):boolean Checks whether the input value is of the **Set** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Set** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isSet(new Set()); ``` ### isSetIterator8+ isSetIterator(value: Object):boolean Checks whether the input value is of the **SetIterator** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **SetIterator** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); const set = new Set(); var result = that.isSetIterator(set.keys()); ``` ### isStringObject8+ isStringObject(value: Object):boolean Checks whether the input value is a string object. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is a string object; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isStringObject(new String('foo')); ``` ### isSymbolObjec8+ isSymbolObjec(value: Object):boolean Checks whether the input value is a symbol object. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is a symbol object; returns **false** otherwise. | - Example ``` var that = new util.Types(); const symbols = Symbol('foo'); var 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**. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **TypedArray** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isTypedArray(new Float64Array([])); ``` ### isUint8Array8+ isUint8Array(value: Object):boolean Checks whether the input value is of the **Uint8Array** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Uint8Array** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isUint8Array(new Uint8Array([])); ``` ### isUint8ClampedArray8+ isUint8ClampedArray(value: Object):boolean Checks whether the input value is of the **Uint8ClampedArray** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Uint8ClampedArray** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isUint8ClampedArray(new Uint8ClampedArray([])); ``` ### isUint16Array8+ isUint16Array(value: Object):boolean Checks whether the input value is of the **Uint16Array** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Uint16Array** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isUint16Array(new Uint16Array([])); ``` ### isUint32Array8+ isUint32Array(value: Object):boolean Checks whether the input value is of the **Uint32Array** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **Uint32Array** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isUint32Array(new Uint32Array([])); ``` ### isWeakMap8+ isWeakMap(value: Object):boolean Checks whether the input value is of the **WeakMap** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **WeakMap** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isWeakMap(new WeakMap()); ``` ### isWeakSet8+ isWeakSet(value: Object):boolean Checks whether the input value is of the **WeakSet** type. - Parameters | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | value | Object | Yes | Object to check. | - Return values | Type | Description | | -------- | -------- | | boolean | Returns **true** if the input value is of the **WeakSet** type; returns **false** otherwise. | - Example ``` var that = new util.Types(); var result = that.isWeakSet(new WeakSet()); ```