Bytes utility——模具行业从业者必备知识手册
-
<h2>Bytes utility</h2>
<p>[![NPM Version][npm-image]][npm-url]</p>
<p>[![NPM Downloads][downloads-image]][downloads-url]</p>
<p>[![Build Status][ci-image]][ci-url]</p>
<p>[![Test Coverage][coveralls-image]][coveralls-url]</p><p>Utility to parse a string bytes (ex: <code>1TB</code>) to bytes (<code>1099511627776</code>) and vice-versa.</p>
<h3>Installation</h3>
<p>This is a <a href="https://nodejs.org/en/">Node.js</a> module available through the</p>
<p><a href="https://www.npmjs.com/">npm registry</a>. Installation is done using the</p>
<p><a href="https://docs.npmjs.com/getting-started/installing-npm-packages-locally"><code>npm install</code> command</a>:</p><pre><code>
$ npm install bytes
</code></pre><h3>Usage</h3>
<pre><code>
var bytes = require('bytes');
</code></pre><p>#### bytes(number|string value, [options]): number|string|null</p>
<p>Default export function. Delegates to either <code>bytes.format</code> or <code>bytes.parse</code> based on the type of <code>value</code>.</p>
<p><strong>Arguments</strong></p>
<table>
<tr><td>Name</td><td>Type</td><td>Description</td></tr>
<tr><td>value</td><td>number|string</td><td>Number value to format or string value to parse</td></tr>
<tr><td>options</td><td>Object</td><td>Conversion options forformat</td></tr>
</table><p><strong>Returns</strong></p>
<table>
<tr><td>Name</td><td>Type</td><td>Description</td></tr>
<tr><td>results</td><td>string|number|null</td><td>Return null upon error. Numeric value in bytes, or string value otherwise.</td></tr>
</table><p><strong>Example</strong></p>
<pre><code>
bytes(1024);
// output: '1KB'bytes('1KB');
// output: 1024
</code></pre><p>#### bytes.format(number value, [options]): string|null</p>
<p>Format the given value in bytes into a string. If the value is negative, it is kept as such. If it is a float, it is</p>
<p> rounded.</p><p><strong>Arguments</strong></p>
<table>
<tr><td>Name</td><td>Type</td><td>Description</td></tr>
<tr><td>value</td><td>number</td><td>Value in bytes</td></tr>
<tr><td>options</td><td>Object</td><td>Conversion options</td></tr>
</table><p><strong>Options</strong></p>
<table>
<tr><td>Property</td><td>Type</td><td>Description</td></tr>
<tr><td>decimalPlaces</td><td>number|null</td><td>Maximum number of decimal places to include in output. Default value to2.</td></tr>
<tr><td>fixedDecimals</td><td>boolean|null</td><td>Whether to always display the maximum number of decimal places. Default value tofalse</td></tr>
<tr><td>thousandsSeparator</td><td>string|null</td><td>Example of values:' ',','and'.'... Default value to''.</td></tr>
<tr><td>unit</td><td>string|null</td><td>The unit in which the result will be returned (B/KB/MB/GB/TB). Default value to''(which means auto detect).</td></tr>
<tr><td>unitSeparator</td><td>string|null</td><td>Separator to use between number and unit. Default value to''.</td></tr>
</table><p><strong>Returns</strong></p>
<table>
<tr><td>Name</td><td>Type</td><td>Description</td></tr>
<tr><td>results</td><td>string|null</td><td>Return null upon error. String value otherwise.</td></tr>
</table><p><strong>Example</strong></p>
<pre><code>
bytes.format(1024);
// output: '1KB'bytes.format(1000);
// output: '1000B'bytes.format(1000, {thousandsSeparator: ' '});
// output: '1 000B'bytes.format(1024 * 1.7, {decimalPlaces: 0});
// output: '2KB'bytes.format(1024, {unitSeparator: ' '});
// output: '1 KB'
</code></pre><p>#### bytes.parse(string|number value): number|null</p>
<p>Parse the string value into an integer in bytes. If no unit is given, or <code>value</code></p>
<p>is a number, it is assumed the value is in bytes.</p><p>Supported units and abbreviations are as follows and are case-insensitive:</p>
<ul>
<li><code>b</code> for bytes</li>
<li><code>kb</code> for kilobytes</li>
</ul><hr>
<p>踩过坑的来评论区说说。</p>