Deviopath › Node.js

Parse a Version

Easy · npm

process.version holds the Node version as a string like "v20.11.1", and package.json versions look like "1.2.3".

Write a function parseVersion(str) that returns { major, minor, patch } as numbers.

- an optional lowercase v at the start is allowed

- return null if the rest is not exactly three whole numbers (digits only) separated by dots

- pre-release tags like "1.2.3-beta" count as invalid here

Next: Format Uptime