vparse

1.1.0 • Public • Published

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.

Build Status Coverage Status

Installing

$ npm install vparse

See also the TypeScript implementation of the library.

Usage

  • Node.js
var parseVersion = require('vparse');
 
parseVersion('1.2.3.4');
  • Browser
<script src="./vparse"></script>
 
<script>
    parseVersion('1.2.3.4');
</script> 

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
    compare: function(v) // comparison function

Function compare takes either a version string or a pre-parsed object, and returns:

  • 0 - when the versions are the same
  • 1 - when this version is greater / later
  • -1 - when this 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.

Package Sidebar

Install

npm i vparse

Weekly Downloads

107

Version

1.1.0

License

MIT

Last publish

Collaborators

  • vitaly.tomilov