Module basic module

Module util

Common tool module

Object

LruCache

LRU (least recently used) cache object, see LruCache object.

1
LruCache util.LruCache;

Static function

format

Format variables according to the specified format

1 2
static String util.format(String fmt, ...args);

Call parameters:

  • fmt : String, format string
  • args : ..., optional parameter list

Return result:

  • String , returns the formatted string

Format variable

1
static String util.format(...args);

Call parameters:

  • args : ..., optional parameter list

Return result:

  • String , returns the formatted string

inherits

Inherit the prototype method from one constructor to another. The prototype of the constructor will be set to a new object created from the superConstructor.

1 2
static util.inherits(Value constructor, Value superConstructor);

Call parameters:

  • constructor : Value, initial constructor
  • superConstructor : Value, the super class being inherited

inspect

The method returns a string representation of obj, which is mainly used for debugging. Additional options can be used to change certain aspects of the format string.

1 2
static String util.inspect(Object obj, Object options = {});

Call parameters:

  • obj : Object, specify the object to be processed
  • options : Object, specify format control options

Return result:

  • String , returns the formatted string

deprecate

Encapsulate the given method, this method is only compatible, and does not output a warning

1 2 3
static Function util.deprecate(Function fn, String msg, String code = "");

Call parameters:

  • fn : Function, given the method to be encapsulated
  • msg : String, given warning message
  • code : String, given warning number

Return result:

  • Function , if the result is wrapped

isEmpty

Check whether the given variable does not contain any value (no enumerable attributes)

1
static Boolean util.isEmpty(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is empty

isArray

Check whether the given variable is an array

1
static Boolean util.isArray(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is an array

isBoolean

Check whether the given variable is Boolean

1
static Boolean util.isBoolean(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , return True if it is Boolean

isNull

Check whether the given variable is Null

1
static Boolean util.isNull(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , return True if it is Null

isNullOrUndefined

Check whether the given variable is Null or Undefined

1
static Boolean util.isNullOrUndefined(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is Null or Undefined

isNumber

Check whether the given variable is a number

1
static Boolean util.isNumber(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is a number

isBigInt

Check whether the given variable is BigInt

1
static Boolean util.isBigInt(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is a number

isString

Check whether the given variable is a string

1
static Boolean util.isString(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is a string

isUndefined

Check whether the given variable is Undefined

1
static Boolean util.isUndefined(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is Undefined

isRegExp

Check whether the given variable is a regular object

1
static Boolean util.isRegExp(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is a regular object

isObject

Check whether the given variable is an object

1
static Boolean util.isObject(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , return True if it is an object

isDate

Check whether the given variable is a date object

1
static Boolean util.isDate(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is a date object

isNativeError

Check whether the given variable is the wrong object

1
static Boolean util.isNativeError(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is an error object

isPrimitive

Check whether the given variable is of primitive type

1
static Boolean util.isPrimitive(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is a primitive type

isSymbol

Check whether the given variable is of type Symbol

1
static Boolean util.isSymbol(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , return True if it is of type Symbol

isDataView

Check whether the given variable is of type DataView

1
static Boolean util.isDataView(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is of DataView type

isExternal

Check whether the given variable is of type External

1
static Boolean util.isExternal(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is an External type

isMap

Check whether the given variable is of type Map

1
static Boolean util.isMap(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is a Map type

isMapIterator

Check whether the given variable is of type MapIterator

1
static Boolean util.isMapIterator(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is of MapIterator type

isPromise

Check whether the given variable is a Promise type

1
static Boolean util.isPromise(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , return True if it is a Promise type

isAsyncFunction

Check whether the given variable is of type AsyncFunction

1
static Boolean util.isAsyncFunction(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is of type AsyncFunction

isSet

Check whether the given variable is of type Set

1
static Boolean util.isSet(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is of Set type

isSetIterator

Check whether the given variable is of type SetIterator

1
static Boolean util.isSetIterator(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is of SetIterator type

isTypedArray

Check whether the given variable is of TypedArray type

1
static Boolean util.isTypedArray(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is of TypedArray type

isUint8Array

Check whether the given variable is of type Uint8Array

1
static Boolean util.isUint8Array(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is of Uint8Array type

isFunction

Check whether the given variable is a function object

1
static Boolean util.isFunction(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , returns True if it is a function object

isBuffer

Check whether the given variable is a function Buffer object

1
static Boolean util.isBuffer(Value v);

Call parameters:

  • v : Value, given the variable to be detected

Return result:

  • Boolean , return True if it is a function Buffer object

has

Query whether the specified object contains the given key

1 2
static 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

1
static 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 all value arrays of the specified object

1
static 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

1
static 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

1
static 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 2
static 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 2
static 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 a copy of object , filtering out only the attribute value of the specified key

1 2
static 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 a copy of object , only by excluding the attribute value of the specified key

1 2
static 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 , return the excluded result

first

Get the first element of the array

1
static Value util.first(Value v);

Call parameters:

  • v : Value, given the array to be obtained

Return result:

  • Value , returns the obtained element

Get the first multiple elements of the array

1 2
static 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

1
static Value util.last(Value v);

Call parameters:

  • v : Value, given the array to be obtained

Return result:

  • Value , returns the obtained element

Get multiple elements at the end of the array

1 2
static 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 duplicate copy of the elements of the array

1 2
static Array util.unique(Value v, Boolean sorted = false);

Call parameters:

  • v : Value, given the array to be duplicated
  • sorted : Boolean, specify whether the array is sorted, if the array is specified to be sorted, a fast algorithm will be used

Return result:

  • Array , returns the array after removing duplicate elements

union

Combine the values ​​of one or more arrays into an array with unique values

1
static Array util.union(...arrs);

Call parameters:

  • arrs : ..., specify one or more arrays for merging

Return result:

  • Array , returns the combined result

intersection

Returns the intersection of an array containing arr and excluding one or more array elements

1
static 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 (nesting can be any level) into an array with only one level. If you pass the shallow parameter, the array will only reduce one-dimensional nesting.

1 2
static 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 , returns the result of the conversion

without

Returns an array that contains one or more elements from the arr array

1 2
static 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 the excluded result

difference

Return an array that contains the arr array and excludes without array elements

1 2
static 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 the excluded result

each

Traverse all the elements in the list, and output each element in sequence. 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 3
static 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, specifies the context object bound when calling iterator

Return result:

  • Value , returns the list itself

map

Map each value in the list to a new array through the transformation function (iterator). 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 3
static 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, specifies 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. Three parameters are passed every time iterator is called: (memo, element, index, list)

1 2 3 4
static 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 memo
  • context : Value, specifies the context object bound when calling iterator

Return result:

  • Value , return the result of resolution

compile

Compile script to binary code

1 2 3
static 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 , returns the compiled binary code

util.compile can compile scripts into v8 internal running data blocks (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 the callback or async method as a synchronous call

1 2
static Function util.sync(Function func, Boolean async_func = false);

Call parameters:

  • func : Function, given the method 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 method to run synchronously

util.sync treats the callback method or async method as a sync method 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));

buildInfo

Query current engine and component version information

1
static 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" } }