Deviopath › Node.js

Caret and Tilde Ranges

Medium · npm

In package.json, "^1.2.3" and "~1.2.3" allow compatible updates. Write satisfies(version, range) that returns whether version (always a full X.Y.Z) is in range, using npm's rules:

- 1.2.3 or =1.2.3: exactly that version.

- ~ allows patch updates: ~1.2.3 → >=1.2.3 <1.3.0, ~1.2 → >=1.2.0 <1.3.0, ~1 → >=1.0.0 <2.0.0.

- ^ allows changes that do not modify the first non-zero part: ^1.2.3 → >=1.2.3 <2.0.0, ^0.2.3 → >=0.2.3 <0.3.0, ^0.0.3 → >=0.0.3 <0.0.4.

- ^ with missing parts: bump the first non-zero written part, or the LAST written part if they are all zero: ^1.2 → <2.0.0, ^0.0 → >=0.0.0 <0.1.0, ^0 → >=0.0.0 <1.0.0.

Compare versions part by part as numbers (1.10.0 > 1.9.0). Simplification: prerelease tags (-beta) never appear here.

Next: Node-style EventEmitter