From 91db30af82fec10d0a4890b54553dac684737638 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 8 Dec 2018 16:37:05 +0100 Subject: [PATCH] Cleanup Codeclimate smells (#11) * Cleanup Codeclimate smells * Add Codeclimate badges --- README.md | 2 + packages/ui-identicon/package.json | 4 +- packages/ui-identicon/src/Polkadot.tsx | 115 ++++++++++-------- packages/ui-identicon/src/index.tsx | 32 ++--- packages/ui-keyring/package.json | 4 +- packages/ui-keyring/src/index.ts | 20 +-- .../src/observable/genericSubject.ts | 35 +++--- packages/ui-keyring/src/observable/types.ts | 11 -- packages/ui-keyring/src/options/index.ts | 5 +- packages/ui-keyring/src/types.ts | 4 +- yarn.lock | 42 +++---- 11 files changed, 134 insertions(+), 140 deletions(-) diff --git a/README.md b/README.md index e280716d..3309cb88 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ [![style](https://img.shields.io/badge/code%20style-semistandard-lightgrey.svg?style=flat-square)](https://github.com/Flet/semistandard) [![npm](https://img.shields.io/npm/v/@polkadot/ui.svg?style=flat-square)](https://www.npmjs.com/package/@polkadot/ui-identicon) [![travis](https://img.shields.io/travis/polkadot-js/ui.svg?style=flat-square)](https://travis-ci.com/polkadot-js/ui) +[![maintainability](https://img.shields.io/codeclimate/maintainability/polkadot-js/ui.svg?style=flat-square)](https://codeclimate.com/github/polkadot-js/ui/maintainability) +[![coverage](https://img.shields.io/coveralls/polkadot-js/ui.svg?style=flat-square)](https://coveralls.io/github/polkadot-js/ui?branch=master) [![greenkeeper](https://img.shields.io/badge/greenkeeper-enabled-brightgreen.svg?style=flat-square)](https://greenkeeper.io/) [![dependency](https://img.shields.io/david/polkadot-js/ui.svg?style=flat-square)](https://david-dm.org/polkadot-js/ui) [![devDependency](https://img.shields.io/david/dev/polkadot-js/ui.svg?style=flat-square)](https://david-dm.org/polkadot-js/ui#info=devDependencies) diff --git a/packages/ui-identicon/package.json b/packages/ui-identicon/package.json index b646e21e..7f53f6c3 100644 --- a/packages/ui-identicon/package.json +++ b/packages/ui-identicon/package.json @@ -10,9 +10,9 @@ "license": "Apache-2.0", "dependencies": { "@babel/runtime": "^7.1.5", - "@polkadot/keyring": "^0.33.9", + "@polkadot/keyring": "^0.33.11", "@polkadot/ui-settings": "^0.22.8", - "@polkadot/util-crypto": "^0.33.9", + "@polkadot/util-crypto": "^0.33.11", "@types/color": "^3.0.0", "@types/react-copy-to-clipboard": "^4.2.6", "color": "^3.0.0", diff --git a/packages/ui-identicon/src/Polkadot.tsx b/packages/ui-identicon/src/Polkadot.tsx index 020df97e..f88a8afa 100644 --- a/packages/ui-identicon/src/Polkadot.tsx +++ b/packages/ui-identicon/src/Polkadot.tsx @@ -9,9 +9,10 @@ // // Here we have done the following to convert the component - // - Converted the code to TypeScript -// - Removed the oo7 dependencies (since not initialised properly, it makes additional connections to wrong endpoints) +// - Removed the oo7 dependencies (since not initialised properly, it makes calls to wrong endpoints) // - Remove encoding functionality, these are catered for in the base // - Remove copy functionality (this is catered from in the base components) +// - Split calculations into relevant functions // - Move constants to file-level // - Overall it is now just a static component, expecting an address as an input value @@ -45,60 +46,11 @@ const schema: { [index: string]: Scheme } = { hmirror: { freq: 128, colors: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 8, 6, 7, 5, 3, 4, 2, 11] } }; -function findScheme (d: number): Scheme { - let cum = 0; - const ks = Object.keys(schema); - - for (let i in ks) { - cum += schema[ks[i]].freq; - - if (d < cum) { - return schema[ks[i]]; - } - } - - throw new Error('Unable to find schema'); -} - export default class Identicon extends React.PureComponent { render () { - const { className, sixPoint = false, size, style, value } = this.props; - - const r = sixPoint - ? (s / 2 / 8 * 5) - : (s / 2 / 4 * 3); - const rroot3o2 = r * Math.sqrt(3) / 2; - const ro2 = r / 2; - const rroot3o4 = r * Math.sqrt(3) / 4; - const ro4 = r / 4; - const r3o4 = r * 3 / 4; - const total = Object.keys(schema).map(k => schema[k].freq).reduce((a, b) => a + b); - const id = Array.from(blake2b(decodeAddress(value))).map((x, i) => - (x + 256 - zero[i]) % 256 - ); - const sat = (Math.floor(id[29] * 70 / 256 + 26) % 80) + 30; - const d = Math.floor((id[30] + id[31] * 256) % total); - const scheme = findScheme(d); - const palette = Array.from(id).map((x, i) => { - const b = (x + i % 28 * 58) % 256; - - if (b === 0) { - return '#444'; - } else if (b === 255) { - return 'transparent'; - } - - const h = Math.floor(b % 64 * 360 / 64); - const l = [53, 15, 35, 75][Math.floor(b / 64)]; - - return `hsl(${h}, ${sat}%, ${l}%)`; - }); - - const rot = (id[28] % 6) * 3; - const colors = scheme.colors.map((_, i) => - palette[scheme.colors[i < 18 ? (i + rot) % 18 : 18]] - ); - + const { className, size, style, value } = this.props; + const { r, ro2, r3o4, ro4, rroot3o2, rroot3o4 } = this.getRotation(); + const colors = this.getColors(); let i = 0; return ( @@ -137,4 +89,61 @@ export default class Identicon extends React.PureComponent { ); } + + private getRotation () { + const { sixPoint = false } = this.props; + const r = sixPoint + ? (s / 2 / 8 * 5) + : (s / 2 / 4 * 3); + const rroot3o2 = r * Math.sqrt(3) / 2; + const ro2 = r / 2; + const rroot3o4 = r * Math.sqrt(3) / 4; + const ro4 = r / 4; + const r3o4 = r * 3 / 4; + + return { r, ro2, r3o4, ro4, rroot3o2, rroot3o4 }; + } + + private getColors () { + const { value } = this.props; + const total = Object.keys(schema).map(k => schema[k].freq).reduce((a, b) => a + b); + const id = Array.from(blake2b(decodeAddress(value))).map((x, i) => (x + 256 - zero[i]) % 256); + const d = Math.floor((id[30] + id[31] * 256) % total); + const rot = (id[28] % 6) * 3; + const sat = (Math.floor(id[29] * 70 / 256 + 26) % 80) + 30; + const scheme = this.findScheme(d); + const palette = Array.from(id).map((x, i) => { + const b = (x + i % 28 * 58) % 256; + + if (b === 0) { + return '#444'; + } else if (b === 255) { + return 'transparent'; + } + + const h = Math.floor(b % 64 * 360 / 64); + const l = [53, 15, 35, 75][Math.floor(b / 64)]; + + return `hsl(${h}, ${sat}%, ${l}%)`; + }); + + return scheme.colors.map((_, i) => + palette[scheme.colors[i < 18 ? (i + rot) % 18 : 18]] + ); + } + + private findScheme (d: number): Scheme { + let cum = 0; + const ks = Object.keys(schema); + + for (let i in ks) { + cum += schema[ks[i]].freq; + + if (d < cum) { + return schema[ks[i]]; + } + } + + throw new Error('Unable to find schema'); + } } diff --git a/packages/ui-identicon/src/index.tsx b/packages/ui-identicon/src/index.tsx index 7f4c719d..3a953958 100644 --- a/packages/ui-identicon/src/index.tsx +++ b/packages/ui-identicon/src/index.tsx @@ -87,13 +87,28 @@ export default class IdentityIcon extends React.PureComponent { } render () { - const { className, isHighlight = false, size = DEFAULT_SIZE, style, theme = settings.uiTheme } = this.props; const { address } = this.state; + const wrapped = this.getWrapped(address); + return !address + ? wrapped + : ( + + {wrapped} + + ); + } + + private getWrapped (address?: string | null) { + const { className, isHighlight = false, size = DEFAULT_SIZE, style, theme = settings.uiTheme } = this.props; const Component = !address ? Empty : Components[theme] || Substrate; - const wrapped = ( + + return ( { /> ); - - if (!address) { - return wrapped; - } - - return ( - - {wrapped} - - ); } private onCopy = (): void => { diff --git a/packages/ui-keyring/package.json b/packages/ui-keyring/package.json index 7fab0de4..d6c99ec8 100644 --- a/packages/ui-keyring/package.json +++ b/packages/ui-keyring/package.json @@ -11,8 +11,8 @@ "license": "Apache-2.0", "dependencies": { "@babel/runtime": "^7.1.5", - "@polkadot/keyring": "^0.33.9", - "@polkadot/types": "^0.33.5", + "@polkadot/keyring": "^0.33.11", + "@polkadot/types": "^0.33.6", "store": "^2.0.12" } } diff --git a/packages/ui-keyring/src/index.ts b/packages/ui-keyring/src/index.ts index e2c7f187..f28754e2 100644 --- a/packages/ui-keyring/src/index.ts +++ b/packages/ui-keyring/src/index.ts @@ -4,7 +4,7 @@ import { Prefix } from '@polkadot/keyring/address/types'; import { KeyringInstance as BaseKeyringInstance, KeyringPair, KeyringPair$Meta, KeyringPair$Json } from '@polkadot/keyring/types'; -import { AccountSubject, AddressSubject, SingleAddress } from './observable/types'; +import { AddressSubject, SingleAddress } from './observable/types'; import { KeyringAddress, KeyringJson, KeyringJson$Meta, KeyringStruct } from './types'; import store from 'store'; @@ -22,7 +22,7 @@ import keyringOption from './options'; // Chain determination occurs outside of Keyring. Loading `keyring.loadAll()` is triggered // from the API after the chain is received class Keyring implements KeyringStruct { - private _accounts: AccountSubject; + private _accounts: AddressSubject; private _addresses: AddressSubject; private _keyring?: BaseKeyringInstance; private _prefix: Prefix; @@ -81,9 +81,7 @@ class Keyring implements KeyringStruct { private addTimestamp (pair: KeyringPair): void { if (!pair.getMeta().whenCreated) { - pair.setMeta({ - whenCreated: Date.now() - }); + pair.setMeta({ whenCreated: Date.now() }); } } @@ -147,12 +145,8 @@ class Keyring implements KeyringStruct { return Object .keys(available) - .map((address) => - this.getAddress(address, 'account') - ) - .filter((account) => - env.isDevelopment() || account.getMeta().isTesting !== true - ); + .map((address) => this.getAddress(address, 'account')) + .filter((account) => env.isDevelopment() || account.getMeta().isTesting !== true); } getAddress (_address: string | Uint8Array, type: 'account' | 'address' = 'address'): KeyringAddress { @@ -181,9 +175,7 @@ class Keyring implements KeyringStruct { return Object .keys(available) - .map((address) => - this.getAddress(address) - ); + .map((address) => this.getAddress(address)); } getPair (address: string | Uint8Array): KeyringPair { diff --git a/packages/ui-keyring/src/observable/genericSubject.ts b/packages/ui-keyring/src/observable/genericSubject.ts index abdc223b..b50645ad 100644 --- a/packages/ui-keyring/src/observable/genericSubject.ts +++ b/packages/ui-keyring/src/observable/genericSubject.ts @@ -11,26 +11,27 @@ import store from 'store'; import createOptionItem from '../options/item'; import development from './development'; +function callNext (current: SubjectInfo, subject: BehaviorSubject, withTest: boolean) { + const isDevMode = development.isDevelopment(); + + subject.next( + Object.keys(current).reduce((filtered, key) => { + const { json: { meta: { isTesting = false } = {} } = {} } = current[key]; + + if (!withTest || isDevMode || isTesting !== true) { + filtered[key] = current[key]; + } + + return filtered; + }, {} as SubjectInfo) + ); +} + export default function genericSubject (keyCreator: (address: string) => string, withTest: boolean = false): AddressSubject { let current: SubjectInfo = {}; const subject = new BehaviorSubject({}); - const next = (): void => { - const isDevMode = development.isDevelopment(); - - subject.next( - Object - .keys(current) - .reduce((filtered, key) => { - const { json: { meta: { isTesting = false } = {} } = {} } = current[key]; - - if (!withTest || isDevMode || isTesting !== true) { - filtered[key] = current[key]; - } - - return filtered; - }, {} as SubjectInfo) - ); - }; + const next = (): void => + callNext(current, subject, withTest); development.subject.subscribe(next); diff --git a/packages/ui-keyring/src/observable/types.ts b/packages/ui-keyring/src/observable/types.ts index 16c0f7ee..adfcdf4c 100644 --- a/packages/ui-keyring/src/observable/types.ts +++ b/packages/ui-keyring/src/observable/types.ts @@ -6,11 +6,6 @@ import { BehaviorSubject } from 'rxjs'; import { KeyringSectionOption } from '../options/types'; import { KeyringJson } from '../types'; -export type SingleAccount = { - json: KeyringJson, - option: KeyringSectionOption -}; - export type SingleAddress = { json: KeyringJson, option: KeyringSectionOption @@ -20,12 +15,6 @@ export type SubjectInfo = { [index: string]: SingleAddress }; -export type AccountSubject = { - add: (account: string, json: KeyringJson) => SingleAccount, - remove: (account: string) => void, - subject: BehaviorSubject -}; - export type AddressSubject = { add: (address: string, json: KeyringJson) => SingleAddress, remove: (address: string) => void, diff --git a/packages/ui-keyring/src/options/index.ts b/packages/ui-keyring/src/options/index.ts index 9106eed3..3aa91cc0 100644 --- a/packages/ui-keyring/src/options/index.ts +++ b/packages/ui-keyring/src/options/index.ts @@ -7,6 +7,7 @@ import { SingleAddress } from '../observable/types'; import { KeyringOptions, KeyringOptionInstance, KeyringSectionOption, KeyringSectionOptions } from './types'; import { BehaviorSubject } from 'rxjs'; +import { assert } from '@polkadot/util'; import observableAll from '../observable'; @@ -26,9 +27,7 @@ class KeyringOption implements KeyringOptionInstance { } init (keyring: KeyringStruct): void { - if (hasCalledInitOptions) { - throw new Error('Unable to initialise options more than once'); - } + assert(!hasCalledInitOptions, 'Unable to initialise options more than once'); observableAll.subscribe((value) => { const options = this.emptyOptions(); diff --git a/packages/ui-keyring/src/types.ts b/packages/ui-keyring/src/types.ts index 3da22d02..d1ed4eb9 100644 --- a/packages/ui-keyring/src/types.ts +++ b/packages/ui-keyring/src/types.ts @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { KeyringInstance as BaseKeyringInstance, KeyringPair, KeyringPair$Meta, KeyringPair$Json } from '@polkadot/keyring/types'; -import { AccountSubject, AddressSubject, SingleAddress } from './observable/types'; +import { AddressSubject, SingleAddress } from './observable/types'; export type KeyringJson$Meta = { isRecent?: boolean, @@ -28,7 +28,7 @@ export type KeyringAddress = { }; export interface KeyringStruct { - readonly accounts: AccountSubject; + readonly accounts: AddressSubject; readonly addresses: AddressSubject; readonly keyring: BaseKeyringInstance | undefined; diff --git a/yarn.lock b/yarn.lock index 5320ac2d..07b3cedc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1430,14 +1430,14 @@ typedoc-plugin-no-inherit "^1.0.2" typescript "^3.2.1" -"@polkadot/keyring@^0.33.9": - version "0.33.9" - resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.33.9.tgz#d192ec400a0ba6f0c4825bf2e6a9f3e180c68a0f" - integrity sha512-nXyXBMdG8SIwyUmOCCaCdvJAbcftzq6ElWHgZFa84VxFYwviWMbRicPSkrvZr+KTrn7av86h7bJsnUbi/j3phQ== +"@polkadot/keyring@^0.33.11": + version "0.33.11" + resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.33.11.tgz#79c6174e9b27147ff69a2375f39a2462b2d581c7" + integrity sha512-AP98VfGbcdXo9Bp650klh0l734kcmrYM1kzDs+mpGmpwMQtjRrEuitWwAvb84Sl6Snq6qMOadLXVAxU8PtOtgA== dependencies: "@babel/runtime" "^7.1.5" - "@polkadot/util" "^0.33.9" - "@polkadot/util-crypto" "^0.33.9" + "@polkadot/util" "^0.33.11" + "@polkadot/util-crypto" "^0.33.11" "@types/bs58" "^3.0.30" bs58 "^4.0.1" @@ -1446,22 +1446,22 @@ resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.41.tgz#647b44b038f94a7acf76633b25cb72df897872fc" integrity sha512-/vYpjqKU+QKHNAlFdLgFZAAr54wxQMgIuszZ5BxaayUoq7ukDlvCPhUGQ3fNoyBSEaL7uTCZ/5SVRc363IehoA== -"@polkadot/types@^0.33.5": - version "0.33.5" - resolved "https://registry.yarnpkg.com/@polkadot/types/-/types-0.33.5.tgz#a6b075c6ed104ed243103849f2ae1ee375b31a61" - integrity sha512-ITvysLq7Y1PdwlsvigKukDoEtbvG+ic/EM1xsIM08Gcy6X11kREGDHbt9ORMqPn/nQZrsd4HkI8gK4UOHp0aSQ== +"@polkadot/types@^0.33.6": + version "0.33.6" + resolved "https://registry.yarnpkg.com/@polkadot/types/-/types-0.33.6.tgz#04b2a9cabf11634adfa6ee7cdd0cd27665c670c5" + integrity sha512-MKhWsuPqKL9PMvq2rkwckrFVv6HfeEktKlJgMozphxUTebNq+vcpz4zAMwCdP4yKjzNDm8eajS2FG0ui25v3xA== dependencies: "@babel/runtime" "^7.1.5" - "@polkadot/keyring" "^0.33.9" - "@polkadot/util" "^0.33.9" + "@polkadot/keyring" "^0.33.11" + "@polkadot/util" "^0.33.11" -"@polkadot/util-crypto@^0.33.9": - version "0.33.9" - resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.33.9.tgz#6f465604b1c5947d1a06fd4c84c845913d45a016" - integrity sha512-6beNVnvsuYd3V9LPU6yNSxjC6rzsXIWVQn4IzLUsQk7S8NEIgVSTl7xVP9OnRX6VzKSjs2DhqUe8p0ZqBygjNg== +"@polkadot/util-crypto@^0.33.11": + version "0.33.11" + resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.33.11.tgz#25b30e69a4da1f6a42d3680536130ca4f9480db6" + integrity sha512-jL1TzlqHGKRu9SN80e0K/0H1ZZ6fRTv96s2f6s27GrGt6+tcPEVcmaQK5vjEDMw2pT9+b5TPwcx2gLwh56GLVg== dependencies: "@babel/runtime" "^7.1.5" - "@polkadot/util" "^0.33.9" + "@polkadot/util" "^0.33.11" "@types/bip39" "^2.4.1" bip39 "^2.5.0" blakejs "^1.1.0" @@ -1469,10 +1469,10 @@ tweetnacl "^1.0.0" xxhashjs "^0.2.2" -"@polkadot/util@^0.33.9": - version "0.33.9" - resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.33.9.tgz#65c745cad3a9583fb6a68f734f10d12318611e89" - integrity sha512-F4doCg/l1TfrM0iNxBaxmwtbaNKcKOdKEX99Uj37So2Rwxg/XVenXa0sctA+4Vk3nauQ6kJRTUPLqiBdKAdmsg== +"@polkadot/util@^0.33.11": + version "0.33.11" + resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.33.11.tgz#3e2433e556ee14c685ebc5920c229cb76e0b985b" + integrity sha512-Tn+WYU+RxOaJhrUsPjBOk77MDKb6bsqVkcs5ysSc1HUDIRqZwGABnX1rhP2/DipS91hj8DQ2HZE18S7mCBv1bQ== dependencies: "@babel/runtime" "^7.1.5" "@types/bn.js" "^4.11.3"