aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJez Ng2012-10-27 15:36:35 -0400
committerJez Ng2012-10-29 17:53:16 -0400
commitc0af54eac713be14d1e3eceeca23139455abb408 (patch)
treec6fee40a7169a6ee55721cc57594aaff71bad368
parent22796c1676decbda4f2785f1cb10667ebaf941c7 (diff)
downloadvimium-c0af54eac713be14d1e3eceeca23139455abb408.tar.bz2
Split out compareVersions into the Utils file.
-rw-r--r--background_scripts/main.coffee16
-rw-r--r--lib/utils.coffee15
-rw-r--r--tests/unit_tests/utils_test.coffee8
3 files changed, 23 insertions, 16 deletions
diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee
index 56e3af07..20eb88d4 100644
--- a/background_scripts/main.coffee
+++ b/background_scripts/main.coffee
@@ -489,20 +489,6 @@ sendRequestToAllTabs = (args) ->
for tab in window.tabs
chrome.tabs.sendRequest(tab.id, args, null))
-# Compares two version strings (e.g. "1.1" and "1.5") and returns
-# -1 if versionA is < versionB, 0 if they're equal, and 1 if versionA is > versionB.
-compareVersions = (versionA, versionB) ->
- versionA = versionA.split(".")
- versionB = versionB.split(".")
- for i in [0...(Math.max(versionA.length, versionB.length))]
- a = parseInt(versionA[i] || 0, 10)
- b = parseInt(versionB[i] || 0, 10)
- if (a < b)
- return -1
- else if (a > b)
- return 1
- 0
-
#
# Returns true if the current extension version is greater than the previously recorded version in
# localStorage, and false otherwise.
@@ -511,7 +497,7 @@ shouldShowUpgradeMessage = ->
# Avoid showing the upgrade notification when previousVersion is undefined, which is the case for new
# installs.
Settings.set("previousVersion", currentVersion) unless Settings.get("previousVersion")
- compareVersions(currentVersion, Settings.get("previousVersion")) == 1
+ Utils.compareVersions(currentVersion, Settings.get("previousVersion")) == 1
openOptionsPageInNewTab = ->
chrome.tabs.getSelected(null, (tab) ->
diff --git a/lib/utils.coffee b/lib/utils.coffee
index c92fa1ff..e89d0aa2 100644
--- a/lib/utils.coffee
+++ b/lib/utils.coffee
@@ -111,6 +111,21 @@ Utils =
# detects both literals and dynamically created strings
isString: (obj) -> typeof obj == 'string' or obj instanceof String
+
+ # Compares two version strings (e.g. "1.1" and "1.5") and returns
+ # -1 if versionA is < versionB, 0 if they're equal, and 1 if versionA is > versionB.
+ compareVersions: (versionA, versionB) ->
+ versionA = versionA.split(".")
+ versionB = versionB.split(".")
+ for i in [0...(Math.max(versionA.length, versionB.length))]
+ a = parseInt(versionA[i] || 0, 10)
+ b = parseInt(versionB[i] || 0, 10)
+ if (a < b)
+ return -1
+ else if (a > b)
+ return 1
+ 0
+
# This creates a new function out of an existing function, where the new function takes fewer arguments. This
# allows us to pass around functions instead of functions + a partial list of arguments.
Function::curry = ->
diff --git a/tests/unit_tests/utils_test.coffee b/tests/unit_tests/utils_test.coffee
index 366133ac..e1aa32c7 100644
--- a/tests/unit_tests/utils_test.coffee
+++ b/tests/unit_tests/utils_test.coffee
@@ -44,9 +44,15 @@ context "convertToUrl",
assert.equal "http://www.google.com/search?q=go%20ogle.com", Utils.convertToUrl("go ogle.com")
context "Function currying",
-
should "Curry correctly", ->
foo = (a, b) -> "#{a},#{b}"
assert.equal "1,2", foo.curry()(1,2)
assert.equal "1,2", foo.curry(1)(2)
assert.equal "1,2", foo.curry(1,2)()
+
+context "compare versions",
+ should "compare correctly", ->
+ assert.equal 0, Utils.compareVersions("1.40.1", "1.40.1")
+ assert.equal -1, Utils.compareVersions("1.40.1", "1.40.2")
+ assert.equal -1, Utils.compareVersions("1.40.1", "1.41")
+ assert.equal 1, Utils.compareVersions("1.41", "1.40")