Check whether a file size satisfies a given constraint — like semver.satisfies but for bytes.
- Node.js v18 or higher
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @nodesecure/size-satisfies
# or
$ yarn add @nodesecure/size-satisfiesimport sizeSatisfies from "@nodesecure/size-satisfies";
// String sizes are parsed using the `bytes` package (B, KB, MB, GB, …)
console.log(sizeSatisfies(">= 45KB", "20MB")); // true — 20 MB is >= 45 KB
console.log(sizeSatisfies("<= 45KB", "10B")); // true — 10 B is <= 45 KB
console.log(sizeSatisfies("= 1MB", "1MB")); // true — exact match
console.log(sizeSatisfies("> 45KB", "45KB")); // false — not strictly greater
// Numeric sizes are treated as bytes
console.log(sizeSatisfies(">= 45KB", 46080)); // true — 46 080 B == 45 KB
console.log(sizeSatisfies("= 45KB", 2000)); // false — 2 000 B != 45 KB
console.log(sizeSatisfies("< 45KB", 0)); // true — 0 B < 45 KB
// Invalid patterns always return false
console.log(sizeSatisfies("", "45KB")); // false — empty pattern
console.log(sizeSatisfies("45KB", "45KB")); // false — missing operator
console.log(sizeSatisfies("!! 45KB", "45KB")); // false — unknown operator
// Unparseable sizes fall back to 0
console.log(sizeSatisfies("> 0KB", "not_a_size")); // false — 0 > 0 is false
console.log(sizeSatisfies("= 0KB", "not_a_size")); // true — 0 == 0
console.log(sizeSatisfies(">= not_a_size", "10KB")); // true — 10 KB >= 0function sizeSatisfies(pattern: string, size: number | string): booleanReturns true when size satisfies the constraint expressed by pattern, false otherwise.
A string composed of an operator followed by a size value (with an optional space between them):
">= 45KB"
"< 1MB"
"= 512B"
| Operator | Meaning |
|---|---|
>= |
greater than or equal to |
<= |
less than or equal to |
> |
strictly greater than |
< |
strictly less than |
= |
exactly equal to |
The size value in the pattern is parsed by the bytes package and therefore supports the same units: B, KB, MB, GB, TB, PB (case-insensitive). An unparseable size value falls back to 0.
A pattern that is empty, has no operator, or uses an unrecognised operator causes the function to return false immediately.
The actual size to test against the pattern.
number— interpreted as a raw byte count.string— parsed by thebytespackage (e.g."45KB","1.5MB"). An unparseable string falls back to0.
MIT