How to use the parse function from bytes

Find comprehensive JavaScript bytes.parse code examples handpicked from public code repositorys.

bytes.parse is a JavaScript function that parses a string argument representing a byte size value into an integer.

48
49
50
51
52
53
54
55
56
57
58
59


function json (options) {
  var opts = options || {}


  var limit = typeof opts.limit !== 'number'
    ? bytes.parse(opts.limit || '100kb')
    : opts.limit
  var inflate = opts.inflate !== false
  var reviver = opts.reviver
  var strict = opts.strict !== false
fork icon0
star icon0
watch icon1

94
95
96
97
98
99
100
101
102
103
var encoding = opts.encoding !== true
  ? opts.encoding
  : 'utf-8'

// convert the limit to an integer
var limit = bytes.parse(opts.limit)

// convert the expected length to an integer
var length = opts.length != null && !isNaN(opts.length)
  ? parseInt(opts.length, 10)
fork icon0
star icon0
watch icon1

How does bytes.parse work?

The bytes.parse function in the bytes library parses the given string value and converts it to a number representing the value in bytes, with optional support for a size descriptor such as KB or MB.

Ai Example

1
2
3
const bytes = require("bytes");

const sizeInBytes = bytes.parse("1GB"); // 1073741824

In this example, bytes.parse is used to convert a string representation of a size ('1GB') to the equivalent number of bytes (1073741824).