From 47a8ca1d72f0f0835b45cfb2c4fb8ab1218dc14a Mon Sep 17 00:00:00 2001 From: Ruy Adorno Date: Wed, 30 Sep 2020 12:17:50 -0400 Subject: [PATCH] byte-size@7.0.0 --- node_modules/byte-size/LICENSE | 2 +- node_modules/byte-size/README.hbs | 136 +++++++++++++++- node_modules/byte-size/README.md | 227 +++++++++++++++++---------- node_modules/byte-size/dist/index.js | 211 +++++++++++-------------- node_modules/byte-size/index.mjs | 211 +++++++++++-------------- node_modules/byte-size/package.json | 36 +++-- package-lock.json | 16 +- package.json | 2 +- 8 files changed, 490 insertions(+), 351 deletions(-) diff --git a/node_modules/byte-size/LICENSE b/node_modules/byte-size/LICENSE index b95c652e2b473..d9e1e9d6918a4 100644 --- a/node_modules/byte-size/LICENSE +++ b/node_modules/byte-size/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014-18 Lloyd Brookes <75pound@gmail.com> +Copyright (c) 2014-20 Lloyd Brookes <75pound@gmail.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/node_modules/byte-size/README.hbs b/node_modules/byte-size/README.hbs index 8a57e4a9c784a..5b677f2a98870 100644 --- a/node_modules/byte-size/README.hbs +++ b/node_modules/byte-size/README.hbs @@ -1,13 +1,135 @@ -[![view on npm](https://img.shields.io/npm/v/byte-size.svg)](https://www.npmjs.org/package/byte-size) -[![npm module downloads](https://img.shields.io/npm/dt/byte-size.svg)](https://www.npmjs.org/package/byte-size) +[![view on npm](https://badgen.net/npm/v/byte-size)](https://www.npmjs.org/package/byte-size) +[![npm module downloads](https://badgen.net/npm/dt/byte-size)](https://www.npmjs.org/package/byte-size) +[![Gihub repo dependents](https://badgen.net/github/dependents-repo/75lb/byte-size)](https://github.com/75lb/byte-size/network/dependents?dependent_type=REPOSITORY) +[![Gihub package dependents](https://badgen.net/github/dependents-pkg/75lb/byte-size)](https://github.com/75lb/byte-size/network/dependents?dependent_type=PACKAGE) [![Build Status](https://travis-ci.org/75lb/byte-size.svg?branch=master)](https://travis-ci.org/75lb/byte-size) -[![Coverage Status](https://coveralls.io/repos/github/75lb/byte-size/badge.svg?branch=master)](https://coveralls.io/github/75lb/byte-size?branch=master) -[![Dependency Status](https://david-dm.org/75lb/byte-size.svg)](https://david-dm.org/75lb/byte-size) +[![Coverage Status](https://coveralls.io/repos/github/75lb/byte-size/badge.svg)](https://coveralls.io/github/75lb/byte-size) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard) +***Upgraders, please check the [release notes](https://github.com/75lb/byte-size/releases).*** + +# 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](https://en.wikipedia.org/wiki/Gigabyte) (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. + +```js +> 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. + +```js +> `Filesize: ${byteSize(12400)}` +'Filesize: 12.4 kB' +``` + +Override the default `toString` behaviour by setting [`options.toStringFn`](#bytesizebytes-options--object-). + +```js +> 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`. + +```js +> 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`. + +```js +> 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. + +```js +> 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. + +```js +> 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' +``` + {{>main}} -### Load anywhere +## 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. @@ -37,4 +159,6 @@ Old browser (adds `window.byteSize`): * * * -© 2014-18 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). +© 2014-20 Lloyd Brookes \<75pound@gmail.com\>. + +Isomorphic test suite by [test-runner](https://github.com/test-runner-js/test-runner) and [web-runner](https://github.com/test-runner-js/web-runner). Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). diff --git a/node_modules/byte-size/README.md b/node_modules/byte-size/README.md index 9a36daaf52f48..71f47b1e49b3c 100644 --- a/node_modules/byte-size/README.md +++ b/node_modules/byte-size/README.md @@ -1,106 +1,169 @@ -[![view on npm](https://img.shields.io/npm/v/byte-size.svg)](https://www.npmjs.org/package/byte-size) -[![npm module downloads](https://img.shields.io/npm/dt/byte-size.svg)](https://www.npmjs.org/package/byte-size) +[![view on npm](https://badgen.net/npm/v/byte-size)](https://www.npmjs.org/package/byte-size) +[![npm module downloads](https://badgen.net/npm/dt/byte-size)](https://www.npmjs.org/package/byte-size) +[![Gihub repo dependents](https://badgen.net/github/dependents-repo/75lb/byte-size)](https://github.com/75lb/byte-size/network/dependents?dependent_type=REPOSITORY) +[![Gihub package dependents](https://badgen.net/github/dependents-pkg/75lb/byte-size)](https://github.com/75lb/byte-size/network/dependents?dependent_type=PACKAGE) [![Build Status](https://travis-ci.org/75lb/byte-size.svg?branch=master)](https://travis-ci.org/75lb/byte-size) -[![Coverage Status](https://coveralls.io/repos/github/75lb/byte-size/badge.svg?branch=master)](https://coveralls.io/github/75lb/byte-size?branch=master) -[![Dependency Status](https://david-dm.org/75lb/byte-size.svg)](https://david-dm.org/75lb/byte-size) +[![Coverage Status](https://coveralls.io/repos/github/75lb/byte-size/badge.svg)](https://coveralls.io/github/75lb/byte-size) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard) - +***Upgraders, please check the [release notes](https://github.com/75lb/byte-size/releases).*** -## byte-size -An isomorphic, load-anywhere function to convert a bytes value into a more human-readable format. Choose between [metric or IEC units](https://en.wikipedia.org/wiki/Gigabyte), summarised below. - -Value | Metric ------ | ------------- -1000 | kB kilobyte -1000^2 | MB megabyte -1000^3 | GB gigabyte -1000^4 | TB terabyte -1000^5 | PB petabyte -1000^6 | EB exabyte -1000^7 | ZB zettabyte -1000^8 | YB yottabyte - -Value | IEC ------ | ------------ -1024 | KiB kibibyte -1024^2 | MiB mebibyte -1024^3 | GiB gibibyte -1024^4 | TiB tebibyte -1024^5 | PiB pebibyte -1024^6 | EiB exbibyte -1024^7 | ZiB zebibyte -1024^8 | YiB yobibyte - -Value | Metric (octet) ------ | ------------- -1000 | ko kilooctet -1000^2 | Mo megaoctet -1000^3 | Go gigaoctet -1000^4 | To teraoctet -1000^5 | Po petaoctet -1000^6 | Eo exaoctet -1000^7 | Zo zettaoctet -1000^8 | Yo yottaoctet - -Value | IEC (octet) ------ | ------------ -1024 | Kio kilooctet -1024^2 | Mio mebioctet -1024^3 | Gio gibioctet -1024^4 | Tio tebioctet -1024^5 | Pio pebioctet -1024^6 | Eio exbioctet -1024^7 | Zio zebioctet -1024^8 | Yio yobioctet - -**Example** -```js -const byteSize = require('byte-size') -``` - +# byte-size -### byteSize(bytes, [options]) ⇒ Object ⏏ -**Kind**: Exported function +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](https://en.wikipedia.org/wiki/Gigabyte) (summarised below) or specify your own custom units. -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| bytes | number | | the bytes value to convert. | -| [options] | object | | optional config. | -| [options.precision] | number | 1 | number of decimal places. | -| [options.units] | string | "metric" | select `'metric'`, `'iec'`, `'metric_octet'` or `'iec_octet'` 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. -**Example** ```js > const byteSize = require('byte-size') > byteSize(1580) -{ value: '1.6', unit: 'kB' } +{ value: '1.6', unit: 'kB', long: 'kilobytes' } +``` + +The object returned by `byteSize` defines a `toString` method therefore can be used directly in string context. + +```js +> `Filesize: ${byteSize(12400)}` +'Filesize: 12.4 kB' +``` + +Override the default `toString` behaviour by setting [`options.toStringFn`](#bytesizebytes-options--object-). + +```js +> 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`. + +```js > byteSize(1580, { units: 'iec' }) -{ value: '1.5', unit: 'KiB' } +{ 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`. +```js > byteSize(1580, { units: 'iec', precision: 3 }) -{ value: '1.543', unit: 'KiB' } +{ value: '1.543', unit: 'KiB', long: 'kibibytes' } > byteSize(1580, { units: 'iec', precision: 0 }) -{ value: '2', unit: 'KiB' } +{ value: '2', unit: 'KiB', long: 'kibibytes' } +``` -> byteSize(1580, { units: 'metric_octet' }) -{ value: '1.6', unit: 'ko' } +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. -> byteSize(1580, { units: 'iec_octet' }) -{ value: '1.5', unit: 'Kio' } +```js +> 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' +``` -> byteSize(1580, { units: 'iec_octet' }).toString() -'1.5 Kio' +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. -> const { value, unit } = byteSize(1580, { units: 'iec_octet' }) -> `${value} ${unit}` -'1.5 Kio' +```js +> 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' ``` -### Load anywhere + + +## byte-size + +* [byte-size](#module_byte-size) + * [byteSize(bytes, [options])](#exp_module_byte-size--byteSize) ⇒ object ⏏ + * [.defaultOptions(options)](#module_byte-size--byteSize.defaultOptions) + + + +### 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](#exp_module_byte-size--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. @@ -130,4 +193,6 @@ Old browser (adds `window.byteSize`): * * * -© 2014-18 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). +© 2014-20 Lloyd Brookes \<75pound@gmail.com\>. + +Isomorphic test suite by [test-runner](https://github.com/test-runner-js/test-runner) and [web-runner](https://github.com/test-runner-js/web-runner). Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). diff --git a/node_modules/byte-size/dist/index.js b/node_modules/byte-size/dist/index.js index 8253a63545b3a..78129d8b02d21 100644 --- a/node_modules/byte-size/dist/index.js +++ b/node_modules/byte-size/dist/index.js @@ -2,151 +2,122 @@ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = global || self, global.byteSize = factory()); -}(this, function () { 'use strict'; +}(this, (function () { 'use strict'; /** - * An isomorphic, load-anywhere function to convert a bytes value into a more human-readable format. Choose between [metric or IEC units](https://en.wikipedia.org/wiki/Gigabyte), summarised below. - * - * Value | Metric - * ----- | ------------- - * 1000 | kB kilobyte - * 1000^2 | MB megabyte - * 1000^3 | GB gigabyte - * 1000^4 | TB terabyte - * 1000^5 | PB petabyte - * 1000^6 | EB exabyte - * 1000^7 | ZB zettabyte - * 1000^8 | YB yottabyte - * - * Value | IEC - * ----- | ------------ - * 1024 | KiB kibibyte - * 1024^2 | MiB mebibyte - * 1024^3 | GiB gibibyte - * 1024^4 | TiB tebibyte - * 1024^5 | PiB pebibyte - * 1024^6 | EiB exbibyte - * 1024^7 | ZiB zebibyte - * 1024^8 | YiB yobibyte - * - * Value | Metric (octet) - * ----- | ------------- - * 1000 | ko kilooctet - * 1000^2 | Mo megaoctet - * 1000^3 | Go gigaoctet - * 1000^4 | To teraoctet - * 1000^5 | Po petaoctet - * 1000^6 | Eo exaoctet - * 1000^7 | Zo zettaoctet - * 1000^8 | Yo yottaoctet - * - * Value | IEC (octet) - * ----- | ------------ - * 1024 | Kio kilooctet - * 1024^2 | Mio mebioctet - * 1024^3 | Gio gibioctet - * 1024^4 | Tio tebioctet - * 1024^5 | Pio pebioctet - * 1024^6 | Eio exbioctet - * 1024^7 | Zio zebioctet - * 1024^8 | Yio yobioctet - * * @module byte-size - * @example - * ```js - * const byteSize = require('byte-size') - * ``` */ + let defaultOptions = {}; + const _options = new WeakMap(); + class ByteSize { constructor (bytes, options) { - options = options || {}; - options.units = options.units || 'metric'; - options.precision = typeof options.precision === 'undefined' ? 1 : options.precision; + options = Object.assign({ + units: 'metric', + precision: 1 + }, defaultOptions, options); + _options.set(this, options); - const table = [ - { expFrom: 0, expTo: 1, metric: 'B', iec: 'B', metric_octet: 'o', iec_octet: 'o' }, - { expFrom: 1, expTo: 2, metric: 'kB', iec: 'KiB', metric_octet: 'ko', iec_octet: 'Kio' }, - { expFrom: 2, expTo: 3, metric: 'MB', iec: 'MiB', metric_octet: 'Mo', iec_octet: 'Mio' }, - { expFrom: 3, expTo: 4, metric: 'GB', iec: 'GiB', metric_octet: 'Go', iec_octet: 'Gio' }, - { expFrom: 4, expTo: 5, metric: 'TB', iec: 'TiB', metric_octet: 'To', iec_octet: 'Tio' }, - { expFrom: 5, expTo: 6, metric: 'PB', iec: 'PiB', metric_octet: 'Po', iec_octet: 'Pio' }, - { expFrom: 6, expTo: 7, metric: 'EB', iec: 'EiB', metric_octet: 'Eo', iec_octet: 'Eio' }, - { expFrom: 7, expTo: 8, metric: 'ZB', iec: 'ZiB', metric_octet: 'Zo', iec_octet: 'Zio' }, - { expFrom: 8, expTo: 9, metric: 'YB', iec: 'YiB', metric_octet: 'Yo', iec_octet: 'Yio' } - ]; + const tables = { + metric: [ + { from: 0 , to: 1e3 , unit: 'B' , long: 'bytes' }, + { from: 1e3 , to: 1e6 , unit: 'kB', long: 'kilobytes' }, + { from: 1e6 , to: 1e9 , unit: 'MB', long: 'megabytes' }, + { from: 1e9 , to: 1e12, unit: 'GB', long: 'gigabytes' }, + { from: 1e12, to: 1e15, unit: 'TB', long: 'terabytes' }, + { from: 1e15, to: 1e18, unit: 'PB', long: 'petabytes' }, + { from: 1e18, to: 1e21, unit: 'EB', long: 'exabytes' }, + { from: 1e21, to: 1e24, unit: 'ZB', long: 'zettabytes' }, + { from: 1e24, to: 1e27, unit: 'YB', long: 'yottabytes' }, + ], + metric_octet: [ + { from: 0 , to: 1e3 , unit: 'o' , long: 'octets' }, + { from: 1e3 , to: 1e6 , unit: 'ko', long: 'kilooctets' }, + { from: 1e6 , to: 1e9 , unit: 'Mo', long: 'megaoctets' }, + { from: 1e9 , to: 1e12, unit: 'Go', long: 'gigaoctets' }, + { from: 1e12, to: 1e15, unit: 'To', long: 'teraoctets' }, + { from: 1e15, to: 1e18, unit: 'Po', long: 'petaoctets' }, + { from: 1e18, to: 1e21, unit: 'Eo', long: 'exaoctets' }, + { from: 1e21, to: 1e24, unit: 'Zo', long: 'zettaoctets' }, + { from: 1e24, to: 1e27, unit: 'Yo', long: 'yottaoctets' }, + ], + iec: [ + { from: 0 , to: Math.pow(1024, 1), unit: 'B' , long: 'bytes' }, + { from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'KiB', long: 'kibibytes' }, + { from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'MiB', long: 'mebibytes' }, + { from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'GiB', long: 'gibibytes' }, + { from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'TiB', long: 'tebibytes' }, + { from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'PiB', long: 'pebibytes' }, + { from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'EiB', long: 'exbibytes' }, + { from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'ZiB', long: 'zebibytes' }, + { from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'YiB', long: 'yobibytes' }, + ], + iec_octet: [ + { from: 0 , to: Math.pow(1024, 1), unit: 'o' , long: 'octets' }, + { from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'Kio', long: 'kibioctets' }, + { from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'Mio', long: 'mebioctets' }, + { from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'Gio', long: 'gibioctets' }, + { from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'Tio', long: 'tebioctets' }, + { from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'Pio', long: 'pebioctets' }, + { from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'Eio', long: 'exbioctets' }, + { from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'Zio', long: 'zebioctets' }, + { from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'Yio', long: 'yobioctets' }, + ], + }; + Object.assign(tables, options.customUnits); - const base = options.units === 'metric' || options.units === 'metric_octet' ? 1000 : 1024; const prefix = bytes < 0 ? '-' : ''; bytes = Math.abs(bytes); - - for (let i = 0; i < table.length; i++) { - const lower = Math.pow(base, table[i].expFrom); - const upper = Math.pow(base, table[i].expTo); - if (bytes >= lower && bytes < upper) { - const units = table[i][options.units]; - if (i === 0) { - this.value = prefix + bytes; - this.unit = units; - return - } else { - this.value = prefix + (bytes / lower).toFixed(options.precision); - this.unit = units; - return - } + const table = tables[options.units]; + if (table) { + const units = table.find(u => bytes >= u.from && bytes < u.to); + if (units) { + const value = units.from === 0 + ? prefix + bytes + : prefix + (bytes / units.from).toFixed(options.precision); + this.value = value; + this.unit = units.unit; + this.long = units.long; + } else { + this.value = prefix + bytes; + this.unit = ''; + this.long = ''; } + } else { + throw new Error(`Invalid units specified: ${options.units}`) } - - this.value = prefix + bytes; - this.unit = ''; } toString () { - return `${this.value} ${this.unit}`.trim() + const options = _options.get(this); + return options.toStringFn ? options.toStringFn.bind(this)() : `${this.value} ${this.unit}` } } /** - * @param {number} - the bytes value to convert. - * @param [options] {object} - optional config. - * @param [options.precision=1] {number} - number of decimal places. - * @param [options.units=metric] {string} - select `'metric'`, `'iec'`, `'metric_octet'` or `'iec_octet'` units. - * @returns {{ value: string, unit: string}} + * 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. + * @param {number} - The bytes value to convert. + * @param [options] {object} - Optional config. + * @param [options.precision] {number} - Number of decimal places. Defaults to `1`. + * @param [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`. + * @param [options.customUnits] {object} - An object containing one or more custom unit lookup tables. + * @param [options.toStringFn] {function} - A `toString` function to override the default. + * @returns {object} * @alias module:byte-size - * @example - * ```js - * > const byteSize = require('byte-size') - * - * > byteSize(1580) - * { value: '1.6', unit: 'kB' } - * - * > byteSize(1580, { units: 'iec' }) - * { value: '1.5', unit: 'KiB' } - * - * > byteSize(1580, { units: 'iec', precision: 3 }) - * { value: '1.543', unit: 'KiB' } - * - * > byteSize(1580, { units: 'iec', precision: 0 }) - * { value: '2', unit: 'KiB' } - * - * > byteSize(1580, { units: 'metric_octet' }) - * { value: '1.6', unit: 'ko' } - * - * > byteSize(1580, { units: 'iec_octet' }) - * { value: '1.5', unit: 'Kio' } - * - * > byteSize(1580, { units: 'iec_octet' }).toString() - * '1.5 Kio' - * - * > const { value, unit } = byteSize(1580, { units: 'iec_octet' }) - * > `${value} ${unit}` - * '1.5 Kio' - * ``` */ function byteSize (bytes, options) { return new ByteSize(bytes, options) } + /** + * Set the default `byteSize` options for the duration of the process. + * @param options {object} - A `byteSize` options object. + */ + byteSize.defaultOptions = function (options) { + defaultOptions = options; + }; + return byteSize; -})); +}))); diff --git a/node_modules/byte-size/index.mjs b/node_modules/byte-size/index.mjs index 2de3e205b087c..bd6548c686aa5 100644 --- a/node_modules/byte-size/index.mjs +++ b/node_modules/byte-size/index.mjs @@ -1,144 +1,115 @@ /** - * An isomorphic, load-anywhere function to convert a bytes value into a more human-readable format. Choose between [metric or IEC units](https://en.wikipedia.org/wiki/Gigabyte), summarised below. - * - * Value | Metric - * ----- | ------------- - * 1000 | kB kilobyte - * 1000^2 | MB megabyte - * 1000^3 | GB gigabyte - * 1000^4 | TB terabyte - * 1000^5 | PB petabyte - * 1000^6 | EB exabyte - * 1000^7 | ZB zettabyte - * 1000^8 | YB yottabyte - * - * Value | IEC - * ----- | ------------ - * 1024 | KiB kibibyte - * 1024^2 | MiB mebibyte - * 1024^3 | GiB gibibyte - * 1024^4 | TiB tebibyte - * 1024^5 | PiB pebibyte - * 1024^6 | EiB exbibyte - * 1024^7 | ZiB zebibyte - * 1024^8 | YiB yobibyte - * - * Value | Metric (octet) - * ----- | ------------- - * 1000 | ko kilooctet - * 1000^2 | Mo megaoctet - * 1000^3 | Go gigaoctet - * 1000^4 | To teraoctet - * 1000^5 | Po petaoctet - * 1000^6 | Eo exaoctet - * 1000^7 | Zo zettaoctet - * 1000^8 | Yo yottaoctet - * - * Value | IEC (octet) - * ----- | ------------ - * 1024 | Kio kilooctet - * 1024^2 | Mio mebioctet - * 1024^3 | Gio gibioctet - * 1024^4 | Tio tebioctet - * 1024^5 | Pio pebioctet - * 1024^6 | Eio exbioctet - * 1024^7 | Zio zebioctet - * 1024^8 | Yio yobioctet - * * @module byte-size - * @example - * ```js - * const byteSize = require('byte-size') - * ``` */ +let defaultOptions = {} +const _options = new WeakMap() + class ByteSize { constructor (bytes, options) { - options = options || {} - options.units = options.units || 'metric' - options.precision = typeof options.precision === 'undefined' ? 1 : options.precision - - const table = [ - { expFrom: 0, expTo: 1, metric: 'B', iec: 'B', metric_octet: 'o', iec_octet: 'o' }, - { expFrom: 1, expTo: 2, metric: 'kB', iec: 'KiB', metric_octet: 'ko', iec_octet: 'Kio' }, - { expFrom: 2, expTo: 3, metric: 'MB', iec: 'MiB', metric_octet: 'Mo', iec_octet: 'Mio' }, - { expFrom: 3, expTo: 4, metric: 'GB', iec: 'GiB', metric_octet: 'Go', iec_octet: 'Gio' }, - { expFrom: 4, expTo: 5, metric: 'TB', iec: 'TiB', metric_octet: 'To', iec_octet: 'Tio' }, - { expFrom: 5, expTo: 6, metric: 'PB', iec: 'PiB', metric_octet: 'Po', iec_octet: 'Pio' }, - { expFrom: 6, expTo: 7, metric: 'EB', iec: 'EiB', metric_octet: 'Eo', iec_octet: 'Eio' }, - { expFrom: 7, expTo: 8, metric: 'ZB', iec: 'ZiB', metric_octet: 'Zo', iec_octet: 'Zio' }, - { expFrom: 8, expTo: 9, metric: 'YB', iec: 'YiB', metric_octet: 'Yo', iec_octet: 'Yio' } - ] + options = Object.assign({ + units: 'metric', + precision: 1 + }, defaultOptions, options) + _options.set(this, options) - const base = options.units === 'metric' || options.units === 'metric_octet' ? 1000 : 1024 - const prefix = bytes < 0 ? '-' : ''; - bytes = Math.abs(bytes); + const tables = { + metric: [ + { from: 0 , to: 1e3 , unit: 'B' , long: 'bytes' }, + { from: 1e3 , to: 1e6 , unit: 'kB', long: 'kilobytes' }, + { from: 1e6 , to: 1e9 , unit: 'MB', long: 'megabytes' }, + { from: 1e9 , to: 1e12, unit: 'GB', long: 'gigabytes' }, + { from: 1e12, to: 1e15, unit: 'TB', long: 'terabytes' }, + { from: 1e15, to: 1e18, unit: 'PB', long: 'petabytes' }, + { from: 1e18, to: 1e21, unit: 'EB', long: 'exabytes' }, + { from: 1e21, to: 1e24, unit: 'ZB', long: 'zettabytes' }, + { from: 1e24, to: 1e27, unit: 'YB', long: 'yottabytes' }, + ], + metric_octet: [ + { from: 0 , to: 1e3 , unit: 'o' , long: 'octets' }, + { from: 1e3 , to: 1e6 , unit: 'ko', long: 'kilooctets' }, + { from: 1e6 , to: 1e9 , unit: 'Mo', long: 'megaoctets' }, + { from: 1e9 , to: 1e12, unit: 'Go', long: 'gigaoctets' }, + { from: 1e12, to: 1e15, unit: 'To', long: 'teraoctets' }, + { from: 1e15, to: 1e18, unit: 'Po', long: 'petaoctets' }, + { from: 1e18, to: 1e21, unit: 'Eo', long: 'exaoctets' }, + { from: 1e21, to: 1e24, unit: 'Zo', long: 'zettaoctets' }, + { from: 1e24, to: 1e27, unit: 'Yo', long: 'yottaoctets' }, + ], + iec: [ + { from: 0 , to: Math.pow(1024, 1), unit: 'B' , long: 'bytes' }, + { from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'KiB', long: 'kibibytes' }, + { from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'MiB', long: 'mebibytes' }, + { from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'GiB', long: 'gibibytes' }, + { from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'TiB', long: 'tebibytes' }, + { from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'PiB', long: 'pebibytes' }, + { from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'EiB', long: 'exbibytes' }, + { from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'ZiB', long: 'zebibytes' }, + { from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'YiB', long: 'yobibytes' }, + ], + iec_octet: [ + { from: 0 , to: Math.pow(1024, 1), unit: 'o' , long: 'octets' }, + { from: Math.pow(1024, 1), to: Math.pow(1024, 2), unit: 'Kio', long: 'kibioctets' }, + { from: Math.pow(1024, 2), to: Math.pow(1024, 3), unit: 'Mio', long: 'mebioctets' }, + { from: Math.pow(1024, 3), to: Math.pow(1024, 4), unit: 'Gio', long: 'gibioctets' }, + { from: Math.pow(1024, 4), to: Math.pow(1024, 5), unit: 'Tio', long: 'tebioctets' }, + { from: Math.pow(1024, 5), to: Math.pow(1024, 6), unit: 'Pio', long: 'pebioctets' }, + { from: Math.pow(1024, 6), to: Math.pow(1024, 7), unit: 'Eio', long: 'exbioctets' }, + { from: Math.pow(1024, 7), to: Math.pow(1024, 8), unit: 'Zio', long: 'zebioctets' }, + { from: Math.pow(1024, 8), to: Math.pow(1024, 9), unit: 'Yio', long: 'yobioctets' }, + ], + } + Object.assign(tables, options.customUnits) - for (let i = 0; i < table.length; i++) { - const lower = Math.pow(base, table[i].expFrom) - const upper = Math.pow(base, table[i].expTo) - if (bytes >= lower && bytes < upper) { - const units = table[i][options.units] - if (i === 0) { - this.value = prefix + bytes - this.unit = units - return - } else { - this.value = prefix + (bytes / lower).toFixed(options.precision) - this.unit = units - return - } + const prefix = bytes < 0 ? '-' : '' + bytes = Math.abs(bytes) + const table = tables[options.units] + if (table) { + const units = table.find(u => bytes >= u.from && bytes < u.to) + if (units) { + const value = units.from === 0 + ? prefix + bytes + : prefix + (bytes / units.from).toFixed(options.precision) + this.value = value + this.unit = units.unit + this.long = units.long + } else { + this.value = prefix + bytes + this.unit = '' + this.long = '' } + } else { + throw new Error(`Invalid units specified: ${options.units}`) } - - this.value = prefix + bytes - this.unit = '' } toString () { - return `${this.value} ${this.unit}`.trim() + const options = _options.get(this) + return options.toStringFn ? options.toStringFn.bind(this)() : `${this.value} ${this.unit}` } } /** - * @param {number} - the bytes value to convert. - * @param [options] {object} - optional config. - * @param [options.precision=1] {number} - number of decimal places. - * @param [options.units=metric] {string} - select `'metric'`, `'iec'`, `'metric_octet'` or `'iec_octet'` units. - * @returns {{ value: string, unit: string}} + * 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. + * @param {number} - The bytes value to convert. + * @param [options] {object} - Optional config. + * @param [options.precision] {number} - Number of decimal places. Defaults to `1`. + * @param [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`. + * @param [options.customUnits] {object} - An object containing one or more custom unit lookup tables. + * @param [options.toStringFn] {function} - A `toString` function to override the default. + * @returns {object} * @alias module:byte-size - * @example - * ```js - * > const byteSize = require('byte-size') - * - * > byteSize(1580) - * { value: '1.6', unit: 'kB' } - * - * > byteSize(1580, { units: 'iec' }) - * { value: '1.5', unit: 'KiB' } - * - * > byteSize(1580, { units: 'iec', precision: 3 }) - * { value: '1.543', unit: 'KiB' } - * - * > byteSize(1580, { units: 'iec', precision: 0 }) - * { value: '2', unit: 'KiB' } - * - * > byteSize(1580, { units: 'metric_octet' }) - * { value: '1.6', unit: 'ko' } - * - * > byteSize(1580, { units: 'iec_octet' }) - * { value: '1.5', unit: 'Kio' } - * - * > byteSize(1580, { units: 'iec_octet' }).toString() - * '1.5 Kio' - * - * > const { value, unit } = byteSize(1580, { units: 'iec_octet' }) - * > `${value} ${unit}` - * '1.5 Kio' - * ``` */ function byteSize (bytes, options) { return new ByteSize(bytes, options) } +/** + * Set the default `byteSize` options for the duration of the process. + * @param options {object} - A `byteSize` options object. + */ +byteSize.defaultOptions = function (options) { + defaultOptions = options +} + export default byteSize diff --git a/node_modules/byte-size/package.json b/node_modules/byte-size/package.json index 5c48479993096..e69b7e5f53ae2 100644 --- a/node_modules/byte-size/package.json +++ b/node_modules/byte-size/package.json @@ -8,14 +8,14 @@ "url": "http://repejota.com" } ], - "version": "5.0.1", + "version": "7.0.0", "main": "dist/index.js", "license": "MIT", "engines": { - "node": ">=6.0.0" + "node": ">=10" }, - "description": "Convert a bytes (and octets) value to a more human-readable format. Choose between metric or IEC units.", - "repository": "https://github.com/75lb/byte-size.git", + "description": "Convert a bytes or octets value (e.g. 34565346) to a human-readable string ('34.6 MB'). Choose between metric or IEC units.", + "repository": "https://github.com/75lb/byte-size", "files": [ "index.mjs", "dist/index.js" @@ -31,17 +31,25 @@ "IEC" ], "scripts": { - "test": "npm run test:js && npm run test:mjs", - "test:js": "rollup -c dist/test.config.js && node dist/test.js", - "test:mjs": "node --experimental-modules test/test.mjs", - "docs": "jsdoc2md -t README.hbs dist/index.js > README.md; echo", - "cover": "istanbul cover ./node_modules/.bin/test-runner test.js && cat coverage/lcov.info | ./node_modules/.bin/coveralls", - "dist": "rollup -c dist/index.config.js" + "test": "npm run dist && npm run test:esm && npm run test:web", + "test:esm": "esm-runner test.mjs", + "test:web": "web-runner test.mjs", + "docs": "jsdoc2md -t README.hbs dist/index.js > README.md", + "cover": "c8 npm test && c8 report --reporter=text-lcov | coveralls", + "dist": "rollup -f umd -n byteSize -o dist/index.js index.mjs" }, "devDependencies": { - "coveralls": "^3.0.2", - "jsdoc-to-markdown": "^4.0.1", - "rollup": "^0.68.1", - "test-runner": "^0.5.1" + "@test-runner/web": "^0.3.4", + "coveralls": "^3.1.0", + "esm-runner": "^0.3.4", + "isomorphic-assert": "^0.1.1", + "jsdoc-to-markdown": "^5.0.3", + "rollup": "^2.10.9", + "test-object-model": "^0.6.1" + }, + "standard": { + "ignore": [ + "dist" + ] } } diff --git a/package-lock.json b/package-lock.json index fed41b0ca7d53..661d088f49672 100644 --- a/package-lock.json +++ b/package-lock.json @@ -98,7 +98,7 @@ "aproba": "^2.0.0", "archy": "~1.0.0", "bin-links": "^2.1.3", - "byte-size": "^5.0.1", + "byte-size": "^7.0.0", "cacache": "^15.0.5", "chalk": "^4.1.0", "chownr": "^2.0.0", @@ -1008,12 +1008,12 @@ "inBundle": true }, "node_modules/byte-size": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/byte-size/-/byte-size-5.0.1.tgz", - "integrity": "sha512-/XuKeqWocKsYa/cBY1YbSJSWWqTi4cFgr9S6OyM7PBaPbr9zvNGwWP33vt0uqGhwDdN+y3yhbXVILEUpnwEWGw==", + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/byte-size/-/byte-size-7.0.0.tgz", + "integrity": "sha512-NNiBxKgxybMBtWdmvx7ZITJi4ZG+CYUgwOSZTfqB1qogkRHrhbQE/R2r5Fh94X+InN5MCYz6SvB/ejHMj/HbsQ==", "inBundle": true, "engines": { - "node": ">=6.0.0" + "node": ">=10" } }, "node_modules/cacache": { @@ -9820,9 +9820,9 @@ "integrity": "sha1-y5T662HIaWRR2zZTThQi+U8K7og=" }, "byte-size": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/byte-size/-/byte-size-5.0.1.tgz", - "integrity": "sha512-/XuKeqWocKsYa/cBY1YbSJSWWqTi4cFgr9S6OyM7PBaPbr9zvNGwWP33vt0uqGhwDdN+y3yhbXVILEUpnwEWGw==" + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/byte-size/-/byte-size-7.0.0.tgz", + "integrity": "sha512-NNiBxKgxybMBtWdmvx7ZITJi4ZG+CYUgwOSZTfqB1qogkRHrhbQE/R2r5Fh94X+InN5MCYz6SvB/ejHMj/HbsQ==" }, "cacache": { "version": "15.0.5", diff --git a/package.json b/package.json index 053a7faef73a6..5404e94d3e148 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "aproba": "^2.0.0", "archy": "~1.0.0", "bin-links": "^2.1.3", - "byte-size": "^5.0.1", + "byte-size": "^7.0.0", "cacache": "^15.0.5", "chalk": "^4.1.0", "chownr": "^2.0.0",