Namespace: util

util

Various utility functions.

Classes

EventEmitter
LongBits
ProtocolError

Namespaces

base64
path
utf8

Members

util.Array { Constructor.<Uint8Array> }

Array implementation used in the browser. Uint8Array if supported, otherwise Array.

util.Buffer { Constructor.<Buffer> }

Node's Buffer class if available.

util.decorateRoot readonly{ Root }

Decorator root (TypeScript).

util.emptyArray constant{ *[] }

An immuable empty array.

util.emptyObject constant{ Object }

An immutable empty object.

util.fs { Object<string, *> }

Node's fs module if available.

util.isNode constant{ boolean }

Whether running within node or not.

util.key2Re constant{ RegExp }

Regular expression used to verify 2 bit (bool) map keys.

util.key32Re constant{ RegExp }

Regular expression used to verify 32 bit (int32 etc.) map keys.

util.key64Re constant{ RegExp }

Regular expression used to verify 64 bit (int64 etc.) map keys.

util.Long { Constructor.<Long> }

Long.js's Long class if available.

util.toJSONOptions { IConversionOptions }

Default conversion options used for Message#toJSON implementations.

These options are close to proto3's JSON mapping with the exception that internal types like Any are handled just like messages. More precisely:

  • Longs become strings
  • Enums become string keys
  • Bytes become base64 encoded strings
  • (Sub-)Messages become plain objects
  • Maps become plain objects with all string keys
  • Repeated fields become arrays
  • NaN and Infinity for float and double fields become strings
See:

Methods

util.asPromise(fn, ctx, params){ Promise<*> }

Returns a promise from a node-style callback function.

Name Type Description
fn asPromiseCallback

Function to call

ctx *

Function context

params * repeatable

Function arguments

Returns:
Type Description
Promise.<*> Promisified function

util.camelCase(str){ string }

Converts a string to camel case.

Name Type Description
str string

String to convert

Returns:
Type Description
string Converted string

util.codegen(functionParams, functionName){ Codegen }

Begins generating a function.

Name Type Description
functionParams Array.<string>

Function parameter names

functionName string optional

Function name if not anonymous

Returns:
Type Description
Codegen Appender that appends code to the function's body

util.codegen(2)(functionName){ Codegen }

Begins generating a function.

Name Type Description
functionName string optional

Function name if not anonymous

Returns:
Type Description
Codegen Appender that appends code to the function's body

util.compareFieldsById(a, b){ number }

Compares reflected fields by id.

Name Type Description
a Field

First field

b Field

Second field

Returns:
Type Description
number Comparison value

util.decorateEnum(object){ Enum }

Decorator helper for enums (TypeScript).

Name Type Description
object Object

Enum object

Returns:
Type Description
Enum Reflected enum

util.decorateType(ctor, typeName){ Type }

Decorator helper for types (TypeScript).

Name Type Description
ctor Constructor.<T>

Constructor function

typeName string optional

Type name, defaults to the constructor's name

Properties:
Name Type Description
root Root

Decorators root

Returns:
Type Description
Type Reflected type

util.fetch(filename, options, callback){ undefined }

Fetches the contents of a file.

Name Type Description
filename string

File path or url

options IFetchOptions

Fetch options

callback FetchCallback

Callback function

Returns:
Type Description
undefined

util.fetch(2)(path, callback){ undefined }

Fetches the contents of a file.

Name Type Description
path string

File path or url

callback FetchCallback

Callback function

Returns:
Type Description
undefined

util.fetch(3)(path, options){ Promise<(string|Uint8Array)> }

Fetches the contents of a file.

Name Type Description
path string

File path or url

options IFetchOptions optional

Fetch options

Returns:
Type Description
Promise.<(string|Uint8Array)> Promise

util.inquire(moduleName){ Object }

Requires a module only if available.

Name Type Description
moduleName string

Module to require

Returns:
Type Description
Object Required module if available and not empty, otherwise null

util.isInteger(value){ boolean }

Tests if the specified value is an integer.

Name Type Description
value *

Value to test

Returns:
Type Description
boolean true if the value is an integer

util.isObject(value){ boolean }

Tests if the specified value is a non-null object.

Name Type Description
value *

Value to test

Returns:
Type Description
boolean true if the value is a non-null object

util.isReserved(name){ boolean }

Tests whether the specified name is a reserved word in JS.

Name Type Description
name string

Name to test

Returns:
Type Description
boolean true if reserved, otherwise false

util.isset(obj, prop){ boolean }

Checks if a property on a message is considered to be present.
This is an alias of util.isSet.

Name Type Description
obj Object

Plain object or message instance

prop string

Property name

Returns:
Type Description
boolean true if considered to be present, otherwise false

util.isSet(obj, prop){ boolean }

Checks if a property on a message is considered to be present.

Name Type Description
obj Object

Plain object or message instance

prop string

Property name

Returns:
Type Description
boolean true if considered to be present, otherwise false

util.isString(value){ boolean }

Tests if the specified value is a string.

Name Type Description
value *

Value to test

Returns:
Type Description
boolean true if the value is a string

util.lcFirst(str){ string }

Converts the first character of a string to lower case.

Name Type Description
str string

String to convert

Returns:
Type Description
string Converted string

util.longFromHash(hash, unsigned){ Long | number }

Converts an 8 characters long hash string to a long or number.

Name Type Default Description
hash string

Hash

unsigned boolean false optional

Whether unsigned or not

Returns:
Type Description
Long | number Original value

util.longToHash(value){ string }

Converts a number or long to an 8 characters long hash string.

Name Type Description
value Long | number

Value to convert

Returns:
Type Description
string Hash

util.merge(dst, src, ifNotSet){ Object<string, *> }

Merges the properties of the source object into the destination object.

Name Type Default Description
dst Object.<string, *>

Destination object

src Object.<string, *>

Source object

ifNotSet boolean false optional

Merges only if the key is not already set

Returns:
Type Description
Object.<string, *> Destination object

util.newBuffer(sizeOrArray){ Uint8Array | Buffer }

Creates a new buffer of whatever type supported by the environment.

Name Type Default Description
sizeOrArray number | Array.<number> 0 optional

Buffer size or number array

Returns:
Type Description
Uint8Array | Buffer Buffer

util.newError(name){ Constructor.<Error> }

Creates a custom error constructor.

Name Type Description
name string

Error name

Returns:
Type Description
Constructor.<Error> Custom error constructor

util.oneOfGetter(fieldNames){ OneOfGetter }

Builds a getter for a oneof's present field name.

Name Type Description
fieldNames Array.<string>

Field names

Returns:
Type Description
OneOfGetter Unbound getter

util.oneOfSetter(fieldNames){ OneOfSetter }

Builds a setter for a oneof's present field name.

Name Type Description
fieldNames Array.<string>

Field names

Returns:
Type Description
OneOfSetter Unbound setter

util.pool(alloc, slice, size){ PoolAllocator }

A general purpose buffer pool.

Name Type Default Description
alloc PoolAllocator

Allocator

slice PoolSlicer

Slicer

size number 8192 optional

Slab size

Returns:
Type Description
PoolAllocator Pooled allocator

util.safeProp(prop){ string }

Returns a safe property accessor for the specified property name.

Name Type Description
prop string

Property name

Returns:
Type Description
string Safe accessor

util.toArray(object){ *[] }

Converts an object's values to an array.

Name Type Description
object Object.<string, *>

Object to convert

Returns:
Type Description
Array.<*> Converted array

util.toObject(array){ Object<string, *> }

Converts an array of keys immediately followed by their respective value to an object, omitting undefined values.

Name Type Description
array Array.<*>

Array to convert

Returns:
Type Description
Object.<string, *> Converted object

util.ucFirst(str){ string }

Converts the first character of a string to upper case.

Name Type Description
str string

String to convert

Returns:
Type Description
string Converted string