Use @polkadot/dev for common tasks (#8)

* Link to docs in README

* Use @polkadot/dev for common tasks

* 0.5.0 since docs added
This commit is contained in:
Jaco Greeff
2017-11-20 23:04:33 +01:00
committed by GitHub
parent 4ec391e81d
commit 1216968145
38 changed files with 2472 additions and 350 deletions
+12 -1
View File
@@ -8,7 +8,18 @@ const isHex = require('../is/hex');
const ZERO_BN = new BN(0);
module.exports = function fromHex (value?: string): BN {
/**
@name bnFromHex
@signature bnFromHex (value?: string): BN
@summary Creates a BN.js bignumber object from a hex string.
@description
`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.
@example
import { bnFromHex } from '@polkadot/util';
console.log('BN object', bnFromHex('0x123480001f'));
*/
module.exports = function bnFromHex (value?: string): BN {
if (!value) {
return ZERO_BN;
}
+3
View File
@@ -4,6 +4,9 @@
const bnFromHex = require('./fromHex');
const bnToHex = require('./toHex');
/**
@summary Utility methods to convert to and from `BN` objects
*/
module.exports = {
bnFromHex,
bnToHex
+13 -1
View File
@@ -8,7 +8,19 @@ const isBN = require('../is/bn');
const ZERO_STR = '0x';
module.exports = function toHex (value?: BN): string {
/**
@name bnToHex
@signature bnToHex (value?: BN): string
@summary Creates a hex value from a BN.js bignumber object.
@description
`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.
@example
import BN from 'bn.js';
import { bnToHex } from '@polkadot/util';
console.log('Hex value', bnToHex(new BN(123456));
*/
module.exports = function bnToHex (value?: BN): string {
if (!value) {
return ZERO_STR;
}
+12 -1
View File
@@ -6,7 +6,18 @@ const hexStripPrefix = require('../hex/stripPrefix');
const EMPTY_BUFFER = Buffer.from([]);
module.exports = function fromHex (value?: string): Buffer {
/**
@name bufferFromHex
@signature bufferFromHex (value?: string): Buffer
@summary Creates a Buffer object from a hex string.
@description
`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.
@example
import { bufferFromHex } from '@polkadot/util';
console.log('Buffer object', bufferFromHex('0x123480001f'));
*/
module.exports = function bufferFromHex (value?: string): Buffer {
if (!value) {
return EMPTY_BUFFER;
}
+3
View File
@@ -4,6 +4,9 @@
const bufferFromHex = require('./fromHex');
const bufferToHex = require('./toHex');
/**
@summary Utility methods to convert to and from `Buffer` objects
*/
module.exports = {
bufferFromHex,
bufferToHex
+12 -1
View File
@@ -6,7 +6,18 @@ const hexAddPrefix = require('../hex/addPrefix');
const ZERO_HEX = '0x';
module.exports = function toHex (value?: Buffer): string {
/**
@name bufferToHex
@signature bufferToHex (value?: Buffer): string
@summary Creates a hex value from a Buffer object.
@description
`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.
@example
import { bufferToHex } from '@polkadot/util';
console.log('Hex value', bufferToHex(Buffer.from([1, 2, 3]));
*/
module.exports = function bufferToHex (value?: Buffer): string {
if (!value) {
return ZERO_HEX;
}
+12 -1
View File
@@ -3,7 +3,18 @@
const hasPrefix = require('./hasPrefix');
module.exports = function addPrefix (value: ?string): string {
/**
@name hexAddPrefix
@signature hexAddPrefix (value: ?string): string
@summary Adds the `0x` prefix to string values.
@description
Returns a `0x` prefixed string from the input value. If the input is already prefixed, it is returned unchanged.
@example
import { hexAddPrefix } from '@polkadot/util';
console.log('With prefix', hexAddPrefix('0a0b12')) // => 0x0a0b12
*/
module.exports = function hexAddPrefix (value: ?string): string {
if (value && hasPrefix(value)) {
return value;
}
+12 -1
View File
@@ -3,6 +3,17 @@
const isHex = require('../is/hex');
module.exports = function hasPrefix (value: ?string): boolean {
/**
@name hexHasPrefix
@signature hexHasPrefix (value: ?string): boolean
@summary Tests for the existence of a `0x` prefix.
@description
Checks for a valid hex input value and if the start matched `0x`
@example
import { hexHasPrefix } from '@polkadot/util';
console.log('has prefix', hexHasPrefix('0x1234')); // => true
*/
module.exports = function hexHasPrefix (value: ?string): boolean {
return !!(value && isHex(value) && value.substr(0, 2) === '0x');
};
+3
View File
@@ -5,6 +5,9 @@ const hexAddPrefix = require('./addPrefix');
const hexHasPrefix = require('./hasPrefix');
const hexStripPrefix = require('./stripPrefix');
/**
@summary Internal utilities to create and test for hex values
*/
module.exports = {
hexAddPrefix,
hexHasPrefix,
+12 -1
View File
@@ -3,7 +3,18 @@
const hasPrefix = require('./hasPrefix');
module.exports = function stripPrefix (value: ?string): string {
/**
@name hexStripPrefix
@signature hexStripPrefix (value: ?string): string
@summary Strips any leading `0x` prefix.
@description
Tests for the existence of a `0x` prefix, and returns the value without the prefix. Un-prefixed values are returned as-is.
@example
import { hexStripPrefix } from '@polkadot/util';
console.log('stripped', hexStripPrefix('0x1234')); // => 1234
*/
module.exports = function hexStripPrefix (value: ?string): string {
if (value && hasPrefix(value)) {
return value.substr(2);
}
+5 -1
View File
@@ -5,8 +5,12 @@ const bn = require('./bn');
const buffer = require('./buffer');
const hex = require('./hex');
const is = require('./is');
const jsonrpc = require('./jsonrpc');
const keccak = require('./keccak');
/**
@summary Utility methods for this package are split into groups
*/
module.exports = Object.assign(
{}, bn, buffer, hex, is, keccak
{}, bn, buffer, hex, is, jsonrpc, keccak
);
+12
View File
@@ -3,6 +3,18 @@
const BN = require('bn.js');
/**
@name isBN
@signature isBN (value: any): boolean
@summary Tests for a `BN` object instance.
@description
Checks to see if the input object is an instance of `BN` (bn.js).
@example
import BN from 'bn.js';
import { isBN } from '@polkadot/util';
console.log('isBN', isBN(new BN(1))); // => true
*/
module.exports = function isBN (value: any): boolean {
return BN.isBN(value);
};
+12 -1
View File
@@ -3,6 +3,17 @@
const isInstanceOf = require('./instanceOf');
module.exports = function isBuffer (value?: any): boolean {
/**
@name isBuffer
@signature isBuffer (value: any): boolean
@summary Tests for a `Buffer` object instance.
@description
Checks to see if the input object is an instance of `Buffer`.
@example
import { isBuffer } from '@polkadot/util';
console.log('isBuffer', isBuffer(Buffer.from([]))); // => true
*/
module.exports = function isBuffer (value: any): boolean {
return isInstanceOf(value, Buffer);
};
+11
View File
@@ -1,6 +1,17 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
/**
@name isFunction
@signature isFunction (value: any): boolean
@summary Tests for a `function`.
@description
Checks to see if the input value is a JavaScript function.
@example
import { isFunction } from '@polkadot/util';
console.log('isFunction', isFunction(() => false)); // => true
*/
module.exports = function isFunction (value: any): boolean {
return typeof value === 'function';
};
+11
View File
@@ -5,6 +5,17 @@ const isString = require('./string');
const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
/**
@name isHex
@signature isHex (value: any): boolean
@summary Tests for a hex string.
@description
Checks to see if the input value is a `0x` prefixed hex string.
@example
import { isHex } from '@polkadot/util';
console.log('isHex', isHex('0x1234')); // => true
*/
module.exports = function isHex (value: any): boolean {
return isString(value) && HEX_REGEX.test(value);
};
+3
View File
@@ -9,6 +9,9 @@ const isNumber = require('./number');
const isString = require('./string');
const isUndefined = require('./undefined');
/**
@summary Type checking utilities
*/
module.exports = {
isBN,
isFunction,
+11
View File
@@ -1,6 +1,17 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
/**
@name isInstanceOf
@signature isInstanceOf (value: any, clazz: any): boolean
@summary Tests for a instance of a class.
@description
Checks to see if the input value is an instance of the test class.
@example
import { isInstanceOf } from '@polkadot/util';
console.log('isinstanceOf', isInstanceOf(new Array(0), Array)); // => true
*/
module.exports = function isInstanceOf (value: any, clazz: any): boolean {
return value instanceof clazz;
};
+11
View File
@@ -1,6 +1,17 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
/**
@name isNumber
@signature isNumber (value: any): boolean
@summary Tests for a JavaScript number.
@description
Checks to see if the input value is a valid number.
@example
import { isNumber } from '@polkadot/util';
console.log('isNumber', isNumber(1234)); // => true
*/
module.exports = function isNumber (value: any): boolean {
return typeof value === 'number';
};
+11
View File
@@ -1,6 +1,17 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
/**
@name isString
@signature isString (value: any): boolean
@summary Tests for a string.
@description
Checks to see if the input value is a JavaScript string.
@example
import { isString } from '@polkadot/util';
console.log('isString', isString('test')); // => true
*/
module.exports = function isString (value: any): boolean {
return typeof value === 'string';
};
+11
View File
@@ -1,6 +1,17 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
/**
@name isUndefined
@signature isUndefined (value: any): boolean
@summary Tests for a `undefined` values.
@description
Checks to see if the input value is `undefined`.
@example
import { isUndefined } from '@polkadot/util';
console.log('isUndefined', isUndefined(void(0))); // => true
*/
module.exports = function isUndefined (value: any): boolean {
return typeof value === 'undefined';
};
+11
View File
@@ -0,0 +1,11 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const jsonrpcSignature = require('./signature');
/**
@summary Convenience functions for values from @polkadot/jsonrpc
*/
module.exports = {
jsonrpcSignature
};
+20
View File
@@ -0,0 +1,20 @@
// ISC, Copyright 2017 Jaco Greeff
const { jsonrpcSignature } = require('./index');
describe('jsonrpc', () => {
describe('jsonrpcSignature', () => {
it('formats the signature according to the specification', () => {
expect(
jsonrpcSignature(
'test_method',
[
{ name: 'b', type: 'A' },
{ name: 'a', type: 'B' }
],
{ type: 'A' }
)
).to.equal('test_method (b: A, a: B): A');
});
});
});
+25
View File
@@ -0,0 +1,25 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
import type { InterfaceInputType, InterfaceOutputType } from '@polkadot/jsonrpc/types';
/**
@name jsonrpcSignature
@signature jsonrpcSignature (name: string, _inputs: Array<InterfaceInputType>, _output: InterfaceOutputType): string
@summary Returns a string representation of the method with inputs and outputs.
@description
Formats the name, inputs and outputs into a human-readable string. This contains the input parameter names, input types and output type.
@example
import { jsonrpcSignature } from '@polkadot/util';
console.log(jsonrpcSignature(
'test_method', [{ name: 'dest', type: 'Address' }], { type: 'Address' }
)); // => test_method (destination: Address): Address
*/
module.exports = function jsonrpcSignature (name: string, _inputs: Array<InterfaceInputType>, _output: InterfaceOutputType): string {
const inputParams: string = _inputs
.map(({ name, type }) => `${name}: ${type}`)
.join(', ');
return `${name} (${inputParams}): ${_output.type}`;
};
+11
View File
@@ -3,6 +3,17 @@
const createKeccak = require('keccak');
/**
@name keccakAsBuffer
@signature keccakAsBuffer (value: Buffer | string): Buffer
@summary Creates a keccak Buffer from the input.
@description
From either a `string` or a `Buffer` input, create the keccak and return the result as a `Buffer`.
@example
import { keccakAsBuffer } from '@polkadot/util';
console.log('asBuffer', keccakAsBuffer('123')) // => Buffer
*/
module.exports = function keccakAsBuffer (value: Buffer | string): Buffer {
return createKeccak('keccak256').update(value).digest();
};
+11
View File
@@ -4,6 +4,17 @@
const keccakAsBuffer = require('./asBuffer');
const bufferToHex = require('../buffer/toHex');
/**
@name keccakAsHex
@signature keccakAsHex (value: Buffer | string): string
@summary Creates a keccak hex string from the input.
@description
From either a `string` or a `Buffer` input, create the keccak and return the result as a `0x` prefixed hex string.
@example
import { keccakAsHex } from '@polkadot/util';
console.log('asHex', keccakAsHex('123')) // => 0x...
*/
module.exports = function keccakAsHex (value: Buffer | string): string {
return bufferToHex(
keccakAsBuffer(value)
+11
View File
@@ -3,6 +3,17 @@
const keccakAsBuffer = require('./asBuffer');
/**
@name keccakAsString
@signature keccakAsString (value: Buffer | string): string
@summary Creates a keccak string from the input.
@description
From either a `string` or a `Buffer` input, create the keccak and return the result as a non-prefixed string.
@example
import { keccakAsString } from '@polkadot/util';
console.log('asString', keccakAsString('123')) // => string
*/
module.exports = function keccakAsString (value: Buffer | string): string {
return keccakAsBuffer(value).toString('hex');
};
+3
View File
@@ -5,6 +5,9 @@ const keccakAsBuffer = require('./asBuffer');
const keccakAsHex = require('./asHex');
const keccakAsString = require('./asString');
/**
@summary Create Keccak256 values as hex, string & buffer output
*/
module.exports = {
keccakAsBuffer,
keccakAsHex,