[Pkg-javascript-commits] [node-pretty-bytes] 01/07: Import Upstream version 4.0.2
Ross Gammon
ross-guest at moszumanska.debian.org
Tue Jan 31 17:55:26 UTC 2017
This is an automated email from the git hooks/post-receive script.
ross-guest pushed a commit to branch master
in repository node-pretty-bytes.
commit 2410a384293e87314dcf299066541852b3336a74
Author: Abhishek Kuvalekar <abhi.kuvalekar at yahoo.com>
Date: Sun Jan 8 05:36:46 2017 +0000
Import Upstream version 4.0.2
---
.editorconfig | 12 ++++++++++++
.gitattributes | 1 +
.gitignore | 1 +
.travis.yml | 5 +++++
index.js | 24 ++++++++++++++++++++++++
license | 21 +++++++++++++++++++++
package.json | 38 ++++++++++++++++++++++++++++++++++++++
readme.md | 40 ++++++++++++++++++++++++++++++++++++++++
test.js | 33 +++++++++++++++++++++++++++++++++
9 files changed, 175 insertions(+)
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..98a761d
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,12 @@
+root = 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
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..176a458
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+* text=auto
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..b18bae5
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+sudo: false
+language: node_js
+node_js:
+ - '6'
+ - '4'
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..cdcec7e
--- /dev/null
+++ b/index.js
@@ -0,0 +1,24 @@
+'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;
+};
diff --git a/license b/license
new file mode 100644
index 0000000..654d0bf
--- /dev/null
+++ b/license
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) Sindre Sorhus <sindresorhus at gmail.com> (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.
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..e6b450e
--- /dev/null
+++ b/package.json
@@ -0,0 +1,38 @@
+{
+ "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 at 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": "*"
+ }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..9bcf73a
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,40 @@
+# 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)
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..c3d7f03
--- /dev/null
+++ b/test.js
@@ -0,0 +1,33 @@
+import 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');
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-pretty-bytes.git
More information about the Pkg-javascript-commits
mailing list