README.en.md
1# js_util_module Subsystems / components
2
3- [Introduction](#Introduction)
4- [Directory](#Directory)
5- [Description](#Description)
6 - [Interface description](#Interface description)
7 - [Instruction for use](#Instruction for use)
8
9- [Related warehouse](#Related warehouse)
10
11## Introduction
12The interface of util is used for character Textencoder, TextDecoder and HelpFunction module.The TextEncoder represents a text encoder that accepts a string as input, encodes it in UTF-8 format, and outputs UTF-8 byte stream. The TextDecoder interface represents a text decoder. The decoder takes the byte stream as the input and outputs the Stirng string. HelpFunction is mainly used to callback and promise functions, write and output error codes, and format class strings.
13Encodes all bytes from the specified u8 array into a newly-allocated u8 array using the Base64 encoding scheme or Encodes the specified byte array into a String using the Base64 encoding scheme.Decodes a Base64 encoded String or input u8 array into a newly-allocated u8 array using the Base64 encoding scheme.The rational number is mainly to compare rational numbers and obtain the numerator and denominator.LruBuffer The algorithm replaces the least used data with new data when the buffer space is insufficient. The algorithm derives from the need to access resources: recently accessed data can be Will visit again in the near future. The least accessed data is the least valuable data that should be kicked out of the cache space. The Scope interface is used to describe the valid range of a field. The constructor for the Scope instance is used to create objects with specified lower and upper bounds and require that these objects be comparable
14
15## 目录
16
17```
18base/compileruntime/js_util_module/
19├── Class:TextEncoder # TextEncoder class
20│ ├── new TextEncoder() # create textencoder object
21│ ├── encode() # encode method
22│ ├── encoding # encoding property
23│ └── encodeInto() # encodeInto method
24├── Class:TextDecoder # TextDecoder class
25│ ├── new TextDecoder() # create TextDecoder object
26│ ├── decode() # decode method
27│ ├── encoding # encoding property
28│ ├── fatal # fatal property
29│ └── ignoreBOM # ignoreBOM property
30├── printf() # printf method
31├── getErrorString() # getErrorString method
32├── callbackWrapper() # callbackWrapper method
33├── promiseWrapper() # promiseWrapper method
34├── Class:Base64 # Base64 class
35│ ├── new Base64() # create Base64 object
36│ ├── encode() # encode method
37│ ├── encodeToString() # encodeToString method
38│ └── decode() # decode method
39├── Class:RationalNumber # RationalNumber class
40│ ├── new RationalNumber() # create RationalNumber object
41│ ├── CreateRationalFromString() # CreatRationalFromString method
42│ ├── CompareTo() # CompareTo method
43│ ├── Equals() # Equals method
44│ ├── Value() # Value method
45│ ├── GetCommonDivisor() # GetCommonDivisor method
46│ ├── GetDenominator() # GetDenominator method
47│ ├── GetNumerator() # GetNumerator method
48│ ├── IsFinite() # IsFinite method
49│ ├── IsNaN() # IsNaN method
50│ ├── IsZero() # IsZero method
51│ └── ToString() # ToString method
52├── Class:LruBuffer # LruBuffer class
53│ ├── new LruBuffer() # create RationalNumber object
54│ ├── updateCapacity() # updateCapacity method
55│ ├── toString() # toString method
56│ ├── values() # values method
57│ ├── size() # size method
58│ ├── capacity() # capacity method
59│ ├── clear() # clear method
60│ ├── getCreateCount # getCreateCount method
61│ ├── getMissCount() # getMissCount method
62│ ├── getRemovalCount() # getRemovalCount method
63│ ├── getMatchCount() # getMatchCount method
64│ ├── getPutCount() # getPutCount method
65│ ├── isEmpty() # isEmpty method
66│ ├── get() # get method
67│ ├── put() # put method
68│ ├── keys() # keys method
69│ ├── remove() # remove method
70│ ├── afterRemoval() # afterRemoval method
71│ ├── contains() # contains method
72│ ├── createDefault() # createDefault method
73│ ├── entries() # entries method
74│ └── [Symbol.iterator]() # Symboliterator method
75└── Class:Scope # Scope class
76 ├── constructor() # create Scope object
77 ├── toString() # toString method
78 ├── intersect() # intersect method
79 ├── intersect() # intersect method
80 ├── getUpper() # getUpper method
81 ├── getLower() # getLower method
82 ├── expand() # expand method
83 ├── expand() # expand method
84 ├── expand() # expand method
85 ├── contains() # contains method
86 ├── contains() # contains method
87 └── clamp() # clamp method
88```
89
90## Description
91
92### Interface description
93
94
95| Interface name | Description |
96| -------- | -------- |
97| readonly encoding : string | Get the encoding format, only UTF-8 is supported. |
98| encode(input : string) : Uint8Array | Input stirng string, encode and output UTF-8 byte stream. |
99| encodeInto(input : string, dest : Uint8Array) : {read : number, written : number} | Enter the stirng string, dest represents the storage location after encoding, and returns an object, read represents the number of characters that have been encoded,and written represents the size of bytes occupied by the encoded characters. |
100| constructor(encoding? : string, options? : {fatal? : boolean, ignoreBOM? : boolean}) | Constructor, the first parameter encoding indicates the format of decoding.The second parameter represents some attributes.Fatal in the attribute indicates whether an exception is thrown, and ignoreBOM indicates whether to ignore the bom flag. |
101| readonly encoding : string | Get the set decoding format. |
102| readonly fatal : boolean | Get the setting that throws the exception. |
103| readonly ignoreBOM : boolean | Get whether to ignore the setting of the bom flag. |
104| decode(input : ArrayBuffer | Input the data to be decoded, and solve the corresponding string character string.The first parameter input represents the data to be decoded, and the second parameter options represents a bool flag, which means that additional data will be followed. The default is false. |
105| function printf(format: string, ...args: Object[]): string | The util.format() method returns a formatted string using the first argument as a printf-like format string which can contain zero or more format specifiers. |
106| function getErrorString(errno: number): string | The geterrorstring () method uses a system error number as a parameter to return system error information. |
107| function callbackWrapper(original: Function): (err: Object, value: Object) => void | Takes an async function (or a function that returns a Promise) and returns a function following the error-first callback style, i.e. taking an (err, value) => ... callback as the last argument. In the callback, the first argument will be the rejection reason (or null if the Promise resolved), and the second argument will be the resolved value. |
108| function promiseWrapper(original: (err: Object, value: Object) => void): Object | Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises. |
109| encode(src: Uint8Array): Uint8Array; | Encodes all bytes from the specified u8 array into a newly-allocated u8 array using the Base64 encoding scheme. |
110| encodeToString(src: Uint8Array): string; | Encodes the specified byte array into a String using the Base64 encoding scheme. |
111| decode(src: Uint8Array | string): Uint8Array; |
112| Decodes a Base64 encoded String or input u8 array into a newly-allocated u8 array using the Base64 encoding scheme. ||
113| CreateRationalFromString(src: string): Rational | Create a RationalNumber object based on the given string |
114| CompareTo(src: RationalNumber): number | Compare the current RationalNumber object with the given object |
115| Equals(src: object): number | Check if the given object is the same as the current RationalNumber object |
116| Value(): number | Take the current RationalNumber object to an integer value or a floating point value |
117| GetCommonDivisor(arg1: int, arg2: int,): number | Obtain the greatest common divisor of two specified numbers |
118| GetDenominator(): number | Get the denominator of the current RationalNumber object |
119| GetNumerator(): number | Get the numerator of the current RationalNumber object |
120| IsFinite(): bool | Check that the current RationalNumber object is limited |
121| IsNaN(): bool | Check whether the current RationalNumber object represents a non-number (NaN) value |
122| IsZero(): bool | Check whether the current RationalNumber object represents a zero value |
123| ToString(): string | Get the string representation of the current RationalNumber object |
124| updateCapacity(newCapacity:number):void | Updates the buffer capacity to the specified capacity. This exception is thrown if newCapacity is less than or equal to 0 |
125| toString():string | Returns the string representation of the object and outputs the string representation of the object |
126| values():V[ ] | Gets a list of all values in the current buffer, and the output returns a list of all values in the current buffer in ascending order, from most recently accessed to least recently accessed |
127| size():number | Gets the total number of values in the current buffer. The output returns the total number of values in the current buffer |
128| capacity():number | Gets the capacity of the current buffer. The output returns the capacity of the current buffer |
129| clear():void | The key value pairs are cleared from the current buffer, after the key value is cleared, the afterRemoval () method is invoked to perform subsequent operations in turn |
130| getCreateCount():number | Get the number of times the returned value of createdefault(), and output the number of times the returned value of createdefault() |
131| getMissCount():number | Get the number of times the query value does not match, and output the number of times the query value does not match |
132| getRemovalCount():number | Gets the number of evictions from the buffer, and outputs the number of evictions from the buffer |
133| getMatchCount():number | Obtain the number of successful matching of query values, and output the number of successful matching of query values |
134| getPutCount():number | Gets the number of times the value was added to the buffer, and the output returns the number of times the value was added to the buffer |
135| isEmpty():boolean | Checks whether the current buffer is empty and returns true if the current buffer does not contain any values |
136| get(k:key):V / undefined | Indicates the key to query. If the specified key exists in the buffer, the value associated with the key will be returned; Otherwise, undefined is returned |
137| put(K key, V value):V | Adding the key value pair to the buffer and outputting the value associated with the added key; If the key to be added already exists, the original value is returned. If the key or value is empty, this exception is thrown |
138| keys():V[ ] | Get the key list of the value in the current buffer, and the output returns the key list sorted from the most recent access to the least recent access |
139| remove(k:key):V / undefined | Deletes the specified key and its associated value from the current buffer |
140| afterRemoval(boolean isEvict, K key, V value, V newValue):void | Perform subsequent operations after deleting the value |
141| contains(k:key):boolean | Checks whether the current buffer contains the specified key, and returns true if the buffer contains the specified key |
142| createDefault(k:key):V | If the value of a specific key is not calculated, subsequent operations are performed. The parameter represents the missing key, and the output returns the value associated with the key |
143| entries() : [K,V] | Allows you to iterate over all key / value pairs contained in this object. The keys and values of each pair are objects |
144| [Symbol.iterator]():[K,V]| Returns a two-dimensional array in the form of key value pairs |
145| constructor(lowerObj: ScopeType, upperObj: ScopeType) | Creates and returns a Scope object that creates a constructor for a scope instance that specifies a lower and upper bound. |
146| toString():string | The stringification method returns a string representation that contains the current range. |
147| intersect(range: Scope): Scope | This method returns the intersection of a given range and the current range. |
148| intersect(lowerObj: ScopeType, upperObj: ScopeType): Scope | Passing in the upper and lower bounds of the given range returns the intersection of the current range and the range specified by the given lower and upper bounds. |
149| getUpper(): ScopeType | Gets the upper bound of the current scope and returns a value of type ScopeType. |
150| getLower(): ScopeType | Gets the lower bound of the current scope and returns a value of type ScopeType. |
151| expand(lowerObj: ScopeType, upperObj: ScopeType): Scope | This method creates and returns a union that includes the current range and a given lower and upper bound. |
152| expand(range: Scope): Scope | This method creates and returns a union that includes the current range and the given range. |
153| expand(value: ScopeType): Scope | This method creates and returns a union that includes the current range and the given value. |
154| contains(value: ScopeType): boolean | Checks whether the given value is included in the current range. If yes, true is returned. Otherwise, false is returned. |
155| contains(range: Scope): boolean | Checks whether the given range is within the current range. Returns true if in, false otherwise. |
156| clamp(value: ScopeType): ScopeType | Clips the specified value to the current range. If the value is less than the lower limit, lowerObj is returned. If the value is greater than the upper limit, upperObj is returned. Returns value if within the current range. |
157
158Each specifier in printf is replaced with a converted value from the corresponding parameter. Supported specifiers are:
159| Stylized character | Style requirements |
160| -------- | -------- |
161| %s: | String will be used to convert all values except BigInt, Object and -0. |
162| %d: | Number will be used to convert all values except BigInt and Symbol. |
163| %i: | parseInt(value, 10) is used for all values except BigInt and Symbol. |
164| %f: | parseFloat(value) is used for all values expect Symbol. |
165| %j: | JSON. Replaced with the string '[Circular]' if the argument contains circular references. |
166| %o: | Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() with options { showHidden: true, showProxy: true }. This will show the full object including non-enumerable properties and proxies. |
167| %O: | Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() without options. This will show the full object not including non-enumerable properties and proxies. |
168| %c: | CSS. This specifier is ignored and will skip any CSS passed in. |
169| %%: | single percent sign ('%'). This does not consume an argument. |
170
171### Instruction for use
172
173
174The use methods of each interface are as follows:
175
1761.readonly encoding()
177
178```
179import util from '@ohos.util'
180var textEncoder = new util.TextEncoder();
181var getEncoding = textEncoder.encoding();
182```
1832.encode()
184```
185import util from '@ohos.util'
186var textEncoder = new util.TextEncoder();
187var result = textEncoder.encode('abc');
188```
1893.encodeInto()
190```
191import util from '@ohos.util'
192var textEncoder = new util.TextEncoder();
193var obj = textEncoder.encodeInto('abc', dest);
194```
1954.textDecoder()
196```
197import util from '@ohos.util'
198var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
199```
2005.readonly encoding()
201```
202import util from '@ohos.util'
203var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
204var getEncoding = textDecoder.encoding();
205```
2066.readonly fatal()
207```
208import util from '@ohos.util'
209var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
210var fatalStr = textDecoder.fatal();
211```
2127.readonly ignoreBOM()
213```
214import util from '@ohos.util'
215var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
216var ignoreBom = textDecoder.ignoreBOM();
217```
2188.decode()
219```
220import util from '@ohos.util'
221var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
222var result = textDecoder.decode(input, {stream : true});
223```
2249.printf()
225```
226import util from '@ohos.util'
227var format = "%%%o%%%i%s";
228var value = function aa(){};
229var value1 = 1.5;
230var value2 = "qwer";
231var result = util.printf(format,value,value1,value2);
232```
23310.getErrorString()
234```
235import util from '@ohos.util'
236var errnum = 13;
237var result = util.getErrorString(errnum);
238```
23911.callbackWrapper()
240```
241import util from '@ohos.util'
242async function promiseFn() {
243 return Promise.resolve('value');
244};
245var cb = util.callbackWrapper(promiseFn);
246cb((err, ret) => {
247 expect(err).strictEqual(null);
248 expect(ret).strictEqual('value');
249})
250```
25112.promiseWrapper()
252```
253import util from '@ohos.util'
254function aysnFun(str1, str2, callback) {
255 if (typeof str1 === 'string' && typeof str1 === 'string') {
256 callback(null, str1 + str2);
257 } else {
258 callback('type err');
259 }
260}
261let newPromiseObj = util.promiseWrapper(aysnFun)("Hello", 'World');
262newPromiseObj.then(res => {
263 expect(res).strictEqual('HelloWorld');
264})
265```
26613.encode()
267```
268import util from '@ohos.util'
269var that = new util.Base64();
270var array = new Uint8Array([115,49,51]);
271var num = 0;
272var result = that.encode(array,num);
273```
27414.encodeToString()
275```
276import util from '@ohos.util'
277var that = new util.Base64();
278var array = new Uint8Array([115,49,51]);
279var num = 0;
280var result = that.encodeToString(array,num);
281```
28215.decode()
283```
284import util from '@ohos.util'
285var that = new util.Base64()
286var buff = 'czEz';
287var num = 0;
288var result = that.decode(buff,num);
289```
29016.createRationalFromString()
291```
292import util from '@ohos.util'
293var pro = new util.RationalNumber(0, 0);
294var res = pro.createRationalFromString("-1:2");
295var result1 = res.value();
296```
29717.compareTo()
298```
299import util from '@ohos.util'
300var pro = new util.RationalNumber(2, 1);
301var proc = new util.RationalNumber(3, 4);
302var res = pro.compareTo(proc);
303```
30418.equals()
305```
306import util from '@ohos.util'
307var pro = new util.RationalNumber(2, 1);
308var proc = new util.RationalNumber(3, 4);
309var res = pro.equals(proc);
310```
31119.value()
312```
313import util from '@ohos.util'
314var pro = new util.RationalNumber(2, 1);
315var res = pro.value();
316```
31720.getCommonDivisor()
318```
319import util from '@ohos.util'
320var pro = new util.RationalNumber(0, 0);
321var res = pro.getCommonDivisor(4, 8);
322```
32321.getDenominator()
324```
325import util from '@ohos.util'
326var pro = new util.RationalNumber(2, 1);
327var res = pro.getDenominator();
328```
32922.getNumerator()
330```
331import util from '@ohos.util'
332var pro = new util.RationalNumber(-2, 1);
333var res = pro.getNumerator();
334```
33523.isFinite()
336```
337import util from '@ohos.util'
338var pro = new util.RationalNumber(-2, 1);
339var res = pro.isFinite();
340```
34124.isNaN()
342```
343import util from '@ohos.util'
344var pro = new util.RationalNumber(-2, 1);
345var res = pro.isNaN();
346```
34725.isZero()
348```
349import util from '@ohos.util'
350var pro = new util.RationalNumber(-2, 1);
351var res = pro.isZero();
352```
35326.toString()
354```
355import util from '@ohos.util'
356var pro = new util.RationalNumber(-2, 1);
357var res = pro.toString();
358```
35927.updateCapacity()
360```
361import util from '@ohos.util'
362var pro = new util.LruBuffer();
363pro.updateCapacity(100);
364```
36528.toString()
366```
367import util from '@ohos.util'
368var pro = new util.LruBuffer();
369pro.put(2,10);
370pro.get(2);
371pro.remove(20);
372var temp = pro.toString();
373```
37429.values()
375```
376import util from '@ohos.util'
377var pro = new util.LruBuffer();
378pro.put(2,10);
379pro.put(2,"anhu");
380pro.put("afaf","grfb");
381var temp = pro.values();
382```
38330.size()
384```
385import util from '@ohos.util'
386var pro = new util.LruBuffer();
387pro.put(2,10);
388pro.put(1,8);
389var temp = pro.size();
390```
39131.capacity()
392```
393import util from '@ohos.util'
394var pro = new util.LruBuffer();
395var temp = pro.capacity();
396```
39732.clear()
398```
399import util from '@ohos.util'
400var pro = new util.LruBuffer();
401pro.put(2,10);
402pro.clear();
403```
40433.getCreatCount()
405```
406import util from '@ohos.util'
407var pro = new util.LruBuffer();
408pro.put(1,8);
409var temp = pro.getCreatCount();
410```
41134.getMissCount()
412```
413import util from '@ohos.util'
414var pro = new util.LruBuffer();
415pro.put(2,10);
416pro.get(2)
417var temp = pro.getMissCount();
418```
41935.getRemovalCount()
420```
421
422import util from '@ohos.util'
423var pro = new util.LruBuffer();
424pro.put(2,10);
425pro.updateCapacity(2);
426pro.put(50,22);
427var temp = pro.getRemovalCount();
428
429```
43036.getMatchCount
431```
432import util from '@ohos.util'
433var pro = new util.LruBuffer();
434pro.put(2,10);
435pro.get(2);
436var temp5 = pro.getMatchCount();
437```
43837.getPutCount()
439```
440import util from '@ohos.util'
441var pro = new util.LruBuffer();
442pro.put(2,10);
443var temp = pro.getPutCount();
444```
44538.isEmpty()
446```
447import util from '@ohos.util'
448var pro = new util.LruBuffer();
449pro.put(2,10);
450var temp = pro.isEmpty();
451```
45239.get()
453
454```
455import util from '@ohos.util'
456var pro = new util.LruBuffer();
457pro.put(2,10);
458var temp = pro.get(2);
459```
46040.put()
461```
462import util from '@ohos.util'
463var pro = new util.LruBuffer();
464var temp = pro.put(2,10);
465```
46641.keys()
467```
468import util from '@ohos.util'
469var pro = new util.LruBuffer();
470pro.put(2,10);
471var temp = pro.keys();
472```
47342.remove()
474```
475import util from '@ohos.util'
476var pro = new util.LruBuffer();
477pro.put(2,10);
478var temp = pro.remove(20);
479```
48043.contains()
481```
482import util from '@ohos.util'
483var pro = new util.LruBuffer();
484pro.put(2,10);
485var temp = pro.contains(20);
486```
48744.createDefault()
488```
489import util from '@ohos.util'
490var pro = new util.LruBuffer();
491var trmp = pro .createDefault(50);
492```
49345.entries()
494```
495import util from '@ohos.util'
496var pro = new util.LruBuffer();
497pro.put(2,10);
498var temp = pro.entries();
499```
50046.[Symbol.iterator]()
501```
502import util from '@ohos.util'
503var pro = new util.LruBuffer();
504pro .put(2,10);
505var temp = pro[symbol.iterator]();
506```
507Construct a new class in the scope interface to implement the compareTo method.
508
509```
510class Temperature {
511 constructor(value) {
512 this._temp = value;
513 }
514 compareTo(value) {
515 return this._temp >= value.getTemp();
516 }
517 getTemp() {
518 return this._temp;
519 }
520 toString() {
521 return this._temp.toString();
522 }
523}
524```
525
52647.constructor()
527
528```
529var tempLower = new Temperature(30);
530var tempUpper = new Temperature(40);
531var range = new Scope(tempLower, tempUpper);
532```
533
53448.toString()
535
536```
537var tempLower = new Temperature(30);
538var tempUpper = new Temperature(40);
539var range = new Scope(tempLower, tempUpper);
540range.toString() // => [30,40]
541```
542
54349.intersect()
544
545```
546var tempLower = new Temperature(30);
547var tempUpper = new Temperature(40);
548var range = new Scope(tempLower, tempUpper);
549var tempMiDF = new Temperature(35);
550var tempMidS = new Temperature(39);
551var rangeFir = new Scope(tempMiDF, tempMidS);
552range.intersect(rangeFir) // => [35,39]
553```
554
55550.intersect()
556
557```
558var tempLower = new Temperature(30);
559var tempUpper = new Temperature(40);
560var tempMiDF = new Temperature(35);
561var tempMidS = new Temperature(39);
562var range = new Scope(tempLower, tempUpper);
563range.intersect(tempMiDF, tempMidS) // => [35,39]
564```
565
56651.getUpper()
567
568```
569var tempLower = new Temperature(30);
570var tempUpper = new Temperature(40);
571var range = new Scope(tempLower, tempUpper);
572range.getUpper() // => 40
573```
574
57552.getLower()
576
577```
578var tempLower = new Temperature(30);
579var tempUpper = new Temperature(40);
580var range = new Scope(tempLower, tempUpper);
581range.getLower() // => 30
582```
583
58453.expand()
585
586```
587var tempLower = new Temperature(30);
588var tempUpper = new Temperature(40);
589var tempMiDF = new Temperature(35);
590var tempMidS = new Temperature(39);
591var range = new Scope(tempLower, tempUpper);
592range.expand(tempMiDF, tempMidS) // => [30,40]
593```
594
59554.expand()
596
597```
598var tempLower = new Temperature(30);
599var tempUpper = new Temperature(40);
600var tempMiDF = new Temperature(35);
601var tempMidS = new Temperature(39);
602var range = new Scope(tempLower, tempUpper);
603var rangeFir = new Scope(tempMiDF, tempMidS);
604range.expand(rangeFir) // => [30,40]
605```
606
60755.expand()
608
609```
610var tempLower = new Temperature(30);
611var tempUpper = new Temperature(40);
612var tempMiDF = new Temperature(35);
613var range = new Scope(tempLower, tempUpper);
614range.expand(tempMiDF) // => [30,40]
615```
616
61756.contains()
618
619```
620var tempLower = new Temperature(30);
621var tempUpper = new Temperature(40);
622var tempMiDF = new Temperature(35);
623var range = new Scope(tempLower, tempUpper);
624range.contains(tempMiDF) // => true
625```
626
62757.contains()
628
629```
630var tempLower = new Temperature(30);
631var tempUpper = new Temperature(40);
632var range = new Scope(tempLower, tempUpper);
633var tempLess = new Temperature(20);
634var tempMore = new Temperature(45);
635var rangeSec = new Scope(tempLess, tempMore);
636range.contains(rangeSec) // => true
637```
638
63958.clamp()
640
641```
642var tempLower = new Temperature(30);
643var tempUpper = new Temperature(40);
644var tempMiDF = new Temperature(35);
645var range = new Scope(tempLower, tempUpper);
646range.clamp(tempMiDF) // => 35
647```
648
649
650
651## Related warehouse
652
653[js_util_module subsystem](https://gitee.com/OHOS_STD/js_util_module)
654
655[base/compileruntime/js_util_module/](base/compileruntime/js_util_module-readme.md)
656
README.md
1# js_util_module Subsystems / components
2
3- [Introduction](#Introduction)
4- [Directory](#Directory)
5- [Description](#Description)
6 - [Interface description](#Interface description)
7 - [Instruction for use](#Instruction for use)
8
9- [Related warehouse](#Related warehouse)
10
11## Introduction
12The interface of util is used for character Textencoder, TextDecoder and HelpFunction module.The TextEncoder represents a text encoder that accepts a string as input, encodes it in UTF-8 format, and outputs UTF-8 byte stream. The TextDecoder interface represents a text decoder. The decoder takes the byte stream as the input and outputs the Stirng string. HelpFunction is mainly used to callback and promise functions, write and output error codes, and format class strings.
13Encodes all bytes from the specified u8 array into a newly-allocated u8 array using the Base64 encoding scheme or Encodes the specified byte array into a String using the Base64 encoding scheme.Decodes a Base64 encoded String or input u8 array into a newly-allocated u8 array using the Base64 encoding scheme.The rational number is mainly to compare rational numbers and obtain the numerator and denominator.The LruBuffer algorithm replaces the least used data with new data when the buffer space is insufficient. The algorithm derives from the need to access resources: recently accessed data can be Will visit again in the near future. The least accessed data is the least valuable data that should be kicked out of the cache space. The Scope interface is used to describe the valid range of a field. The constructor for the Scope instance is used to create objects with specified lower and upper bounds and require that these objects be comparable.
14## 目录
15
16```
17base/compileruntime/js_util_module/
18├── Class:TextEncoder # TextEncoder class
19│ ├── new TextEncoder() # create textencoder object
20│ ├── encode() # encode method
21│ ├── encoding # encoding property
22│ └── encodeInto() # encodeInto method
23├── Class:TextDecoder # TextDecoder class
24│ ├── new TextDecoder() # create TextDecoder object
25│ ├── decode() # decode method
26│ ├── encoding # encoding property
27│ ├── fatal # fatal property
28│ └── ignoreBOM # ignoreBOM property
29├── printf() # printf method
30├── getErrorString() # getErrorString method
31├── callbackWrapper() # callbackWrapper method
32├── promiseWrapper() # promiseWrapper method
33├── Class:Base64 # Base64 class
34│ ├── new Base64() # create Base64 object
35│ ├── encode() # encode method
36│ ├── encodeToString() # encodeToString method
37│ ├── decode() # decode method
38│ ├── encodeAsync() # encodeAsync method
39│ ├── encodeToStringAsync() # encodeToStringAsync method
40│ └── decodeAsync() # decodeAsync method
41├── Class:RationalNumber # RationalNumber class
42│ ├── new RationalNumber() # create RationalNumber object
43│ ├── createRationalFromString() # creatRationalFromString method
44│ ├── compareTo() # compareTo method
45│ ├── equals() # equals method
46│ ├── value() # value method
47│ ├── getCommonDivisor() # getCommonDivisor method
48│ ├── getDenominator() # getDenominator method
49│ ├── getNumerator() # getNumerator method
50│ ├── isFinite() # isFinite method
51│ ├── isNaN() # isNaN method
52│ ├── isZero() # isZero method
53│ └── toString() # toString method
54├── Class:LruBuffer # LruBuffer class
55│ ├── new LruBuffer() # create RationalNumber object
56│ ├── updateCapacity() # updateCapacity method
57│ ├── toString() # toString method
58│ ├── values() # values method
59│ ├── size() # size method
60│ ├── capacity() # capacity method
61│ ├── clear() # clear method
62│ ├── getCreateCount # getCreateCount method
63│ ├── getMissCount() # getMissCount method
64│ ├── getRemovalCount() # getRemovalCount method
65│ ├── getMatchCount() # getMatchCount method
66│ ├── getPutCount() # getPutCount method
67│ ├── isEmpty() # isEmpty method
68│ ├── get() # get method
69│ ├── put() # put method
70│ ├── keys() # keys method
71│ ├── remove() # remove method
72│ ├── afterRemoval() # afterRemoval method
73│ ├── contains() # contains method
74│ ├── createDefault() # createDefault method
75│ ├── entries() # entries method
76│ └── [Symbol.iterator]() # Symboliterator method
77├── Class:Scope # Scope class
78| ├── constructor() # create Scope object
79| ├── toString() # toString method
80| ├── intersect() # intersect method
81| ├── intersect() # intersect method
82| ├── getUpper() # getUpper method
83| ├── getLower() # getLower method
84| ├── expand() # expand method
85| ├── expand() # expand method
86| ├── expand() # expand method
87| ├── contains() # contains method
88| ├── contains() # contains method
89| └── clamp() # clamp method
90└── Class:Types # Types class
91 ├── isAnyArrayBuffer() # isAnyArrayBuffer method
92 ├── isArrayBufferView() # isArrayBufferView method
93 ├── isArgumentsObject() # isArgumentsObject method
94 ├── isArrayBuffer() # isArrayBuffer method
95 ├── isAsyncFunction() # isAsyncFunction method
96 ├── isBigInt64Array() # isBigInt64Array method
97 ├── isBigUint64Array() # isBigUint64Array method
98 ├── isBooleanObject() # isBooleanObject method
99 ├── isBoxedPrimitive() # isBoxedPrimitive method
100 ├── isCryptoKey() # isCryptoKey method
101 ├── isDataView() # isDataView method
102 ├── isDate() # isDate method
103 ├── isExternal() # isExternal method
104 ├── isFloat32Array() # isFloat32Arraymethod
105 ├── isFloat64Array() # isFloat64Array method
106 ├── isGeneratorFunction() # isGeneratorFunction method
107 ├── isGeneratorObject() # isGeneratorObject method
108 ├── isInt8Array() # isInt8Array method
109 ├── isInt16Array() # isInt16Array method
110 ├── isInt32Array() # isInt32Array method
111 ├── isKeyObject() # isKeyObject method
112 ├── isMap() # isMap method
113 ├── isMapIterator() # isMapIterator method
114 ├── isModuleNamespaceObject() # isModuleNamespaceObject method
115 ├── isNativeError() # isNativeError method
116 ├── isNumberObject() # isNumberObject method
117 ├── isPromise() # isPromise method
118 ├── isProxy() # isProxy method
119 ├── isRegExp() # isRegExp method
120 ├── isSet() # isSet method
121 ├── isSetIterator() # isSetIterator method
122 ├── isSharedArrayBuffer() # isSharedArrayBuffer method
123 ├── isStringObject() # isStringObject method
124 ├── isSymbolObject() # isSymbolObject method
125 ├── isTypedArray() # isTypedArray method
126 ├── isUint8Array() # isUint8Array method
127 ├── isUint8ClampedArray() # isUint8ClampedArray method
128 ├── isUint16Array() # isUint16Array method
129 ├── isUint32Array() # isUint32Array method
130 ├── isWeakMap() # isWeakMap method
131 └── isWeakSet() # isWeakSet method
132```
133
134## Description
135
136### Interface description
137| Interface name | Description |
138| -------- | -------- |
139| readonly encoding : string | In the TextEncoder module, get the encoding format, only UTF-8 is supported. |
140| encode(input : string) : Uint8Array | Input stirng string, encode and output UTF-8 byte stream. |
141| encodeInto(input : string, dest : Uint8Array) : {read : number, written : number} | Enter the stirng string, dest represents the storage location after encoding, and returns an object, read represents the number of characters that have been encoded,and written represents the size of bytes occupied by the encoded characters. |
142| constructor(encoding? : string, options? : {fatal? : boolean, ignoreBOM? : boolean}) | Constructor, the first parameter encoding indicates the format of decoding.The second parameter represents some attributes.Fatal in the attribute indicates whether an exception is thrown, and ignoreBOM indicates whether to ignore the bom flag. |
143| readonly encoding : string | In the TextDecoder module, get the set decoding format. |
144| readonly fatal : boolean | Get the setting that throws the exception. |
145| readonly ignoreBOM : boolean | Get whether to ignore the setting of the bom flag. |
146| decode(input : Uint8Array, options?: { stream?: false }) : string | Input the data to be decoded, and solve the corresponding string character string.The first parameter input represents the data to be decoded, and the second parameter options represents a bool flag, which means that additional data will be followed. The default is false. |
147| encode(src: Uint8Array): Uint8Array; | Encodes all bytes in the specified u8 array into the newly allocated u8 array using the Base64 encoding scheme. |
148| encodeToString(src: Uint8Array): string; | Encodes the specified byte array as a String using the Base64 encoding scheme. |
149| decode(src: Uint8Array \| string): Uint8Array; | Decodes the Base64-encoded string or input u8 array into the newly allocated u8 array using the Base64 encoding scheme. |
150| encodeAsync(src: Uint8Array): Promise\<Uint8Array\>; | Asynchronously encodes all bytes in the specified u8 array into the newly allocated u8 array using the Base64 encoding scheme. |
151| encodeToStringAsync(src: Uint8Array): Promise\<string\>; | Asynchronously encodes the specified byte array into a String using the Base64 encoding scheme. |
152| decodeAsync(src: Uint8Array \| string): Promise\<Uint8Array\>; | Use the Base64 encoding scheme to asynchronously decode a Base64-encoded string or input u8 array into a newly allocated u8 array. |
153| static createRationalFromString(rationalString: string): RationalNumber | Create a RationalNumber object based on the given string. |
154| compareTo(another: RationalNumber): number | Compare the current RationalNumber object with the given object. |
155| equals(obj: object): number | Check if the given object is the same as the current RationalNumber object.|
156| value(): number | Take the current RationalNumber object to an integer value or a floating point value. |
157| static getCommonDivisor(number1: number, number2: number,): number | Obtain the greatest common divisor of two specified numbers. |
158| getDenominator(): number | Get the denominator of the current RationalNumber object. |
159| getNumerator(): number | Get the numerator of the current RationalNumber object. |
160| isFinite(): boolean | Check that the current RationalNumber object is limited. |
161| isNaN(): boolean | Check whether the current RationalNumber object represents a non-number (NaN) value. |
162| isZero(): boolean | Check whether the current RationalNumber object represents a zero value. |
163| toString(): string | Get the string representation of the current RationalNumber object. |
164| constructor(capacity?: number) | The Create Default constructor is used to create a new LruBuffer instance with a default capacity of 64. |
165| updateCapacity(newCapacity: number): void | Updates the buffer capacity to the specified capacity. This exception is thrown if newCapacity is less than or equal to 0. |
166| toString(): string | Returns the string representation of the object and outputs the string representation of the object. |
167| values(): V[ ] | Gets a list of all values in the current buffer, and the output returns a list of all values in the current buffer in ascending order, from most recently accessed to least recently accessed. |
168| size(): number | Gets the total number of values in the current buffer. The output returns the total number of values in the current buffer. |
169| capacity(): number | Gets the capacity of the current buffer. The output returns the capacity of the current buffer. |
170| clear(): void | The key value pairs are cleared from the current buffer, after the key value is cleared, the afterRemoval () method is invoked to perform subsequent operations in turn. |
171| getCreateCount(): number | Get the number of times the returned value of createdefault(), and output the number of times the returned value of createdefault(). |
172| getMissCount(): number | Get the number of times the query value does not match, and output the number of times the query value does not match. |
173| getRemovalCount(): number | Gets the number of evictions from the buffer, and outputs the number of evictions from the buffer. |
174| getMatchCount(): number | Obtain the number of successful matching of query values, and output the number of successful matching of query values. |
175| getPutCount(): number | Gets the number of times the value was added to the buffer, and the output returns the number of times the value was added to the buffer. |
176| isEmpty(): boolean | Checks whether the current buffer is empty and returns true if the current buffer does not contain any values. |
177| get(key: K):V \| undefined | Indicates the key to query. If the specified key exists in the buffer, the value associated with the key will be returned; Otherwise, undefined is returned. |
178| put(key: K, value: V): V | Adding the key value pair to the buffer and outputting the value associated with the added key; If the key to be added already exists, the original value is returned. If the key or value is empty, this exception is thrown. |
179| keys(): K[ ] | Get the key list of the value in the current buffer, and the output returns the key list sorted from the most recent access to the least recent access. |
180| remove(key: K):V \| undefined | Deletes the specified key and its associated value from the current buffer. |
181| afterRemoval(isEvict: boolean, key: K, value: V, newValue: V): void | Perform subsequent operations after deleting the value. |
182| contains(key: K): boolean | Checks whether the current buffer contains the specified key, and returns true if the buffer contains the specified key. |
183| createDefault(key: K): V | If the value of a specific key is not calculated, subsequent operations are performed. The parameter represents the missing key, and the output returns the value associated with the key. |
184| entries(): [K,V] | Allows you to iterate over all key value pairs contained in this object. The keys and values of each pair are objects. |
185| \[Symbol.iterator\](): [K,V]| Returns a two-dimensional array in the form of key value pairs. |
186| constructor(lowerObj: ScopeType, upperObj: ScopeType) | Creates and returns a Scope object that creates a constructor for a scope instance that specifies a lower and upper bound. |
187| toString():string | The stringification method returns a string representation that contains the current range. |
188| intersect(range: Scope): Scope | Gets the intersection of the given range and the current range. |
189| intersect(lowerObj: ScopeType, upperObj: ScopeType): Scope | Gets the intersection of the current range with a given lower and upper bound range. |
190| getUpper(): ScopeType | Gets the upper bound of the current scope. |
191| getLower(): ScopeType | Gets the lower bound of the current scope. |
192| expand(lowerObj: ScopeType, upperObj: ScopeType): Scope | Creates and returns a union that includes the current range and a given lower and upper bound. |
193| expand(range: Scope): Scope | Creates and returns a union that includes the current range and the given range. |
194| expand(value: ScopeType): Scope | Creates and returns a union that includes the current range and the given value. |
195| contains(value: ScopeType): boolean | Checks whether the given value is included in the current range. |
196| contains(range: Scope): boolean | Checks whether the given range is within the current range. |
197| clamp(value: ScopeType): ScopeType | Clips the specified value to the current range. |
198| function printf(format: string, ...args: Object[]): string | The util.format() method returns a formatted string using the first argument as a printf-like format string which can contain zero or more format specifiers. |
199| function getErrorString(errno: number): string | The geterrorstring () method uses a system error number as a parameter to return system error information. |
200| function callbackWrapper(original: Function): (err: Object, value: Object) => void | Takes an async function (or a function that returns a Promise) and returns a function following the error-first callback style, i.e. taking an (err, value) => ... callback as the last argument. In the callback, the first argument will be the rejection reason (or null if the Promise resolved), and the second argument will be the resolved value. |
201| function promiseWrapper(original: (err: Object, value: Object) => void): Object | Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises. |
202| isAnyArrayBuffer(value: Object): boolean | Check whether the entered value is of arraybuffer or sharedarraybuffer type. |
203| isArrayBufferView(value: Object): boolean | Check whether the entered value is napi_ int8_ array or napi_ uint8_ array or naPi_ uint8_ clamped_ array or naPi_ int16_ array or naPi_ uint16_ array or napi_ int32_ array or napi_ uint32_ array or napi_ float32_ array or napi_ float64_ array array or DataView type. |
204| isArgumentsObject(value: Object): boolean | Check whether the entered value is an arguments object type. |
205| isArrayBuffer(value: Object): boolean | Check whether the entered value is of arraybuffer type. |
206| isAsyncFunction(value: Object): boolean | Check whether the value entered is an asynchronous function type. |
207| isBigInt64Array(value: Object): boolean | Check whether the entered value is of bigint64array array type. |
208| isBigUint64Array(value: Object): boolean | Check whether the entered value is of biguint64array array array type. |
209| isBooleanObject(value: Object): boolean | Check whether the entered value is a Boolean object type. |
210| isBoxedPrimitive(value: Object): boolean | Check whether the entered value is a Boolean or number or string or symbol object type. |
211| isCryptoKey(value: Object): boolean | Check whether the entered value is the cryptokey object type. |
212| isDataView(value: Object): boolean | Check whether the entered value is of DataView type. |
213| isDate(value: Object): boolean | Check whether the entered value is of type date. |
214| isExternal(value: Object): boolean | Check whether the entered value is a native external value type. |
215| isFloat32Array(value: Object): boolean | Check whether the entered value is of float32array array type. |
216| isFloat64Array(value: Object): boolean | Check whether the entered value is of float64array array type. |
217| isGeneratorFunction(value: Object): boolean | Check whether the input value is a generator function type. |
218| isGeneratorObject(value: Object): boolean | Check whether the entered value is a generator object type. |
219| isInt8Array(value: Object): boolean | Check whether the entered value is of int8array array type. |
220| isInt16Array(value: Object): boolean | Check whether the entered value is the int16array type. |
221| isInt32Array(value: Object): boolean | Check whether the entered value is the int32array array type. |
222| isKeyObject(value: Object): boolean | Check whether the entered value is the keyobject object type. |
223| isMap(value: Object): boolean | Check whether the entered value is of map type. |
224| isMapIterator(value: Object): boolean | Check whether the entered value is the iterator type of map. |
225| isModuleNamespaceObject(value: Object): boolean | Check whether the entered value is the module namespace object object type. |
226| isNativeError(value: Object): boolean | Check whether the value entered is of type error. |
227| isNumberObject(value: Object): boolean | Check whether the entered value is of the number object type. |
228| isPromise(value: Object): boolean | Check whether the entered value is of promise type. |
229| isProxy(value: Object): boolean | Check whether the value entered is of proxy type. |
230| isRegExp(value: Object): boolean | Check whether the entered value is of type regexp. |
231| isSet(value: Object): boolean | Check whether the entered value is of type set. |
232| isSetIterator(value: Object): boolean | Check whether the entered value is the iterator type of set. |
233| isSharedArrayBuffer(value: Object): boolean | Check whether the entered value is of type sharedarraybuffer. |
234| isStringObject(value: Object): boolean | Check whether the entered value is a string object type. |
235| isSymbolObject(value: Object): boolean | Check whether the entered value is a symbol object type. |
236| isTypedArray(value: Object): boolean | Check whether the entered value is a type contained in typedarray. |
237| isUint8Array(value: Object): boolean | Check whether the entered value is the uint8array array type. |
238| isUint8ClampedArray(value: Object): boolean | Check whether the entered value is the uint8clapedarray array type. |
239| isUint16Array(value: Object): boolean | Check whether the entered value is the uint16array array array type. |
240| isUint32Array(value: Object): boolean | Check whether the entered value is the uint32array array type. |
241| isWeakMap(value: Object): boolean | Check whether the entered value is of type weakmap. |
242| isWeakSet(value: Object): boolean | Check whether the entered value is of type weakset. |
243
244Each specifier in printf is replaced with a converted value from the corresponding parameter. Supported specifiers are:
245| Stylized character | Style requirements |
246| -------- | -------- |
247| %s: | String will be used to convert all values except BigInt, Object and -0. |
248| %d: | Number will be used to convert all values except BigInt and Symbol. |
249| %i: | parseInt(value, 10) is used for all values except BigInt and Symbol. |
250| %f: | parseFloat(value) is used for all values expect Symbol. |
251| %j: | JSON. Replaced with the string '[Circular]' if the argument contains circular references. |
252| %o: | Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() with options { showHidden: true, showProxy: true }. This will show the full object including non-enumerable properties and proxies. |
253| %O: | Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() without options. This will show the full object not including non-enumerable properties and proxies. |
254| %c: | CSS. This specifier is ignored and will skip any CSS passed in. |
255| %%: | single percent sign ('%'). This does not consume an argument. |
256
257### Instruction for use
258
259
260The use methods of each interface are as follows:
261
2621.readonly encoding()
263
264```
265import util from '@ohos.util'
266var textEncoder = new util.TextEncoder();
267var getEncoding = textEncoder.encoding();
268```
2692.encode()
270```
271import util from '@ohos.util'
272var textEncoder = new util.TextEncoder();
273var result = textEncoder.encode('abc');
274```
2753.encodeInto()
276```
277import util from '@ohos.util'
278var textEncoder = new util.TextEncoder();
279var obj = textEncoder.encodeInto('abc', dest);
280```
2814.textDecoder()
282```
283import util from '@ohos.util'
284var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
285```
2865.readonly encoding()
287```
288import util from '@ohos.util'
289var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
290var getEncoding = textDecoder.encoding();
291```
2926.readonly fatal()
293```
294import util from '@ohos.util'
295var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
296var fatalStr = textDecoder.fatal();
297```
2987.readonly ignoreBOM()
299```
300import util from '@ohos.util'
301var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
302var ignoreBom = textDecoder.ignoreBOM();
303```
3048.decode()
305```
306import util from '@ohos.util'
307var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
308var result = textDecoder.decode(input, {stream : true});
309```
3109.printf()
311```
312import util from '@ohos.util'
313var format = "%%%o%%%i%s";
314var value = function aa(){};
315var value1 = 1.5;
316var value2 = "qwer";
317var result = util.printf(format,value,value1,value2);
318```
31910.getErrorString()
320```
321import util from '@ohos.util'
322var errnum = 13;
323var result = util.getErrorString(errnum);
324```
32511.callbackWrapper()
326```
327import util from '@ohos.util'
328async function promiseFn() {
329 return Promise.resolve('value');
330};
331var cb = util.callbackWrapper(promiseFn);
332cb((err, ret) => {
333 expect(err).strictEqual(null);
334 expect(ret).strictEqual('value');
335})
336```
33712.promiseWrapper()
338```
339import util from '@ohos.util'
340function aysnFun(str1, str2, callback) {
341 if (typeof str1 === 'string' && typeof str1 === 'string') {
342 callback(null, str1 + str2);
343 } else {
344 callback('type err');
345 }
346}
347let newPromiseObj = util.promiseWrapper(aysnFun)("Hello", 'World');
348newPromiseObj.then(res => {
349 expect(res).strictEqual('HelloWorld');
350})
351```
35213.encode()
353```
354import util from '@ohos.util'
355var that = new util.Base64();
356var array = new Uint8Array([115,49,51]);
357var result = that.encode(array);
358```
35914.encodeToString()
360```
361import util from '@ohos.util'
362var that = new util.Base64();
363var array = new Uint8Array([115,49,51]);
364var result = that.encodeToString(array);
365```
36615.decode()
367```
368import util from '@ohos.util'
369var that = new util.Base64()
370var buff = 'czEz';
371var result = that.decode(buff);
372```
37316.encodeAsync()
374```
375import util from '@ohos.util'
376var that = new util.Base64()
377var array = new Uint8Array([115,49,51]);
378await that.encodeAsync(array).then(val=>{
379})
380done()
381```
38217.encodeToStringAsync()
383```
384import util from '@ohos.util'
385var that = new util.Base64()
386var array = new Uint8Array([115,49,51]);
387await that.encodeToStringAsync(array).then(val=>{
388})
389done()
390```
39118.decodeAsync()
392```
393import util from '@ohos.util'
394var that = new util.Base64()
395var buff = 'czEz';
396await that.decodeAsync(buff).then(val=>{
397})
398done()
399```
40019.createRationalFromString()
401```
402import util from '@ohos.util'
403var pro = new util.RationalNumber(0, 0);
404var res = pro.createRationalFromString("-1:2");
405var result1 = res.value();
406```
40720.compareTo()
408```
409import util from '@ohos.util'
410var pro = new util.RationalNumber(2, 1);
411var proc = new util.RationalNumber(3, 4);
412var res = pro.compareTo(proc);
413```
41421.equals()
415```
416import util from '@ohos.util'
417var pro = new util.RationalNumber(2, 1);
418var proc = new util.RationalNumber(3, 4);
419var res = pro.equals(proc);
420```
42122.value()
422```
423import util from '@ohos.util'
424var pro = new util.RationalNumber(2, 1);
425var res = pro.value();
426```
42723.getCommonDivisor()
428```
429import util from '@ohos.util'
430var pro = new util.RationalNumber(0, 0);
431var res = pro.getCommonDivisor(4, 8);
432```
43324.getDenominator()
434```
435import util from '@ohos.util'
436var pro = new util.RationalNumber(2, 1);
437var res = pro.getDenominator();
438```
43925.getNumerator()
440```
441import util from '@ohos.util'
442var pro = new util.RationalNumber(-2, 1);
443var res = pro.getNumerator();
444```
44526.isFinite()
446```
447import util from '@ohos.util'
448var pro = new util.RationalNumber(-2, 1);
449var res = pro.isFinite();
450```
45127.isNaN()
452```
453import util from '@ohos.util'
454var pro = new util.RationalNumber(-2, 1);
455var res = pro.isNaN();
456```
45728.isZero()
458```
459import util from '@ohos.util'
460var pro = new util.RationalNumber(-2, 1);
461var res = pro.isZero();
462
463```
46429.toString()
465```
466import util from '@ohos.util'
467var pro = new util.RationalNumber(-2, 1);
468var res = pro.toString();
469
470```
47130.updateCapacity()
472```
473import util from '@ohos.util'
474var pro = new util.LruBuffer();
475var result = pro.updateCapacity(100);
476```
47731.toString()
478```
479import util from '@ohos.util'
480var pro = new util.LruBuffer();
481pro.put(2,10);
482pro.get(2);
483pro.remove(20);
484var result = pro.toString();
485```
48632.values()
487```
488import util from '@ohos.util'
489var pro = new util.LruBuffer();
490pro.put(2,10);
491pro.put(2,"anhu");
492pro.put("afaf","grfb");
493var result = pro.values();
494```
49533.size()
496```
497import util from '@ohos.util'
498var pro = new util.LruBuffer();
499pro.put(2,10);
500pro.put(1,8);
501var result = pro.size();
502```
50334.capacity()
504```
505import util from '@ohos.util'
506var pro = new util.LruBuffer();
507var result = pro.capacity();
508```
50935.clear()
510```
511import util from '@ohos.util'
512var pro = new util.LruBuffer();
513pro.put(2,10);
514pro.clear();
515```
51636.getCreateCount()
517```
518import util from '@ohos.util'
519var pro = new util.LruBuffer();
520pro.put(1,8);
521var result = pro.getCreateCount();
522```
52337.getMissCount()
524```
525import util from '@ohos.util'
526var pro = new util.LruBuffer();
527pro.put(2,10);
528pro.get(2)
529var result = pro.getMissCount();
530```
53138.getRemovalCount()
532```
533import util from '@ohos.util'
534var pro = new util.LruBuffer();
535pro.put(2,10);
536pro.updateCapacity(2);
537pro.put(50,22);
538var result = pro.getRemovalCount();
539```
54039.getMatchCount()
541```
542import util from '@ohos.util'
543var pro = new util.LruBuffer();
544pro.put(2,10);
545pro.get(2);
546var result = pro.getMatchCount();
547```
54840.getPutCount()
549```
550import util from '@ohos.util'
551var pro = new util.LruBuffer();
552pro.put(2,10);
553var result = pro.getPutCount();
554```
55541.isEmpty()
556```
557import util from '@ohos.util'
558var pro = new util.LruBuffer();
559pro.put(2,10);
560var result = pro.isEmpty();
561```
56242.get()
563```
564import util from '@ohos.util'
565var pro = new util.LruBuffer();
566pro.put(2,10);
567var result = pro.get(2);
568```
56943.put()
570```
571import util from '@ohos.util'
572var pro = new util.LruBuffer();
573var result = pro.put(2,10);
574```
57544.keys()
576```
577import util from '@ohos.util'
578var pro = new util.LruBuffer();
579pro.put(2,10);
580var result = pro.keys();
581```
58245.remove()
583```
584import util from '@ohos.util'
585var pro = new util.LruBuffer();
586pro.put(2,10);
587var result = pro.remove(20);
588```
58946.contains()
590```
591import util from '@ohos.util'
592var pro = new util.LruBuffer();
593pro.put(2,10);
594var result = pro.contains(20);
595```
59647.createDefault()
597```
598import util from '@ohos.util'
599var pro = new util.LruBuffer();
600var result = pro.createDefault(50);
601```
60248.entries()
603```
604import util from '@ohos.util'
605var pro = new util.LruBuffer();
606pro.put(2,10);
607var result = pro.entries();
608```
60949.\[Symbol.iterator\]()
610```
611import util from '@ohos.util'
612var pro = new util.LruBuffer();
613pro.put(2,10);
614var result = pro[symbol.iterator]();
615```
61650.afterRemoval()
617```
618import util from '@ohos.util'
619var arr = [];
620class ChildLruBuffer extends util.LruBuffer
621{
622 constructor()
623 {
624 super();
625 }
626 static getInstance()
627 {
628 if(this.instance == null)
629 {
630 this.instance = new ChildLruBuffer();
631 }
632 return this.instance;
633 }
634 afterRemoval(isEvict, key, value, newValue)
635 {
636 if (isEvict === false)
637 {
638 arr = [key, value, newValue];
639 }
640 }
641}
642ChildLruBuffer.getInstance().afterRemoval(false,10,30,null)
643```
644Construct a new class in the Scope interface to implement the compareTo method.
645
646```
647class Temperature {
648 constructor(value) {
649 this._temp = value;
650 }
651 compareTo(value) {
652 return this._temp >= value.getTemp();
653 }
654 getTemp() {
655 return this._temp;
656 }
657 toString() {
658 return this._temp.toString();
659 }
660}
661```
66251.constructor()
663```
664var tempLower = new Temperature(30);
665var tempUpper = new Temperature(40);
666var range = new Scope(tempLower, tempUpper);
667```
66852.toString()
669```
670var tempLower = new Temperature(30);
671var tempUpper = new Temperature(40);
672var range = new Scope(tempLower, tempUpper);
673var result = range.toString() // => [30,40]
674```
67553.intersect()
676```
677var tempLower = new Temperature(30);
678var tempUpper = new Temperature(40);
679var range = new Scope(tempLower, tempUpper);
680var tempMiDF = new Temperature(35);
681var tempMidS = new Temperature(39);
682var rangeFir = new Scope(tempMiDF, tempMidS);
683var result = range.intersect(rangeFir) // => [35,39]
684```
68554.intersect()
686```
687var tempLower = new Temperature(30);
688var tempUpper = new Temperature(40);
689var tempMiDF = new Temperature(35);
690var tempMidS = new Temperature(39);
691var range = new Scope(tempLower, tempUpper);
692var result = range.intersect(tempMiDF, tempMidS) // => [35,39]
693```
69455.getUpper()
695```
696var tempLower = new Temperature(30);
697var tempUpper = new Temperature(40);
698var range = new Scope(tempLower, tempUpper);
699var result = range.getUpper() // => 40
700```
70156.getLower()
702```
703var tempLower = new Temperature(30);
704var tempUpper = new Temperature(40);
705var range = new Scope(tempLower, tempUpper);
706var result = range.getLower() // => 30
707```
70857.expand()
709```
710var tempLower = new Temperature(30);
711var tempUpper = new Temperature(40);
712var tempMiDF = new Temperature(35);
713var tempMidS = new Temperature(39);
714var range = new Scope(tempLower, tempUpper);
715var result = range.expand(tempMiDF, tempMidS) // => [30,40]
716```
71758.expand()
718```
719var tempLower = new Temperature(30);
720var tempUpper = new Temperature(40);
721var tempMiDF = new Temperature(35);
722var tempMidS = new Temperature(39);
723var range = new Scope(tempLower, tempUpper);
724var rangeFir = new Scope(tempMiDF, tempMidS);
725var result = range.expand(rangeFir) // => [30,40]
726```
72759.expand()
728```
729var tempLower = new Temperature(30);
730var tempUpper = new Temperature(40);
731var tempMiDF = new Temperature(35);
732var range = new Scope(tempLower, tempUpper);
733var result = range.expand(tempMiDF) // => [30,40]
734```
73560.contains()
736```
737var tempLower = new Temperature(30);
738var tempUpper = new Temperature(40);
739var tempMiDF = new Temperature(35);
740var range = new Scope(tempLower, tempUpper);
741var result = range.contains(tempMiDF) // => true
742```
74361.contains()
744```
745var tempLower = new Temperature(30);
746var tempUpper = new Temperature(40);
747var range = new Scope(tempLower, tempUpper);
748var tempLess = new Temperature(20);
749var tempMore = new Temperature(45);
750var rangeSec = new Scope(tempLess, tempMore);
751var result = range.contains(rangeSec) // => true
752```
75362.clamp()
754```
755var tempLower = new Temperature(30);
756var tempUpper = new Temperature(40);
757var tempMiDF = new Temperature(35);
758var range = new Scope(tempLower, tempUpper);
759var result = range.clamp(tempMiDF) // => 35
760```
76163.isAnyArrayBuffer()
762```
763import util from '@ohos.util'
764var proc = new util.Types();
765var result = proc.isAnyArrayBuffer(new ArrayBuffer([]))
766```
76764.isArrayBufferView()
768```
769import util from '@ohos.util'
770var proc = new util.Types();
771var result = proc.isArrayBufferView(new DataView(new ArrayBuffer(16)));
772```
77365.isArgumentsObject()
774```
775import util from '@ohos.util'
776function foo() {
777 var result = proc.isArgumentsObject(arguments);
778 }
779var f = foo();
780```
78166.isArrayBuffer()
782```
783import util from '@ohos.util'
784var proc = new util.Types();
785var result = proc.isArrayBuffer(new ArrayBuffer([]));
786```
78767.isAsyncFunction()
788```
789import util from '@ohos.util'
790var proc = new util.Types();
791var result = proc.isAsyncFunction(async function foo() {});
792```
79368.isBigInt64Array()
794```
795import util from '@ohos.util'
796var proc = new util.Types();
797var result = proc.isBigInt64Array(new Int16Array([]));
798```
79969.isBigUint64Array()
800```
801import util from '@ohos.util'
802var proc = new util.Types();
803var result = proc.isBigUint64Array(new Int16Array([]));
804```
80570.isBooleanObject()
806```
807import util from '@ohos.util'
808var proc = new util.Types();
809var result = proc.isBooleanObject(new Boolean(false));
810```
81171.isBoxedPrimitive()
812```
813import util from '@ohos.util'
814var proc = new util.Types();
815var result = proc.isBoxedPrimitive(new Boolean(false));
816```
81772.isCryptoKey()
818```
819import util from '@ohos.util'
820var proc = new util.Types();
821var result = proc.isCryptoKey(false);
822```
82373.isDataView()
824```
825import util from '@ohos.util'
826var proc = new util.Types();
827const ab = new ArrayBuffer(20);
828var result = proc.isDataView(new DataView(ab));
829```
83074.isDate()
831```
832import util from '@ohos.util'
833var proc = new util.Types();
834var result = proc.isDate(new Date());
835```
83675.isExternal()
837```
838import util from '@ohos.util'
839const data = util.createExternalType();
840var reult13 = proc.isExternal(data);
841```
84276.isFloat32Array()
843```
844import util from '@ohos.util'
845var proc = new util.Types();
846var result = proc.isFloat32Array(new Float32Array([]));
847```
84877.isFloat64Array()
849```
850import util from '@ohos.util'
851var proc = new util.Types();
852var result = proc.isFloat64Array(new Float64Array([]));
853```
85478.isGeneratorFunction()
855```
856import util from '@ohos.util'
857var proc = new util.Types();
858var result = proc.isGeneratorFunction(function* foo() {});
859```
86079.isGeneratorObject()
861```
862import util from '@ohos.util'
863var proc = new util.Types();
864function* foo() {}
865const generator = foo();
866var result = proc.isGeneratorObject(generator);
867```
86880.isInt8Array()
869```
870import util from '@ohos.util'
871var proc = new util.Types();
872var result = proc.isInt8Array(new Int8Array([]));
873```
87481.isInt16Array()
875```
876import util from '@ohos.util'
877var proc = new util.Types();
878var result = proc.isInt16Array(new Int16Array([]));
879```
88082.isInt32Array()
881```
882import util from '@ohos.util'
883var proc = new util.Types();
884var result = proc.isInt32Array(new Int32Array([]));
885```
88683.isKeyObject()
887```
888import util from '@ohos.util'
889var proc = new util.Types();
890var result = proc.isKeyObject(0);
891```
89284.isMap()
893```
894import util from '@ohos.util'
895var proc = new util.Types();
896var result = proc.isMap(new Map());
897```
89885.isMapIterator()
899```
900import util from '@ohos.util'
901var proc = new util.Types();
902var result = proc.isMapIterator(map.keys());
903```
90486.isModuleNamespaceObject()
905```
906import util from '@ohos.util'
907var proc = new util.Types();
908var result = proc.isModuleNamespaceObject(util);
909```
91087.isNativeError()
911```
912import util from '@ohos.util'
913var proc = new util.Types();
914var result = proc.isNativeError(new TypeError());
915```
91688.isNumberObject()
917```
918import util from '@ohos.util'
919var proc = new util.Types();
920var result = proc.isNumberObject(new Number(0));
921```
92289.isPromise()
923```
924import util from '@ohos.util'
925var proc = new util.Types();
926var result = proc.isPromise(Promise.resolve(42));
927```
92890.isProxy()
929```
930import util from '@ohos.util'
931var proc = new util.Types();
932const target = {};
933const proxy = new Proxy(target, {});
934var result = proc.isProxy(proxy);
935```
93691.isRegExp()
937```
938import util from '@ohos.util'
939var proc = new util.Types();
940var result = proc.isRegExp(new RegExp('abc'));
941```
94292.isSet()
943```
944import util from '@ohos.util'
945var proc = new util.Types();
946var result = proc.isSet(new Set());
947```
94893.isSetIterator()
949```
950import util from '@ohos.util'
951var proc = new util.Types();
952const set = new Set();
953var result = proc.isSetIterator(set.keys());
954```
95594.isSharedArrayBuffer()
956```
957import util from '@ohos.util'
958var proc = new util.Types();
959var result = proc.isSharedArrayBuffer(new ArrayBuffer([]));
960```
96195.isStringObject()
962```
963import util from '@ohos.util'
964var proc = new util.Types();
965var result = proc.isStringObject(new String('foo'));
966```
96796.isSymbolObject()
968```
969import util from '@ohos.util'
970var proc = new util.Types();
971const symbols = Symbol('foo');
972var result = proc.isSymbolObject(Object(symbols));
973```
97497.isTypedArray()
975```
976import util from '@ohos.util'
977var proc = new util.Types();
978var result = proc.isTypedArray(new Float64Array([]));
979```
98098.isUint8Array()
981```
982import util from '@ohos.util'
983var proc = new util.Types();
984var result = proc.isUint8Array(new Uint8Array([]));
985```
98699.isUint8ClampedArray()
987```
988import util from '@ohos.util'
989var proc = new util.Types();
990var result = proc.isUint8ClampedArray(new Uint8ClampedArray([]));
991```
992100.isUint16Array()
993```
994import util from '@ohos.util'
995var proc = new util.Types();
996var result = proc.isUint16Array(new Uint16Array([]));
997```
998101.isUint32Array()
999```
1000import util from '@ohos.util'
1001var proc = new util.Types();
1002var result = proc.isUint32Array(new Uint32Array([]));
1003```
1004102.isWeakMap()
1005```
1006import util from '@ohos.util'
1007var proc = new util.Types();
1008var result = proc.isWeakMap(new WeakMap());
1009```
1010103.isWeakSet()
1011```
1012import util from '@ohos.util'
1013var proc = new util.Types();
1014var result = proc.isWeakSet(new WeakSet());
1015```
1016## Related warehouse
1017
1018[js_util_module subsystem](https://gitee.com/OHOS_STD/js_util_module)
1019
1020[base/compileruntime/js_util_module/](base/compileruntime/js_util_module-readme.md)
1021
1022## License
1023
1024Util is available under [Mozilla license](https://www.mozilla.org/en-US/MPL/), and the documentation is detailed in [documentation](https://gitee.com/openharmony/js_util_module/blob/master/mozilla_docs.txt). See [LICENSE](https://gitee.com/openharmony/js_util_module/blob/master/LICENSE) for the full license text.
README_zh.md
1# js_util_module子系统/组件
2
3- [简介](#简介)
4- [目录](#目录)
5- [说明](#说明)
6 - [接口说明](#接口说明)
7 - [使用说明](#使用说明)
8
9- [相关仓](#相关仓)
10
11## 简介
12
13UTIL接口用于字符编码TextEncoder、解码TextDecoder、帮助函数HelpFunction、基于Base64的字节编码encode和解码decode、有理数RationalNumber。TextEncoder表示一个文本编码器,接受字符串作为输入,以UTF-8格式进行编码,输出UTF-8字节流。TextDecoder接口表示一个文本解码器,解码器将字节流作为输入,输出stirng字符串。HelpFunction主要是对函数做callback化、promise化以及对错误码进行编写输出,及类字符串的格式化输出。encode接口使用Base64编码方案将指定u8数组中的所有字节编码到新分配的u8数组中或者使用Base64编码方案将指定的字节数组编码为String。decode接口使用Base64编码方案将Base64编码的字符串或输入u8数组解码为新分配的u8数组。RationalNumber有理数主要是对有理数进行比较,获取分子分母等方法。LruBuffer该算法在缓存空间不够的时候,将近期最少使用的数据替换为新数据。该算法源自这样一种访问资源的需求:近期访问的数据,可能在不久的将来会再次访问。于是最少访问的数据就是价值最小的,是最应该踢出缓存空间的数据。Scope接口用于描述一个字段的有效范围。 Scope实例的构造函数用于创建具有指定下限和上限的对象,并要求这些对象必须具有可比性。
14## 目录
15
16```
17base/compileruntime/js_util_module/
18├── Class:TextEncoder # TextEncoder类
19│ ├── new TextEncoder() # 创建TextEncoder对象
20│ ├── encode() # encode方法
21│ ├── encoding # encoding属性
22│ └── encodeInto() # encodeInto方法
23├── Class:TextDecoder # TextDecoder类
24│ ├── new TextDecoder() # 创建TextDecoder对象
25│ ├── decode() # decode方法
26│ ├── encoding # encoding属性
27│ ├── fatal # fatal属性
28│ └── ignoreBOM # ignoreBOM属性
29├── printf() # printf方法
30├── getErrorString() # getErrorString方法
31├── callbackWrapper() # callbackWrapper方法
32├── promiseWrapper() # promiseWrapper方法
33├── Class:Base64 # Base64类
34│ ├── new Base64() # 创建Base64对象
35│ ├── encode() # encode方法
36│ ├── encodeToString() # encodeToString方法
37│ ├── decode() # decode方法
38│ ├── encodeAsync() # encodeAsync方法
39│ ├── encodeToStringAsync() # encodeToStringAsync方法
40│ └── decodeAsync() # decodeAsync方法
41├── Class:RationalNumber # RationalNumber类
42│ ├── new RationalNumber() # 创建RationalNumber对象
43│ ├── createRationalFromString() # createRationalFromString方法
44│ ├── compareTo() # compareTo方法
45│ ├── equals() # equals方法
46│ ├── value() # value方法
47│ ├── getCommonDivisor() # getCommonDivisor方法
48│ ├── getDenominator() # getDenominator方法
49│ ├── getNumerator() # getNumerator方法
50│ ├── isFinite() # isFinite方法
51│ ├── isNaN() # isNaN方法
52│ ├── isZero() # isZero方法
53│ └── toString() # toString方法
54├── Class:LruBuffer # LruBuffer类
55│ ├── new LruBuffer() # 创建LruBuffer对象
56│ ├── updateCapacity() # updateCapacity方法
57│ ├── toString() # toString方法
58│ ├── values() # values方法
59│ ├── size() # size方法
60│ ├── capacity() # capacity方法
61│ ├── clear() # clear方法
62│ ├── getCreateCount() # getCreateCount方法
63│ ├── getMissCount() # getMissCount方法
64│ ├── getRemovalCount() # getRemovalCount方法
65│ ├── getMatchCount() # getMatchCount方法
66│ ├── getPutCount() # getPutCount方法
67│ ├── isEmpty() # isEmpty方法
68│ ├── get() # get方法
69│ ├── put() # put方法
70│ ├── keys() # keys方法
71│ ├── remove() # remove方法
72│ ├── afterRemoval() # afterRemoval方法
73│ ├── contains() # contains方法
74│ ├── createDefault() # createDefault方法
75│ ├── entries() # entries方法
76│ └── [Symbol.iterator]() # Symboliterator方法
77|—— Class:Scope # Scope类
78| ├── constructor() # 创建Scope对象
79| ├── toString() # toString方法
80| ├── intersect() # intersect方法
81| ├── intersect() # intersect方法
82| ├── getUpper() # getUpper方法
83| ├── getLower() # getLower方法
84| ├── expand() # expand方法
85| ├── expand() # expand方法
86| ├── expand() # expand法
87| ├── contains() # contains方法
88| ├── contains() # contains方法
89| └── clamp() # clamp方法
90└── Class:Types # Types类
91 ├── isAnyArrayBuffer() # isAnyArrayBuffer方法
92 ├── isArrayBufferView() # isArrayBufferView方法
93 ├── isArgumentsObject() # isArgumentsObject方法
94 ├── isArrayBuffer() # isArrayBuffer方法
95 ├── isAsyncFunction() # isAsyncFunction方法
96 ├── isBigInt64Array() # isBigInt64Array方法
97 ├── isBigUint64Array() # isBigUint64Array方法
98 ├── isBooleanObject() # isBooleanObject方法
99 ├── isBoxedPrimitive() # isBoxedPrimitive方法
100 ├── isCryptoKey() # isCryptoKey方法
101 ├── isDataView() # isDataView方法
102 ├── isDate() # isDate方法
103 ├── isExternal() # isExternal方法
104 ├── isFloat32Array() # isFloat32Array方法
105 ├── isFloat64Array() # isFloat64Array方法
106 ├── isGeneratorFunction() # isGeneratorFunction方法
107 ├── isGeneratorObject() # isGeneratorObject方法
108 ├── isInt8Array() # isInt8Array方法
109 ├── isInt16Array() # isInt16Array方法
110 ├── isInt32Array() # isInt32Array方法
111 ├── isKeyObject() # isKeyObject方法
112 ├── isMap() # isMap方法
113 ├── isMapIterator() # isMapIterator方法
114 ├── isModuleNamespaceObject() # isModuleNamespaceObject方法
115 ├── isNativeError() # isNativeError方法
116 ├── isNumberObject() # isNumberObject方法
117 ├── isPromise() # isPromise方法
118 ├── isProxy() # isProxy方法
119 ├── isRegExp() # isRegExp方法
120 ├── isSet() # isSet方法
121 ├── isSetIterator() # isSetIterator方法
122 ├── isSharedArrayBuffer() # isSharedArrayBuffer方法
123 ├── isStringObject() # isStringObject方法
124 ├── isSymbolObject() # isSymbolObject方法
125 ├── isTypedArray() # isTypedArray方法
126 ├── isUint8Array() # isUint8Array方法
127 ├── isUint8ClampedArray() # isUint8ClampedArray方法
128 ├── isUint16Array() # isUint16Array方法
129 ├── isUint32Array() # isUint32Array方法
130 ├── isWeakMap() # isWeakMap方法
131 └── isWeakSet() # isWeakSet方法
132```
133## 说明
134
135### 接口说明
136
137
138| 接口名 | 说明 |
139| -------- | -------- |
140| readonly encoding : string | 在TextEncoder类中,获取编码的格式,只支持UTF-8。 |
141| encode(input : string) : Uint8Array | 输入stirng字符串,编码并输出UTF-8字节流。 |
142| encodeInto(input : string, dest : Uint8Array) : {read : number, written : number} | 输入stirng字符串,dest表示编码后存放位置,返回一个对象,read表示已经编码的字符的个数,written表示已编码字符所占字节的大小。 |
143| constructor(encoding? : string, options? : {fatal? : boolean, ignoreBOM? : boolean}) | 构造函数,第一个参数encoding表示解码的格式。第二个参数表示一些属性。属性中fatal表示是否抛出异常,ignoreBOM表示是否忽略bom标志。 |
144| readonly encoding : string | 在TextDecoder类中,获取设置的解码格式。 |
145| readonly fatal : boolean | 获取抛出异常的设置。 |
146| readonly ignoreBOM : boolean | 获取是否忽略bom标志的设置。 |
147| decode(input : Uint8Array, options?: { stream?: false }) : string | 输入要解码的数据,解出对应的string字符串。第一个参数input表示要解码的数据,第二个参数options表示一个bool标志,表示将跟随附加数据,默认为false。 |
148| encode(src: Uint8Array): Uint8Array; | 使用Base64编码方案将指定u8数组中的所有字节编码到新分配的u8数组中。 |
149| encodeToString(src: Uint8Array): string; | 使用Base64编码方案将指定的字节数组编码为String。 |
150| decode(src: Uint8Array \| string): Uint8Array; | 使用Base64编码方案将Base64编码的字符串或输入u8数组解码为新分配的u8数组。 |
151| encodeAsync(src: Uint8Array): Promise\<Uint8Array\>; | 使用Base64编码方案将指定u8数组中的所有字节异步编码到新分配的u8数组中。 |
152| encodeToStringAsync(src: Uint8Array): Promise\<string\>; | 使用Base64编码方案将指定的字节数组异步编码为String。 |
153| decodeAsync(src: Uint8Array \| string): Promise\<Uint8Array\>; | 使用Base64编码方案将Base64编码的字符串或输入u8数组异步解码为新分配的u8数组。 |
154| static createRationalFromString(rationalString: string): RationalNumber | 基于给定的字符串创建一个RationalNumber对象。 |
155| compareTo(another: RationalNumber): number | 将当前的RationalNumber对象与给定的对象进行比较。 |
156| equals(obj: object): number | 检查给定对象是否与当前 RationalNumber 对象相同。 |
157| value(): number | 将当前的RationalNumber对象进行取整数值或者浮点数值。 |
158| static getCommonDivisor(number1: number, number2: number,): number | 获得两个指定数的最大公约数。 |
159| getDenominator(): number | 获取当前的RationalNumber对象的分母。 |
160| getNumerator(): number | 获取当前的RationalNumber对象的分子。 |
161| isFinite(): boolean | 检查当前的RationalNumber对象是有限的。 |
162| isNaN(): boolean | 检查当前RationalNumber对象是否表示非数字(NaN)值。 |
163| isZero(): boolean | 检查当前RationalNumber对象是否表示零值。 |
164| toString(): string | 获取当前RationalNumber对象的字符串表示形式。 |
165| constructor(capacity?: number) | 创建默认构造函数用于创建一个新的LruBuffer实例,默认容量为64。 |
166| updateCapacity(newCapacity: number): void | 将缓冲区容量更新为指定容量,如果 newCapacity 小于或等于 0,则抛出此异常。 |
167| toString(): string | 返回对象的字符串表示形式,输出对象的字符串表示 |
168| values(): V[] | 获取当前缓冲区中所有值的列表,输出按升序返回当前缓冲区中所有值的列表,从最近访问到最近最少访问。 |
169| size(): number | 获取当前缓冲区中值的总数,输出返回当前缓冲区中值的总数。 |
170| capacity(): number | 获取当前缓冲区的容量,输出返回当前缓冲区的容量。 |
171| clear(): void | 从当前缓冲区清除键值对,清除键值对后,调用afterRemoval()方法依次对其执行后续操作。 |
172| getCreateCount(): number | 获取createDefault()返回值的次数,输出返回createDefault()返回值的次数。 |
173| getMissCount(): number | 获取查询值不匹配的次数,输出返回查询值不匹配的次数。 |
174| getRemovalCount(): number | 获取从缓冲区中逐出值的次数,输出从缓冲区中驱逐的次数。 |
175| getMatchCount(): number | 获取查询值匹配成功的次数,输出返回查询值匹配成功的次数。 |
176| getPutCount(): number | 获取将值添加到缓冲区的次数,输出返回将值添加到缓冲区的次数。 |
177| isEmpty(): boolean | 检查当前缓冲区是否为空,输出如果当前缓冲区不包含任何值,则返回 true 。 |
178| get(key: K) : V \| undefined | 表示要查询的键,输出如果指定的键存在于缓冲区中,则返回与键关联的值;否则返回undefined。 |
179| put(key: K , value: V): V | 将键值对添加到缓冲区,输出与添加的键关联的值;如果要添加的键已经存在,则返回原始值,如果键或值为空,则抛出此异常。 |
180| keys(): K[ ] | 获取当前缓冲区中值的键列表,输出返回从最近访问到最近最少访问排序的键列表。 |
181| remove(key: K): V \| undefined | 从当前缓冲区中删除指定的键及其关联的值。 |
182| afterRemoval(isEvict: boolean, key: K, value : V, newValue : V):void | 删除值后执行后续操作。 |
183| contains(key: K): boolean | 检查当前缓冲区是否包含指定的键,输出如果缓冲区包含指定的键,则返回 true 。 |
184| createDefault(key: K): V | 如果未计算特定键的值,则执行后续操作,参数表示丢失的键,输出返回与键关联的值。 |
185| entries(): [K,V] | 允许迭代包含在这个对象中的所有键值对。每对的键和值都是对象。 |
186| \[Symbol.iterator\](): [K,V] | 返回以键值对得形式得一个二维数组。 |
187| constructor(lowerObj: ScopeType, upperObj : ScopeType) | 创建并返回一个Scope对象,用于创建指定下限和上限的作用域实例的构造函数。 |
188| toString(): string | 该字符串化方法返回一个包含当前范围的字符串表示形式。 |
189| intersect(range: Scope): Scope | 获取给定范围和当前范围的交集。 |
190| intersect(lowerObj: ScopeType, upperObj: ScopeType): Scope | 获取当前范围与给定下限和上限范围的交集。 |
191| getUpper(): ScopeType | 获取当前范围的上限。 |
192| getLower(): ScopeType | 获取当前范围的下限。 |
193| expand(lowerObj: ScopeType, upperObj: ScopeType): Scope | 创建并返回包括当前范围和给定下限和上限的并集。 |
194| expand(range: Scope): Scope | 创建并返回包括当前范围和给定范围的并集。 |
195| expand(value: ScopeType): Scope | 创建并返回包括当前范围和给定值的并集。 |
196| contains(value: ScopeType): boolean | 检查给定value是否包含在当前范围内。 |
197| contains(range: Scope): boolean | 检查给定range是否在当前范围内。 |
198| clamp(value: ScopeType): ScopeType | 将给定value限定到当前范围内。 |
199| function printf(format: string, ...args: Object[]): string | printf()方法使用第一个参数作为格式字符串(其可以包含零个或多个格式说明符)来返回格式化的字符串。 |
200| function getErrorString(errno: number): string | getErrorString()方法使用一个系统的错误数字作为参数,用来返回系统的错误信息。 |
201| function callbackWrapper(original: Function): (err: Object, value: Object) => void | 参数为一个采用 async 函数(或返回 Promise 的函数)并返回遵循错误优先回调风格的函数,即将 (err, value) => ... 回调作为最后一个参数。 在回调中,第一个参数将是拒绝原因(如果 Promise 已解决,则为 null),第二个参数将是已解决的值。 |
202| function promiseWrapper(original: (err: Object, value: Object) => void): Object | 参数为采用遵循常见的错误优先的回调风格的函数(也就是将 (err, value) => ... 回调作为最后一个参数),并返回一个返回 promise 的版本。 |
203| isAnyArrayBuffer(value: Object): boolean | 检查输入的value是否是ArrayBuffer或SharedArrayBuffer类型。 |
204| isArrayBufferView(value: Object): boolean | 检查输入的value是否是napi_int8_array或napi_uint8_array或napi_uint8_clamped_array或napi_int16_array或napi_uint16_array或napi_int32_array或napi_uint32_array或napi_float32_array或napi_float64_array数组或DataView类型。 |
205| isArgumentsObject(value: Object): boolean | 检查输入的value是否是一个arguments对象类型。 |
206| isArrayBuffer(value: Object): boolean | 检查输入的value是否是ArrayBuffer类型。 |
207| isAsyncFunction(value: Object): boolean | 检查输入的value是否是异步函数类型。 |
208| isBigInt64Array(value: Object): boolean | 检查输入的value是否是BigInt64Array数组类型。 |
209| isBigUint64Array(value: Object): boolean | 检查输入的value是否是BigUint64Array数组类型。 |
210| isBooleanObject(value: Object): boolean | 检查输入的value是否是一个布尔对象类型。 |
211| isBoxedPrimitive(value: Object): boolean | 检查输入的value是否是Boolean或Number或String或Symbol对象类型。 |
212| isCryptoKey(value: Object): boolean | 检查输入的value是否是CryptoKey对象类型。 |
213| isDataView(value: Object): boolean | 检查输入的value是否是DataView类型。 |
214| isDate(value: Object): boolean | 检查输入的value是否是Date类型。 |
215| isExternal(value: Object): boolean | 检查输入的value是否是一个native External值类型。 |
216| isFloat32Array(value: Object): boolean | 检查输入的value是否是Float32Array数组类型。 |
217| isFloat64Array(value: Object): boolean | 检查输入的value是否是Float64Array数组类型。 |
218| isGeneratorFunction(value: Object): boolean | 检查输入的value是否是一个generator函数类型。 |
219| isGeneratorObject(value: Object): boolean | 检查输入的value是否是一个generator对象类型。 |
220| isInt8Array(value: Object): boolean | 检查输入的value是否是Int8Array数组类型。 |
221| isInt16Array(value: Object): boolean | 检查输入的value是否是Int16Array数组类型。 |
222| isInt32Array(value: Object): boolean | 检查输入的value是否是Int32Array数组类型。 |
223| isKeyObject(value: Object): boolean | 检查输入的value是否是KeyObject对象类型。 |
224| isMap(value: Object): boolean | 检查输入的value是否是Map类型。 |
225| isMapIterator(value: Object): boolean | 检查输入的value是否是Map的iterator类型。 |
226| isModuleNamespaceObject(value: Object): boolean | 检查输入的value是否是Module Namespace Object对象类型。 |
227| isNativeError(value: Object): boolean | 检查输入的value是否是Error类型。 |
228| isNumberObject(value: Object): boolean | 检查输入的value是否是Number对象类型。 |
229| isPromise(value: Object): boolean | 检查输入的value是否是Promise类型。 |
230| isProxy(value: Object): boolean | 检查输入的value是否是Proxy类型。 |
231| isRegExp(value: Object): boolean | 检查输入的value是否是RegExp类型。 |
232| isSet(value: Object): boolean | 检查输入的value是否是Set类型。 |
233| isSetIterator(value: Object): boolean | 检查输入的value是否是Set的iterator类型。 |
234| isSharedArrayBuffer(value: Object): boolean | 检查输入的value是否是SharedArrayBuffer类型。 |
235| isStringObject(value: Object): boolean | 检查输入的value是否是一个String对象类型。 |
236| isSymbolObject(value: Object): boolean | 检查输入的value是否是一个Symbol对象类型。 |
237| isTypedArray(value: Object): boolean | 检查输入的value是否是TypedArray包含的类型。 |
238| isUint8Array(value: Object): boolean | 检查输入的value是否是Uint8Array数组类型。 |
239| isUint8ClampedArray(value: Object): boolean | 检查输入的value是否是Uint8ClampedArray数组类型。 |
240| isUint16Array(value: Object): boolean | 检查输入的value是否是Uint16Array数组类型。 |
241| isUint32Array(value: Object): boolean | 检查输入的value是否是Uint32Array数组类型。 |
242| isWeakMap(value: Object): boolean | 检查输入的value是否是WeakMap类型。 |
243| isWeakSet(value: Object): boolean | 检查输入的value是否是WeakSet类型。 |
244
245printf中每个说明符都替换为来自相应参数的转换后的值。 支持的说明符有:
246| 式样化字符 | 式样要求 |
247| -------- | -------- |
248| %s: | String 将用于转换除 BigInt、Object 和 -0 之外的所有值。|
249| %d: | Number 将用于转换除 BigInt 和 Symbol 之外的所有值。|
250| %i: | parseInt(value, 10) 用于除 BigInt 和 Symbol 之外的所有值。|
251| %f: | parseFloat(value) 用于除 Symbol 之外的所有值。|
252| %j: | JSON。 如果参数包含循环引用,则替换为字符串 '[Circular]'。|
253| %o: | Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。类似于具有选项 { showHidden: true, showProxy: true } 的 util.inspect()。这将显示完整的对象,包括不可枚举的属性和代理。|
254| %O: | Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。类似于没有选项的 util.inspect()。 这将显示完整的对象,但不包括不可枚举的属性和代理。|
255| %c: | 此说明符被忽略,将跳过任何传入的 CSS 。|
256| %%: | 单个百分号 ('%')。 这不消耗待式样化参数。|
257
258### 使用说明
259各接口使用方法如下:
260
2611.readonly encoding()
262
263```
264import util from '@ohos.util'
265var textEncoder = new util.TextEncoder();
266var getEncoding = textEncoder.encoding();
267```
2682.encode()
269```
270import util from '@ohos.util'
271var textEncoder = new util.TextEncoder();
272var result = textEncoder.encode('abc');
273```
2743.encodeInto()
275```
276import util from '@ohos.util'
277var textEncoder = new util.TextEncoder();
278var obj = textEncoder.encodeInto('abc', dest);
279```
2804.textDecoder()
281```
282import util from '@ohos.util'
283var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
284```
2855.readonly encoding()
286```
287import util from '@ohos.util'
288var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
289var getEncoding = textDecoder.encoding();
290```
2916.readonly fatal()
292```
293import util from '@ohos.util'
294var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
295var fatalStr = textDecoder.fatal();
296```
2977.readonly ignoreBOM()
298```
299import util from '@ohos.util'
300var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
301var ignoreBom = textDecoder.ignoreBOM();
302```
3038.decode()
304```
305import util from '@ohos.util'
306var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
307var result = textDecoder.decode(input, {stream : true});
308```
3099.printf()
310```
311import util from '@ohos.util'
312var format = "%%%o%%%i%s";
313var value = function aa(){};
314var value1 = 1.5;
315var value2 = "qwer";
316var result = util.printf(format,value,value1,value2);
317```
31810.getErrorString()
319```
320import util from '@ohos.util'
321var errnum = 13;
322var result = util.getErrorString(errnum);
323```
32411.callbackWrapper()
325```
326import util from '@ohos.util'
327async function promiseFn() {
328 return Promise.resolve('value');
329};
330var cb = util.callbackWrapper(promiseFn);
331cb((err, ret) => {
332 expect(err).strictEqual(null);
333 expect(ret).strictEqual('value');
334})
335```
33612.promiseWrapper()
337```
338import util from '@ohos.util'
339function aysnFun(str1, str2, callback) {
340 if (typeof str1 === 'string' && typeof str1 === 'string') {
341 callback(null, str1 + str2);
342 } else {
343 callback('type err');
344 }
345}
346let newPromiseObj = util.promiseWrapper(aysnFun)("Hello", 'World');
347newPromiseObj.then(res => {
348 expect(res).strictEqual('HelloWorld');
349})
350```
35113.encode()
352```
353import util from '@ohos.util'
354var that = new util.Base64();
355var array = new Uint8Array([115,49,51]);
356var result = that.encode(array);
357```
35814.encodeToString()
359```
360import util from '@ohos.util'
361var that = new util.Base64();
362var array = new Uint8Array([115,49,51]);
363var result = that.encodeToString(array);
364```
36515.decode()
366```
367import util from '@ohos.util'
368var that = new util.Base64()
369var buff = 'czEz';
370var result = that.decode(buff);
371
372```
37316.encodeAsync()
374```
375import util from '@ohos.util'
376var that = new util.Base64()
377var array = new Uint8Array([115,49,51]);
378await that.encodeAsync(array).then(val=>{
379})
380done()
381```
38217.encodeToStringAsync()
383```
384import util from '@ohos.util'
385var that = new util.Base64()
386var array = new Uint8Array([115,49,51]);
387await that.encodeToStringAsync(array).then(val=>{
388})
389done()
390```
39118.decodeAsync()
392```
393import util from '@ohos.util'
394var that = new util.Base64()
395var buff = 'czEz';
396await that.decodeAsync(buff).then(val=>{
397})
398done()
399```
40019.createRationalFromString()
401```
402import util from '@ohos.util'
403var pro = new util.RationalNumber(0, 0);
404var res = pro.createRationalFromString("-1:2");
405var result1 = res.value();
406```
40720.compareTo()
408```
409import util from '@ohos.util'
410var pro = new util.RationalNumber(2, 1);
411var proc = new util.RationalNumber(3, 4);
412var res = pro.compareTo(proc);
413```
41421.equals()
415```
416import util from '@ohos.util'
417var pro = new util.RationalNumber(2, 1);
418var proc = new util.RationalNumber(3, 4);
419var res = pro.equals(proc);
420```
42122.value()
422```
423import util from '@ohos.util'
424var pro = new util.RationalNumber(2, 1);
425var res = pro.value();
426```
42723.getCommonDivisor()
428```
429import util from '@ohos.util'
430var pro = new util.RationalNumber(0, 0);
431var res = pro.getCommonDivisor(4, 8);
432```
43324.getDenominator()
434```
435import util from '@ohos.util'
436var pro = new util.RationalNumber(2, 1);
437var res = pro.getDenominator();
438```
43925.getNumerator()
440```
441import util from '@ohos.util'
442var pro = new util.RationalNumber(-2, 1);
443var res = pro.getNumerator();
444```
44526.isFinite()
446```
447import util from '@ohos.util'
448var pro = new util.RationalNumber(-2, 1);
449var res = pro.isFinite();
450```
45127.isNaN()
452```
453import util from '@ohos.util'
454var pro = new util.RationalNumber(-2, 1);
455var res = pro.isNaN();
456```
45728.isZero()
458```
459import util from '@ohos.util'
460var pro = new util.RationalNumber(-2, 1);
461var res = pro.isZero();
462
463```
46429.toString()
465```
466import util from '@ohos.util'
467var pro = new util.RationalNumber(-2, 1);
468var res = pro.toString();
469
470```
47130.updateCapacity()
472```
473import util from '@ohos.util'
474var pro = new util.LruBuffer();
475var result = pro.updateCapacity(100);
476```
47731.toString()
478```
479import util from '@ohos.util'
480var pro = new util.LruBuffer();
481pro.put(2,10);
482pro.get(2);
483pro.remove(20);
484var result = pro.toString();
485```
48632.values()
487```
488import util from '@ohos.util'
489var pro = new util.LruBuffer();
490pro.put(2,10);
491pro.put(2,"anhu");
492pro.put("afaf","grfb");
493var result = pro.values();
494```
49533.size()
496```
497import util from '@ohos.util'
498var pro = new util.LruBuffer();
499pro.put(2,10);
500pro.put(1,8);
501var result = pro.size();
502```
50334.capacity()
504```
505import util from '@ohos.util'
506var pro = new util.LruBuffer();
507var result = pro.capacity();
508```
50935.clear()
510```
511import util from '@ohos.util'
512var pro = new util.LruBuffer();
513pro.put(2,10);
514pro.clear();
515```
51636.getCreateCount()
517```
518import util from '@ohos.util'
519var pro = new util.LruBuffer();
520pro.put(1,8);
521var result = pro.getCreateCount();
522```
52337.getMissCount()
524```
525import util from '@ohos.util'
526var pro = new util.LruBuffer();
527pro.put(2,10);
528pro.get(2)
529var result = pro.getMissCount();
530```
53138.getRemovalCount()
532```
533import util from '@ohos.util'
534var pro = new util.LruBuffer();
535pro.put(2,10);
536pro.updateCapacity(2);
537pro.put(50,22);
538var result = pro.getRemovalCount();
539```
54039.getMatchCount()
541```
542import util from '@ohos.util'
543var pro = new util.LruBuffer();
544pro.put(2,10);
545pro.get(2);
546var result = pro.getMatchCount();
547```
54840.getPutCount()
549```
550import util from '@ohos.util'
551var pro = new util.LruBuffer();
552pro.put(2,10);
553var result = pro.getPutCount();
554```
55541.isEmpty()
556```
557import util from '@ohos.util'
558var pro = new util.LruBuffer();
559pro.put(2,10);
560var result = pro.isEmpty();
561```
56242.get()
563```
564import util from '@ohos.util'
565var pro = new util.LruBuffer();
566pro.put(2,10);
567var result = pro.get(2);
568```
56943.put()
570```
571import util from '@ohos.util'
572var pro = new util.LruBuffer();
573var result = pro.put(2,10);
574```
57544.keys()
576```
577import util from '@ohos.util'
578var pro = new util.LruBuffer();
579pro.put(2,10);
580var result = pro.keys();
581```
58245.remove()
583```
584import util from '@ohos.util'
585var pro = new util.LruBuffer();
586pro.put(2,10);
587var result = pro.remove(20);
588```
58946.contains()
590```
591import util from '@ohos.util'
592var pro = new util.LruBuffer();
593pro.put(2,10);
594var result = pro.contains(20);
595```
59647.createDefault()
597```
598import util from '@ohos.util'
599var pro = new util.LruBuffer();
600var result = pro.createDefault(50);
601```
60248.entries()
603```
604import util from '@ohos.util'
605var pro = new util.LruBuffer();
606pro.put(2,10);
607var result = pro.entries();
608```
60949.\[Symbol.iterator\]()
610
611```
612import util from '@ohos.util'
613var pro = new util.LruBuffer();
614pro.put(2,10);
615var result = pro[symbol.iterator]();
616```
61750.afterRemoval()
618```
619import util from '@ohos.util'
620var arr = [ ];
621class ChildLruBuffer extends util.LruBuffer
622{
623 constructor()
624 {
625 super();
626 }
627 static getInstance()
628 {
629 if(this.instance == null)
630 {
631 this.instance = new ChildLruBuffer();
632 }
633 return this.instance;
634 }
635 afterRemoval(isEvict, key, value, newValue)
636 {
637 if (isEvict === false)
638 {
639 arr = [key, value, newValue];
640 }
641 }
642}
643ChildLruBuffer.getInstance().afterRemoval(false,10,30,null)
644```
645Scope接口中构造新类,实现compareTo方法。
646
647```
648class Temperature {
649 constructor(value) {
650 this._temp = value;
651 }
652 compareTo(value) {
653 return this._temp >= value.getTemp();
654 }
655 getTemp() {
656 return this._temp;
657 }
658 toString() {
659 return this._temp.toString();
660 }
661}
662```
66351.constructor()
664```
665var tempLower = new Temperature(30);
666var tempUpper = new Temperature(40);
667var range = new Scope(tempLower, tempUpper);
668```
66952.toString()
670```
671var tempLower = new Temperature(30);
672var tempUpper = new Temperature(40);
673var range = new Scope(tempLower, tempUpper);
674var result = range.toString() // => [30,40]
675```
67653.intersect()
677```
678var tempLower = new Temperature(30);
679var tempUpper = new Temperature(40);
680var range = new Scope(tempLower, tempUpper);
681var tempMiDF = new Temperature(35);
682var tempMidS = new Temperature(39);
683var rangeFir = new Scope(tempMiDF, tempMidS);
684var result = range.intersect(rangeFir) // => [35,39]
685```
68654.intersect()
687```
688var tempLower = new Temperature(30);
689var tempUpper = new Temperature(40);
690var tempMiDF = new Temperature(35);
691var tempMidS = new Temperature(39);
692var range = new Scope(tempLower, tempUpper);
693var result = range.intersect(tempMiDF, tempMidS) // => [35,39]
694```
69555.getUpper()
696```
697var tempLower = new Temperature(30);
698var tempUpper = new Temperature(40);
699var range = new Scope(tempLower, tempUpper);
700var result = range.getUpper() // => 40
701```
70256.getLower()
703```
704var tempLower = new Temperature(30);
705var tempUpper = new Temperature(40);
706var range = new Scope(tempLower, tempUpper);
707var result = range.getLower() // => 30
708```
70957.expand()
710```
711var tempLower = new Temperature(30);
712var tempUpper = new Temperature(40);
713var tempMiDF = new Temperature(35);
714var tempMidS = new Temperature(39);
715var range = new Scope(tempLower, tempUpper);
716var result = range.expand(tempMiDF, tempMidS) // => [30,40]
717```
71858.expand()
719```
720var tempLower = new Temperature(30);
721var tempUpper = new Temperature(40);
722var tempMiDF = new Temperature(35);
723var tempMidS = new Temperature(39);
724var range = new Scope(tempLower, tempUpper);
725var rangeFir = new Scope(tempMiDF, tempMidS);
726var result = range.expand(rangeFir) // => [30,40]
727```
72859.expand()
729```
730var tempLower = new Temperature(30);
731var tempUpper = new Temperature(40);
732var tempMiDF = new Temperature(35);
733var range = new Scope(tempLower, tempUpper);
734var result = range.expand(tempMiDF) // => [30,40]
735```
73660.contains()
737```
738var tempLower = new Temperature(30);
739var tempUpper = new Temperature(40);
740var tempMiDF = new Temperature(35);
741var range = new Scope(tempLower, tempUpper);
742var result = range.contains(tempMiDF) // => true
743```
74461.contains()
745```
746var tempLower = new Temperature(30);
747var tempUpper = new Temperature(40);
748var range = new Scope(tempLower, tempUpper);
749var tempLess = new Temperature(20);
750var tempMore = new Temperature(45);
751var rangeSec = new Scope(tempLess, tempMore);
752var result = range.contains(rangeSec) // => true
753```
75462.clamp()
755```
756var tempLower = new Temperature(30);
757var tempUpper = new Temperature(40);
758var tempMiDF = new Temperature(35);
759var range = new Scope(tempLower, tempUpper);
760var result = range.clamp(tempMiDF) // => 35
761```
76263.isAnyArrayBuffer()
763```
764import util from '@ohos.util'
765var proc = new util.Types();
766var result = proc.isAnyArrayBuffer(new ArrayBuffer([]))
767```
76864.isArrayBufferView()
769```
770import util from '@ohos.util'
771var proc = new util.Types();
772var result = proc.isArrayBufferView(new DataView(new ArrayBuffer(16)));
773```
77465.isArgumentsObject()
775```
776import util from '@ohos.util'
777function foo() {
778 var result = proc.isArgumentsObject(arguments);
779 }
780var f = foo();
781```
78266.isArrayBuffer()
783```
784import util from '@ohos.util'
785var proc = new util.Types();
786var result = proc.isArrayBuffer(new ArrayBuffer([]));
787```
78867.isAsyncFunction()
789```
790import util from '@ohos.util'
791var proc = new util.Types();
792var result = proc.isAsyncFunction(async function foo() {});
793```
79468.isBigInt64Array()
795```
796import util from '@ohos.util'
797var proc = new util.Types();
798var result = proc.isBigInt64Array(new Int16Array([]));
799```
80069.isBigUint64Array()
801```
802import util from '@ohos.util'
803var proc = new util.Types();
804var result = proc.isBigUint64Array(new Int16Array([]));
805```
80670.isBooleanObject()
807```
808import util from '@ohos.util'
809var proc = new util.Types();
810var result = proc.isBooleanObject(new Boolean(false));
811```
81271.isBoxedPrimitive()
813```
814import util from '@ohos.util'
815var proc = new util.Types();
816var result = proc.isBoxedPrimitive(new Boolean(false));
817```
81872.isCryptoKey()
819```
820import util from '@ohos.util'
821var proc = new util.Types();
822var result = proc.isCryptoKey(false);
823```
82473.isDataView()
825```
826import util from '@ohos.util'
827var proc = new util.Types();
828const ab = new ArrayBuffer(20);
829var result = proc.isDataView(new DataView(ab));
830```
83174.isDate()
832```
833import util from '@ohos.util'
834var proc = new util.Types();
835var result = proc.isDate(new Date());
836```
83775.isExternal()
838```
839import util from '@ohos.util'
840const data = util.createExternalType();
841var reult13 = proc.isExternal(data);
842```
84376.isFloat32Array()
844```
845import util from '@ohos.util'
846var proc = new util.Types();
847var result = proc.isFloat32Array(new Float32Array([]));
848```
84977.isFloat64Array()
850```
851import util from '@ohos.util'
852var proc = new util.Types();
853var result = proc.isFloat64Array(new Float64Array([]));
854```
85578.isGeneratorFunction()
856```
857import util from '@ohos.util'
858var proc = new util.Types();
859var result = proc.isGeneratorFunction(function* foo() {});
860```
86179.isGeneratorObject()
862```
863import util from '@ohos.util'
864var proc = new util.Types();
865function* foo() {}
866const generator = foo();
867var result = proc.isGeneratorObject(generator);
868```
86980.isInt8Array()
870```
871import util from '@ohos.util'
872var proc = new util.Types();
873var result = proc.isInt8Array(new Int8Array([]));
874```
87581.isInt16Array()
876```
877import util from '@ohos.util'
878var proc = new util.Types();
879var result = proc.isInt16Array(new Int16Array([]));
880```
88182.isInt32Array()
882```
883import util from '@ohos.util'
884var proc = new util.Types();
885var result = proc.isInt32Array(new Int32Array([]));
886```
88783.isKeyObject()
888```
889import util from '@ohos.util'
890var proc = new util.Types();
891var result = proc.isKeyObject(0);
892```
89384.isMap()
894```
895import util from '@ohos.util'
896var proc = new util.Types();
897var result = proc.isMap(new Map());
898```
89985.isMapIterator()
900```
901import util from '@ohos.util'
902var proc = new util.Types();
903var result = proc.isMapIterator(map.keys());
904```
90586.isModuleNamespaceObject()
906```
907import util from '@ohos.util'
908var proc = new util.Types();
909var result = proc.isModuleNamespaceObject(util);
910```
91187.isNativeError()
912```
913import util from '@ohos.util'
914var proc = new util.Types();
915var result = proc.isNativeError(new TypeError());
916```
91788.isNumberObject()
918```
919import util from '@ohos.util'
920var proc = new util.Types();
921var result = proc.isNumberObject(new Number(0));
922```
92389.isPromise()
924```
925import util from '@ohos.util'
926var proc = new util.Types();
927var result = proc.isPromise(Promise.resolve(42));
928```
92990.isProxy()
930```
931import util from '@ohos.util'
932var proc = new util.Types();
933const target = {};
934const proxy = new Proxy(target, {});
935var result = proc.isProxy(proxy);
936```
93791.isRegExp()
938```
939import util from '@ohos.util'
940var proc = new util.Types();
941var result = proc.isRegExp(new RegExp('abc'));
942```
94392.isSet()
944```
945import util from '@ohos.util'
946var proc = new util.Types();
947var result = proc.isSet(new Set());
948```
94993.isSetIterator()
950```
951import util from '@ohos.util'
952var proc = new util.Types();
953const set = new Set();
954var result = proc.isSetIterator(set.keys());
955```
95694.isSharedArrayBuffer()
957```
958import util from '@ohos.util'
959var proc = new util.Types();
960var result = proc.isSharedArrayBuffer(new ArrayBuffer([]));
961```
96295.isStringObject()
963```
964import util from '@ohos.util'
965var proc = new util.Types();
966var result = proc.isStringObject(new String('foo'));
967```
96896.isSymbolObject()
969```
970import util from '@ohos.util'
971var proc = new util.Types();
972const symbols = Symbol('foo');
973var result = proc.isSymbolObject(Object(symbols));
974```
97597.isTypedArray()
976```
977import util from '@ohos.util'
978var proc = new util.Types();
979var result = proc.isTypedArray(new Float64Array([]));
980```
98198.isUint8Array()
982```
983import util from '@ohos.util'
984var proc = new util.Types();
985var result = proc.isUint8Array(new Uint8Array([]));
986```
98799.isUint8ClampedArray()
988```
989import util from '@ohos.util'
990var proc = new util.Types();
991var result = proc.isUint8ClampedArray(new Uint8ClampedArray([]));
992```
993100.isUint16Array()
994```
995import util from '@ohos.util'
996var proc = new util.Types();
997var result = proc.isUint16Array(new Uint16Array([]));
998```
999101.isUint32Array()
1000```
1001import util from '@ohos.util'
1002var proc = new util.Types();
1003var result = proc.isUint32Array(new Uint32Array([]));
1004```
1005102.isWeakMap()
1006```
1007import util from '@ohos.util'
1008var proc = new util.Types();
1009var result = proc.isWeakMap(new WeakMap());
1010```
1011103.isWeakSet()
1012```
1013import util from '@ohos.util'
1014var proc = new util.Types();
1015var result = proc.isWeakSet(new WeakSet());
1016```
1017## 相关仓
1018
1019[js_util_module子系统](https://gitee.com/OHOS_STD/js_util_module)
1020
1021[base/compileruntime/js_util_module/](base/compileruntime/js_util_module-readme.md)
1022
1023## 许可证
1024
1025Util在[Mozilla许可证](https://www.mozilla.org/en-US/MPL/)下可用,说明文档详见[说明文档](https://gitee.com/openharmony/js_util_module/blob/master/mozilla_docs.txt)。有关完整的许可证文本,有关完整的许可证文本,请参见[许可证](https://gitee.com/openharmony/js_util_module/blob/master/LICENSE)