npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

version-js

v1.0.3

Published

Manipulate and compare semantic versions in Javascript.

Downloads

6

Readme

version-js

A lightweight version class to handle semantic version manipulation in Javascript.

License: MIT npm (scoped) CircleCI

Installation

To install run:

npm install version-js

And import by adding:

import Version from 'version-js';

Data Types

A Version object can be instantiated with either a Semantic Version string or an object with preformatted properties resembling a semantic version object.

String

let string = '3.2.1-beta.release+meta.data'

The formatted string must follow conventions outlined in the Semantic Version documentation. It may be preceeded by the character v which will be omitted when parsed.

Object

let object = {
    major: 3, 
    minor: 2, 
    patch: 1, 
    prerelease: ['beta', 'release'], 
    metadata: ['meta', 'data']
}

The basic version object must follow the above format. Pre-release and metadata items should be set in an array.

Usage

Instantiation

A Version object can be instantiated like the following:

let version = new Version('3.2.1-beta.release+meta.data');

or

let version = new Version({
    major: 3, 
    minor: 2, 
    patch: 1, 
    prerelease: ['beta', 'release'], 
    metadata: ['meta', 'data']
});

Modification

As of now, a Version object's version levels can be modified like the following:

Add version level

version.addMajor();
// version.major = 3+1 = 4

Subtract version level

version.subMajor();
// version.major = 3-1 = 2

Set version level

version.setMajor(10);
// version.major = 10

The above mathematical operations accept an optional value parameter which allows for nonstandard version changing like the following:

version.addMajor(5);
// version.major = 2+5 = 7

The following methods are available for Version modification:

addMajor(value = 1); // Add to Major version
subMajor(value = 1); // Subtract from Major version
setMajor(); // Set Major version
addMinor(value = 1); // Add to Minor version
subMinor(value = 1); // Subtract from Minor version
setMinor(); // Set Minor version
addPatch(value = 1); // Add to Patch version
subPatch(value = 1); // Subtract from Patch version
setPatch(); // Set Patch version

All modifiers accept either a string or integer representation of a positive integer.

Comparison

A Version object may be compared to another.

Assuming a second version2 object:

let version2 = new Version({
    major: 5, 
    minor: 4, 
    patch: 3, 
    prerelease: ['beta', 'release'], 
    metadata: ['meta', 'data']
});

We can determine if version is greater than version2:

version.isGreaterThan(version2);    //	False (3.2.1 << 5.4.3)

Or less than:

version.isLessThan(version2);   //	True (3.2.1 << 5.4.3)

Or equal to:

version.isEqualTo(version2, precise = false);   //	False (3.2.1 != 5.4.3)

Or less than or equal to:

version.isEqualOrLessThan(version2, precise = false);   //  True (3.2.1 << 5.4.3)

Or greater than or equal to:

version.isEqualOrGreaterThan(version2, precise = false);    //  False (3.2.1 << 5.4.3)

Note: isEqualTo(), isEqualOrLessThan() and isEqualOrGreaterThan() accept an optional second parameter precise which tells it to do a precise match when compared for equality. If precise = false (default) then the version will only be compared by Major, Minor and Patch values. If precise = true, the pre-release and metadata will also be evaluated (This would be considered a perfect match).

License

The version-js package is open-source software under the MIT License.