Merge pull request #351 from AnalogJ/app_db_settings
commit
d201f798fb
@ -1,75 +1,27 @@
|
|||||||
/**
|
|
||||||
* @license
|
|
||||||
* Copyright (c) 2019 Jonathan Catmull.
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in all
|
|
||||||
* copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
|
||||||
import {Pipe, PipeTransform} from '@angular/core';
|
import {Pipe, PipeTransform} from '@angular/core';
|
||||||
|
|
||||||
type unit = 'bytes' | 'KB' | 'MB' | 'GB' | 'TB' | 'PB';
|
|
||||||
type unitPrecisionMap = {
|
|
||||||
[u in unit]: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
const defaultPrecisionMap: unitPrecisionMap = {
|
|
||||||
bytes: 0,
|
|
||||||
KB: 0,
|
|
||||||
MB: 1,
|
|
||||||
GB: 1,
|
|
||||||
TB: 2,
|
|
||||||
PB: 2
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Convert bytes into largest possible unit.
|
|
||||||
* Takes an precision argument that can be a number or a map for each unit.
|
|
||||||
* Usage:
|
|
||||||
* bytes | fileSize:precision
|
|
||||||
* @example
|
|
||||||
* // returns 1 KB
|
|
||||||
* {{ 1500 | fileSize }}
|
|
||||||
* @example
|
|
||||||
* // returns 2.1 GB
|
|
||||||
* {{ 2100000000 | fileSize }}
|
|
||||||
* @example
|
|
||||||
* // returns 1.46 KB
|
|
||||||
* {{ 1500 | fileSize:2 }}
|
|
||||||
*/
|
|
||||||
@Pipe({name: 'fileSize'})
|
@Pipe({name: 'fileSize'})
|
||||||
export class FileSizePipe implements PipeTransform {
|
export class FileSizePipe implements PipeTransform {
|
||||||
private readonly units: unit[] = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
|
|
||||||
|
|
||||||
transform(bytes: number = 0, precision: number | unitPrecisionMap = defaultPrecisionMap): string {
|
|
||||||
if (isNaN(parseFloat(String(bytes))) || !isFinite(bytes)) return '?';
|
|
||||||
|
|
||||||
let unitIndex = 0;
|
transform(bytes: number = 0, si = false, dp = 1): string {
|
||||||
|
const thresh = si ? 1000 : 1024;
|
||||||
|
|
||||||
while (bytes >= 1024) {
|
if (Math.abs(bytes) < thresh) {
|
||||||
bytes /= 1024;
|
return bytes + ' B';
|
||||||
unitIndex++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const unit = this.units[unitIndex];
|
const units = si
|
||||||
|
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
||||||
|
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
||||||
|
let u = -1;
|
||||||
|
const r = 10 ** dp;
|
||||||
|
|
||||||
if (typeof precision === 'number') {
|
do {
|
||||||
return `${bytes.toFixed(+precision)} ${unit}`;
|
bytes /= thresh;
|
||||||
}
|
++u;
|
||||||
return `${bytes.toFixed(precision[unit])} ${unit}`;
|
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
|
||||||
|
|
||||||
|
|
||||||
|
return bytes.toFixed(dp) + ' ' + units[u];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue