diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..11f0a1e0d --- /dev/null +++ b/docs/README.md @@ -0,0 +1,10 @@ +# Available interfaces + +Utility methods for this package are split into groups + +- [bn](bn.md) Utility methods to convert to and from `BN` objects +- [buffer](buffer.md) Utility methods to convert to and from `Buffer` objects +- [hex](hex.md) Internal utilities to create and test for hex values +- [is](is.md) Type checking utilities +- [jsonrpc](jsonrpc.md) Convenience functions for values from @polkadot/jsonrpc +- [keccak](keccak.md) Create Keccak256 values as hex, string & buffer output \ No newline at end of file diff --git a/docs/bn.md b/docs/bn.md new file mode 100644 index 000000000..76e00a694 --- /dev/null +++ b/docs/bn.md @@ -0,0 +1,41 @@ +# bn + +Utility methods to convert to and from `BN` objects + +- (bnFromHex)[#bnFromHex] Creates a BN.js bignumber object from a hex string. +- (bnToHex)[#bnToHex] Creates a hex value from a BN.js bignumber object. + +## bnFromHex + +Creates a BN.js bignumber object from a hex string. + +```js +bnFromHex (value?: string): BN +``` + + +`null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error. + +```js +import { bnFromHex } from '@polkadot/util'; + +console.log('BN object', bnFromHex('0x123480001f')); +``` + +## bnToHex + +Creates a hex value from a BN.js bignumber object. + +```js +bnToHex (value?: BN): string +``` + + +`null` inputs returns a `0x` result, BN values return the actual value as a `0x` prefixed hex value. Anything that is not a BN object throws an error. + +```js +import BN from 'bn.js'; +import { bnToHex } from '@polkadot/util'; + +console.log('Hex value', bnToHex(new BN(123456)); +``` \ No newline at end of file diff --git a/docs/buffer.md b/docs/buffer.md new file mode 100644 index 000000000..bfa825953 --- /dev/null +++ b/docs/buffer.md @@ -0,0 +1,40 @@ +# buffer + +Utility methods to convert to and from `Buffer` objects + +- (bufferFromHex)[#bufferFromHex] Creates a Buffer object from a hex string. +- (bufferToHex)[#bufferToHex] Creates a hex value from a Buffer object. + +## bufferFromHex + +Creates a Buffer object from a hex string. + +```js +bufferFromHex (value?: string): Buffer +``` + + +`null` inputs returns an empty `Buffer` result. Hex input values return the actual bytes value converted to a Buffer. Anything that is not a hex string (including the `0x` prefix) throws an error. + +```js +import { bufferFromHex } from '@polkadot/util'; + +console.log('Buffer object', bufferFromHex('0x123480001f')); +``` + +## bufferToHex + +Creates a hex value from a Buffer object. + +```js +bufferToHex (value?: Buffer): string +``` + + +`null` inputs returns a `0x` result, `Buffer` values return the actual value as a `0x` prefixed hex value. Anything that is not a `Buffer` object throws an error. + +```js +import { bufferToHex } from '@polkadot/util'; + +console.log('Hex value', bufferToHex(Buffer.from([1, 2, 3])); +``` \ No newline at end of file diff --git a/docs/hex.md b/docs/hex.md new file mode 100644 index 000000000..d77cb3f0b --- /dev/null +++ b/docs/hex.md @@ -0,0 +1,58 @@ +# hex + +Internal utilities to create and test for hex values + +- (hexAddPrefix)[#hexAddPrefix] Adds the `0x` prefix to string values. +- (hexHasPrefix)[#hexHasPrefix] Tests for the existence of a `0x` prefix. +- (hexStripPrefix)[#hexStripPrefix] Strips any leading `0x` prefix. + +## hexAddPrefix + +Adds the `0x` prefix to string values. + +```js +hexAddPrefix (value: ?string): string +``` + + +Returns a `0x` prefixed string from the input value. If the input is already prefixed, it is returned unchanged. + +```js +import { hexAddPrefix } from '@polkadot/util'; + +console.log('With prefix', hexAddPrefix('0a0b12')) // => 0x0a0b12 +``` + +## hexHasPrefix + +Tests for the existence of a `0x` prefix. + +```js +hexHasPrefix (value: ?string): boolean +``` + + +Checks for a valid hex input value and if the start matched `0x` + +```js +import { hexHasPrefix } from '@polkadot/util'; + +console.log('has prefix', hexHasPrefix('0x1234')); // => true +``` + +## hexStripPrefix + +Strips any leading `0x` prefix. + +```js +hexStripPrefix (value: ?string): string +``` + + +Tests for the existence of a `0x` prefix, and returns the value without the prefix. Un-prefixed values are returned as-is. + +```js +import { hexStripPrefix } from '@polkadot/util'; + +console.log('stripped', hexStripPrefix('0x1234')); // => 1234 +``` \ No newline at end of file diff --git a/docs/is.md b/docs/is.md new file mode 100644 index 000000000..01ecd92a2 --- /dev/null +++ b/docs/is.md @@ -0,0 +1,149 @@ +# is + +Type checking utilities + +- (isBN)[#isBN] Tests for a `BN` object instance. +- (isBuffer)[#isBuffer] Tests for a `Buffer` object instance. +- (isFunction)[#isFunction] Tests for a `function`. +- (isHex)[#isHex] Tests for a hex string. +- (isInstanceOf)[#isInstanceOf] Tests for a instance of a class. +- (isNumber)[#isNumber] Tests for a JavaScript number. +- (isString)[#isString] Tests for a string. +- (isUndefined)[#isUndefined] Tests for a `undefined` values. + +## isBN + +Tests for a `BN` object instance. + +```js +isBN (value: any): boolean +``` + + +Checks to see if the input object is an instance of `BN` (bn.js). + +```js +import BN from 'bn.js'; +import { isBN } from '@polkadot/util'; + +console.log('isBN', isBN(new BN(1))); // => true +``` + +## isBuffer + +Tests for a `Buffer` object instance. + +```js +isBuffer (value: any): boolean +``` + + +Checks to see if the input object is an instance of `Buffer`. + +```js +import { isBuffer } from '@polkadot/util'; + +console.log('isBuffer', isBuffer(Buffer.from([]))); // => true +``` + +## isFunction + +Tests for a `function`. + +```js +isFunction (value: any): boolean +``` + + +Checks to see if the input value is a JavaScript function. + +```js +import { isFunction } from '@polkadot/util'; + +console.log('isFunction', isFunction(() => false)); // => true +``` + +## isHex + +Tests for a hex string. + +```js +isHex (value: any): boolean +``` + + +Checks to see if the input value is a `0x` prefixed hex string. + +```js +import { isHex } from '@polkadot/util'; + +console.log('isHex', isHex('0x1234')); // => true +``` + +## isInstanceOf + +Tests for a instance of a class. + +```js +isInstanceOf (value: any, clazz: any): boolean +``` + + +Checks to see if the input value is an instance of the test class. + +```js +import { isInstanceOf } from '@polkadot/util'; + +console.log('isinstanceOf', isInstanceOf(new Array(0), Array)); // => true +``` + +## isNumber + +Tests for a JavaScript number. + +```js +isNumber (value: any): boolean +``` + + +Checks to see if the input value is a valid number. + +```js +import { isNumber } from '@polkadot/util'; + +console.log('isNumber', isNumber(1234)); // => true +``` + +## isString + +Tests for a string. + +```js +isString (value: any): boolean +``` + + +Checks to see if the input value is a JavaScript string. + +```js +import { isString } from '@polkadot/util'; + +console.log('isString', isString('test')); // => true +``` + +## isUndefined + +Tests for a `undefined` values. + +```js +isUndefined (value: any): boolean +``` + + +Checks to see if the input value is `undefined`. + +```js +import { isUndefined } from '@polkadot/util'; + +console.log('isUndefined', isUndefined(void(0))); // => true +``` \ No newline at end of file diff --git a/docs/jsonrpc.md b/docs/jsonrpc.md new file mode 100644 index 000000000..dc5564e3b --- /dev/null +++ b/docs/jsonrpc.md @@ -0,0 +1,24 @@ +# jsonrpc + +Convenience functions for values from @polkadot/jsonrpc + +- (jsonrpcSignature)[#jsonrpcSignature] Returns a string representation of the method with inputs and outputs. + +## jsonrpcSignature + +Returns a string representation of the method with inputs and outputs. + +```js +jsonrpcSignature (name: string, _inputs: Array, _output: InterfaceOutputType): string +``` + + +Formats the name, inputs and outputs into a human-readable string. This contains the input parameter names, input types and output type. + +```js +import { jsonrpcSignature } from '@polkadot/util'; + +console.log(jsonrpcSignature( +'test_method', [{ name: 'dest', type: 'Address' }], { type: 'Address' } +)); // => test_method (destination: Address): Address +``` \ No newline at end of file diff --git a/docs/keccak.md b/docs/keccak.md new file mode 100644 index 000000000..949277dca --- /dev/null +++ b/docs/keccak.md @@ -0,0 +1,58 @@ +# keccak + +Create Keccak256 values as hex, string & buffer output + +- (keccakAsBuffer)[#keccakAsBuffer] Creates a keccak Buffer from the input. +- (keccakAsHex)[#keccakAsHex] Creates a keccak hex string from the input. +- (keccakAsString)[#keccakAsString] Creates a keccak string from the input. + +## keccakAsBuffer + +Creates a keccak Buffer from the input. + +```js +keccakAsBuffer (value: Buffer | string): Buffer +``` + + +From either a `string` or a `Buffer` input, create the keccak and return the result as a `Buffer`. + +```js +import { keccakAsBuffer } from '@polkadot/util'; + +console.log('asBuffer', keccakAsBuffer('123')) // => Buffer +``` + +## keccakAsHex + +Creates a keccak hex string from the input. + +```js +keccakAsHex (value: Buffer | string): string +``` + + +From either a `string` or a `Buffer` input, create the keccak and return the result as a `0x` prefixed hex string. + +```js +import { keccakAsHex } from '@polkadot/util'; + +console.log('asHex', keccakAsHex('123')) // => 0x... +``` + +## keccakAsString + +Creates a keccak string from the input. + +```js +keccakAsString (value: Buffer | string): string +``` + + +From either a `string` or a `Buffer` input, create the keccak and return the result as a non-prefixed string. + +```js +import { keccakAsString } from '@polkadot/util'; + +console.log('asString', keccakAsString('123')) // => string +``` \ No newline at end of file