# util
A Node.js-compatible implementation of the core util
module
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
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
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 |
Returns
a callback style function
- Type
- Function
# debug
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 |
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
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 |
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
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
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 |
args | any | variable arguments to inject into the formatted string |
Returns
- Type
- void
# formatWithOptions
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 |
Returns
- Type
- void
# inherits
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
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
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
Returns true
if the given object
is a Boolean
. Otherwise, returns false
.
Parameters
Name | Type | Description |
---|---|---|
object | any | any value |
Returns
- Type
- Boolean
# isBuffer
Returns true
if the given object
is a Buffer
. Otherwise, returns false
.
Parameters
Name | Type | Description |
---|---|---|
object | any | any value |
Returns
- Type
- Boolean
# isDate
Returns true
if the given object
is a Date
. Otherwise, returns false
.
Parameters
Name | Type | Description |
---|---|---|
object | any | any value |
Returns
- Type
- Boolean
# isError
Returns true
if the given object
is an Error
. Otherwise, returns false
.
Parameters
Name | Type | Description |
---|---|---|
object | any | any value |
Returns
- Type
- Boolean
# isFunction
Returns true
if the given object
is a Function
. Otherwise, returns false
.
Parameters
Name | Type | Description |
---|---|---|
object | any | any value |
Returns
- Type
- Boolean
# isNull
Returns true
if the given object
is strictly null
. Otherwise, returns false
.
Parameters
Name | Type | Description |
---|---|---|
object | any | any value |
Returns
- Type
- Boolean
# isNullOrUndefined
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
Returns true
if the given object
is a Number
. Otherwise, returns false
.
Parameters
Name | Type | Description |
---|---|---|
object | any | any value |
Returns
- Type
- Boolean
# isObject
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
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
Returns true
if the given object
is a RegExp
. Otherwise, returns false
.
Parameters
Name | Type | Description |
---|---|---|
object | any | any value |
Returns
- Type
- Boolean
# isString
Returns true
if the given object
is a string
. Otherwise, returns false
.
Parameters
Name | Type | Description |
---|---|---|
object | any | any value |
Returns
- Type
- Boolean
# isSymbol
Returns true
if the given object
is a Symbol
. Otherwise, returns false
.
Parameters
Name | Type | Description |
---|---|---|
object | any | any value |
Returns
- Type
- Boolean
# isUndefined
Returns true
if the given object
is undefined
. Otherwise, returns false
.
Parameters
Name | Type | Description |
---|---|---|
object | any | any value |
Returns
- Type
- Boolean
# log
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
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