模极社社区 - 制造业全产业链交流平台

    • 登录
    • 版块
    • 最新
    • 话题
    • 热门
    • 用户
    • 群组

    Bytes utility——模具行业从业者必备知识手册

    社区公告
    1
    1
    0
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • S
      system 2 最后由 编辑

      <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 for format</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 to 2.</td></tr>
      <tr><td>fixedDecimals</td><td>boolean|null</td><td>Whether to always display the maximum number of decimal places. Default value to false</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>

      1 条回复 最后回复 回复 引用 0
      • First post
        Last post
      Powered by NodeBB | Contributors