Use utils from @polkadot/util (#2)

This commit is contained in:
Jaco Greeff
2017-11-18 14:56:10 +01:00
committed by GitHub
parent 4a8dc92a5b
commit a9a5131fe7
11 changed files with 926 additions and 169 deletions
+1 -1
View File
@@ -7,9 +7,9 @@ import type { ProviderInterface } from './provider/types';
const interfaces = require('@polkadot/jsonrpc');
const { callSignature } = require('@polkadot/jsonrpc/lib/util');
const { isFunction } = require('@polkadot/util/lib/is');
const { formatInputs, formatOutput } = require('./format');
const { HttpProvider } = require('./provider');
const { isFunction } = require('./util/is');
module.exports = class Api implements ApiInterface {
_provider: ProviderInterface;
+1 -1
View File
@@ -4,7 +4,7 @@
const sinon = require('sinon');
const { isFunction } = require('./util/is');
const { isFunction } = require('@polkadot/util/lib/is');
const Api = require('./api');
+1 -1
View File
@@ -1,7 +1,7 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const { addHexPrefix, hasHexPrefix, stripHexPrefix } = require('../../util/hex');
const { addHexPrefix, hasHexPrefix, stripHexPrefix } = require('@polkadot/util/lib/hex');
const H64_ZERO: string = '00000000000000000000000000000000';
const H128_ZERO: string = `${H64_ZERO}${H64_ZERO}`;
+1 -1
View File
@@ -3,8 +3,8 @@
import type { FormatterFunction } from './types';
const { isUndefined } = require('@polkadot/util/lib/is');
const formatNoop = require('./noop');
const { isUndefined } = require('../util/is');
function format (formatters: { [any]: FormatterFunction }, type: string, value: any): any {
const formatter = formatters[type];
+1 -1
View File
@@ -3,7 +3,7 @@
import type { JsonRpcRequest, JsonRpcResponse } from './types';
const { isNumber, isUndefined } = require('../util/is');
const { isNumber, isUndefined } = require('@polkadot/util/lib/is');
module.exports = class JsonRpcCoder {
_id: number = 0;
-28
View File
@@ -1,28 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
function addHexPrefix (value: ?string): string {
if (value && hasHexPrefix(value)) {
return value;
}
return `0x${value || ''}`;
}
function hasHexPrefix (value: ?string): boolean {
return !!(value && value.substr(0, 2) === '0x');
}
function stripHexPrefix (value: ?string): string {
if (value && hasHexPrefix(value)) {
return value.substr(2);
}
return value || '';
}
module.exports = {
addHexPrefix,
hasHexPrefix,
stripHexPrefix
};
-67
View File
@@ -1,67 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
/* eslint-disable no-unused-expressions */
const { addHexPrefix, hasHexPrefix, stripHexPrefix } = require('./hex');
describe('util/hex', () => {
describe('addHexPrefix', () => {
it('does not add when prefix is available', () => {
expect(
addHexPrefix('0x123')
).to.equal('0x123');
});
it('adds the prefix when it is not available', () => {
expect(
addHexPrefix('123')
).to.equal('0x123');
});
it('returns null as 0x', () => {
expect(
addHexPrefix(null)
).to.equal('0x');
});
});
describe('hasHexPrefix', () => {
it('returns true when hex prefix is found', () => {
expect(
hasHexPrefix('0x123')
).to.be.true;
});
it('returns false when no prefix attached', () => {
expect(
hasHexPrefix('123')
).to.be.false;
});
it('returns false when null value supplied', () => {
expect(
hasHexPrefix(null)
).to.be.false;
});
});
describe('stripHexPrefix', () => {
it('returns the value as-is when no prefix', () => {
expect(
stripHexPrefix('01ab')
).to.equal('01ab');
});
it('returns an empty string when null value supplied', () => {
expect(
stripHexPrefix(null)
).to.equal('');
});
it('strips the prefix from other strings', () => {
expect(
stripHexPrefix('0x1223')
).to.equal('1223');
});
});
});
-20
View File
@@ -1,20 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
function isFunction (value: any): boolean {
return typeof value === 'function';
}
function isNumber (value: any): boolean {
return typeof value === 'number';
}
function isUndefined (value: any): boolean {
return typeof value === 'undefined';
}
module.exports = {
isFunction,
isNumber,
isUndefined
};
-49
View File
@@ -1,49 +0,0 @@
// ISC, Copyright 2017 Jaco Greeff
/* eslint-disable no-unused-expressions */
const { isFunction, isNumber, isUndefined } = require('./is');
describe('util/is', () => {
describe('isFunction', () => {
it('returns true on valid functions', () => {
expect(
isFunction(isFunction)
).to.be.true;
});
it('returns false of invalid functions', () => {
expect(
isFunction('notAFunction')
).to.be.false;
});
});
describe('isNumber', () => {
it('returns true on valid numbers', () => {
expect(
isNumber(2)
).to.be.true;
});
it('returns fals on invalid numbers', () => {
expect(
isNumber('2')
).to.be.false;
});
});
describe('isUndefined', () => {
it('returns true on undefined values', () => {
expect(
isUndefined()
).to.be.true;
});
it('returns false on defined values', () => {
expect(
isUndefined(null)
).to.be.false;
});
});
});