Comparing version 1.0.0 to 2.0.0
{ | ||
"name": "bytes", | ||
"description": "byte size string parser / serializer", | ||
"keywords": ["bytes", "utility"], | ||
"description": "Byte size string parser / serializer.", | ||
"repository": "visionmedia/bytes.js.git", | ||
"keywords": [ | ||
"bytes", | ||
"utility" | ||
], | ||
"version": "0.2.1", | ||
"scripts": ["index.js"] | ||
"scripts": ["index.js"], | ||
"license": "MIT" | ||
} |
2.0.0 / 2015-04-12 | ||
================== | ||
* rewrite to fix a few issues, see: https://github.com/visionmedia/bytes.js/pull/20#issuecomment-92121650 | ||
1.0.0 / 2014-05-05 | ||
@@ -12,3 +17,3 @@ ================== | ||
0.2.1 / 2013-04-01 | ||
0.2.1 / 2013-04-01 | ||
================== | ||
@@ -18,3 +23,3 @@ | ||
0.2.0 / 2012-10-28 | ||
0.2.0 / 2012-10-28 | ||
================== | ||
@@ -24,5 +29,5 @@ | ||
0.1.0 / 2012-07-04 | ||
0.1.0 / 2012-07-04 | ||
================== | ||
* add bytes to string conversion [yields] | ||
* add bytes to string conversion [yields] |
81
index.js
@@ -0,41 +1,66 @@ | ||
'use strict'; | ||
var convertFunction = require(__dirname + '/lib/byte-convert.js'); | ||
var parseFunction = require(__dirname + '/lib/byte-parse.js'); | ||
/** | ||
* Parse byte `size` string. | ||
* Convert the given value in bytes into a string or parse to string to an integer in bytes. | ||
* | ||
* @param {String} size | ||
* @return {Number} | ||
* @api public | ||
* @constructor | ||
*/ | ||
module.exports = function(size) { | ||
if ('number' == typeof size) return convert(size); | ||
var parts = size.match(/^(\d+(?:\.\d+)?) *(kb|mb|gb|tb)$/) | ||
, n = parseFloat(parts[1]) | ||
, type = parts[2]; | ||
function Bytes() {} | ||
var map = { | ||
kb: 1 << 10 | ||
, mb: 1 << 20 | ||
, gb: 1 << 30 | ||
, tb: ((1 << 30) * 1024) | ||
}; | ||
/** | ||
* Convert 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 rounded. | ||
* | ||
* @param {number} value Value to convert | ||
* @param {{ | ||
* case: ?string=, | ||
* thousandsSeparator: ?string= | ||
* }} [options] See byte parser options. | ||
* | ||
* @return {string} | ||
*/ | ||
return map[type] * n; | ||
}; | ||
Bytes.prototype.convert = convertFunction; | ||
/** | ||
* convert bytes into string. | ||
* Parse the string value into an integer in bytes. If no unit is given, it is assumed the value is in bytes. | ||
* | ||
* @param {Number} b - bytes to convert | ||
* @return {String} | ||
* @api public | ||
* @param {number} value | ||
* | ||
* @returns {number|null} | ||
*/ | ||
function convert (b) { | ||
var tb = ((1 << 30) * 1024), gb = 1 << 30, mb = 1 << 20, kb = 1 << 10, abs = Math.abs(b); | ||
if (abs >= tb) return (Math.round(b / tb * 100) / 100) + 'tb'; | ||
if (abs >= gb) return (Math.round(b / gb * 100) / 100) + 'gb'; | ||
if (abs >= mb) return (Math.round(b / mb * 100) / 100) + 'mb'; | ||
if (abs >= kb) return (Math.round(b / kb * 100) / 100) + 'kb'; | ||
return b + 'b'; | ||
Bytes.prototype.parse = parseFunction; | ||
/** | ||
*Convert the given value in bytes into a string or parse to string to an integer in bytes. | ||
* | ||
* @param {string|number} value | ||
* @param {{ | ||
* case: [string], | ||
* thousandsSeparator: [string] | ||
* }} [options] bytes options. | ||
* | ||
* @returns {string|number|null} | ||
*/ | ||
function bytes(value, options) { | ||
var bytesObj = new Bytes(); | ||
if (typeof value === 'string') { | ||
return bytesObj.parse(value); | ||
} | ||
if (typeof value === 'number') { | ||
return bytesObj.convert(value, options); | ||
} | ||
return null; | ||
} | ||
module.exports = bytes; |
{ | ||
"name": "bytes", | ||
"author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)", | ||
"description": "byte size string parser / serializer", | ||
"version": "2.0.0", | ||
"description": "Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa.", | ||
"keywords": [ | ||
"byte", | ||
"bytes", | ||
"utility", | ||
"parse", | ||
"parser", | ||
"convert", | ||
"converter" | ||
], | ||
"repository": { | ||
@@ -9,9 +18,6 @@ "type": "git", | ||
}, | ||
"version": "1.0.0", | ||
"bugs": { | ||
"url": "https://github.com/visionmedia/bytes.js/issues" | ||
}, | ||
"main": "index.js", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"should": "*" | ||
}, | ||
"component": { | ||
@@ -21,3 +27,43 @@ "scripts": { | ||
} | ||
} | ||
}, | ||
"scripts": { | ||
"test": "node_modules/mocha/bin/mocha --check-leaks" | ||
}, | ||
"dependencies": { | ||
"node.extend": "*" | ||
}, | ||
"devDependencies": { | ||
"chai": "*", | ||
"mocha": "*" | ||
}, | ||
"author": { | ||
"name": "TJ Holowaychuk", | ||
"email": "tj@vision-media.ca", | ||
"url": "http://tjholowaychuk.com" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "michaelsanford", | ||
"url": "https://medium.com/@msanford" | ||
}, | ||
{ | ||
"name": "vladikoff", | ||
"email": "github@vf.io", | ||
"url": "http://vf.io" | ||
}, | ||
{ | ||
"name": "justindeguzman", | ||
"url": "http://justindeguzman.net" | ||
}, | ||
{ | ||
"name": "mixu", | ||
"url": "http://mixu.net" | ||
}, | ||
{ | ||
"name": "theofidry", | ||
"email": "theo.fidry@gmail.com", | ||
"url": "https://github.com/theofidry" | ||
} | ||
], | ||
"license": "MIT" | ||
} |
104
Readme.md
@@ -1,54 +0,84 @@ | ||
# node-bytes | ||
# Bytes utility | ||
Byte string parser / formatter. | ||
Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa. | ||
## Example: | ||
## Usage | ||
```js | ||
bytes('1kb') | ||
// => 1024 | ||
var bytes = require('bytes'); | ||
``` | ||
bytes('2mb') | ||
// => 2097152 | ||
#### bytes(number value, [options]): string|null | ||
bytes('1gb') | ||
// => 1073741824 | ||
Convert 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 | ||
rounded. | ||
bytes(1073741824) | ||
// => 1gb | ||
**Arguments** | ||
bytes(1099511627776) | ||
// => 1tb | ||
``` | ||
| Name | Type | Description | | ||
|---------|--------|--------------------| | ||
| value | `number` | Value in bytes | | ||
| options | `Object` | Conversion options | | ||
## Installation | ||
**Options** | ||
| Property | Type | Description | | ||
|-------------------|--------|-----------------------------------------------------------------------------------------| | ||
| thousandsSeparator | `string`|`null` | Example of values: `' '`, `','` and `.`... Default value to `' '`. | | ||
**Returns** | ||
| Name | Type | Description | | ||
|---------|-------------|-------------------------| | ||
| results | `string`|`null` | Return null upon error. String value otherwise. | | ||
**Example** | ||
```js | ||
bytes(1024); | ||
// output: '1kB' | ||
bytes(1000); | ||
// output: '1000B' | ||
bytes(1000, {thousandsSeparator: ' '}); | ||
// output: '1 000B' | ||
``` | ||
$ npm install bytes | ||
$ component install visionmedia/bytes.js | ||
``` | ||
## License | ||
#### Bytes(string value): number|null | ||
(The MIT License) | ||
Parse the string value into an integer in bytes. If no unit is given, it is assumed the value is in bytes. | ||
Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca> | ||
**Arguments** | ||
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: | ||
| Name | Type | Description | | ||
|---------------|--------|--------------------| | ||
| value | `string` | String to parse. | | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
**Returns** | ||
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. | ||
| Name | Type | Description | | ||
|---------|-------------|-------------------------| | ||
| results | `number`|`null` | Return null upon error. Value in bytes otherwise. | | ||
**Example** | ||
```js | ||
bytes('1kB'); | ||
// output: 1024 | ||
bytes('1024'); | ||
// output: 1024 | ||
``` | ||
## Installation | ||
```bash | ||
npm install bytes --save | ||
component install visionmedia/bytes.js | ||
``` | ||
## License | ||
[![npm](https://img.shields.io/npm/l/express.svg)](https://github.com/visionmedia/bytes.js/blob/master/LICENSE) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
16358
13
0
353
0
85
1
1
5
1
+ Addednode.extend@*
+ Addedfunction-bind@1.1.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis@3.3.0(transitive)
+ Addednode.extend@2.0.3(transitive)