@transloadit/prettier-bytes
Advanced tools
| # @transloadit/prettier-bytes | ||
| ## 0.3.5 | ||
| ### Patch Changes | ||
| - 07ca1ed: Switch to turbo |
| import assert from 'node:assert' | ||
| import { describe, it } from 'node:test' | ||
| import prettierBytes from './prettierBytes' | ||
| const testData = [ | ||
| [2, '2 B'], | ||
| [9, '9 B'], | ||
| [25, '25 B'], | ||
| [235, '235 B'], | ||
| [2335, '2.3 KB'], | ||
| [23552, '23 KB'], | ||
| [235520, '230 KB'], | ||
| [2355520, '2.2 MB'], | ||
| [23555520, '22 MB'], | ||
| [235555520, '225 MB'], | ||
| [2355555520, '2.2 GB'], | ||
| [23555555520, '22 GB'], | ||
| [235556555520, '219 GB'], | ||
| [2355556655520, '2.1 TB'], | ||
| [23555566655520, '21 TB'], | ||
| [235555566665520, '214 TB'], | ||
| ] satisfies [input: number, output: string][] | ||
| describe('prettierBytes', () => { | ||
| it('should convert the specified number of bytes to a human-readable string like 236 MB', () => { | ||
| for (const [input, expected] of testData) { | ||
| assert.strictEqual(prettierBytes(input), expected) | ||
| } | ||
| }) | ||
| it('throws on non-number', () => { | ||
| assert.throws(() => { | ||
| // @ts-expect-error - testing invalid input | ||
| prettierBytes('this is a string') | ||
| }) | ||
| }) | ||
| it('throws on NaN', () => { | ||
| assert.throws(() => { | ||
| prettierBytes(Number.NaN) | ||
| }) | ||
| }) | ||
| }) |
| { | ||
| "extends": "../../tsconfig.json", | ||
| "compilerOptions": { | ||
| "outDir": "dist/", | ||
| "rootDir": "src/" | ||
| }, | ||
| "include": ["src/"] | ||
| } |
@@ -1,2 +0,2 @@ | ||
| declare const _default: (num: number) => string; | ||
| declare const _default: (input: number) => string; | ||
| export = _default; |
+10
-12
| "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 |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"prettierBytes.js","sourceRoot":"","sources":["../src/prettierBytes.ts"],"names":[],"mappings":";AAGA,iBAAS,SAAS,aAAa,CAAC,GAAW;IACzC,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACjD,MAAM,IAAI,SAAS,CAAC,0BAA0B,OAAO,GAAG,EAAE,CAAC,CAAA;IAC7D,CAAC;IAED,MAAM,GAAG,GAAG,GAAG,GAAG,CAAC,CAAA;IACnB,MAAM,KAAK,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAA;IAEnE,IAAI,GAAG,EAAE,CAAC;QACR,GAAG,GAAG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M,GAAG,GAAG,KAAK,GAAG,CAAC,CAAA;IACrB,IAAI,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;IAEzB,IAAI,GAAG,EAAE,CAAC;QACR,GAAG,GAAG,CAAC,GAAG,CAAA;IACZ,CAAC;IAED,IAAI,GAAG,KAAK,CAAC,EAAE,CAAC;QACd,OAAO,KAAK,CAAA;IACd,CAAC;IAED,MAAM,KAAK,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAA;IACnE,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IACvF,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,GAAG,IAAI,IAAI,QAAQ,CAAC,CAAA;IAC5C,MAAM,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAA;IAE5B,OAAO,GAAG,KAAK,IAAI,EAAE,IAAI,KAAK,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAA;AAC3F,CAAC,CAAA"} |
+6
-9
| { | ||
| "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" | ||
| } | ||
| } |
+10
-15
| // 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}` | ||
| } |
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
16.47%11
57.14%89
85.42%0
-100%