New base code for mempool blockchain explorerer
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
/* tslint:disable */
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
import { isNumberFinite, isPositive, isInteger, toDecimal } from './utils';
|
||||
|
||||
export type ByteUnit = 'B' | 'kB' | 'MB' | 'GB' | 'TB';
|
||||
|
||||
@Pipe({
|
||||
name: 'bytes'
|
||||
})
|
||||
export class BytesPipe implements PipeTransform {
|
||||
|
||||
static formats: { [key: string]: { max: number, prev?: ByteUnit } } = {
|
||||
'B': {max: 1000},
|
||||
'kB': {max: Math.pow(1000, 2), prev: 'B'},
|
||||
'MB': {max: Math.pow(1000, 3), prev: 'kB'},
|
||||
'GB': {max: Math.pow(1000, 4), prev: 'MB'},
|
||||
'TB': {max: Number.MAX_SAFE_INTEGER, prev: 'GB'}
|
||||
};
|
||||
|
||||
transform(input: any, decimal: number = 0, from: ByteUnit = 'B', to?: ByteUnit): any {
|
||||
|
||||
if (!(isNumberFinite(input) &&
|
||||
isNumberFinite(decimal) &&
|
||||
isInteger(decimal) &&
|
||||
isPositive(decimal))) {
|
||||
return input;
|
||||
}
|
||||
|
||||
let bytes = input;
|
||||
let unit = from;
|
||||
while (unit !== 'B') {
|
||||
bytes *= 1024;
|
||||
unit = BytesPipe.formats[unit].prev!;
|
||||
}
|
||||
|
||||
if (to) {
|
||||
const format = BytesPipe.formats[to];
|
||||
|
||||
const result = toDecimal(BytesPipe.calculateResult(format, bytes), decimal);
|
||||
|
||||
return BytesPipe.formatResult(result, to);
|
||||
}
|
||||
|
||||
for (const key in BytesPipe.formats) {
|
||||
const format = BytesPipe.formats[key];
|
||||
if (bytes < format.max) {
|
||||
|
||||
const result = toDecimal(BytesPipe.calculateResult(format, bytes), decimal);
|
||||
|
||||
return BytesPipe.formatResult(result, key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static formatResult(result: number, unit: string): string {
|
||||
return `${result} ${unit}`;
|
||||
}
|
||||
|
||||
static calculateResult(format: { max: number, prev?: ByteUnit }, bytes: number) {
|
||||
const prev = format.prev ? BytesPipe.formats[format.prev] : undefined;
|
||||
return prev ? bytes / prev.max : bytes;
|
||||
}
|
||||
}
|
||||
@@ -1,311 +0,0 @@
|
||||
/* tslint:disable */
|
||||
|
||||
export type CollectionPredicate = (item?: any, index?: number, collection?: any[]) => boolean;
|
||||
|
||||
export function isUndefined(value: any): value is undefined {
|
||||
|
||||
return typeof value === 'undefined';
|
||||
}
|
||||
|
||||
export function isNull(value: any): value is null {
|
||||
return value === null;
|
||||
}
|
||||
|
||||
export function isNumber(value: any): value is number {
|
||||
return typeof value === 'number';
|
||||
}
|
||||
|
||||
export function isNumberFinite(value: any): value is number {
|
||||
return isNumber(value) && isFinite(value);
|
||||
}
|
||||
|
||||
// Not strict positive
|
||||
export function isPositive(value: number): boolean {
|
||||
return value >= 0;
|
||||
}
|
||||
|
||||
|
||||
export function isInteger(value: number): boolean {
|
||||
// No rest, is an integer
|
||||
return (value % 1) === 0;
|
||||
}
|
||||
|
||||
export function isNil(value: any): value is (null | undefined) {
|
||||
return value === null || typeof (value) === 'undefined';
|
||||
}
|
||||
|
||||
export function isString(value: any): value is string {
|
||||
return typeof value === 'string';
|
||||
}
|
||||
|
||||
export function isObject(value: any): boolean {
|
||||
return value !== null && typeof value === 'object';
|
||||
}
|
||||
|
||||
export function isArray(value: any): boolean {
|
||||
return Array.isArray(value);
|
||||
}
|
||||
|
||||
export function isFunction(value: any): boolean {
|
||||
return typeof value === 'function';
|
||||
}
|
||||
|
||||
export function toDecimal(value: number, decimal: number): number {
|
||||
return Math.round(value * Math.pow(10, decimal)) / Math.pow(10, decimal);
|
||||
}
|
||||
|
||||
export function upperFirst(value: string): string {
|
||||
return value.slice(0, 1).toUpperCase() + value.slice(1);
|
||||
}
|
||||
|
||||
export function createRound(method: string): Function {
|
||||
// <any>Math to suppress error
|
||||
const func: any = (<any>Math)[method];
|
||||
return function (value: number, precision: number = 0) {
|
||||
if (typeof value === 'string') {
|
||||
throw new TypeError('Rounding method needs a number');
|
||||
}
|
||||
if (typeof precision !== 'number' || isNaN(precision)) {
|
||||
precision = 0;
|
||||
}
|
||||
if (precision) {
|
||||
let pair = `${value}e`.split('e');
|
||||
const val = func(`${pair[0]}e` + (+pair[1] + precision));
|
||||
pair = `${val}e`.split('e');
|
||||
return +(pair[0] + 'e' + (+pair[1] - precision));
|
||||
}
|
||||
return func(value);
|
||||
};
|
||||
}
|
||||
|
||||
export function leftPad(str: string, len: number = 0, ch: any = ' ') {
|
||||
str = String(str);
|
||||
ch = toString(ch);
|
||||
let i = -1;
|
||||
const length = len - str.length;
|
||||
while (++i < length && (str.length + ch.length) <= len) {
|
||||
str = ch + str;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
export function rightPad(str: string, len: number = 0, ch: any = ' ') {
|
||||
str = String(str);
|
||||
ch = toString(ch);
|
||||
let i = -1;
|
||||
const length = len - str.length;
|
||||
while (++i < length && (str.length + ch.length) <= len) {
|
||||
str += ch;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
export function toString(value: number | string) {
|
||||
return `${value}`;
|
||||
}
|
||||
|
||||
export function pad(str: string, len: number = 0, ch: any = ' '): string {
|
||||
str = String(str);
|
||||
ch = toString(ch);
|
||||
let i = -1;
|
||||
const length = len - str.length;
|
||||
|
||||
let left = true;
|
||||
while (++i < length) {
|
||||
const l = (str.length + ch.length <= len) ? (str.length + ch.length) : (str.length + 1);
|
||||
if (left) {
|
||||
str = leftPad(str, l, ch);
|
||||
} else {
|
||||
str = rightPad(str, l, ch);
|
||||
}
|
||||
left = !left;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
export function flatten(input: any[], index: number = 0): any[] {
|
||||
|
||||
if (index >= input.length) {
|
||||
return input;
|
||||
}
|
||||
|
||||
if (isArray(input[index])) {
|
||||
return flatten(
|
||||
input.slice(0, index).concat(input[index], input.slice(index + 1)),
|
||||
index
|
||||
);
|
||||
}
|
||||
|
||||
return flatten(input, index + 1);
|
||||
|
||||
}
|
||||
|
||||
|
||||
export function getProperty(value: { [key: string]: any }, key: string): any {
|
||||
|
||||
if (isNil(value) || !isObject(value)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const keys: string[] = key.split('.');
|
||||
let result: any = value[keys.shift()!];
|
||||
|
||||
for (const kk of keys) {
|
||||
if (isNil(result) || !isObject(result)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
result = result[kk];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function sum(input: Array<number>, initial = 0): number {
|
||||
|
||||
return input.reduce((previous: number, current: number) => previous + current, initial);
|
||||
}
|
||||
|
||||
// http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript
|
||||
export function shuffle(input: any): any {
|
||||
|
||||
if (!isArray(input)) {
|
||||
return input;
|
||||
}
|
||||
|
||||
const copy = [...input];
|
||||
|
||||
for (let i = copy.length; i; --i) {
|
||||
const j = Math.floor(Math.random() * i);
|
||||
const x = copy[i - 1];
|
||||
copy[i - 1] = copy[j];
|
||||
copy[j] = x;
|
||||
}
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
export function deepIndexOf(collection: any[], value: any) {
|
||||
|
||||
let index = -1;
|
||||
const length = collection.length;
|
||||
|
||||
while (++index < length) {
|
||||
if (deepEqual(value, collection[index])) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
export function deepEqual(a: any, b: any) {
|
||||
|
||||
if (a === b) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(typeof a === 'object' && typeof b === 'object')) {
|
||||
return a === b;
|
||||
}
|
||||
|
||||
const keysA = Object.keys(a);
|
||||
const keysB = Object.keys(b);
|
||||
|
||||
if (keysA.length !== keysB.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Test for A's keys different from B.
|
||||
const hasOwn = Object.prototype.hasOwnProperty;
|
||||
for (let i = 0; i < keysA.length; i++) {
|
||||
const key = keysA[i];
|
||||
if (!hasOwn.call(b, keysA[i]) || !deepEqual(a[key], b[key])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export function isDeepObject(object: any) {
|
||||
|
||||
return object.__isDeepObject__;
|
||||
}
|
||||
|
||||
export function wrapDeep(object: any) {
|
||||
|
||||
return new DeepWrapper(object);
|
||||
}
|
||||
|
||||
export function unwrapDeep(object: any) {
|
||||
|
||||
if (isDeepObject(object)) {
|
||||
return object.data;
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
export class DeepWrapper {
|
||||
|
||||
public __isDeepObject__ = true;
|
||||
|
||||
constructor(public data: any) { }
|
||||
}
|
||||
|
||||
export function count(input: any): any {
|
||||
|
||||
if (!isArray(input) && !isObject(input) && !isString(input)) {
|
||||
return input;
|
||||
}
|
||||
|
||||
if (isObject(input)) {
|
||||
return Object.keys(input).map((value) => input[value]).length;
|
||||
}
|
||||
|
||||
return input.length;
|
||||
}
|
||||
|
||||
export function empty(input: any): any {
|
||||
|
||||
if (!isArray(input)) {
|
||||
return input;
|
||||
}
|
||||
|
||||
return input.length === 0;
|
||||
}
|
||||
|
||||
export function every(input: any, predicate: CollectionPredicate) {
|
||||
|
||||
if (!isArray(input) || !predicate) {
|
||||
return input;
|
||||
}
|
||||
|
||||
let result = true;
|
||||
let i = -1;
|
||||
|
||||
while (++i < input.length && result) {
|
||||
result = predicate(input[i], i, input);
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function takeUntil(input: any[], predicate: CollectionPredicate) {
|
||||
|
||||
let i = -1;
|
||||
const result: any = [];
|
||||
while (++i < input.length && !predicate(input[i], i, input)) {
|
||||
result[i] = input[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function takeWhile(input: any[], predicate: CollectionPredicate) {
|
||||
return takeUntil(input, (item: any, index: number | undefined, collection: any[] | undefined) =>
|
||||
!predicate(item, index, collection));
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
/* tslint:disable */
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
import { isNumberFinite, isPositive, isInteger, toDecimal } from './utils';
|
||||
|
||||
export type ByteUnit = 'vB' | 'kvB' | 'MvB' | 'GvB' | 'TvB';
|
||||
|
||||
@Pipe({
|
||||
name: 'vbytes'
|
||||
})
|
||||
export class VbytesPipe implements PipeTransform {
|
||||
|
||||
static formats: { [key: string]: { max: number, prev?: ByteUnit } } = {
|
||||
'vB': {max: 1000},
|
||||
'kvB': {max: Math.pow(1000, 2), prev: 'vB'},
|
||||
'MvB': {max: Math.pow(1000, 3), prev: 'kvB'},
|
||||
'GvB': {max: Math.pow(1000, 4), prev: 'MvB'},
|
||||
'TvB': {max: Number.MAX_SAFE_INTEGER, prev: 'GvB'}
|
||||
};
|
||||
|
||||
transform(input: any, decimal: number = 0, from: ByteUnit = 'vB', to?: ByteUnit): any {
|
||||
|
||||
if (!(isNumberFinite(input) &&
|
||||
isNumberFinite(decimal) &&
|
||||
isInteger(decimal) &&
|
||||
isPositive(decimal))) {
|
||||
return input;
|
||||
}
|
||||
|
||||
let bytes = input;
|
||||
let unit = from;
|
||||
while (unit !== 'vB') {
|
||||
bytes *= 1024;
|
||||
unit = VbytesPipe.formats[unit].prev!;
|
||||
}
|
||||
|
||||
if (to) {
|
||||
const format = VbytesPipe.formats[to];
|
||||
|
||||
const result = toDecimal(VbytesPipe.calculateResult(format, bytes), decimal);
|
||||
|
||||
return VbytesPipe.formatResult(result, to);
|
||||
}
|
||||
|
||||
for (const key in VbytesPipe.formats) {
|
||||
const format = VbytesPipe.formats[key];
|
||||
if (bytes < format.max) {
|
||||
|
||||
const result = toDecimal(VbytesPipe.calculateResult(format, bytes), decimal);
|
||||
|
||||
return VbytesPipe.formatResult(result, key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static formatResult(result: number, unit: string): string {
|
||||
return `${result} ${unit}`;
|
||||
}
|
||||
|
||||
static calculateResult(format: { max: number, prev?: ByteUnit }, bytes: number) {
|
||||
const prev = format.prev ? VbytesPipe.formats[format.prev] : undefined;
|
||||
return prev ? bytes / prev.max : bytes;
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
/* tslint:disable */
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
import { isNumberFinite, isPositive, isInteger, toDecimal } from './utils';
|
||||
|
||||
export type ByteUnit = 'WU' | 'kWU' | 'MWU' | 'GWU' | 'TWU';
|
||||
|
||||
@Pipe({
|
||||
name: 'wuBytes'
|
||||
})
|
||||
export class WuBytesPipe implements PipeTransform {
|
||||
|
||||
static formats: { [key: string]: { max: number, prev?: ByteUnit } } = {
|
||||
'WU': {max: 1000},
|
||||
'kWU': {max: Math.pow(1000, 2), prev: 'WU'},
|
||||
'MWU': {max: Math.pow(1000, 3), prev: 'kWU'},
|
||||
'GWU': {max: Math.pow(1000, 4), prev: 'MWU'},
|
||||
'TWU': {max: Number.MAX_SAFE_INTEGER, prev: 'GWU'}
|
||||
};
|
||||
|
||||
transform(input: any, decimal: number = 0, from: ByteUnit = 'WU', to?: ByteUnit): any {
|
||||
|
||||
if (!(isNumberFinite(input) &&
|
||||
isNumberFinite(decimal) &&
|
||||
isInteger(decimal) &&
|
||||
isPositive(decimal))) {
|
||||
return input;
|
||||
}
|
||||
|
||||
let bytes = input;
|
||||
let unit = from;
|
||||
while (unit !== 'WU') {
|
||||
bytes *= 1024;
|
||||
unit = WuBytesPipe.formats[unit].prev!;
|
||||
}
|
||||
|
||||
if (to) {
|
||||
const format = WuBytesPipe.formats[to];
|
||||
|
||||
const result = toDecimal(WuBytesPipe.calculateResult(format, bytes), decimal);
|
||||
|
||||
return WuBytesPipe.formatResult(result, to);
|
||||
}
|
||||
|
||||
for (const key in WuBytesPipe.formats) {
|
||||
const format = WuBytesPipe.formats[key];
|
||||
if (bytes < format.max) {
|
||||
|
||||
const result = toDecimal(WuBytesPipe.calculateResult(format, bytes), decimal);
|
||||
|
||||
return WuBytesPipe.formatResult(result, key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static formatResult(result: number, unit: string): string {
|
||||
return `${result} ${unit}`;
|
||||
}
|
||||
|
||||
static calculateResult(format: { max: number, prev?: ByteUnit }, bytes: number) {
|
||||
const prev = format.prev ? WuBytesPipe.formats[format.prev] : undefined;
|
||||
return prev ? bytes / prev.max : bytes;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
|
||||
@Pipe({ name: 'ceil' })
|
||||
export class CeilPipe implements PipeTransform {
|
||||
transform(nr: number) {
|
||||
return Math.ceil(nr);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
|
||||
@Pipe({ name: 'round' })
|
||||
export class RoundPipe implements PipeTransform {
|
||||
transform(nr: number) {
|
||||
return Math.round(nr);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
|
||||
@Pipe({ name: 'shortenString' })
|
||||
export class ShortenStringPipe implements PipeTransform {
|
||||
transform(str: string, length: number = 12) {
|
||||
const half = length / 2;
|
||||
return str.substring(0, half) + '...' + str.substring(str.length - half);
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
|
||||
@Pipe({ name: 'timeSince' })
|
||||
export class TimeSincePipe implements PipeTransform {
|
||||
transform(timestamp: number) {
|
||||
const minutes = ((new Date().getTime()) - (new Date(timestamp * 1000).getTime())) / 1000 / 60;
|
||||
if (minutes >= 120) {
|
||||
return Math.floor(minutes / 60) + ' hours';
|
||||
}
|
||||
if (minutes >= 60) {
|
||||
return Math.floor(minutes / 60) + ' hour';
|
||||
}
|
||||
if (minutes <= 1) {
|
||||
return '< 1 minute';
|
||||
}
|
||||
if (minutes === 1) {
|
||||
return '1 minute';
|
||||
}
|
||||
return Math.round(minutes) + ' minutes';
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { NgbButtonsModule, NgbModalModule } from '@ng-bootstrap/ng-bootstrap';
|
||||
|
||||
import { BytesPipe } from './pipes/bytes-pipe/bytes.pipe';
|
||||
import { VbytesPipe } from './pipes/bytes-pipe/vbytes.pipe';
|
||||
import { RoundPipe } from './pipes/math-round-pipe/math-round.pipe';
|
||||
import { CeilPipe } from './pipes/math-ceil/math-ceil.pipe';
|
||||
import { ChartistComponent } from '../statistics/chartist.component';
|
||||
import { TimeSincePipe } from './pipes/time-since/time-since.pipe';
|
||||
import { WuBytesPipe } from './pipes/bytes-pipe/wubytes.pipe';
|
||||
import { ShortenStringPipe } from './pipes/shorten-string-pipe/shorten-string.pipe';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
CommonModule,
|
||||
NgbButtonsModule,
|
||||
NgbModalModule,
|
||||
],
|
||||
declarations: [
|
||||
ChartistComponent,
|
||||
RoundPipe,
|
||||
CeilPipe,
|
||||
BytesPipe,
|
||||
VbytesPipe,
|
||||
WuBytesPipe,
|
||||
ShortenStringPipe,
|
||||
TimeSincePipe,
|
||||
],
|
||||
exports: [
|
||||
RoundPipe,
|
||||
CeilPipe,
|
||||
BytesPipe,
|
||||
VbytesPipe,
|
||||
WuBytesPipe,
|
||||
TimeSincePipe,
|
||||
ShortenStringPipe,
|
||||
NgbButtonsModule,
|
||||
NgbModalModule,
|
||||
ChartistComponent,
|
||||
],
|
||||
providers: [
|
||||
BytesPipe,
|
||||
VbytesPipe,
|
||||
WuBytesPipe,
|
||||
ShortenStringPipe,
|
||||
]
|
||||
})
|
||||
export class SharedModule { }
|
||||
Reference in New Issue
Block a user