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
+1 -10
View File
@@ -1,12 +1,3 @@
{
"presets": [
"env",
"stage-0"
],
"plugins": [
"transform-flow-strip-types",
"transform-class-properties",
"transform-object-rest-spread",
"transform-runtime"
]
"extends": "@polkadot/dev/.babelrc"
}
+2 -32
View File
@@ -1,35 +1,5 @@
{
"env": {
"browser": true,
"mocha": true,
"node": true
},
"extends": [
"semistandard",
"plugin:flowtype/recommended"
],
"globals": {
"expect": true
},
"parser": "babel-eslint",
"plugins": [
"flowtype"
],
"rules": {
"curly": ["error", "all"],
"newline-after-var": ["error", "always"],
"no-alert": "error",
"no-debugger": "error",
"no-duplicate-imports": ["error", {
"includeExports": true
}],
"object-curly-spacing": ["error", "always"],
"object-property-newline": 0,
"one-var-declaration-per-line": ["error", "always"],
"padded-blocks": ["error", {
"blocks": "never",
"classes": "never",
"switches": "never"
}]
}
"./node_modules/@polkadot/dev/.eslintrc.json"
]
}
+2 -1
View File
@@ -1,4 +1,5 @@
build/
coverage/
lib/
docs/
node_modules/
tmp/
+1
View File
@@ -1,4 +1,5 @@
coverage
docs
scripts
src
test
+1 -1
View File
@@ -1,4 +1,4 @@
language: node_js
node_js:
- "8"
script: ./scripts/travis.sh
script: node_modules/.bin/polkadot-dev-build-travis build check coveralls npm test
+12 -3
View File
@@ -1,6 +1,15 @@
ISC License (ISC)
Copyright 2017 Jaco Greeff
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
Copyright 2017 Jaco Greeff <jacogr@gmail.com>
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
+16
View File
@@ -15,3 +15,19 @@ Installation -
```
npm install --save @polkadot/util
```
Functions can be imported directly from the package, e.g.
```js
import { isHex } from '@polkadot/util';
```
Alternatively the function can be accessed directly,
```js
import isHex from '@polkadot/util/is/hex';
```
## Available Utilities
For a list of currently exposed methods, see the [library documentation](docs/README.md).
View File
+2146 -208
View File
File diff suppressed because it is too large Load Diff
+7 -29
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util",
"version": "0.4.1",
"version": "0.5.0",
"description": "A collection of useful utilities for @polkadot",
"main": "index.js",
"keywords": [
@@ -24,47 +24,25 @@
},
"homepage": "https://github.com/polkadot-js/util#readme",
"scripts": {
"build": "npm run build:js",
"build:js": "rimraf lib && babel src --out-dir lib --ignore *.spec.js,types.js",
"check": "npm run check:flow && npm run check:lint",
"build": "npm run build:js && npm run build:md",
"build:js": "rimraf lib && babel src --out-dir build --ignore *.spec.js",
"build:md": "polkadot-dev-build-docs",
"check": "npm run check:lint && npm run check:flow",
"check:flow": "flow check",
"check:lint": "eslint src",
"ci:makeshift": "makeshift",
"ci:coveralls": "coveralls < coverage/lcov.info",
"test": "cross-env NODE_ENV=test babel-istanbul cover _mocha 'src/*.spec.js' 'src/**/*.spec.js'"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.2",
"@polkadot/dev": "^0.1.5",
"@polkadot/jsonrpc": "^0.3.4",
"babel-istanbul": "^0.12.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"babel-preset-flow": "^6.23.0",
"babel-preset-stage-0": "^6.24.1",
"chai": "^4.1.2",
"coveralls": "^3.0.0",
"cross-env": "^5.1.1",
"eslint": "^4.11.0",
"eslint-config-semistandard": "^11.0.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-flowtype": "^2.39.1",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"flow-bin": "^0.59.0",
"makeshift": "^1.1.0",
"mocha": "^4.0.1",
"rimraf": "^2.6.2",
"sinon": "^4.1.2",
"sinon-chai": "^2.14.0"
},
"dependencies": {
"babel-runtime": "^6.26.0",
"bn.js": "^4.11.8",
"keccak": "^1.3.0"
}
-57
View File
@@ -1,57 +0,0 @@
#!/bin/bash
# ISC, Copyright 2017 Jaco Greeff
set -e
function setupGit () {
REPO=$1
echo "Setting up GitHub config for $REPO"
git config push.default simple
git config merge.ours.driver true
git config user.name "Travis CI"
git config user.email "$COMMIT_AUTHOR_EMAIL"
git remote set-url origin https://${GH_TOKEN}@github.com/${REPO}.git > /dev/null 2>&1
}
echo "Running code checks & build"
npm run check
npm run build
npm run test
npm run ci:coveralls
# Pull requests and commits to other branches shouldn't try to deploy, just build to verify
if [ "$TRAVIS_PULL_REQUEST" != "false" -o "$TRAVIS_BRANCH" != "master" ]; then
echo "Branch check completed"
exit 0
fi
setupGit ${TRAVIS_REPO_SLUG}
if [ -n "$(git status --untracked-files=no --porcelain)" ]; then
echo "Adding build artifacts"
git add .
git commit -m "[CI Skip] Build artifacts"
fi
echo "Incrementing package version"
npm --no-git-tag-version version
npm version patch -m "[CI Skip] Version bump"
git push --quiet origin HEAD:refs/heads/$TRAVIS_BRANCH > /dev/null 2>&1
echo "Publishing to npm"
npm run ci:makeshift
cp LICENSE package.json package-lock.json lib/
cd lib
npm publish
cd ..
echo "Release completed"
exit 0
+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,