HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Improve this function that compares jQuery versions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thiscomparesfunctionimprovejquerythatversions

Problem

Please help me improve this function that compares the jQuery version currently available (or not) with the one required.

function(need) {
    if (typeof(jQuery) != 'undefined') {
        if (!need) return true;
        var re = /(\d+)\.(\d+)\.(\d+)/,
            cur = re.exec(jQuery.fn.jquery),
            need = re.exec(need);
        return (need[1] <= cur[1] && need[2] <= cur[2] && need[3] <= cur[3]);
    } else return false;
}

Solution

I'm sorry, but that code is completely broken.

  • It doesn't work if either version contains only two numbers such as the current "1.6".



  • It uses string comparison instead of integer comparison, so that it will return true if you "need" (theoretical) version "1.4.10" but only "1.4.2" is included, because "10"



  • It doesn't stop comparing minor version numbers, if the major number is already bigger. For example it will return false if "1.4.2" is "needed", but "1.5.1" is included, because "2" > "1"



  • And finally you should keep in mind that "newer" isn't necessarily better. For example, the new 1.6 version changes how .attr() works, and scripts that rely on the old functionality of .attr()` may break.

Context

StackExchange Code Review Q#2340, answer score: 5

Revisions (0)

No revisions yet.