All Projects → 75lb → Byte Size

75lb / Byte Size

Licence: mit
Isomorphic function to convert a bytes value (e.g. 3456) to a human-readable string ('3.5 kB')

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Byte Size

validid
A Javascript library to validate ID card numbers of China, Taiwan, Hong Kong and South Korea
Stars: ✭ 37 (+12.12%)
Mutual labels:  npm-package, javascript-library
html-to-react
A lightweight library that converts raw HTML to a React DOM structure.
Stars: ✭ 696 (+2009.09%)
Mutual labels:  npm-package, javascript-library
jsdoc-api
A programmatic interface for jsdoc3 with a few extra features
Stars: ✭ 55 (+66.67%)
Mutual labels:  npm-package, javascript-library
Typy
Minimal JavaScript type checking library
Stars: ✭ 215 (+551.52%)
Mutual labels:  npm-package, javascript-library
Length.js
📏 JavaScript library for length units conversion.
Stars: ✭ 292 (+784.85%)
Mutual labels:  npm-package, javascript-library
ansicolor
A JavaScript ANSI color/style management. ANSI parsing. ANSI to CSS. Small, clean, no dependencies.
Stars: ✭ 91 (+175.76%)
Mutual labels:  npm-package, javascript-library
example-typescript-package
Example TypeScript Package ready to be published on npm & Tutorial / Instruction / Workflow for 2021
Stars: ✭ 71 (+115.15%)
Mutual labels:  npm-package, javascript-library
Ansi Escape Sequences
A simple, isomorphic library containing all known terminal ansi escape codes and sequences.
Stars: ✭ 44 (+33.33%)
Mutual labels:  isomorphic, javascript-library
node-split-file
🌱 NodeJS Module to split and merge files for several purposes like transporting over unstable networks.
Stars: ✭ 33 (+0%)
Mutual labels:  npm-package, bytes
typical
Isomorphic, functional type-checking for Javascript
Stars: ✭ 17 (-48.48%)
Mutual labels:  isomorphic, javascript-library
Ag Psd
Javascript library for reading and writing PSD files
Stars: ✭ 135 (+309.09%)
Mutual labels:  npm-package, javascript-library
Crypto Hash
Tiny hashing module that uses the native crypto API in Node.js and the browser
Stars: ✭ 501 (+1418.18%)
Mutual labels:  isomorphic, npm-package
react-circle-flags
🚀 A React component with a collection of 300+ minimal circular SVG country flags. Wrapper of HatScripts/circle-flags
Stars: ✭ 29 (-12.12%)
Mutual labels:  npm-package, javascript-library
ionic-image-upload
Ionic Plugin for Uploading Images to Amazon S3
Stars: ✭ 26 (-21.21%)
Mutual labels:  npm-package, javascript-library
Ky Universal
Use Ky in both Node.js and browsers
Stars: ✭ 421 (+1175.76%)
Mutual labels:  isomorphic, npm-package
Rando.js
The world's easiest, most powerful random function.
Stars: ✭ 659 (+1896.97%)
Mutual labels:  npm-package, javascript-library
Epoh
Multiplayer turn-based browser strategy game
Stars: ✭ 11 (-66.67%)
Mutual labels:  isomorphic
Nuxt.js
The Intuitive Vue(2) Framework
Stars: ✭ 38,986 (+118039.39%)
Mutual labels:  isomorphic
Termynal
⬛️ Lightweight and modern terminal animations using async/await
Stars: ✭ 858 (+2500%)
Mutual labels:  javascript-library
Isomorph
Shared utilities for browsers and Node.js
Stars: ✭ 9 (-72.73%)
Mutual labels:  isomorphic

view on npm npm module downloads Gihub repo dependents Gihub package dependents Build Status Coverage Status js-standard-style

Upgraders, please check the release notes.

byte-size

An isomorphic, load-anywhere function to convert a bytes value (e.g. 3456) to a human-readable string ('3.5 kB'). Choose between metric or IEC units (summarised below) or specify your own custom units.

Value Metric Metric (octet)
1000 kB kilobyte ko kilooctet
1000^2 MB megabyte Mo megaoctet
1000^3 GB gigabyte Go gigaoctet
1000^4 TB terabyte To teraoctet
1000^5 PB petabyte Po petaoctet
1000^6 EB exabyte Eo exaoctet
1000^7 ZB zettabyte Zo zettaoctet
1000^8 YB yottabyte Yo yottaoctet
Value IEC IEC (octet)
1024 KiB kibibyte Kio kibioctet
1024^2 MiB mebibyte Mio mebioctet
1024^3 GiB gibibyte Gio gibioctet
1024^4 TiB tebibyte Tio tebioctet
1024^5 PiB pebibyte Pio pebioctet
1024^6 EiB exbibyte Eio exbioctet
1024^7 ZiB zebibyte Zio zebioctet
1024^8 YiB yobibyte Yio yobioctet

Synopsis

By default, byteSize converts the input number to a human readable string with metric units and a precision of 1.

> const byteSize = require('byte-size')

> byteSize(1580)
{ value: '1.6', unit: 'kB', long: 'kilobytes' }

The object returned by byteSize defines a toString method therefore can be used directly in string context.

> `Filesize: ${byteSize(12400)}`
'Filesize: 12.4 kB'

Override the default toString behaviour by setting options.toStringFn.

> function toStringFn () {
  return `**${this.value}${this.unit}**`
}

> `Filesize: ${byteSize(12400, { toStringFn })}`
'Filesize: **12.4kB**'

Beside the default of metric, there are three other built-in units available: metric_octet, iec and iec_octet.

> byteSize(1580, { units: 'iec' })
{ value: '1.5', unit: 'KiB', long: 'kibibytes' }

> byteSize(1580, { units: 'iec_octet' })
{ value: '1.5', unit: 'Kio', long: 'kibioctets' }

> byteSize(1580, { units: 'metric_octet' })
{ value: '1.6', unit: 'ko', long: 'kilooctets' }

You can adjust the precision.

> byteSize(1580, { units: 'iec', precision: 3 })
{ value: '1.543', unit: 'KiB', long: 'kibibytes' }

> byteSize(1580, { units: 'iec', precision: 0 })
{ value: '2', unit: 'KiB', long: 'kibibytes' }

Define custom units by passing an object containing one or more additional conversion tables to options.customUnits. In options.units, specify the name of a property from the customUnits object.

> const customUnits = {
  simple: [
    { from: 0   , to: 1e3 , unit: ''  },
    { from: 1e3 , to: 1e6 , unit: 'K', long: 'thousand' },
    { from: 1e6 , to: 1e9 , unit: 'Mn', long: 'million' },
    { from: 1e9 , to: 1e12, unit: 'Bn', long: 'billion' }
  ]
}

> const { value, unit } = byteSize(10000, { customUnits, units: 'simple' })

> `${value}${unit}`
'10.0K'

Override the built-in defaults for the duration of the process by passing an options object to byteSize.defaultOptions(). This results in cleaner code in cases where byteSize is used often with the same options.

> byteSize.defaultOptions({
  units: 'simple',
  precision: 2,
  customUnits: {
    simple: [
      { from: 0, to: 1e3, unit: '' },
      { from: 1e3, to: 1e6, unit: 'k' },
      { from: 1e6, to: 1e9, unit: 'm' },
      { from: 1e9, to: 1e12, unit: 'bn' },
    ]
  },
  toStringFn: function () {
    return this.value + this.unit
  }
})

> [2400, 16400, 3991200].map(byteSize).join(', ')
'2.40k, 16.40k, 3.99m'

byte-size

byteSize(bytes, [options]) ⇒ object

Returns an object with the spec { value: string, unit: string, long: string }. The returned object defines a toString method meaning it can be used in any string context.

Kind: Exported function

Param Type Description
bytes number The bytes value to convert.
[options] object Optional config.
[options.precision] number Number of decimal places. Defaults to 1.
[options.units] string Specify 'metric', 'iec', 'metric_octet', 'iec_octet' or the name of a property from the custom units table in options.customUnits. Defaults to metric.
[options.customUnits] object An object containing one or more custom unit lookup tables.
[options.toStringFn] function A toString function to override the default.

byteSize.defaultOptions(options)

Set the default byteSize options for the duration of the process.

Kind: static method of byteSize

Param Type Description
options object A byteSize options object.

Load anywhere

This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.

Node.js:

const byteSize = require('byte-size')

Within Node.js with ECMAScript Module support enabled:

import byteSize from 'byte-size'

Within a modern browser ECMAScript Module:

import byteSize from './node_modules/byte-size/index.mjs'

Old browser (adds window.byteSize):

<script nomodule src="./node_modules/byte-size/dist/index.js"></script>

© 2014-21 Lloyd Brookes <[email protected]>.

Isomorphic test suite by test-runner and web-runner. Documented by jsdoc-to-markdown.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].