@transloadit/prettier-bytes
Advanced tools
Comparing version 0.3.4 to 0.3.5
@@ -1,2 +0,2 @@ | ||
declare const _default: (num: number) => string; | ||
declare const _default: (input: number) => string; | ||
export = _default; |
"use strict"; | ||
module.exports = function prettierBytes(num) { | ||
if (typeof num !== 'number' || Number.isNaN(num)) { | ||
throw new TypeError(`Expected a number, got ${typeof num}`); | ||
module.exports = function prettierBytes(input) { | ||
if (typeof input !== 'number' || Number.isNaN(input)) { | ||
throw new TypeError(`Expected a number, got ${typeof input}`); | ||
} | ||
const neg = num < 0; | ||
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
const neg = input < 0; | ||
let num = Math.abs(input); | ||
if (neg) { | ||
num = -num; | ||
} | ||
if (num < 1) { | ||
return `${(neg ? '-' : '') + num} B`; | ||
if (num === 0) { | ||
return '0 B'; | ||
} | ||
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1024)), units.length - 1); | ||
num = Number(num / 1024 ** exponent); | ||
const value = Number(num / 1024 ** exponent); | ||
const unit = units[exponent]; | ||
if (num >= 10 || num % 1 === 0) { | ||
return `${(neg ? '-' : '') + num.toFixed(0)} ${unit}`; | ||
} | ||
return `${(neg ? '-' : '') + num.toFixed(1)} ${unit}`; | ||
return `${value >= 10 || value % 1 === 0 ? Math.round(value) : value.toFixed(1)} ${unit}`; | ||
}; | ||
//# sourceMappingURL=prettierBytes.js.map |
{ | ||
"name": "@transloadit/prettier-bytes", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"repository": { | ||
@@ -12,14 +12,11 @@ "type": "git", | ||
"types": "dist/prettierBytes.d.ts", | ||
"directories": { | ||
"lib": "dist", | ||
"test": "dist" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: run tests from root\" && exit 1", | ||
"tsc": "tsc --build --clean && tsc --build" | ||
"build": "tsc --build --clean && tsc --build", | ||
"typecheck": "tsc --noEmit", | ||
"test": "tsx --test src/**/*.test.ts", | ||
"test:watch": "tsx --test --watch src/**/*.test.ts" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"gitHead": "714bbf75e54490ceb20bea49a728230668d456c1" | ||
} | ||
} |
// Adapted from https://github.com/Flet/prettier-bytes/ | ||
// Changing 1000 bytes to 1024, so we can keep uppercase KB vs kB | ||
// ISC License (c) Dan Flettre https://github.com/Flet/prettier-bytes/blob/master/LICENSE | ||
export = function prettierBytes(num: number): string { | ||
if (typeof num !== 'number' || Number.isNaN(num)) { | ||
throw new TypeError(`Expected a number, got ${typeof num}`) | ||
export = function prettierBytes(input: number): string { | ||
if (typeof input !== 'number' || Number.isNaN(input)) { | ||
throw new TypeError(`Expected a number, got ${typeof input}`) | ||
} | ||
const neg = num < 0 | ||
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] | ||
const neg = input < 0 | ||
let num = Math.abs(input) | ||
@@ -16,17 +16,12 @@ if (neg) { | ||
if (num < 1) { | ||
return `${(neg ? '-' : '') + num} B` | ||
if (num === 0) { | ||
return '0 B' | ||
} | ||
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] | ||
const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1024)), units.length - 1) | ||
num = Number(num / 1024 ** exponent) | ||
const value = Number(num / 1024 ** exponent) | ||
const unit = units[exponent] | ||
if (num >= 10 || num % 1 === 0) { | ||
// Do not show decimals when the number is two-digit, or if the number has no | ||
// decimal component. | ||
return `${(neg ? '-' : '') + num.toFixed(0)} ${unit}` | ||
} | ||
return `${(neg ? '-' : '') + num.toFixed(1)} ${unit}` | ||
return `${value >= 10 || value % 1 === 0 ? Math.round(value) : value.toFixed(1)} ${unit}` | ||
} |
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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
6294
11
89
0