1.0.6 • Published 3 years ago

@theredhead/formatbytesize v1.0.6

Weekly downloads
-
License
WTFPL
Repository
github
Last release
3 years ago

Purpose

Providing human readable file sizes.

Installing

npm install @theredhead/formatbytesize

Simple usage

import { formatByteSize } from '@theredhead/formatbytesize';

const bytes = 100; // or any number >= 0

// the simplest form
const result = formatByteSize(bytes) // result = "100B";

Advanced usage

To keep things simple yet user extensible, there are several ways to customize the result this funtion returns.

Function Declaration

export const formatByteSize = (
  numberOfBytes: number,
  options: FormatByteSizeOptions
): string {...

Parameters

  • numberOfBytes: the number of bytes to format
  • options expects an opject that conforms to to the FormatByteSizeOptions interfece, this currently has two options (both are optional):
    • kilo: either 1000 or 1024, defaulting to 1024
    • postProcessor: a function to post process the result, defaulting to stripPointZeroZero

Using your own post processing

Sometimes there is this one edge-case that just requires a whole lot more code and introduces a bunch of new opportunities to get buggy. So, formatByteSize is open for expansion by design. Most importantly, you can create and use PostProcessor functions

const mySpecialPostProcessor: FormatByteSizePostProcessorFunc = (n, s) => {
    If (n < 1024 && s == 'B') {
        return 'Less than 1KB';
    }
    return [Math.floor(n), s].join('');
};

const result = formatByteSize(bytes, {
  postProcessor: mySpecialPostProcessor
}); // result = 'Less than 1KB';

Built-in post processors

There are two built in FormatByteSizePostProcessorFuncs that you can use out of the box:

  • stripPointZeroZero will return two decimals except when that would make the number end in ".00", in which case it just returns the whole number. This is the default.
  • alwaysTwoDecimals will return two decimals.

Using alwaysTwoDecimals

const bytes = 1024;

const resultA = formatByteSize(bytes, {
  postProcessor: alwaysTwoDecimals
}) // resultA = "1.00KB";

const resultB = formatByteSize(bytes) // resultB = "1KB";

Using 1000 as a base for kilo instead of 1024:

const bytes = 1024;
// if you really need 1000byte megabytes
const result = formatByteSize(bytes, {kilo: 1000}) // result = "1.20KB";
1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago