UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

54 lines 7.87 kB
/** * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE */ import { Pipe } from '@angular/core'; import { isNumberFinite, toDecimal } from 'ng-zorro-antd/core/util'; export class NzBytesPipe { transform(input, decimal = 0, from = 'B', to) { if (!(isNumberFinite(input) && isNumberFinite(decimal) && decimal % 1 === 0 && decimal >= 0)) { return input; } let bytes = input; let unit = from; while (unit !== 'B') { bytes *= 1024; unit = NzBytesPipe.formats[unit].prev; } if (to) { const format = NzBytesPipe.formats[to]; const result = toDecimal(NzBytesPipe.calculateResult(format, bytes), decimal); return NzBytesPipe.formatResult(result, to); } for (const key in NzBytesPipe.formats) { if (NzBytesPipe.formats.hasOwnProperty(key)) { const format = NzBytesPipe.formats[key]; if (bytes < format.max) { const result = toDecimal(NzBytesPipe.calculateResult(format, bytes), decimal); return NzBytesPipe.formatResult(result, key); } } } } static formatResult(result, unit) { return `${result} ${unit}`; } static calculateResult(format, bytes) { const prev = format.prev ? NzBytesPipe.formats[format.prev] : undefined; return prev ? bytes / prev.max : bytes; } } NzBytesPipe.formats = { B: { max: 1024 }, kB: { max: Math.pow(1024, 2), prev: 'B' }, KB: { max: Math.pow(1024, 2), prev: 'B' }, MB: { max: Math.pow(1024, 3), prev: 'kB' }, GB: { max: Math.pow(1024, 4), prev: 'MB' }, TB: { max: Number.MAX_SAFE_INTEGER, prev: 'GB' } }; NzBytesPipe.decorators = [ { type: Pipe, args: [{ name: 'nzBytes' },] } ]; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnotYnl0ZXMucGlwZS5qcyIsInNvdXJjZVJvb3QiOiIvaG9tZS92c3RzL3dvcmsvMS9zL2NvbXBvbmVudHMvcGlwZXMvIiwic291cmNlcyI6WyJuei1ieXRlcy5waXBlLnRzIl0sIm5hbWVzIjpbXSwibWFwcG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