Skip to content
Titanium SDK
Version12.7.1

You are reading 12.7.1. See this page in 13.4.1.

util

pseudoAndroidiPhoneiPadmacOSsince 8.1.0

A Node.js-compatible implementation of the core util module

Titanium provides a number of shims and ports of core Node.js module functionality.

This module is intended to provide a Node-compatible port of the util core module.

More details on the Node.js API can be found in their util module documentation

Properties 1

util.types provides type checks for different kinds of built-in objects. Unlike instanceof or Object.prototype.toString.call(value), these checks do not inspect properties of the object that are accessible from JavaScript (like their prototype), and usually have the overhead of calling into C++.

The result generally does not make any guarantees about what kinds of properties or behavior a value exposes in JavaScript. They are primarily useful for addon developers who prefer to do type checking in JavaScript.

Methods 25

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.

Parameters
ParametersTypeDescription
originalFunction

An async function

Returns Function

a callback style function

Alias for util.debuglog. Usage allows for readability of that doesn't imply logging when only using util.debuglog().enabled.

Parameters
ParametersTypeDescription
sectionString

A string identifying the portion of the application for which the debuglog function is being created.

callback?Function

A callback invoked the first time the logging function is called with a function argument that is a more optimized logging function.

Returns Function

The logging function

The util.debuglog() method is used to create a function that conditionally writes debug messages to stderr based on the existence of the NODE_DEBUG environment variable. If the section name appears within the value of that environment variable, then the returned function operates similar to console.error(). If not, then the returned function is a no-op.

NOTE: Titanium currently returns a no-op regardless of the NODE_DEBUG environment variable.

Parameters
ParametersTypeDescription
sectionString

A string identifying the portion of the application for which the debuglog function is being created.

callback?Function

A callback invoked the first time the logging function is called with a function argument that is a more optimized logging function.

Returns Function

The logging function

The util.deprecate() method wraps fn (which may be a function or class) in such a way that it is marked as deprecated.

Parameters
ParametersTypeDescription
fnFunction

The function that is being deprecated.

msgString

A warning message to display when the deprecated function is invoked.

code?String

A deprecation code. NOTE: Titanium currently ignores this argument.

Returns Function

The deprecated function wrapped to emit a warning.

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. Each specifier is replaced with the converted value from the corresponding argument.

Parameters
ParametersTypeDescription
formatString

A printf-like format string.

args?any

variable arguments to inject into the formatted string

This function is identical to util.format(), except in that it takes an inspectOptions argument which specifies options that are passed along to util.inspect().

Parameters
ParametersTypeDescription
inspectOptionsUtilInspectOptions

Options passed along to util.inspect()

11 fields
breakLength? Number

The length at which input values are split across multiple lines. Set to Infinity to format the input as a single line (in combination with compact set to true or any number >= 1).

colors? Boolean

If true, the output is styled with ANSI color codes. Colors are customizable.

compact? Boolean | Number

Setting this to false causes each object key to be displayed on a new line. It will also add new lines to text that is longer than breakLength. If set to a number, the most n inner elements are united on a single line as long as all properties fit into breakLength. Short array elements are also grouped together. No text will be reduced below 16 characters, no matter the breakLength size. For more information, see the example below.

customInspect? Boolean

If false, [util.inspect.custom](depth, opts) functions are not invoked.

depth? Number

Specifies the number of times to recurse while formatting object. This is useful for inspecting large objects. To recurse up to the maximum call stack size pass Infinity or null.

getters? Boolean | String

If set to true, getters are inspected. If set to 'get', only getters without a corresponding setter are inspected. If set to 'set', only getters with a corresponding setter are inspected. This might cause side effects depending on the getter function.

maxArrayLength? Number

Specifies the maximum number of Array, TypedArray, WeakMap and WeakSet elements to include when formatting. Set to null or Infinity to show all elements. Set to 0 or negative to show no elements.

maxStringLength? Number

Specifies the maximum number of characters to include when formatting. Set to null or Infinity to show all elements. Set to 0 or negative to show no characters.

showHidden? Boolean

If true, object's non-enumerable symbols and properties are included in the formatted result. WeakMap and WeakSet entries are also included as well as user defined prototype properties (excluding method properties).

showProxy? Boolean

If true, Proxy inspection includes the target and handler objects.

sorted? Boolean | Function

If set to true or a function, all properties of an object, and Set and Map entries are sorted in the resulting string. If set to true the default sort is used. If set to a function, it is used as a compare function.

formatString

A printf-like format string.

Usage of util.inherits() is discouraged. Please use the ES6 class and extends keywords to get language level inheritance support. Also note that the two styles are semantically incompatible.

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

This mainly adds some input validation on top of Object.setPrototypeOf(constructor.prototype, superConstructor.prototype). As an additional convenience, superConstructor will be accessible through the constructor.super_ property.

Parameters
ParametersTypeDescription
constructorFunction

constructor function for "subclass"

superConstructorFunction

constructor function for "super class"

The util.inspect() method returns a string representation of object that is intended for debugging. The output of util.inspect may change at any time and should not be depended upon programmatically. Additional options may be passed that alter the result. util.inspect() will use the constructor's name and/or @@toStringTag to make an identifiable tag for an inspected value.

Parameters
ParametersTypeDescription
objectany

Any JavaScript primitive or Object.

options?UtilInspectOptions

Additional options to modify the output of the inspect function

11 fields
breakLength? Number

The length at which input values are split across multiple lines. Set to Infinity to format the input as a single line (in combination with compact set to true or any number >= 1).

colors? Boolean

If true, the output is styled with ANSI color codes. Colors are customizable.

compact? Boolean | Number

Setting this to false causes each object key to be displayed on a new line. It will also add new lines to text that is longer than breakLength. If set to a number, the most n inner elements are united on a single line as long as all properties fit into breakLength. Short array elements are also grouped together. No text will be reduced below 16 characters, no matter the breakLength size. For more information, see the example below.

customInspect? Boolean

If false, [util.inspect.custom](depth, opts) functions are not invoked.

depth? Number

Specifies the number of times to recurse while formatting object. This is useful for inspecting large objects. To recurse up to the maximum call stack size pass Infinity or null.

getters? Boolean | String

If set to true, getters are inspected. If set to 'get', only getters without a corresponding setter are inspected. If set to 'set', only getters with a corresponding setter are inspected. This might cause side effects depending on the getter function.

maxArrayLength? Number

Specifies the maximum number of Array, TypedArray, WeakMap and WeakSet elements to include when formatting. Set to null or Infinity to show all elements. Set to 0 or negative to show no elements.

maxStringLength? Number

Specifies the maximum number of characters to include when formatting. Set to null or Infinity to show all elements. Set to 0 or negative to show no characters.

showHidden? Boolean

If true, object's non-enumerable symbols and properties are included in the formatted result. WeakMap and WeakSet entries are also included as well as user defined prototype properties (excluding method properties).

showProxy? Boolean

If true, Proxy inspection includes the target and handler objects.

sorted? Boolean | Function

If set to true or a function, all properties of an object, and Set and Map entries are sorted in the resulting string. If set to true the default sort is used. If set to a function, it is used as a compare function.

Returns String

Alias for Array.isArray().

Returns true if the given object is an Array. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is a Boolean. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is a Buffer. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is a Date. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is an Error. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is a Function. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is strictly null. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is null or undefined. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is a Number. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is strictly an Object and not a Function (even though functions are objects in JavaScript). Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is a primitive type. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is a RegExp. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is a string. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is a Symbol. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

Returns true if the given object is undefined. Otherwise, returns false.

Parameters
ParametersTypeDescription
objectany

any value

Returns Boolean

The util.log() method prints the given string to stdout with an included timestamp.

Parameters
ParametersTypeDescription
stringString

string to print

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.

Parameters
ParametersTypeDescription
originalFunction

A typical Node.js style callback function

Returns Function

a function that returns a Promise