Module util
Common tool module
Object
LruCache
LRU (least recently used) cache object, see LruCache Object.
1LruCache util.LruCache;
Static function
format
Format variables according to the specified format
1
2static String util.format(String fmt,
...args);
Call parameters:
- fmt: String, format string
- args: ..., optional parameter list
Return result:
- String, Returns the formatted string
Formatting variables
1static String util.format(...args);
Call parameters:
- args: ..., optional parameter list
Return result:
- String, Returns the formatted string
inherits
Inherit the prototype function from one constructor to another. The prototype of the constructor will be set to a new object created from the superConstructor.
1
2static util.inherits(Value constructor,
Value superConstructor);
Call parameters:
- constructor: Value, the initial constructor
- superConstructor: Value, the superclass to be inherited
inspect
The function returns the string representation of obj, which is mainly used for debugging. Additional options can be used to change certain aspects of the format string.
1
2static String util.inspect(Value obj,
Object options = {});
Call parameters:
- obj: Value, specify the object to be processed
- options: Object, specify format control options
Return result:
- String, Returns the formatted string
The following parameters are supported:
1
2
3
4
5
6{
"colors": false, // 指定是否输出 ansi 作色字符串,缺省为 false
"table": false, // 指定输出 table 格式,缺省为 false
"encode_string": true, // 指定表格中的字符串是否编码,缺省为 true
"fields": [], // 当 table 为 true 时指定显示字段
}
deprecate
Encapsulate the given function, this function is only compatible, and does not output a warning
1
2
3static Function util.deprecate(Function fn,
String msg,
String code = "");
Call parameters:
- fn: Function, given the function to be encapsulated
- msg: String, given warning message
- code: String, given warning number
Return result:
- Function, If the encapsulation result
isEmpty
Check whether the given variable does not contain any value (no enumerable attributes)
1static Boolean util.isEmpty(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is empty
isArray
Check whether the given variable is an array
1static Boolean util.isArray(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Returns True if it is an array
isBoolean
Check whether the given variable is Boolean
1static Boolean util.isBoolean(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is Boolean
isNull
Check whether the given variable is Null
1static Boolean util.isNull(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is Null
isNullOrUndefined
Check whether the given variable is Null or Undefined
1static Boolean util.isNullOrUndefined(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is Null or Undefined
isNumber
Check whether the given variable is a number
1static Boolean util.isNumber(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is a number
isBigInt
Check whether the given variable is BigInt
1static Boolean util.isBigInt(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is a number
isString
Check whether the given variable is a string
1static Boolean util.isString(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Returns True if it is a string
isUndefined
Check whether the given variable is Undefined
1static Boolean util.isUndefined(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is Undefined
isRegExp
Check whether the given variable is a regular object
1static Boolean util.isRegExp(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is a regular object
isObject
Check whether the given variable is an object
1static Boolean util.isObject(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is an object
isDate
Check whether the given variable is a date object
1static Boolean util.isDate(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is a date object
isNativeError
Check whether the given variable is the wrong object
1static Boolean util.isNativeError(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is an error object
isPrimitive
Check whether the given variable is of primitive type
1static Boolean util.isPrimitive(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is a primitive type
isSymbol
Check whether the given variable is of type Symbol
1static Boolean util.isSymbol(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, If it is a Symbol type, it returns True
isDataView
Check whether the given variable is of type DataView
1static Boolean util.isDataView(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is of DataView type
isExternal
Check whether the given variable is of type External
1static Boolean util.isExternal(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, If it is an External type, it returns True
isMap
Check whether the given variable is of type Map
1static Boolean util.isMap(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is a Map type
isMapIterator
Check whether the given variable is of type MapIterator
1static Boolean util.isMapIterator(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is of MapIterator type
isPromise
Check whether the given variable is of Promise type
1static Boolean util.isPromise(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is a Promise type
isAsyncFunction
Check whether the given variable is of type AsyncFunction
1static Boolean util.isAsyncFunction(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is of type AsyncFunction
isSet
Check whether the given variable is of type Set
1static Boolean util.isSet(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, If it is a Set type, it returns True
isSetIterator
Check whether the given variable is of type SetIterator
1static Boolean util.isSetIterator(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Return True if it is of SetIterator type
isTypedArray
Check whether the given variable is of TypedArray type
1static Boolean util.isTypedArray(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, If it is a TypedArray type, it returns True
isUint8Array
Check whether the given variable is of type Uint8Array
1static Boolean util.isUint8Array(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Returns True if it is of Uint8Array type
isFunction
Check whether the given variable is a function object
1static Boolean util.isFunction(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, Returns True if it is a function object
isBuffer
Check whether the given variable is a function Buffer Object
1static Boolean util.isBuffer(Value v);
Call parameters:
- v: Value, given the variable to be tested
Return result:
- Boolean, If it is a function Buffer Object returns True
isDeepEqual
The depth of the test value is equal to the expected value
1
2static Boolean util.isDeepEqual(Value actual,
Value expected);
Call parameters:
- actual: Value, the value to be tested
- expected: Value, expected value
Return result:
- Boolean, Return True if the depths are equal
has
Query whether the specified object contains the given key
1
2static Boolean util.has(Value v,
String key);
Call parameters:
- v: Value, given the object to be queried
- key: String, specify the key to be queried
Return result:
- Boolean, Returns an array of all keys of the object
keys
Query all key arrays of the specified object
1static Array util.keys(Value v);
Call parameters:
- v: Value, given the object to be queried
Return result:
- Array, Returns an array of all keys of the object
values
Query the array of all values of the specified object
1static Array util.values(Value v);
Call parameters:
- v: Value, given the object to be queried
Return result:
- Array, Returns an array of all values of the object
clone
Clone the given variable, if it is an object or an array, copy the content to the new object
1static Value util.clone(Value v);
Call parameters:
- v: Value, given the variable to be cloned
Return result:
- Value, Return the cloned result
deepFreeze
Deep freeze an object, the frozen object and its contained objects will not be allowed to be modified
1static util.deepFreeze(Value v);
Call parameters:
- v: Value, specify the object to be frozen
extend
Extend the key value of one or more objects to the specified object
1
2static Value util.extend(Value v,
...objs);
Call parameters:
- v: Value, specify the object to be expanded
- objs: ..., specify one or more objects for expansion
Return result:
- Value, Returns the expanded result
_extend
Extend the key value of one or more objects to the specified object, which is an alias of extend
1
2static Value util._extend(Value v,
...objs);
Call parameters:
- v: Value, specify the object to be expanded
- objs: ..., specify one or more objects for expansion
Return result:
- Value, Returns the expanded result
pick
Return aobjectCopy, only filter out the attribute value of the specified key
1
2static Object util.pick(Value v,
...objs);
Call parameters:
- v: Value, specify the object to be filtered
- objs: ..., specify one or more keys for selection
Return result:
- Object, Return the filtered result
omit
Return aobjectCopy, just exclude the attribute value of the specified key
1
2static Object util.omit(Value v,
...keys);
Call parameters:
- v: Value, specify the object to be filtered
- keys: ..., specify one or more keys to exclude
Return result:
- Object, Returns excluded results
first
Get the first element of the array
1static Value util.first(Value v);
Call parameters:
- v: Value, given the array to be obtained
Return result:
- Value, Return the retrieved element
Get the first multiple elements of the array
1
2static Value util.first(Value v,
Integer n);
Call parameters:
- v: Value, given the array to be obtained
- n: Integer, specify the number of elements to be obtained
Return result:
- Value, Returns the obtained element array
last
Get the first element of the array
1static Value util.last(Value v);
Call parameters:
- v: Value, given the array to be obtained
Return result:
- Value, Return the retrieved element
Get multiple elements at the end of the array
1
2static Value util.last(Value v,
Integer n);
Call parameters:
- v: Value, given the array to be obtained
- n: Integer, specify the number of elements to be obtained
Return result:
- Value, Returns the obtained element array
unique
Get a copy of the elements of the array after de-duplication
1
2static Array util.unique(Value v,
Boolean sorted = false);
Call parameters:
- v: Value, given the array to be deduplicated
- sorted: Boolean, specify whether the array is sorted or not, if the array is specified to be sorted, a fast algorithm will be used
Return result:
- Array, Return the array after removing duplicate elements
union
Combine the values of one or more arrays into an array with unique values
1static Array util.union(...arrs);
Call parameters:
- arrs: ..., specify one or more arrays for merging
Return result:
- Array, Return the merged result
intersection
Returns the intersection of an array containing arr and excluding one or more array elements
1static Array util.intersection(...arrs);
Call parameters:
- arrs: ..., specify one or more arrays for computing intersection
Return result:
- Array, Returns the result of computing the intersection
flatten
Convert an array with multiple levels of nesting (the nesting can be any number of levels) into an array with only one level. If you pass the shallow parameter, the array will only reduce one-dimensional nesting.
1
2static Array util.flatten(Value arr,
Boolean shallow = false);
Call parameters:
- arr: Value, specify the array to be converted
- shallow: Boolean, specify whether to reduce only one-dimensional nesting, the default is false
Return result:
- Array, Return the result of the conversion
without
Returns an array containing one or more elements excluded from the arr array
1
2static Array util.without(Value arr,
...els);
Call parameters:
- arr: Value, specify the array to be excluded
- els: ..., specify one or more elements to exclude
Return result:
- Array, Returns excluded results
difference
Return an array that contains the arr array and excludes the without array element
1
2static Array util.difference(Array list,
...arrs);
Call parameters:
- list: Array, specify the array to be excluded
- arrs: ..., specify one or more arrays to exclude
Return result:
- Array, Returns excluded results
each
Traverse all the elements in the list, and output each element in order with traversal. If the context parameter is passed, the iterator is bound to the context object. Each call to iterator will pass three parameters: (element, index, list)
1
2
3static Value util.each(Value list,
Function iterator,
Value context = undefined);
Call parameters:
- list: Value, specify the list or object to be traversed
- iterator: Function, specify the callback function used for traversal
- context: Value, specify the context object bound when calling iterator
Return result:
- Value, Return the list itself
map
Through the transformation function (iterator), each value in the list is mapped to a new array. If the context parameter is passed, the iterator is bound to the context object. Each call to iterator will pass three parameters: (element, index, list)
1
2
3static Array util.map(Value list,
Function iterator,
Value context = undefined);
Call parameters:
- list: Value, specify the list or object to be transformed
- iterator: Function, specify the callback function used for transformation
- context: Value, specify the context object bound when calling iterator
Return result:
- Array, Returns the result of the transformation
reduce
Reduce the elements in the list to a single value. If the context parameter is passed, the iterator is bound to the context object. Each call to iterator will pass three parameters: (memo, element, index, list)
1
2
3
4static Value util.reduce(Value list,
Function iterator,
Value memo,
Value context = undefined);
Call parameters:
- list: Value, specify the list or object to be resolved
- iterator: Function, specify the callback function used for resolution
- memo: Value, specify the initial value of resolution
- context: Value, specify the context object bound when calling iterator
Return result:
- Value, Return the result of resolution
parseArgs
Parse the command line string and return the parameter list
1static NArray util.parseArgs(String command);
Call parameters:
- command: String, specify the command line string to be parsed
Return result:
- NArray, Return the parsed parameter list
compile
Compile script as binary code
1
2
3static Buffer util.compile(String srcname,
String script,
Integer mode = 0);
Call parameters:
- srcname: String, specify the name of the script to be added
- script: String, specify the script code to be compiled
- mode: Integer, compilation mode, 0: module, 1: script, 2: worker, the default is 0
Return result:
- Buffer, Return the compiled binary code
util.compileThe script can be compiled into v8 internal running data block (non-machine execution code). After the compiled code is saved as *.jsc, it can be loaded and executed directly by run and require.
After compilation, the target code will not be able to obtain the source code in reverse, and the program that depends on Function.toString will not run normally.
sync
Wrap callback or async functions as synchronous calls
1
2static Function util.sync(Function func,
Boolean async_func = false);
Call parameters:
- func: Function, given the function to be wrapped
- async_func: Boolean, specify to process func as async function, if it is false, it will be judged automatically
Return result:
- Function, Returns the function that runs synchronously
util.sync Treat the callback function or async function as a sync function for easy calling.
The callback example is as follows:
1
2
3
4
5
6
7
8
9
10
11// callback
var util = require('util');
function cb_test(a, b, cb) {
setTimeout(() => {
cb(null, a + b);
}, 100);
}
var fn_sync = util.sync(cb_test);
console.log(fn_sync(100, 200));
The async example is as follows:
1
2
3
4
5
6
7
8
9// async/await
var util = require('util');
async function async_test(a, b) {
return a + b;
}
var fn_sync = util.sync(async_test);
console.log(fn_sync(100, 200));
For functions that return promises that are not marked as async, you can manually specify the sync mode:
1
2
3
4
5
6
7
8
9
10
11// async/await
var util = require('util');
function async_test(a, b) {
return new Promise(function(resolve, reject) {
resolve(a + b);
});
}
var fn_sync = util.sync(async_test, true);
console.log(fn_sync(100, 200));
promisify
Wrap the callback function as async call
1static Function util.promisify(Function func);
Call parameters:
- func: Function, given the function to be wrapped
Return result:
- Function, Return the async function
util.promisify Treat the callback function as an async function for easy calling.
The callback example is as follows:
1
2
3
4
5
6
7
8
9
10
11// callback
var util = require('util');
function cb_test(a, b, cb) {
setTimeout(() => {
cb(null, a + b);
}, 100);
}
var fn_sync = util.promisify(cb_test);
console.log(async fn_sync(100, 200));
callbackify
Wrap the async function as a callback call
1static Function util.callbackify(Function func);
Call parameters:
- func: Function, given the function to be wrapped
Return result:
- Function, Return callback function
util.callbackify Treat the async function as a callback function for easy calling.
The async example is as follows:
1
2
3
4
5
6
7
8
9
10
11
12// async
var util = require('util');
async function async_test(a, b) {
return a + b;
}
var fn_callback = util.callbackify(async_test);
fn_callback(100, 200, (err, result) => {
console.log(result);
});
buildInfo
Query current engine and component version information
1static Object util.buildInfo();
Return result:
- Object, Returns the component version object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24{
"fibjs": "0.25.0",
"clang": "9.1",
"date": "Jun 12 2018 07:22:40",
"vender": {
"ev": "4.24",
"expat": "2.2.5",
"gd": "2.2.4",
"jpeg": "8.3",
"leveldb": "1.17",
"mongo": "0.7",
"pcre": "8.21",
"png": "1.5.4",
"mbedtls": "2.6.1",
"snappy": "1.1.2",
"sqlite": "3.23.0",
"tiff": "3.9.5",
"uuid": "1.6.2",
"v8": "6.7.288.20",
"v8-snapshot": true,
"zlib": "1.2.7",
"zmq": "3.1"
}
}