Refactoring

Move Top Played part.
Move pipes and dashboard
Rename object folder to model
Doc pipes
This commit is contained in:
2018-03-20 23:05:46 +01:00
parent efac03a1c7
commit 6300e9cfc7
27 changed files with 239 additions and 175 deletions

View File

@@ -0,0 +1,8 @@
import { ConvertMoreExactPipe } from './convert-more-exact.pipe';
describe('ConvertMoreExactPipe', () => {
it('create an instance', () => {
const pipe = new ConvertMoreExactPipe();
expect(pipe).toBeTruthy();
});
});

View File

@@ -0,0 +1,56 @@
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'convertMoreExact'
})
export class ConvertMoreExactPipe implements PipeTransform {
/**
* Convert milliseconds to rounded time e.g. ~84d
* @param timeMs time in milliseconds
*/
transform(timeMs: number): string {
let x = timeMs / 1000;
const seconds = Math.round(x % 60);
x /= 60;
let minutes = 0;
if (x > 1) { minutes = Math.round(x % 60); }
x /= 60;
let hours = 0;
if (x > 1) { hours = Math.round(x % 24); }
// TODO Enable/disable day
x /= 24;
const days = Math.round(x);
// Final string
let ret = '~';
if (days > 0) {
return '~' + (days + (hours > 12 ? 1 : 0)) + 'd';
}
if (hours > 0) {
if (minutes > 45) {
ret += (hours + 1) + 'h';
} else if (minutes < 15) {
ret += hours + 'h';
} else {
ret += hours + 'h 1/2';
}
return ret;
}
if (minutes > 0) {
ret += (minutes + (seconds > 35 ? 1 : 0)) + 'min';
return ret;
}
if (seconds > 0) {
if (seconds > 35) {
return '~1min';
} else {
return seconds + 's';
}
}
}
}

View File

@@ -0,0 +1,8 @@
import { ConvertSizeToStringPipe } from './convert-size-to-string.pipe';
describe('ConvertSizeToStringPipe', () => {
it('create an instance', () => {
const pipe = new ConvertSizeToStringPipe();
expect(pipe).toBeTruthy();
});
});

View File

@@ -0,0 +1,26 @@
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'convertSizeToString'
})
export class ConvertSizeToStringPipe implements PipeTransform {
/**
* Convert size to human readable size
* @param size size in byte
*/
transform(size: number): any {
const units = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB'];
if (size === 0) {
return '0 Byte';
}
const i = Math.floor(Math.log(size) / Math.log(1024));
let calcSize = size / Math.pow(1024, i);
calcSize = Math.round(calcSize * 100) / 100;
return calcSize + ' ' + units[i];
}
}

View File

@@ -0,0 +1,8 @@
import { ConvertMsPipe } from './convertms.pipe';
describe('ConvertMsPipe', () => {
it('create an instance', () => {
const pipe = new ConvertMsPipe();
expect(pipe).toBeTruthy();
});
});

View File

@@ -0,0 +1,38 @@
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'convertMs'
})
export class ConvertMsPipe implements PipeTransform {
/**
* Convert milliseconds to "readable" duration e.g. 1d21h35m24s
* @param timeMs time in milliseconds
*/
transform(timeMs: number): string {
let x = timeMs / 1000;
const seconds = Math.round(x % 60);
x /= 60;
let minutes = 0;
if (x > 1) { minutes = Math.round(x % 60); }
x /= 60;
let hours = 0;
if (x > 1) { hours = Math.round(x % 24); }
// TODO Enable/disable day
x /= 24;
const days = Math.round(x);
// Final string
let ret = '';
if (days > 0) { ret += ('0' + days).slice(-2) + 'd'; }
if (hours > 0) { ret += ('0' + hours).slice(-2) + 'h'; }
if (minutes > 0) { ret += ('0' + minutes).slice(-2) + 'm'; }
if (seconds > 0) { ret += ('0' + seconds).slice(-2) + 's'; }
return ret;
// return ('0' + days).slice(-2) + ':' + ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2);
}
}

View File

@@ -0,0 +1,8 @@
import { SortByPipe } from './sort-by.pipe';
describe('SortByPipe', () => {
it('create an instance', () => {
const pipe = new SortByPipe();
expect(pipe).toBeTruthy();
});
});

View File

@@ -0,0 +1,40 @@
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'sortBy'
})
export class SortByPipe implements PipeTransform {
/**
* Sort an array by multiple elements.
*
* Example to sort an array respectively by Year, Album, Disc Number, etc.
* Typescript usage:
* <pre>
* new SortByPipe().transform(this.songs, 'Year', 'Album', 'Disc Number', 'Track Number', 'Play Count');
* </pre>
* In HTML usage:
* <pre>
* <tr *ngFor="let song of songs | sortBy : 'Year':'Album':'Disc Number':'Track Number'">
* </pre>
*
* @param array array to sort
* @param args elements used to sort in oder
*/
transform(array: Array<any>, ...args: any[]): Array<any> {
array.sort((a: any, b: any) => {
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (a[arg] === undefined && b[arg] !== undefined) { return -1; }
if (a[arg] !== undefined && b[arg] === undefined) { return 1; }
if (a[arg] < b[arg]) { return -1; }
if (a[arg] > b[arg]) { return 1; }
}
return 0;
});
return array;
}
}