Skip to content

Commit 2a53ac0

Browse files
authored
Merge pull request rstudio#1830 from rstudio/wch-compare-version
Add Shiny.compareVersion() function
2 parents 4fa2af7 + e512d3c commit 2a53ac0

File tree

6 files changed

+69
-6
lines changed

6 files changed

+69
-6
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ shiny 1.0.5.9000
99

1010
### Minor new features and improvements
1111

12-
* The version of Shiny is now accessible from Javascript, with `Shiny.version`. ([#1826](https://github.com/rstudio/shiny/pull/1826))
12+
* 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))
1313

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

inst/www/shared/shiny.js

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/www/shared/shiny.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/www/shared/shiny.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/www/shared/shiny.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

srcjs/utils.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,42 @@ function equal(...args) {
286286
return true;
287287
};
288288

289+
// Compare version strings like "1.0.1", "1.4-2". `op` must be a string like
290+
// "==" or "<".
291+
exports.compareVersion = function(a, op, b) {
292+
function versionParts(ver) {
293+
return (ver + "")
294+
.replace(/-/, ".")
295+
.replace(/(\.0)+[^\.]*$/, "")
296+
.split(".");
297+
}
298+
299+
function cmpVersion(a, b) {
300+
a = versionParts(a);
301+
b = versionParts(b);
302+
var len = Math.min(a.length, b.length);
303+
var cmp;
304+
305+
for(var i=0; i<len; i++) {
306+
cmp = parseInt(a[i], 10) - parseInt(b[i], 10);
307+
if(cmp !== 0) {
308+
return cmp;
309+
}
310+
}
311+
return a.length - b.length;
312+
}
313+
314+
var diff = cmpVersion(a, b);
315+
316+
if (op === "==") return (diff === 0);
317+
else if (op === ">=") return (diff >= 0);
318+
else if (op === ">") return (diff > 0);
319+
else if (op === "<=") return (diff <= 0);
320+
else if (op === "<") return (diff < 0);
321+
else throw `Unknown operator: ${op}`;
322+
};
323+
324+
289325
// multimethod: Creates functions — "multimethods" — that are polymorphic on one
290326
// or more of their arguments.
291327
//

0 commit comments

Comments
 (0)