Feature/shared refactor (#1176)

* refactor: move most of the shared entities to @start8labs/shared library
This commit is contained in:
Alex Inkin
2022-02-15 18:13:05 +03:00
committed by GitHub
parent 1769f7e2e6
commit 7c26b18c73
164 changed files with 2908 additions and 2860 deletions

View File

@@ -1,18 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
// converts bytes to gigabytes
@Pipe({
name: 'convertBytes',
})
export class ConvertBytesPipe implements PipeTransform {
transform (bytes: number): string {
if (bytes === 0) return '0 Bytes'
const k = 1024
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]
}
}

View File

@@ -1,12 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
import { isEmptyObject } from '../util/misc.util'
@Pipe({
name: 'empty',
})
export class EmptyPipe implements PipeTransform {
transform (val: object | [] = { }): boolean {
if (Array.isArray(val)) return !val.length
return isEmptyObject(val)
}
}

View File

@@ -1,46 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
import { Emver } from '../services/emver.service'
@Pipe({
name: 'satisfiesEmver',
})
export class EmverSatisfiesPipe implements PipeTransform {
constructor (private readonly emver: Emver) { }
transform (versionUnderTest: string, range: string): boolean {
return this.emver.satisfies(versionUnderTest, range)
}
}
@Pipe({
name: 'compareEmver',
})
export class EmverComparesPipe implements PipeTransform {
constructor (private readonly emver: Emver) { }
transform (first: string, second: string): SemverResult {
try {
return this.emver.compare(first, second) as SemverResult
} catch (e) {
console.warn(`emver comparison failed`, e, first, second)
return 'comparison-impossible'
}
}
}
type SemverResult = 0 | 1 | -1 | 'comparison-impossible'
@Pipe({
name: 'displayEmver',
})
export class EmverDisplayPipe implements PipeTransform {
constructor () { }
transform (version: string): string {
return displayEmver(version)
}
}
export function displayEmver (version: string): string {
const vs = version.split('.')
if (vs.length === 4) return `${vs[0]}.${vs[1]}.${vs[2]}~${vs[3]}`
return version
}

View File

@@ -1,10 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
@Pipe({
name: 'includes',
})
export class IncludesPipe implements PipeTransform {
transform<T> (list: T[], val: T): boolean {
return list.includes(val)
}
}

View File

@@ -1,14 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
import { InstallProgress } from '../services/patch-db/data-model'
import { packageLoadingProgress } from '../util/package-loading-progress'
@Pipe({
name: 'installProgress',
})
export class InstallProgressPipe implements PipeTransform {
transform(loadData: InstallProgress): string {
const { totalProgress } = packageLoadingProgress(loadData)
return totalProgress < 99 ? totalProgress + '%' : 'finalizing'
}
}

View File

@@ -1,15 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
import { PackageDataEntry } from '../services/patch-db/data-model'
import {
packageLoadingProgress,
ProgressData,
} from '../util/package-loading-progress'
@Pipe({
name: 'installState',
})
export class InstallState implements PipeTransform {
transform(pkg: PackageDataEntry): ProgressData | null {
return packageLoadingProgress(pkg['install-progress'])
}
}

View File

@@ -0,0 +1,8 @@
import { NgModule } from '@angular/core'
import { LaunchablePipe } from './launchable.pipe'
@NgModule({
declarations: [LaunchablePipe],
exports: [LaunchablePipe],
})
export class LaunchablePipeModule {}

View File

@@ -0,0 +1,22 @@
import { Pipe, PipeTransform } from '@angular/core'
import { PackageState } from '@start9labs/shared'
import {
InterfaceDef,
PackageMainStatus,
} from 'src/app/services/patch-db/data-model'
import { ConfigService } from '../../services/config.service'
@Pipe({
name: 'isLaunchable',
})
export class LaunchablePipe implements PipeTransform {
constructor(private configService: ConfigService) {}
transform(
state: PackageState,
status: PackageMainStatus,
interfaces: Record<string, InterfaceDef>,
): boolean {
return this.configService.isLaunchable(state, status, interfaces)
}
}

View File

@@ -1,17 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
import { marked } from 'marked'
import * as DOMPurify from 'dompurify'
@Pipe({
name: 'markdown',
})
export class MarkdownPipe implements PipeTransform {
transform(value: any): any {
if (value && value.length > 0) {
const html = marked(value)
const sanitized = DOMPurify.sanitize(html)
return sanitized
}
return value
}
}

View File

@@ -0,0 +1,8 @@
import { NgModule } from '@angular/core'
import { MaskPipe } from './mask.pipe'
@NgModule({
declarations: [MaskPipe],
exports: [MaskPipe],
})
export class MaskPipeModule {}

View File

@@ -4,9 +4,9 @@ import { Pipe, PipeTransform } from '@angular/core'
name: 'mask',
})
export class MaskPipe implements PipeTransform {
transform (val: string, max = 16): string {
transform(val: string, max = 16): string {
if (!val) return val
const times = val.length <= max ? val.length : max
return '●'.repeat(times)
}
}
}

View File

@@ -1,23 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
import { NotificationLevel, ServerNotification } from '../services/api/api.types'
@Pipe({
name: 'notificationColor',
})
export class NotificationColorPipe implements PipeTransform {
transform (notification: ServerNotification<any>): string {
const level = notification.level
switch (level) {
case NotificationLevel.Info:
return 'primary'
case NotificationLevel.Success:
return 'success'
case NotificationLevel.Warning:
return 'warning'
case NotificationLevel.Error:
return 'danger'
default:
return ''
}
}
}

View File

@@ -1,33 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
@Pipe({
name: 'truncateCenter',
})
export class TruncateCenterPipe implements PipeTransform {
transform (value: string, front: number, back: number, fullOnDesktop: boolean = false): unknown {
if (value.length <= front + back + 3) return value
if (fullOnDesktop && screen.width > 500) return value
return value.slice(0, front) + '...' + value.slice(value.length - back, value.length)
}
}
@Pipe({
name: 'truncateEnd',
})
export class TruncateEndPipe implements PipeTransform {
transform (val: string, length: number): unknown {
if (val.length <= length) return val
return val.slice(0, length) + '...'
}
}
@Pipe({
name: 'truncateTail',
})
export class TruncateTailPipe implements PipeTransform {
transform (val: string, length: number): unknown {
if (val.length <= length) return val
return '...' + val.substr(length * -1)
}
}

View File

@@ -1,16 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
@Pipe({
name: 'typeof',
})
export class TypeofPipe implements PipeTransform {
transform (value: any): any {
if (value === null) {
return 'null'
} else if (Array.isArray(value)) {
return 'array'
}
return typeof value
}
}

View File

@@ -1,43 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
import {
InterfaceDef,
PackageMainStatus,
PackageState,
} from '../services/patch-db/data-model'
import { ConfigService, hasUi } from '../services/config.service'
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'
@Pipe({
name: 'hasUi',
})
export class HasUiPipe implements PipeTransform {
transform(interfaces: { [id: string]: InterfaceDef }): boolean {
return hasUi(interfaces)
}
}
@Pipe({
name: 'isLaunchable',
})
export class LaunchablePipe implements PipeTransform {
constructor(private configService: ConfigService) {}
transform(
state: PackageState,
status: PackageMainStatus,
interfaces: { [id: string]: InterfaceDef },
): boolean {
return this.configService.isLaunchable(state, status, interfaces)
}
}
@Pipe({
name: 'sanitize',
})
export class SanitizePipe implements PipeTransform {
constructor(public readonly sanitizer: DomSanitizer) {}
transform(base64Icon: string): SafeResourceUrl {
return this.sanitizer.bypassSecurityTrustResourceUrl(base64Icon)
}
}

View File

@@ -0,0 +1,8 @@
import { NgModule } from '@angular/core'
import { UiPipe } from './ui.pipe'
@NgModule({
declarations: [UiPipe],
exports: [UiPipe],
})
export class UiPipeModule {}

View File

@@ -0,0 +1,12 @@
import { Pipe, PipeTransform } from '@angular/core'
import { InterfaceDef } from '../../services/patch-db/data-model'
import { hasUi } from '../../services/config.service'
@Pipe({
name: 'hasUi',
})
export class UiPipe implements PipeTransform {
transform(interfaces: Record<string, InterfaceDef>): boolean {
return hasUi(interfaces)
}
}

View File

@@ -1,24 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core'
@Pipe({
name: 'durationToSeconds',
})
export class DurationToSecondsPipe implements PipeTransform {
transform (duration: string | null): number {
if (!duration) return 0
const splitUnit = duration.match(/^([0-9]*(\.[0-9]+)?)(ns|µs|ms|s|m|d)$/)
const unit = splitUnit[3]
const num = splitUnit[1]
return Number(num) * unitsToSeconds[unit]
}
}
const unitsToSeconds = {
'ns': 1e-9,
'µs': 1e-6,
'ms': 0.001,
's': 1,
'm': 60,
'h': 3600,
'd': 86400,
}