vparse
The smallest version parser, for client + server, done natively in JavaScript + TypeScript.
It is the minimum for parsing and comparing versions, when use of semver
is an overkill.
Installing
$ npm install vparse
See also the TypeScript implementation of the library.
Usage
- Node.js
var parseVersion = ; ;
- Browser
Function parseVersion
returns an object:
major: 1 minor: 2 patch: 3 build: 4 parsed: 1 2 3 4 // is always an array of 4 integers isEmpty: false // = true when 'parsed' = [0, 0, 0, 0] text: '1.2.3.4' // normalized version string // comparison function
Function compare
takes either a version string or a pre-parsed object, and returns:
0
- when the versions are the same1
- whenthis
version is greater / later-1
- whenthis
version is lesser / earlier
Features
All symbols that are not digits or dots are removed before parsing the string.
As a result, a version string like ^1.2alfa.3 0.*
becomes 1.2.30.0
.
The parser also supports the following syntax:
- Skipping numbers, i.e.
.1..4
=0.1.0.4
- An empty string is the same as
0.0.0.0
Passing in a non-string value will throw an error.