What edge cases (negative, NaN, Infinity) are tested?
What edge cases (negative, NaN, Infinity) are tested?
About sindresorhus/pretty-bytes · 2 messages · last activity 7/6/2026
The repository tests several important edge cases:
Negative numbers: Negative values are supported and formatted with a leading minus sign. Tests include [test.js:L56-L62]:
prettyBytes(-0.4)→'-0.4 B'prettyBytes(-999)→'-999 B'prettyBytes(-1001)→'-1 kB'
The code also handles negative numbers with the signed option and fixedWidth [test.js:L328]:
prettyBytes(-1337, {fixedWidth: 8})→'-1.34 kB'
Invalid numeric inputs (throws errors): The function validates input strictly [test.js:L3-L27]:
prettyBytes(Number.NaN)- throws TypeErrorprettyBytes(Number.POSITIVE_INFINITY)- throws TypeErrorprettyBytes(Number.NEGATIVE_INFINITY)- throws TypeError
The validation logic is in [index.js:L125-L127], which checks:
if (typeof number !== 'bigint' && !Number.isFinite(number)) {
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
}
Invalid fixedWidth values (throws errors) [test.js:L32-L40, L309-L325]:
- Negative values:
fixedWidth: -1orfixedWidth: -5 - Non-integers:
fixedWidth: 1.5orfixedWidth: 3.5 - Non-numbers:
fixedWidth: '10'(string) - Special numbers:
NaN,Infinity - Values exceeding safe integer bounds
The validation enforces that fixedWidth must be a non-negative safe integer [index.js:L99-L101].
FOLLOW_UPS:
- How does the
signedoption handle zero values? - What units does pretty-bytes support beyond bytes?
- How does binary mode differ from standard SI mode?
Want to ask your own question?
Open chat for sindresorhus/pretty-bytesPublic Q&A. Generated by RepoPilot from the actual source of sindresorhus/pretty-bytes. AI answers can be incomplete or stale — verify before relying on them.