Skip to content

Commit

Permalink
Merge pull request #1830 from rstudio/wch-compare-version
Browse files Browse the repository at this point in the history
Add Shiny.compareVersion() function
  • Loading branch information
jcheng5 committed Sep 5, 2017
2 parents 4fa2af7 + e512d3c commit 2a53ac0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 6 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ shiny 1.0.5.9000

### Minor new features and improvements

* The version of Shiny is now accessible from Javascript, with `Shiny.version`. ([#1826](https://github.com/rstudio/shiny/pull/1826))
* The version of Shiny is now accessible from Javascript, with `Shiny.version`. There is also a new function for comparing version strings, `Shiny.compareVersion()`. ([#1826](https://github.com/rstudio/shiny/pull/1826), [#1830](https://github.com/rstudio/shiny/pull/1830))

* Addressed [#1784](https://github.com/rstudio/shiny/issues/1784): `runApp()` will avoid port 6697, which is considered unsafe by Chrome.

Expand Down
27 changes: 27 additions & 0 deletions inst/www/shared/shiny.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion inst/www/shared/shiny.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions inst/www/shared/shiny.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/www/shared/shiny.min.js.map

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions srcjs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,42 @@ function equal(...args) {
return true;
};

// Compare version strings like "1.0.1", "1.4-2". `op` must be a string like
// "==" or "<".
exports.compareVersion = function(a, op, b) {
function versionParts(ver) {
return (ver + "")
.replace(/-/, ".")
.replace(/(\.0)+[^\.]*$/, "")
.split(".");
}

function cmpVersion(a, b) {
a = versionParts(a);
b = versionParts(b);
var len = Math.min(a.length, b.length);
var cmp;

for(var i=0; i<len; i++) {
cmp = parseInt(a[i], 10) - parseInt(b[i], 10);
if(cmp !== 0) {
return cmp;
}
}
return a.length - b.length;
}

var diff = cmpVersion(a, b);

if (op === "==") return (diff === 0);
else if (op === ">=") return (diff >= 0);
else if (op === ">") return (diff > 0);
else if (op === "<=") return (diff <= 0);
else if (op === "<") return (diff < 0);
else throw `Unknown operator: ${op}`;
};


// multimethod: Creates functions — "multimethods" — that are polymorphic on one
// or more of their arguments.
//
Expand Down

0 comments on commit 2a53ac0

Please sign in to comment.