# util

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

Availability
8.1.0
8.1.0
9.2.0
Extends
Object

NOTE

This is an abstract type. Any object of this structure can be used where this type is used.

# Overview

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 (opens new window)

# Properties

# types

Availability
8.1.0
8.1.0
9.2.0
types :util.types

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

# callbackify

Availability
8.1.0
8.1.0
9.2.0
callbackify(original) Function

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

Name Type Description
original Function

An async function

Returns

a callback style function

Type
Function

# debug

Availability
8.1.0
8.1.0
9.2.0
debug(section[, callback]) Function

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

Parameters

Name Type Description
section String

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

The logging function

Type
Function

# debuglog

Availability
8.1.0
8.1.0
9.2.0
debuglog(section[, callback]) 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

Name Type Description
section String

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

The logging function

Type
Function

# deprecate

Availability
8.1.0
8.1.0
9.2.0
deprecate(fn, msg[, code]) 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

Name Type Description
fn Function

The function that is being deprecated.

msg String

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

code String

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

Returns

The deprecated function wrapped to emit a warning.

Type
Function

# format

Availability
8.1.0
8.1.0
9.2.0
format(format[, ...args]) void

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

Name Type Description
format String

A printf-like format string.

args any

variable arguments to inject into the formatted string

Returns

Type
void

# formatWithOptions

Availability
8.1.0
8.1.0
9.2.0
formatWithOptions(inspectOptions, format) void

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

Name Type Description
inspectOptions UtilInspectOptions

Options passed along to inspect()

format String

A printf-like format string.

Returns

Type
void

# inherits

Availability
8.1.0
8.1.0
9.2.0
inherits(constructor, superConstructor) void

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

Name Type Description
constructor Function

constructor function for "subclass"

superConstructor Function

constructor function for "super class"

Returns

Type
void

# inspect

Availability
8.1.0
8.1.0
9.2.0
inspect(object[, options]) String

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

Name Type Description
object any

Any JavaScript primitive or Object.

options UtilInspectOptions

Additional options to modify the output of the inspect function

Returns

Type
String

# isArray

Availability
8.1.0
8.1.0
9.2.0
isArray(object) Boolean

Alias for Array.isArray().

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isBoolean

Availability
8.1.0
8.1.0
9.2.0
isBoolean(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isBuffer

Availability
8.1.0
8.1.0
9.2.0
isBuffer(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isDate

Availability
8.1.0
8.1.0
9.2.0
isDate(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isError

Availability
8.1.0
8.1.0
9.2.0
isError(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isFunction

Availability
8.1.0
8.1.0
9.2.0
isFunction(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isNull

Availability
8.1.0
8.1.0
9.2.0
isNull(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isNullOrUndefined

Availability
8.1.0
8.1.0
9.2.0
isNullOrUndefined(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isNumber

Availability
8.1.0
8.1.0
9.2.0
isNumber(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isObject

Availability
8.1.0
8.1.0
9.2.0
isObject(object) 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

Name Type Description
object any

any value

Returns

Type
Boolean

# isPrimitive

Availability
8.1.0
8.1.0
9.2.0
isPrimitive(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isRegExp

Availability
8.1.0
8.1.0
9.2.0
isRegExp(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isString

Availability
8.1.0
8.1.0
9.2.0
isString(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isSymbol

Availability
8.1.0
8.1.0
9.2.0
isSymbol(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# isUndefined

Availability
8.1.0
8.1.0
9.2.0
isUndefined(object) Boolean

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

Parameters

Name Type Description
object any

any value

Returns

Type
Boolean

# log

Availability
8.1.0
8.1.0
9.2.0
log(string) void

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

Parameters

Name Type Description
string String

string to print

Returns

Type
void

# promisify

Availability
8.1.0
8.1.0
9.2.0
promisify(original) Function

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

Name Type Description
original Function

A typical Node.js style callback function

Returns

a function that returns a Promise

Type
Function