Additional complexity adjustments (#751)
* Adjust encoder/decoder fallbacks * Dep-dup is functions * Complexity cleanups * More complexity adjustments
This commit is contained in:
@@ -5,15 +5,16 @@ import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { keccakAsU8a } from '../keccak';
|
||||
|
||||
function isInvalidChar (char: string, byte: number): boolean {
|
||||
return (byte > 7 && char !== char.toUpperCase()) || (byte <= 7 && char !== char.toLowerCase());
|
||||
}
|
||||
|
||||
export default function isEthereumChecksum (_address: string): boolean {
|
||||
const address = _address.replace('0x', '');
|
||||
const hash = u8aToHex(keccakAsU8a(address.toLowerCase()), -1, false);
|
||||
|
||||
for (let index = 0; index < 40; index++) {
|
||||
const char = address[index];
|
||||
const hashval = parseInt(hash[index], 16);
|
||||
|
||||
if ((hashval > 7 && char !== char.toUpperCase()) || (hashval <= 7 && char !== char.toLowerCase())) {
|
||||
if (isInvalidChar(address[index], parseInt(hash[index], 16))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ const ADDRESS = '0x00a329c0648769A73afAc7F9381E08FB43dBEA72';
|
||||
|
||||
describe('isEthereumChecksum', () => {
|
||||
it('returns false on invalid address', () => {
|
||||
expect(isEthereumChecksum('0xinvalid')).toBe(false);
|
||||
expect(isEthereumChecksum('0x00a329c0648769')).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false on non-checksum address', () => {
|
||||
|
||||
@@ -23,6 +23,48 @@ function addTime (a: Time, b: Time): Time {
|
||||
|
||||
const ZERO = { days: 0, hours: 0, milliseconds: 0, minutes: 0, seconds: 0 };
|
||||
|
||||
function extractDays (milliseconds: number, hrs: number): Time {
|
||||
const days = Math.floor(hrs / 24);
|
||||
|
||||
return addTime({ ...ZERO, days }, extractTime(milliseconds - (days * DAY * 1000)));
|
||||
}
|
||||
|
||||
function extractHrs (milliseconds: number, mins: number): Time {
|
||||
const hrs = mins / 60;
|
||||
|
||||
if (hrs < 24) {
|
||||
const hours = Math.floor(hrs);
|
||||
|
||||
return addTime({ ...ZERO, hours }, extractTime(milliseconds - (hours * HRS * 1000)));
|
||||
}
|
||||
|
||||
return extractDays(milliseconds, hrs);
|
||||
}
|
||||
|
||||
function extractMins (milliseconds: number, secs: number): Time {
|
||||
const mins = secs / 60;
|
||||
|
||||
if (mins < 60) {
|
||||
const minutes = Math.floor(mins);
|
||||
|
||||
return addTime({ ...ZERO, minutes }, extractTime(milliseconds - (minutes * 60 * 1000)));
|
||||
}
|
||||
|
||||
return extractHrs(milliseconds, mins);
|
||||
}
|
||||
|
||||
function extractSecs (milliseconds: number): Time {
|
||||
const secs = milliseconds / 1000;
|
||||
|
||||
if (secs < 60) {
|
||||
const seconds = Math.floor(secs);
|
||||
|
||||
return addTime({ ...ZERO, seconds }, extractTime(milliseconds - (seconds * 1000)));
|
||||
}
|
||||
|
||||
return extractMins(milliseconds, secs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name extractTime
|
||||
* @summary Convert a quantity of seconds to Time array representing accumulated {days, minutes, hours, seconds, milliseconds}
|
||||
@@ -42,31 +84,5 @@ export default function extractTime (milliseconds?: number): Time {
|
||||
return { ...ZERO, milliseconds };
|
||||
}
|
||||
|
||||
const secs = milliseconds / 1000;
|
||||
|
||||
if (secs < 60) {
|
||||
const seconds = Math.floor(secs);
|
||||
|
||||
return addTime({ ...ZERO, seconds }, extractTime(milliseconds - (seconds * 1000)));
|
||||
}
|
||||
|
||||
const mins = secs / 60;
|
||||
|
||||
if (mins < 60) {
|
||||
const minutes = Math.floor(mins);
|
||||
|
||||
return addTime({ ...ZERO, minutes }, extractTime(milliseconds - (minutes * 60 * 1000)));
|
||||
}
|
||||
|
||||
const hrs = mins / 60;
|
||||
|
||||
if (hrs < 24) {
|
||||
const hours = Math.floor(hrs);
|
||||
|
||||
return addTime({ ...ZERO, hours }, extractTime(milliseconds - (hours * HRS * 1000)));
|
||||
}
|
||||
|
||||
const days = Math.floor(hrs / 24);
|
||||
|
||||
return addTime({ ...ZERO, days }, extractTime(milliseconds - (days * DAY * 1000)));
|
||||
return extractSecs(milliseconds);
|
||||
}
|
||||
|
||||
@@ -5,19 +5,7 @@ import { ToBn } from '../types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import isToBn from '../is/toBn';
|
||||
|
||||
function getValue <ExtToBn extends ToBn> (value: BN | ExtToBn | Date | number): number {
|
||||
if (isToBn(value)) {
|
||||
return getValue(value.toBn());
|
||||
} else if (value instanceof Date) {
|
||||
return getValue(value.getTime());
|
||||
} else if (value instanceof BN) {
|
||||
return getValue(value.toNumber());
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
import bnToBn from '../bn/toBn';
|
||||
|
||||
function formatValue (elapsed: number): string {
|
||||
if (elapsed < 15) {
|
||||
@@ -31,9 +19,11 @@ function formatValue (elapsed: number): string {
|
||||
return `${elapsed / 3600 | 0}h`;
|
||||
}
|
||||
|
||||
export default function formatElapsed <ExtToBn extends ToBn> (now?: Date | null, value?: BN | ExtToBn | Date | number | null): string {
|
||||
export default function formatElapsed <ExtToBn extends ToBn> (now?: Date | null, value?: BigInt | BN | ExtToBn | Date | number | null): string {
|
||||
const tsNow = (now && now.getTime()) || 0;
|
||||
const tsValue = getValue(value || 0);
|
||||
const tsValue = value instanceof Date
|
||||
? value.getTime()
|
||||
: bnToBn(value).toNumber();
|
||||
|
||||
return (tsNow && tsValue)
|
||||
? formatValue(Math.max(Math.abs(tsNow - tsValue), 0) / 1000)
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
const ALPHABET = new Array(256) as string[];
|
||||
|
||||
for (let n = 0; n < 256; ++n) {
|
||||
ALPHABET[n] = n.toString(16).padStart(2, '0');
|
||||
}
|
||||
const ALPHABET = new Array(256).fill(0).map((_, n) => n.toString(16).padStart(2, '0'));
|
||||
|
||||
/** @internal */
|
||||
function extract (value: Uint8Array): string {
|
||||
|
||||
Reference in New Issue
Block a user