Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Oct 27, 2019
1 parent 6af63e7 commit d9be560
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
36 changes: 35 additions & 1 deletion README.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
```

<a name="module_byte-size"></a>

## byte-size
Expand Down Expand Up @@ -119,11 +153,13 @@ Returns an object with the spec `{ value: string, unit: string, long: string }`.
<a name="module_byte-size--byteSize.defaultOptions"></a>

#### byteSize.defaultOptions([options])
Set the default `byteSize` options for the duration of the process.

**Kind**: static method of [<code>byteSize</code>](#exp_module_byte-size--byteSize)

| Param | Type | Description |
| --- | --- | --- |
| [options] | <code>object</code> | Default options. |
| [options] | <code>object</code> | A `byteSize` options object. |


## Load anywhere
Expand Down
9 changes: 6 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
*/

let defaultOptions = {};
const _options = new WeakMap();

class ByteSize {
constructor (bytes, options) {
options = Object.assign({
units: 'metric',
precision: 1
}, defaultOptions, options);
this.options = options;
_options.set(this, options);

const tables = {
metric: [
Expand Down Expand Up @@ -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}`
}
}

Expand All @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
*/

let defaultOptions = {}
const _options = new WeakMap()

class ByteSize {
constructor (bytes, options) {
options = Object.assign({
units: 'metric',
precision: 1
}, defaultOptions, options)
this.options = options
_options.set(this, options)

const tables = {
metric: [
Expand Down Expand Up @@ -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}`
}
}

Expand All @@ -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
Expand Down

0 comments on commit d9be560

Please sign in to comment.