0

How to pass a long version number as an integer in Javascript?

I am writing a unit test in Jasmine and I want to pass an application version number to assert against, however the tests fail when I try to pass an integer such as 4.0.2.

It seems integers may not have more than one decimal. How to work around this? Must you do some kind of conversion?

Marco V
  • 2,553
  • 8
  • 36
  • 58

1 Answers1

3

As pointed by comments, 4.0.2 is not an integer...
Anyway, following this: How to do version numbers?, version numbers are usually more that a simple integer.

What is usually done is to have a structure to hold version number:

var currentVersion = {
  major : 4,
  minor: 0,
  release: 2,
  // build : undefined 
};

This can be built from a string "4.0.2" using a split function:

(function(outputVersion, inputString) {
  "use strict";

  var splitted = myString.split('.');
  outputVersion.major = splitted.length > 0 ? splitted[0]: undefined;
  outputVersion.minor = splitted.length > 1 ? splitted[1]: undefined;
  // so on for other properties
}(window.applicationVersion, myString));

Please note that above code is just a sample, not the way it should be done, it all depends on your application code. In particular, you might want to compare versions together, which means you will need comparison functions based on lexicographic order: How to compare software version number using js? (only number)

Community
  • 1
  • 1
Regis Portalez
  • 4,675
  • 1
  • 29
  • 41