Namespace: util

util

Various utility functions.

Classes

Namespaces

Members

util.Array { Constructor.<Uint8Array> }

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

Node's Buffer class if available.

Long.js's Long class if available.

util.decorateRoot readonly{ Root }

Decorator root (TypeScript).

Deprecated:
Legacy TypeScript decorator support. Will be removed in a future release.

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.global { Object }

Global object reference.

util.isNode { 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.nestingLimit { number }

Schema declaration nesting limit.

util.recursionLimit { number }

Recursion limit.

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.

Parameters:
fn asPromiseCallback

Function to call

ctx *

Function context

params * repeatable

Function arguments

Returns:
Promise.<*> Promisified function

util.boolFromKey(key){ boolean }

Converts a boolean key to a boolean value.

Parameters:
key string

Map key

Returns:
boolean Boolean value

util.camelCase(str){ string }

Converts a string to camel case.

Parameters:
str string

String to convert

Returns:
string Converted string

util.codegen(functionParams, functionName){ Codegen }

Begins generating a function.

Parameters:
functionParams Array.<string>

Function parameter names

functionName string optional

Function name if not anonymous

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

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

Begins generating a function.

Parameters:
functionName string optional

Function name if not anonymous

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

util.compareFieldsById(a, b){ number }

Compares reflected fields by id.

Parameters:
a Field

First field

b Field

Second field

Returns:
number Comparison value

util.decorateEnum(object){ Enum }

Decorator helper for enums (TypeScript).

Parameters:
object Object

Enum object

Deprecated:
Legacy TypeScript decorator support. Will be removed in a future release.
Returns:
Enum Reflected enum

util.decorateType(ctor, typeName){ Type }

Decorator helper for types (TypeScript).

Parameters:
ctor Constructor.<T>

Constructor function

typeName string optional

Type name, defaults to the constructor's name

Properties:
root Root

Decorators root

Deprecated:
Legacy TypeScript decorator support. Will be removed in a future release.
Returns:
Type Reflected type

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

Fetches the contents of a file.

Parameters:
filename string

File path or url

options IFetchOptions

Fetch options

callback FetchCallback

Callback function

Returns:
undefined

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

Fetches the contents of a file.

Parameters:
path string

File path or url

callback FetchCallback

Callback function

Returns:
undefined

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

Fetches the contents of a file.

Parameters:
path string

File path or url

options IFetchOptions optional

Fetch options

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

util.inquire(moduleName){ Object }

Requires a module only if available.

Parameters:
moduleName string

Module to require

Deprecated:
Legacy optional require helper. Will be removed in a future release.
Returns:
Object Required module if available and not empty, otherwise null

util.isInteger(value){ boolean }

Tests if the specified value is an integer.

Parameters:
value *

Value to test

Returns:
boolean true if the value is an integer

util.isObject(value){ boolean }

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

Parameters:
value *

Value to test

Returns:
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.

Parameters:
name string

Name to test

Returns:
boolean true if reserved, otherwise false

util.isSet(obj, prop){ boolean }

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

Parameters:
obj Object

Plain object or message instance

prop string

Property name

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

util.isString(value){ boolean }

Tests if the specified value is a string.

Parameters:
value *

Value to test

Returns:
boolean true if the value is a string

util.isset(obj, prop){ boolean }

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

Parameters:
obj Object

Plain object or message instance

prop string

Property name

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

util.lcFirst(str){ string }

Converts the first character of a string to lower case.

Parameters:
str string

String to convert

Returns:
string Converted string

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

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

Parameters:
hash string

Hash

unsigned boolean false optional

Whether unsigned or not

Returns:
Long | number Original value

util.longFromKey(key, unsigned){ Long | number | string }

Converts a 64 bit key to a long or number if it is an 8 characters long hash string.

Parameters:
key string

Map key

unsigned boolean false optional

Whether unsigned or not

Returns:
Long | number | string Original value

util.longToHash(value){ string }

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

Parameters:
value Long | number

Value to convert

Returns:
string Hash

util.makeProp(obj, key, enumerable){ undefined }

Makes a property safe for assignment as an own property.

Parameters:
obj Object.<string, *>

Object

key string

Property key

enumerable boolean true optional

Whether the property should be enumerable

Returns:
undefined

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

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

Parameters:
dst Object.<string, *>

Destination object

src Object.<string, *>

Source object

ifNotSet boolean false optional

Merges only if the key is not already set

Returns:
Object.<string, *> Destination object

util.newBuffer(sizeOrArray){ Uint8Array | Buffer }

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

Parameters:
sizeOrArray number | Array.<number> 0 optional

Buffer size or number array

Returns:
Uint8Array | Buffer Buffer

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

Creates a custom error constructor.

Parameters:
name string

Error name

Returns:
Constructor.<Error> Custom error constructor

util.oneOfGetter(fieldNames){ OneOfGetter }

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

Parameters:
fieldNames Array.<string>

Field names

Returns:
OneOfGetter Unbound getter

util.oneOfSetter(fieldNames){ OneOfSetter }

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

Parameters:
fieldNames Array.<string>

Field names

Returns:
OneOfSetter Unbound setter

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

A general purpose buffer pool.

Parameters:
alloc PoolAllocator

Allocator

slice PoolSlicer

Slicer

size number 8192 optional

Slab size

Returns:
PoolAllocator Pooled allocator

util.safeProp(prop){ string }

Returns a safe property accessor for the specified property name.

Parameters:
prop string

Property name

Returns:
string Safe accessor

util.setProperty(dst, path, value, ifNotSet){ Object<string, *> }

Sets the value of a property by property path. If a value already exists, it is turned to an array

Parameters:
dst Object.<string, *>

Destination object

path string

dot '.' delimited path of the property to set

value Object

the value to set

ifNotSet boolean | undefined optional

Sets the option only if it isn't currently set

Returns:
Object.<string, *> Destination object

util.toArray(object){ *[] }

Converts an object's values to an array.

Parameters:
object Object.<string, *>

Object to convert

Returns:
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.

Parameters:
array Array.<*>

Array to convert

Returns:
Object.<string, *> Converted object

util.ucFirst(str){ string }

Converts the first character of a string to upper case.

Parameters:
str string

String to convert

Returns:
string Converted string