filesize

  • Version 11.0.2
  • Published
  • 38.6 kB
  • No dependencies
  • BSD-3-Clause license

Install

npm i filesize
yarn add filesize
pnpm add filesize

Overview

JavaScript library to generate a human readable String describing the file size

Index

Functions

function filesize

filesize: <T extends FilesizeOptions = {}>(
arg: number | string | bigint,
options?: T
) => FilesizeReturn<T>;
  • Converts a file size in bytes to a human-readable string with appropriate units

    Parameter arg

    The file size in bytes to convert

    Parameter options

    Configuration options for formatting

    Returns

    Formatted file size based on output option

    Throws

    {TypeError} When arg is not a valid number or roundingMethod is invalid

    Example 1

    filesize(1024) // "1 KB" filesize(1024, {bits: true}) // "8 Kb" filesize(1024, {output: "object"}) // {value: 1, symbol: "KB", exponent: 1, unit: "KB"}

function partial

partial: <T extends FilesizeOptions = {}>(
options?: T
) => (arg: number | string | bigint) => FilesizeReturn<T>;
  • Creates a partially applied version of filesize with preset options

    Parameter options

    Default options to apply to the returned function

    Returns

    A function that takes a file size and returns formatted output

    Example 1

    const formatBytes = partial({round: 1, standard: "iec"}); formatBytes(1024) // "1.0 KiB" formatBytes(2048) // "2.0 KiB"

Interfaces

interface FilesizeObject

interface FilesizeObject {}
  • Object format return type when output is "object"

property exponent

exponent: number;
  • The exponent used in calculation

property symbol

symbol: string;
  • The unit symbol

property unit

unit: string;
  • The original unit before symbol customization

property value

value: number | string;
  • The numeric value

interface FilesizeOptions

interface FilesizeOptions {}
  • Options interface for configuring filesize behavior

property base

base?: number;
  • Number base (2 for binary, 10 for decimal, -1 for auto)

property bits

bits?: boolean;
  • If true, calculates bits instead of bytes

property exponent

exponent?: number;
  • Force specific exponent (-1 for auto)

property fullform

fullform?: boolean;
  • If true, uses full unit names instead of abbreviations

property fullforms

fullforms?: string[];
  • Custom full unit names

property locale

locale?: string | boolean;
  • Locale for number formatting, true for system locale

property localeOptions

localeOptions?: Intl.NumberFormatOptions;
  • Additional options for locale formatting

property output

output?: 'string' | 'array' | 'object' | 'exponent';
  • Output format: "string", "array", "object", or "exponent"

property pad

pad?: boolean;
  • If true, pads decimal places to match round parameter

property precision

precision?: number;
  • Number of significant digits (0 for auto)

property round

round?: number;
  • Number of decimal places to round to

property roundingMethod

roundingMethod?: 'round' | 'floor' | 'ceil';
  • Math rounding method to use

property separator

separator?: string;
  • Custom decimal separator

property spacer

spacer?: string;
  • String to separate value and unit

property standard

standard?: 'si' | 'iec' | 'jedec' | '';
  • Unit standard to use (SI, IEC, JEDEC)

property symbols

symbols?: Record<string, string>;
  • Custom unit symbols

Type Aliases

type FilesizeArray

type FilesizeArray = [number | string, string];
  • Array format return type when output is "array"

type FilesizeReturn

type FilesizeReturn<T extends FilesizeOptions = {}> = T['output'] extends 'object'
? FilesizeObject
: T['output'] extends 'array'
? FilesizeArray
: T['output'] extends 'exponent'
? number
: string;
  • Return type based on output option

Package Files (1)

Dependencies (0)

No dependencies.

Dev Dependencies (7)

Peer Dependencies (0)

No peer dependencies.

Badge

To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/filesize.

  • Markdown
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/filesize)
  • HTML
    <a href="https://www.jsdocs.io/package/filesize"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>