Update with flow lint (#46)
* Flow lint warning fixes * Flow updates fro new flow-bin
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
module.exports = function echo (value: any): any {
|
||||
module.exports = function echo (value: mixed): mixed {
|
||||
return value;
|
||||
};
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
import type { FormatInputType, InterfaceInputType } from '@polkadot/api-jsonrpc/types';
|
||||
import type { FormatterFunction } from './types';
|
||||
import type { InterfaceInputType } from '@polkadot/api-jsonrpc/types';
|
||||
|
||||
const accountIdEncode = require('@polkadot/primitives-json/accountId/encode');
|
||||
const h256Encode = require('@polkadot/primitives-json/h256/encode');
|
||||
@@ -12,7 +11,7 @@ const headerHashEncode = require('@polkadot/primitives-json/headerHash/encode');
|
||||
const echo = require('./echo');
|
||||
const util = require('./util');
|
||||
|
||||
const formatters: { [FormatInputType]: FormatterFunction } = {
|
||||
const formatters = {
|
||||
'Address': accountIdEncode,
|
||||
'CallData': hashEncode,
|
||||
'H256': h256Encode,
|
||||
@@ -20,7 +19,7 @@ const formatters: { [FormatInputType]: FormatterFunction } = {
|
||||
'String': echo
|
||||
};
|
||||
|
||||
module.exports = function formatInputs (inputs: Array<InterfaceInputType>, values: Array<any>): Array<any> {
|
||||
module.exports = function formatInputs (inputs: Array<InterfaceInputType>, values: Array<mixed>): Array<mixed> {
|
||||
const types = inputs.map(({ type }) => type);
|
||||
|
||||
return util.formatArray(formatters, types, values);
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
import type { FormatOutputType, InterfaceOutputType } from '@polkadot/api-jsonrpc/types';
|
||||
import type { FormatterFunction } from './types';
|
||||
import type { InterfaceOutputType } from '@polkadot/api-jsonrpc/types';
|
||||
|
||||
const headerDecode = require('@polkadot/primitives-json/header/decode');
|
||||
|
||||
const echo = require('./echo');
|
||||
const util = require('./util');
|
||||
|
||||
const formatters: { [FormatOutputType]: FormatterFunction } = {
|
||||
const formatters = {
|
||||
'Header': headerDecode,
|
||||
'OutData': echo,
|
||||
'StorageData': echo
|
||||
};
|
||||
|
||||
module.exports = function formatOutput (output: InterfaceOutputType, value: any): any {
|
||||
module.exports = function formatOutput (output: InterfaceOutputType, value: mixed): mixed {
|
||||
return util.format(formatters, output.type, value);
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
export type FormatterFunction = (value: any) => any;
|
||||
// flowlint-next-line unclear-type:off
|
||||
export type FormatterFunction = (value: any) => mixed;
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
import type { FormatInputType, FormatOutputType } from '@polkadot/api-jsonrpc/types';
|
||||
import type { FormatterFunction } from './types';
|
||||
|
||||
type FormattersFunctionType = FormatInputType | FormatOutputType;
|
||||
type FormattersFunctionMap = {
|
||||
[FormattersFunctionType]: FormatterFunction
|
||||
};
|
||||
|
||||
const isUndefined = require('@polkadot/util/is/undefined');
|
||||
const echo = require('./echo');
|
||||
|
||||
const arrayTypeRegex = /\[\]$/;
|
||||
|
||||
function formatSingleType (formatters: { [any]: FormatterFunction }, type: string, value: any): any {
|
||||
function formatSingleType (formatters: FormattersFunctionMap, type: FormattersFunctionType, value: mixed): mixed {
|
||||
const formatter: FormatterFunction = formatters[type];
|
||||
|
||||
if (isUndefined(formatter)) {
|
||||
@@ -20,28 +26,31 @@ function formatSingleType (formatters: { [any]: FormatterFunction }, type: strin
|
||||
try {
|
||||
return formatter(value);
|
||||
} catch (error) {
|
||||
// $FlowFixMe just let JS coerce all it wants to
|
||||
throw new Error(`Error formatting '${value}' as '${type}': ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
function formatArrayType (formatters: { [any]: FormatterFunction }, type: string, value: Array<any>): any {
|
||||
type = type.replace(arrayTypeRegex, '');
|
||||
function formatArrayType (formatters: FormattersFunctionMap, type: FormattersFunctionType, value: Array<mixed>): mixed {
|
||||
// flowlint-next-line unclear-type:off
|
||||
type = ((type.replace(arrayTypeRegex, ''): any): FormattersFunctionType);
|
||||
|
||||
return value.map((value) => {
|
||||
return formatSingleType(formatters, type, value);
|
||||
});
|
||||
}
|
||||
|
||||
function format (formatters: { [any]: FormatterFunction }, type: string, value: any): any {
|
||||
function format (formatters: FormattersFunctionMap, type: FormattersFunctionType, value: mixed | Array<mixed>): mixed {
|
||||
if (arrayTypeRegex.test(type)) {
|
||||
// $FlowFixMe array type
|
||||
return formatArrayType(formatters, type, value);
|
||||
}
|
||||
|
||||
return formatSingleType(formatters, type, value);
|
||||
}
|
||||
|
||||
function formatArray (formatters: { [any]: FormatterFunction }, types: Array<string>, values: Array<any>): Array<any> {
|
||||
return types.map((type, index) => {
|
||||
function formatArray (formatters: FormattersFunctionMap, types: Array<FormattersFunctionType>, values: Array<mixed>): Array<mixed> {
|
||||
return types.map((type, index): mixed => {
|
||||
return format(formatters, type, values[index]);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user