Giter Club home page Giter Club logo

formatbytesize's Introduction

function formatByteSize(n: number): string

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";

formatbytesize's People

Contributors

theredhead avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.