version.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /** https://github.com/omichelsen/compare-versions/ The MIT License (MIT) Copyright (c) 2015-2019 Ole Michelsen*/
  2. !function(e,r){"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?module.exports=r():e.compareSemverVersions=r()}(this,function(){var e=/^v?(?:\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+))?(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;function r(e){var r,t,n=e.replace(/^v/,"").replace(/\+.*$/,""),i=(t="-",-1===(r=n).indexOf(t)?r.length:r.indexOf(t)),o=n.substring(0,i).split(".");return o.push(n.substring(i+1)),o}function t(e){return isNaN(Number(e))?e:Number(e)}function n(r){if("string"!=typeof r)throw new TypeError("Invalid argument expected string");if(!e.test(r))throw new Error("Invalid argument not valid semver ('"+r+"' received)")}function i(e,i){[e,i].forEach(n);for(var o=r(e),f=r(i),a=0;a<Math.max(o.length-1,f.length-1);a++){var p=parseInt(o[a]||0,10),u=parseInt(f[a]||0,10);if(p>u)return 1;if(u>p)return-1}var d=o[o.length-1],s=f[f.length-1];if(d&&s){var c=d.split(".").map(t),l=s.split(".").map(t);for(a=0;a<Math.max(c.length,l.length);a++){if(void 0===c[a]||"string"==typeof l[a]&&"number"==typeof c[a])return-1;if(void 0===l[a]||"string"==typeof c[a]&&"number"==typeof l[a])return 1;if(c[a]>l[a])return 1;if(l[a]>c[a])return-1}}else if(d||s)return d?-1:1;return 0}var o=[">",">=","=","<","<="],f={">":[1],">=":[0,1],"=":[0],"<=":[-1,0],"<":[-1]};return i.compare=function(e,r,t){!function(e){if("string"!=typeof e)throw new TypeError("Invalid operator type, expected string but got "+typeof e);if(-1===o.indexOf(e))throw new TypeError("Invalid operator, expected one of "+o.join("|"))}(t);var n=i(e,r);return f[t].indexOf(n)>-1},i});
  3. /**
  4. * Format version string
  5. * @param {string} versionString
  6. * @return {string}
  7. */
  8. function formatVersion(versionString)
  9. {
  10. versionString += '';
  11. if (versionString === 'XXBVERSION') {
  12. return '99.9.9';
  13. }
  14. return versionString.replace(/^([0-9]+)((?:\.[0-9]+)?)((?:\.[0-9]+)?)(?:[\.\s-+]?)((?:[A-Za-z]+)?)((?:\.?[0-9]+)?)/gi, function(_, major, minor, patch, preRelease, build)
  15. {
  16. const versionStrs = [major, minor || '.0', patch || '.0'];
  17. if(preRelease || build) versionStrs.push('-');
  18. if(preRelease) versionStrs.push(preRelease);
  19. if(build)
  20. {
  21. if(!preRelease) versionStrs.push('build');
  22. if(build[0] !== '.') versionStrs.push('.');
  23. versionStrs.push(build);
  24. }
  25. return versionStrs.join('');
  26. });
  27. }
  28. /**
  29. * Simplify version string
  30. * @param {string} versionString
  31. * @return {string}
  32. */
  33. function simplifyVersion(versionString)
  34. {
  35. versionString += '';
  36. return versionString.replace(/^([0-9]+)((?:\.[0-9]+)?)((?:\.[0-9]+)?)(?:[\.\s-+]?)((?:[A-Za-z]+)?)((?:\.?[0-9]+)?)/gi, function(_, major, minor, patch, preRelease, build)
  37. {
  38. const versionStrs = [major, minor || '.0'];
  39. if(patch && patch !== '.0' && patch !== '0') versionStrs.push(patch);
  40. if(preRelease || build) versionStrs.push('.');
  41. if(preRelease) versionStrs.push(preRelease);
  42. if(build)
  43. {
  44. if(!preRelease) versionStrs.push('build');
  45. versionStrs.push(build[0] === '.' ? build.substr(1) : build);
  46. }
  47. return versionStrs.join('');
  48. });
  49. }
  50. /**
  51. * Compare two versions
  52. * @param {string} version1
  53. * @param {string} version2
  54. * @return {number}
  55. */
  56. function compareVersions(version1, version2)
  57. {
  58. return window.compareSemverVersions(formatVersion(version1), formatVersion(version2))
  59. }
  60. window.compareVersions = compareVersions;