Module: utils/util

Members

(private, static) _paths :Object

Cached mkdirp paths.

Type:
  • Object
Source:

(static, constant) global :Object

Reference to the global object.

Type:
  • Object
Source:

(static, constant) isBrowser :Boolean

Whether we're in a browser or not.

Type:
  • Boolean
Source:

(static, constant) MAX_SAFE_ADDITION :Number

Max 52 bit integer (safe for additions). (MAX_SAFE_INTEGER - 1) / 2

Type:
  • Number
Default Value:
  • 4503599627370495
Source:

(static, constant) MAX_SAFE_INTEGER :Number

Max safe integer (53 bits).

Type:
  • Number
Default Value:
  • 9007199254740991
Source:

Methods

(static) binaryInsert(items, item, compare) → {Number}

Perform a binary insert on a sorted array.

Parameters:
Name Type Description
items Array
item Object
compare function
Source:
Returns:

index

Type
Number

(static) binaryRemove(items, item, compare) → {Boolean}

Perform a binary removal on a sorted array.

Parameters:
Name Type Description
items Array
item Object
compare function
Source:
Returns:
Type
Boolean

(static) binarySearch(items, key, compare, insertnullable) → {Number}

Perform a binary search on a sorted array.

Parameters:
Name Type Attributes Description
items Array
key Object
compare function
insert Boolean <nullable>
Source:
Returns:

Index.

Type
Number

(static) cmp(a, b) → {Number}

Buffer comparator (memcmp + length comparison).

Parameters:
Name Type Description
a Buffer
b Buffer
Source:
Returns:

-1, 1, or 0.

Type
Number

(static) concat(a, b) → {Buffer}

Concatenate two buffers.

Parameters:
Name Type Description
a Buffer
b Buffer
Source:
Returns:
Type
Buffer

(static) copy(data) → {Buffer}

Clone a buffer.

Parameters:
Name Type Description
data Buffer
Source:
Returns:
Type
Buffer

(static) date(tsnullable) → {String}

Create a Date ISO string from time in unix time (seconds).

Parameters:
Name Type Attributes Description
ts Number <nullable>

Seconds in unix time.

Source:
Returns:
Type
String

(static) equal(anullable, bnullable) → {Boolean}

Test whether two buffers are equal.

Parameters:
Name Type Attributes Description
a Buffer <nullable>
b Buffer <nullable>
Source:
Returns:
Type
Boolean

(static) error(obj, …args)

Write a message to stderr (console in browser).

Parameters:
Name Type Attributes Description
obj Object | String
args String <repeatable>
Source:

(static) fastProp(obj)

Ensure hidden-class mode for object.

Parameters:
Name Type Description
obj Object
Source:

(static) fmt()

Format a string.

Source:

(static) format(args, colornullable) → {String}

Format a string.

Parameters:
Name Type Attributes Description
args Array
color Boolean <nullable>
Source:
Returns:
Type
String

(static) gc()

Garbage collector for --expose-gc.

Source:

(static) hex8(num) → {String}

Convert a number to a padded uint8 string (2 digits in hex).

Parameters:
Name Type Description
num Number
Source:
Returns:

Padded number.

Type
String

(static) hex32(num) → {String}

Convert a number to a padded uint32 string (8 digits in hex).

Parameters:
Name Type Description
num Number
Source:
Returns:

Padded number.

Type
String

(static) hrtime(time) → {Array}

Return hrtime (shim for browser).

Parameters:
Name Type Description
time Array
Source:
Returns:
Type
Array

(static) indexOf(obj, data) → {Number}

Find index of a buffer in an array of buffers.

Parameters:
Name Type Description
obj Array:.<Buffer:>
data Buffer

Target buffer to find.

Source:
Returns:

Index (-1 if not found).

Type
Number

(static) inherits(obj, from)

Inheritance.

Parameters:
Name Type Description
obj function

Constructor to inherit.

from function

Parent constructor.

Source:

(static) inspectify(obj, colornullable) → {String}

util.inspect() with 20 levels of depth.

Parameters:
Name Type Attributes Description
obj Object | String
color Boolean <nullable>
Source:
Returns:
Type
String

(static) isBase58(objnullable) → {Boolean}

Test whether a string is base58 (note that you may get a false positive on a hex string).

Parameters:
Name Type Attributes Description
obj String <nullable>
Source:
Returns:
Type
Boolean

(static) isFloat(valuenullable) → {Boolean}

Test whether a string qualifies as a float.

Parameters:
Name Type Attributes Description
value String <nullable>
Source:
Returns:
Type
Boolean

(static) isHex(objnullable) → {Boolean}

Test whether a string is hex (length must be even). Note that this could yield a false positive on base58 strings.

Parameters:
Name Type Attributes Description
obj String <nullable>
Source:
Returns:
Type
Boolean

(static) isHex160(valuenullable) → {Boolean}

Test whether an object is a 160 bit hash (hex string).

Parameters:
Name Type Attributes Description
value String <nullable>
Source:
Returns:
Type
Boolean

(static) isHex256(valuenullable) → {Boolean}

Test whether an object is a 256 bit hash (hex string).

Parameters:
Name Type Attributes Description
value String <nullable>
Source:
Returns:
Type
Boolean

(static) isInt(valuenullable) → {Boolean}

Test whether an object is an int.

Parameters:
Name Type Attributes Description
value Number <nullable>
Source:
Returns:
Type
Boolean

(static) isInt8(valuenullable) → {Boolean}

Test whether an object is an int8.

Parameters:
Name Type Attributes Description
value Number <nullable>
Source:
Returns:
Type
Boolean

(static) isInt32(valuenullable) → {Boolean}

Test whether an object is an int32.

Parameters:
Name Type Attributes Description
value Number <nullable>
Source:
Returns:
Type
Boolean

(static) isInt53(valuenullable) → {Boolean}

Test whether an object is a int53.

Parameters:
Name Type Attributes Description
value Number <nullable>
Source:
Returns:
Type
Boolean

(static) isNumber(valuenullable) → {Boolean}

Test whether a number is Number, finite, and below MAX_SAFE_INTEGER.

Parameters:
Name Type Attributes Description
value Number <nullable>
Source:
Returns:
Type
Boolean

(static) isSafeInteger(value) → {Boolean}

Test whether a number is below MAX_SAFE_INTEGER.

Parameters:
Name Type Description
value Number
Source:
Returns:
Type
Boolean

(static) isUInt8(valuenullable) → {Boolean}

Test whether an object is a uint8.

Parameters:
Name Type Attributes Description
value Number <nullable>
Source:
Returns:
Type
Boolean

(static) isUInt32(valuenullable) → {Boolean}

Test whether an object is a uint32.

Parameters:
Name Type Attributes Description
value Number <nullable>
Source:
Returns:
Type
Boolean

(static) isUInt53(valuenullable) → {Boolean}

Test whether an object is a uint53.

Parameters:
Name Type Attributes Description
value Number <nullable>
Source:
Returns:
Type
Boolean

(static) isUpperCase(str) → {Boolean}

Quick test to see if a string is uppercase.

Parameters:
Name Type Description
str String
Source:
Returns:
Type
Boolean

(static) log(obj, …args)

Write a message to stdout (console in browser).

Parameters:
Name Type Attributes Description
obj Object | String
args String <repeatable>
Source:

(static) mb(size) → {Number}

Convert bytes to mb.

Parameters:
Name Type Description
size Number
Source:
Returns:

mb

Type
Number

(static) merge(target, …args) → {Object}

Shallow merge between multiple objects.

Parameters:
Name Type Attributes Description
target Object
args Object <repeatable>
Source:
Returns:

target

Type
Object

(static) mkdir(path, dirnamenullable)

Ensure a directory.

Parameters:
Name Type Attributes Description
path String
dirname Boolean <nullable>
Source:

(static) mkdirp(path)

Create a full directory structure.

Parameters:
Name Type Description
path String
Source:

(static) ms() → {Number}

Get current time in unix time (milliseconds).

Source:
Returns:
Type
Number

(static) nextTick() → {Promise}

Call setImmediate, process.nextTick, or setInterval depending.

Source:
Returns:
Type
Promise

(static) nonce() → {Buffer}

Create a 64 bit nonce.

Source:
Returns:
Type
Buffer

(static) nop()

Global NOP function.

Source:

(static) normalize(path, dirnamenullable)

Normalize a path.

Parameters:
Name Type Attributes Description
path String
dirname Boolean <nullable>
Source:

(static) now() → {Number}

Get current time in unix time (seconds).

Source:
Returns:
Type
Number

(static) pad8(num) → {String}

Convert a number to a padded uint8 string (3 digits in decimal).

Parameters:
Name Type Description
num Number
Source:
Returns:

Padded number.

Type
String

(static) pad32(num) → {String}

Convert a number to a padded uint32 string (10 digits in decimal).

Parameters:
Name Type Description
num Number
Source:
Returns:

Padded number.

Type
String

(static) promisify(func) → {function}

Promisify a function.

Parameters:
Name Type Description
func function
Source:
Returns:
Type
function

(static) random(min, max) → {Number}

Get random range.

Parameters:
Name Type Description
min Number
max Number
Source:
Returns:
Type
Number

(static) revHex(data) → {String}

Reverse a hex-string (used because of bitcoind's affinity for uint256le).

Parameters:
Name Type Description
data String

Hex string.

Source:
Returns:

Reversed hex string.

Type
String

(static) revMap(map) → {Object}

Reverse a map.

Parameters:
Name Type Description
map Object
Source:
Returns:

Reversed map.

Type
Object

(static) startsWith(str, prefix) → {Boolean}

Test to see if a string starts with a prefix.

Parameters:
Name Type Description
str String
prefix String
Source:
Returns:
Type
Boolean

(static) strcmp(a, b) → {Number}

String comparator (memcmp + length comparison).

Parameters:
Name Type Description
a Buffer
b Buffer
Source:
Returns:

-1, 1, or 0.

Type
Number

(static) time(date) → {Number}

Get unix seconds from a Date string.

Parameters:
Name Type Description
date String

Date ISO String.

Source:
Returns:
Type
Number

(static) toMap(obj) → {Object}

Convert an array to a map.

Parameters:
Name Type Description
obj Array:.<String:>
Source:
Returns:

Map.

Type
Object

(static) uniq(obj) → {Array:.<String:>}

Unique-ify an array of strings.

Parameters:
Name Type Description
obj Array:.<String:>
Source:
Returns:
Type
Array:.<String:>

(static) uniqBuffer(items) → {Array:.<Buffer:>}

Unique-ify and sort an array of buffers.

Parameters:
Name Type Description
items Array:.<Buffer:>
Source:
Returns:
Type
Array:.<Buffer:>

(static) values(map) → {Array}

Get object values.

Parameters:
Name Type Description
map Object
Source:
Returns:

Values.

Type
Array