pax_global_header00006660000000000000000000000064127605407320014520gustar00rootroot0000000000000052 comment=6c64218b1c117468521e2d4c0e95e91dfd0412eb pretty-bytes-4.0.2/000077500000000000000000000000001276054073200141765ustar00rootroot00000000000000pretty-bytes-4.0.2/.editorconfig000066400000000000000000000002761276054073200166600ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 pretty-bytes-4.0.2/.gitattributes000066400000000000000000000000141276054073200170640ustar00rootroot00000000000000* text=auto pretty-bytes-4.0.2/.gitignore000066400000000000000000000000151276054073200161620ustar00rootroot00000000000000node_modules pretty-bytes-4.0.2/.travis.yml000066400000000000000000000000671276054073200163120ustar00rootroot00000000000000sudo: false language: node_js node_js: - '6' - '4' pretty-bytes-4.0.2/index.js000066400000000000000000000011051276054073200156400ustar00rootroot00000000000000'use strict'; const UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; module.exports = num => { if (!Number.isFinite(num)) { throw new TypeError(`Expected a finite number, got ${typeof num}: ${num}`); } const neg = num < 0; if (neg) { num = -num; } if (num < 1) { return (neg ? '-' : '') + num + ' B'; } const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), UNITS.length - 1); const numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3)); const unit = UNITS[exponent]; return (neg ? '-' : '') + numStr + ' ' + unit; }; pretty-bytes-4.0.2/license000066400000000000000000000021371276054073200155460ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (sindresorhus.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 in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pretty-bytes-4.0.2/package.json000066400000000000000000000012141276054073200164620ustar00rootroot00000000000000{ "name": "pretty-bytes", "version": "4.0.2", "description": "Convert bytes to a human readable string: 1337 → 1.34 kB", "license": "MIT", "repository": "sindresorhus/pretty-bytes", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "pretty", "bytes", "byte", "filesize", "size", "file", "human", "humanized", "readable", "si", "data" ], "devDependencies": { "ava": "*", "xo": "*" } } pretty-bytes-4.0.2/readme.md000066400000000000000000000014271276054073200157610ustar00rootroot00000000000000# pretty-bytes [![Build Status](https://travis-ci.org/sindresorhus/pretty-bytes.svg?branch=master)](https://travis-ci.org/sindresorhus/pretty-bytes) > Convert bytes to a human readable string: `1337` → `1.34 kB` Useful for displaying file sizes for humans. - *Note that it uses base-10 (e.g. kilobyte). [Read about the difference between kilobyte and kibibyte.](http://pacoup.com/2009/05/26/kb-kb-kib-whats-up-with-that/)* ## Install ``` $ npm install --save pretty-bytes ``` ## Usage ```js const prettyBytes = require('pretty-bytes'); prettyBytes(1337); //=> '1.34 kB' prettyBytes(100); //=> '100 B' ``` ## Related - [pretty-bytes-cli](https://github.com/sindresorhus/pretty-bytes-cli) - CLI for this module ## License MIT © [Sindre Sorhus](https://sindresorhus.com) pretty-bytes-4.0.2/test.js000066400000000000000000000013641276054073200155170ustar00rootroot00000000000000import test from 'ava'; import m from './'; test('throws on invalid input', t => { t.throws(() => m('')); t.throws(() => m('1')); t.throws(() => m(NaN)); t.throws(() => m(true)); t.throws(() => m(Infinity)); t.throws(() => m(-Infinity)); t.throws(() => m(null)); }); test('converts bytes to human readable strings', t => { t.is(m(0), '0 B'); t.is(m(0.4), '0.4 B'); t.is(m(0.7), '0.7 B'); t.is(m(10), '10 B'); t.is(m(10.1), '10.1 B'); t.is(m(999), '999 B'); t.is(m(1001), '1 kB'); t.is(m(1001), '1 kB'); t.is(m(1e16), '10 PB'); t.is(m(1e30), '1000000 YB'); }); test('supports negative number', t => { t.is(m(-0.4), '-0.4 B'); t.is(m(-0.7), '-0.7 B'); t.is(m(-10.1), '-10.1 B'); t.is(m(-999), '-999 B'); t.is(m(-1001), '-1 kB'); });