diff --git a/README.hbs b/README.hbs index ec3a1ff..e009f5b 100644 --- a/README.hbs +++ b/README.hbs @@ -44,13 +44,24 @@ By default, `byteSize` converts the input number to a human readable string with { value: '1.6', unit: 'kB', long: 'kilobytes' } ``` -The object returned by `byteSize` defines a `toString` method therefore can be used directly in string context (you can override the default behaviour by setting [`options.toStringFn`](https://github.com/75lb/byte-size#bytesizebytes-options--object-)). +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`](https://github.com/75lb/byte-size#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 @@ -92,6 +103,29 @@ Define custom units by passing an object containing one or more additional conve '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 diff --git a/README.md b/README.md index c867237..5603955 100644 --- a/README.md +++ b/README.md @@ -44,13 +44,24 @@ By default, `byteSize` converts the input number to a human readable string with { value: '1.6', unit: 'kB', long: 'kilobytes' } ``` -The object returned by `byteSize` defines a `toString` method therefore can be used directly in string context (you can override the default behaviour by setting [`options.toStringFn`](https://github.com/75lb/byte-size#bytesizebytes-options--object-)). +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`](https://github.com/75lb/byte-size#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 @@ -92,6 +103,29 @@ Define custom units by passing an object containing one or more additional conve '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 +``` + ## byte-size @@ -119,11 +153,13 @@ Returns an object with the spec `{ value: string, unit: string, long: string }`. #### 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 | Default options. | +| [options] | object | A `byteSize` options object. | ## Load anywhere diff --git a/dist/index.js b/dist/index.js index 30cadcc..eec1d45 100644 --- a/dist/index.js +++ b/dist/index.js @@ -9,6 +9,7 @@ */ let defaultOptions = {}; + const _options = new WeakMap(); class ByteSize { constructor (bytes, options) { @@ -16,7 +17,7 @@ units: 'metric', precision: 1 }, defaultOptions, options); - this.options = options; + _options.set(this, options); const tables = { metric: [ @@ -89,7 +90,8 @@ } toString () { - return this.options.toStringFn ? this.options.toStringFn.bind(this)() : `${this.value} ${this.unit}` + const options = _options.get(this); + return options.toStringFn ? options.toStringFn.bind(this)() : `${this.value} ${this.unit}` } } @@ -109,7 +111,8 @@ } /** - * @param [options] {object} - Default 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; diff --git a/index.mjs b/index.mjs index 2aefd31..cf8bd02 100644 --- a/index.mjs +++ b/index.mjs @@ -3,6 +3,7 @@ */ let defaultOptions = {} +const _options = new WeakMap() class ByteSize { constructor (bytes, options) { @@ -10,7 +11,7 @@ class ByteSize { units: 'metric', precision: 1 }, defaultOptions, options) - this.options = options + _options.set(this, options) const tables = { metric: [ @@ -83,7 +84,8 @@ class ByteSize { } toString () { - return this.options.toStringFn ? this.options.toStringFn.bind(this)() : `${this.value} ${this.unit}` + const options = _options.get(this) + return options.toStringFn ? options.toStringFn.bind(this)() : `${this.value} ${this.unit}` } } @@ -103,7 +105,8 @@ function byteSize (bytes, options) { } /** - * @param [options] {object} - Default 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