├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 'stable' 4 | - '0.12' 5 | - '0.10' 6 | sudo: false 7 | cache: 8 | directories: 9 | - node_modules 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # prettier-bytes change log 2 | 3 | All notable changes to this project will be documented in this file. 4 | This project adheres to [Semantic Versioning](http://semver.org/). 5 | 6 | ## Unreleased 7 | * engage 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Contributions welcome! 4 | 5 | **Before spending lots of time on something, ask for feedback on your idea first!** 6 | 7 | Please search issues and pull requests before adding something new to avoid duplicating efforts and conversations. 8 | 9 | In addition to improving the project by refactoring code and implementing relevant features, this project welcomes the following types of contributions: 10 | 11 | - **Ideas**: participate in an issue thread or start your own to have your voice heard. 12 | - **Writing**: contribute your expertise in an area by helping expand the included content. 13 | - **Copy editing**: fix typos, clarify language, and generally improve the quality of the content. 14 | - **Formatting**: help keep content easy to read with consistent formatting. 15 | 16 | ## Installing 17 | 18 | Fork and clone the repo, then `npm install` to install all dependencies. 19 | 20 | ## Testing 21 | 22 | Tests are run with `npm test`. Unless you're creating a failing test to increase test coverage or show a problem, please make sure all tests are passing before submitting a pull request. 23 | 24 | ## Code Style 25 | 26 | [![standard][standard-image]][standard-url] 27 | 28 | This repository uses [`standard`][standard-url] to maintain code style and consistency and avoid style arguments. `npm test` runs `standard` so you don't have to! 29 | 30 | [standard-image]: https://cdn.rawgit.com/feross/standard/master/badge.svg 31 | [standard-url]: https://github.com/feross/standard 32 | [semistandard-image]: https://cdn.rawgit.com/flet/semistandard/master/badge.svg 33 | [semistandard-url]: https://github.com/Flet/semistandard 34 | 35 | --- 36 | 37 | # Collaborating Guidelines 38 | 39 | **This is an OPEN Open Source Project.** 40 | 41 | ## What? 42 | 43 | Individuals making significant and valuable contributions are given commit access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project. 44 | 45 | ## Rules 46 | 47 | There are a few basic ground rules for collaborators: 48 | 49 | 1. **No `--force` pushes** or modifying the Git history in any way. 50 | 1. **Non-master branches** ought to be used for ongoing work. 51 | 1. **External API changes and significant modifications** ought to be subject to an **internal pull request** to solicit feedback from other collaborators. 52 | 1. Internal pull requests to solicit feedback are *encouraged* for any other non-trivial contribution but left to the discretion of the contributor. 53 | 1. Contributors should attempt to adhere to the prevailing code style. 54 | 55 | ## Releases 56 | 57 | Declaring formal releases remains the prerogative of the project maintainer. 58 | 59 | ## Changes to this arrangement 60 | 61 | This is an experiment and feedback is welcome! This document may also be subject to pull requests or changes by collaborators where you believe you have something valuable to add or change. 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Dan Flettre 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 4 | 5 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # prettier-bytes 2 | 3 | [![npm][npm-image]][npm-url] 4 | [![travis][travis-image]][travis-url] 5 | [![standard][standard-image]][standard-url] 6 | 7 | [npm-image]: https://img.shields.io/npm/v/prettier-bytes.svg?style=flat-square 8 | [npm-url]: https://www.npmjs.com/package/prettier-bytes 9 | [travis-image]: https://img.shields.io/travis/Flet/prettier-bytes.svg?style=flat-square 10 | [travis-url]: https://travis-ci.org/Flet/prettier-bytes 11 | [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 12 | [standard-url]: http://npm.im/standard 13 | 14 | Augment pretty-bytes to make the output a little more readable (and a little less precise) 15 | 16 | Differences from `pretty-bytes`: 17 | - The fractional portion is rounded to one digit (ex: `2.1 MB`). 18 | - If there is more than one digit to the left of the decimal, the fractional portion is rounded off (ex: `11 KB`). 19 | - Changed `kB` to `KB`, for more prettiness. Regular users are not likely to care about the technical difference. 20 | - No dependencies. 21 | 22 | ## Install 23 | 24 | ``` 25 | npm install prettier-bytes 26 | ``` 27 | 28 | ## Usage 29 | 30 | ```js 31 | var prettierBytes = require('prettier-bytes') 32 | 33 | var pretty = prettierBytes(1337) 34 | console.log(pretty) 35 | // logs 1.3 KB 36 | ``` 37 | 38 | Examples from test output: 39 | ```bash 40 | ✔ bytes: 2 -> 2 B 41 | ✔ bytes: 9 -> 9 B 42 | ✔ bytes: 25 -> 25 B 43 | ✔ bytes: 235 -> 235 B 44 | ✔ bytes: 2335 -> 2.3 KB 45 | ✔ bytes: 23552 -> 24 KB 46 | ✔ bytes: 235520 -> 236 KB 47 | ✔ bytes: 2355520 -> 2.4 MB 48 | ✔ bytes: 23555520 -> 24 MB 49 | ✔ bytes: 235555520 -> 236 MB 50 | ✔ bytes: 2355555520 -> 2.4 GB 51 | ✔ bytes: 23555555520 -> 24 GB 52 | ✔ bytes: 235556555520 -> 236 GB 53 | ✔ bytes: 2355556655520 -> 2.4 TB 54 | ✔ bytes: 23555566655520 -> 24 TB 55 | ✔ bytes: 235555566665520 -> 236 TB 56 | ``` 57 | 58 | ## Contributing 59 | 60 | Contributions welcome! Please read the [contributing guidelines](CONTRIBUTING.md) first. 61 | 62 | ## License 63 | 64 | [ISC](LICENSE) 65 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = prettierBytes 2 | 3 | function prettierBytes (num) { 4 | if (typeof num !== 'number' || isNaN(num)) { 5 | throw new TypeError('Expected a number, got ' + typeof num) 6 | } 7 | 8 | var neg = num < 0 9 | var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] 10 | 11 | if (neg) { 12 | num = -num 13 | } 14 | 15 | if (num < 1) { 16 | return (neg ? '-' : '') + num + ' B' 17 | } 18 | 19 | var exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1) 20 | num = Number(num / Math.pow(1000, exponent)) 21 | var unit = units[exponent] 22 | 23 | if (num >= 10 || num % 1 === 0) { 24 | // Do not show decimals when the number is two-digit, or if the number has no 25 | // decimal component. 26 | return (neg ? '-' : '') + num.toFixed(0) + ' ' + unit 27 | } else { 28 | return (neg ? '-' : '') + num.toFixed(1) + ' ' + unit 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "prettier-bytes", 3 | "description": "augment pretty-bytes to make the output a little more readable (and a little less precise)", 4 | "version": "1.0.4", 5 | "author": "Dan Flettre ", 6 | "bugs": { 7 | "url": "https://github.com/Flet/prettier-bytes/issues" 8 | }, 9 | "devDependencies": { 10 | "standard": "*", 11 | "tap-spec": "^4.0.2", 12 | "tape": "^4.0.0" 13 | }, 14 | "homepage": "https://github.com/Flet/prettier-bytes", 15 | "keywords": [ 16 | "bytes", 17 | "output", 18 | "pretty", 19 | "pretty-bytes" 20 | ], 21 | "license": "ISC", 22 | "main": "index.js", 23 | "repository": { 24 | "type": "git", 25 | "url": "https://github.com/Flet/prettier-bytes.git" 26 | }, 27 | "scripts": { 28 | "test": "tape test/*.js | tap-spec && standard" 29 | }, 30 | "dependencies": {} 31 | } 32 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | 3 | var prettierBytes = require('../') 4 | 5 | var testData = [ 6 | [2, '2 B'], 7 | [9, '9 B'], 8 | [25, '25 B'], 9 | [235, '235 B'], 10 | [2335, '2.3 KB'], 11 | [23552, '24 KB'], 12 | [235520, '236 KB'], 13 | [2355520, '2.4 MB'], 14 | [23555520, '24 MB'], 15 | [235555520, '236 MB'], 16 | [2355555520, '2.4 GB'], 17 | [23555555520, '24 GB'], 18 | [235556555520, '236 GB'], 19 | [2355556655520, '2.4 TB'], 20 | [23555566655520, '24 TB'], 21 | [235555566665520, '236 TB'] 22 | ] 23 | 24 | test('pretty bytes', function (t) { 25 | testData.forEach(function (data) { 26 | t.equals(prettierBytes(data[0]), data[1], 'bytes: ' + data[0] + ' -> ' + data[1]) 27 | }) 28 | 29 | t.end() 30 | }) 31 | 32 | test('throws on non-number', function (t) { 33 | t.throws(function () { 34 | prettierBytes('this is a string') 35 | }) 36 | t.end() 37 | }) 38 | 39 | test('throws on NaN', function (t) { 40 | t.throws(function () { 41 | prettierBytes(NaN) 42 | }) 43 | t.end() 44 | }) 45 | --------------------------------------------------------------------------------