Comparing version 3.5.9 to 3.5.10
@@ -5,2 +5,7 @@ # Changelog | ||
## [3.5.10][] - 2021-08-25 | ||
- Add and optimize `bytesToSize` and `sizeToBytes` from metarhia/common | ||
- Update dependencies | ||
## [3.5.9][] - 2021-07-27 | ||
@@ -92,3 +97,4 @@ | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.9...HEAD | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.10....HEAD | ||
[3.5.10]: https://github.com/metarhia/metautil/compare/v3.5.9...v3.5.10 | ||
[3.5.9]: https://github.com/metarhia/metautil/compare/v3.5.8...v3.5.9 | ||
@@ -95,0 +101,0 @@ [3.5.8]: https://github.com/metarhia/metautil/compare/v3.5.7...v3.5.8 |
@@ -119,2 +119,32 @@ 'use strict'; | ||
const SIZE_UNITS = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
const bytesToSize = (bytes) => { | ||
if (bytes === 0) return '0'; | ||
const exp = Math.floor(Math.log(bytes) / Math.log(1000)); | ||
const size = bytes / 1000 ** exp; | ||
const short = Math.round(size, 2); | ||
const unit = exp === 0 ? '' : ' ' + SIZE_UNITS[exp - 1]; | ||
return short.toString() + unit; | ||
}; | ||
const UNIT_SIZES = { | ||
yb: 24, // yottabyte | ||
zb: 21, // zettabyte | ||
eb: 18, // exabyte | ||
pb: 15, // petabyte | ||
tb: 12, // terabyte | ||
gb: 9, // gigabyte | ||
mb: 6, // megabyte | ||
kb: 3, // kilobyte | ||
}; | ||
const sizeToBytes = (size) => { | ||
const [num, unit] = size.toLowerCase().split(' '); | ||
const exp = UNIT_SIZES[unit]; | ||
const value = parseInt(num, 10); | ||
if (!exp) return value; | ||
return value * 10 ** exp; | ||
}; | ||
const ORDINAL = ['st', 'nd', 'rd', 'th']; | ||
@@ -297,2 +327,4 @@ | ||
duration, | ||
bytesToSize, | ||
sizeToBytes, | ||
parseEvery, | ||
@@ -299,0 +331,0 @@ nextEvent, |
@@ -36,2 +36,4 @@ import { EventEmitter } from 'events'; | ||
export function duration(s: string | number): number; | ||
export function bytesToSize(bytes: number): string; | ||
export function sizeToBytes(size: string): number; | ||
@@ -38,0 +40,0 @@ type Every = { |
{ | ||
"name": "metautil", | ||
"version": "3.5.9", | ||
"version": "3.5.10", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -41,7 +41,7 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/node": "^16.3.3", | ||
"eslint": "^7.31.0", | ||
"@types/node": "^16.6.2", | ||
"eslint": "^7.32.0", | ||
"eslint-config-metarhia": "^7.0.1", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.23.4", | ||
"eslint-plugin-import": "^2.24.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
@@ -48,0 +48,0 @@ "metatests": "^0.7.2", |
@@ -32,2 +32,4 @@ # Metarhia utilities | ||
- `duration(s: string | number): number` | ||
- `bytesToSize(bytes: number): string` | ||
- `sizeToBytes(size: string): number` | ||
- `makePrivate(instance: object): object` | ||
@@ -34,0 +36,0 @@ - `protect(allowMixins: Array<string>, ...namespaces: Array<object>): void` |
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
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
23948
534
60