Javascript validation for latest OS version in JSON

I have such JSON data:

var jsondata = [ { data: 'MacOS Sierra 10.12.13' }, { data: 'MacOS Sierra 10.12.5' } ]; 

I need to select the latest version of MacOS in this array. What is the best way to implement it? I tried to remove all alpha-numeric values ​​and convert mac versions to integers, but may not work in some situations (e.g. 101213 will be larger than 10135).

Thank your help.

+5
source share
3 answers

You can just split the strings into an array and then compare each element. I wrote a simple function for this. Something like this should work

https://jsfiddle.net/3xmjmjsb/8/

 function compare_versions(ver1,ver2){ var version1=ver1.replace(/[^0-9.]/g, "").split('.'); var version2=ver2.replace(/[^0-9.]/g, "").split('.'); for (var i=0, l=version1.length; i<l; i++) { if(parseInt(version1[i])>parseInt(version2[i])) {return ver1;} else if(parseInt(version1[i])<parseInt(version2[i])) {return ver2;} } return false; } 

and use it like this:

  var jsondata = [ { data: 'MacOS Sierra 10.12.13' }, { data: 'MacOS Sierra 10.12.11' } ]; var newest_version=compare_versions(jsondata[0]["data"],jsondata[1]["data"]); if(newest_version){ alert(newest_version); } 
+1
source

So your problem with converting to integers is that if the number of digits does not match, and the comparison may give the wrong result. You can try adding an addition to each part of the version so that they all have the same number of digits, join the parts, and then simply compare the numbers. Not sure if this is the best way, but another option is to split by . and compare each part individually, but the advantage of the first method is that you can get an integer representation for each version that can be easily compared.

  function getIntVersion(version) { // Remove non digits and non-points version = version.replace(/[^\d.]/g, ''); var parts = version.split('.'); // Make each part same length of 4, add 0 where necessary parts = parts.map(p => p.padEnd(4, '0')); version = parts.join(''); return +version; } var version = 'MacOS Sierra 10.13.5'; var version2 = 'MacOS Sierra 10.12.13'; var intVersion = getIntVersion(version); var intVersion2 = getIntVersion(version2); console.log(intVersion + ' > ' + intVersion2, (intVersion > intVersion2)); 

Then, to get the latest version from the array, just iterate over this array and keep the maximum index and value.

Here is a more compact version:

 function getIntVersion(version) { return +version.replace(/[^\d.]/g, '').split('.').map(p => p.padEnd(4, '0')).join(''); } 
0
source

To do this, you can use the semver module:

4000+ other modules on npm use this module instead of re-implementing version parsing and with 25 million monthly downloads, it has been tested quite extensively.

In this module, what you need to do is basically single-line (divided into 3 lines for readability)

 const newest = jsondata .map(o => o.data.replace(/.* /, '')) .sort(semver.rcompare)[0]; 

Now console.log(newest); prints: 10.12.13

Or, if you want to save the whole object:

 const newest = jsondata .map(o => [o, o.data.replace(/.* /, '')]) .sort((a, b) => semver.rcompare(a[1], b[1]))[0][0]; 

Now console.log(newest); prints: { data: 'MacOS Sierra 10.12.13' }

Remember to use: const semver = require('semver');

0
source

Source: https://habr.com/ru/post/1269437/


All Articles