Files
common/packages/util/docs
Jaco Greeff e832aebb07 Introduce @polkadot/util-rlp (#76)
* Add toU8a conversions

* Add u8a utilities

* @polkadot/rlp with encoding

* Decoding test runthrough

* Update util locations

* Convert util-triehash to use util-rlp

* Allow keccak from Uint8Array

* Pass Uint8Array to keccak input

* Update documentation

* Skip some doc generation

* Update @polkadot/dev

* Update doc typo

* Update allocations

* Retrieve tests from ethereumjs-testing

* Simplify test retrieval

* Exclude etereumjs-testing from flow
2018-02-08 14:50:54 +01:00
..
2018-02-08 14:50:54 +01:00
2018-01-30 12:36:01 +01:00
2018-02-08 14:50:54 +01:00
2018-02-08 14:50:54 +01:00

Available interfaces

Utility methods for this package are split into groups

  • array Utility methods that operates on arrays
  • bn Utility methods to convert to and from BN objects
  • buffer Utility methods to convert to and from Buffer objects
  • ext Extensions to basic classes
  • hex Internal utilities to create and test for hex values
  • is Type checking utilities
  • jsonrpc Convenience functions for values from @polkadot/api-jsonrpc
  • number Utility methods to convert to and from number values
  • string Utility methods to convert to work with string values
  • u8a Utility methods to convert to and from Uint8Array objects

Available methods

assert

Checks for a valid test, if not ExtError is thrown.

assert (test: mixed, message: string | () => string, code: number = ExtError.CODES.ASSERT, data: mixed): void

Checks that test is a truthy value. If value is falsy (null, undefined, false, ...), it throws an ExtError with the supplied message and an optional code and data. When test passes, true is returned.

const assert = require('@polkadot/util/assert');

assert(true, 'True should be true'); // true returned
assert(false, 'False should not be true'); // ExtError thrown
assert(false, () => 'message'); // ExtError with 'message'

Logger

Creates a consistent log interface for messages

logger (type: string): Logger

Returns a Logger that has .log, .error, .warn and .debug (constrolled with environment DEBUG=typeA,typeB) methods. Logging is done with a consistent prefix (type of logger, date) followed by the actual message using the underlying console.

const l = require('@polkadot/util/logger')('test');

l.log('blah'); // <date>     TEST: blah

promisify

Wraps an async callback into a Promise

function promisify (this: ?Object, fn: Function, ...params: Array<mixed>): Promise<any>

Wraps the supplied async function fn that has a standard JS callback (error: Error, result: any) into a Promise, passing the supplied parameters. When error is set, the Promise is rejected, else the Promise resolves with the result value.

const promisify = require('@polkadot/util/promisify');

await promisify(null, ((a, cb) => cb(null, a), true); // resolves with `true`
await promisify(null, (cb) => cb(new Error('error!'))); // rejects with `error!`