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

Generate [min, max] range from strings

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

Problem

I need to generate a minimum and maximum value for a range of speeds (slow, medium, fast), the user can specify any combination of the 3 values, and receive a range that encompasses all.

Given any combination of "slow", "medium", "fast", getRange() will return a [min, max] range.

The function can be called in the following ways:

// Any combination of the values
getRange(['slow', 'medium', 'fast']); // [0, 100]
getRange(['medium', 'fast']); // [36, 100]
getRange(['slow']); // [0, 35]

// Or just a single string
getRange('fast'); // [76, 100]

// No string specified
getRange(); // [0, 100]


Is there any way to simplify my code? It seems a bit clunky and there's probably a simpler way of writing this:

function getRange(speed){
    var min, max;

    if(speed && speed.length){
        var obj = {};

        if(Array.isArray(speed)){
            speed.forEach(function(s){
                obj[s] = true;
            });
        }
        else{
            obj[speed] = true;
        }

        if(obj.slow){
            min = 0;
            max = 35;
        }

        if(obj.medium){
            if(typeof min == 'undefined'){
                min = 36;
            }
            max = 75;
        }

        if(obj.fast){
            if(typeof min == 'undefined'){
                min = 76;
            }
            max = 100;
        }
    }

    if(typeof min == 'undefined'){
        min = 0;
    }

    if(typeof max == 'undefined'){
        max = 100;
    }

    return [min, max];
};

Solution

I do believe this could be done easier. You could use the || shortcut to set min and max if you could not find a speed range, and you could extract the numbers into a data structure and go from there.

Something like this:

function getRange(speed){
    //Speed range config, in array to preserve order
    var speedRanges = [
            { name: 'slow',   min: 1  , max : 35 },
            { name: 'medium', min: 36 , max : 75 },
            { name: 'fast',   min: 76 , max : 100 }
        ],
        min, max, i, range;
    //Build logic once for arrays, if we are not dealing with an array,
    //then turn speed into an array
    if (!Array.isArray(speed))
        speed = [speed];
    //Check for each speed range, apply if applicable 
    for (i = 0; i < speedRanges.length; i++) {
        range = speedRanges[i];
        if (~speed.indexOf(range.name)) {
            min = min || range.min;
            max = range.max;
        }
    }

    min = min || 1;
    max = max || 100;
    return [min, max];
}


If you absolutely need slow to be 0 to 35 then you will have to enhance min = min || range.min; since 0 counts as not set..

Code Snippets

function getRange(speed){
    //Speed range config, in array to preserve order
    var speedRanges = [
            { name: 'slow',   min: 1  , max : 35 },
            { name: 'medium', min: 36 , max : 75 },
            { name: 'fast',   min: 76 , max : 100 }
        ],
        min, max, i, range;
    //Build logic once for arrays, if we are not dealing with an array,
    //then turn speed into an array
    if (!Array.isArray(speed))
        speed = [speed];
    //Check for each speed range, apply if applicable 
    for (i = 0; i < speedRanges.length; i++) {
        range = speedRanges[i];
        if (~speed.indexOf(range.name)) {
            min = min || range.min;
            max = range.max;
        }
    }

    min = min || 1;
    max = max || 100;
    return [min, max];
}

Context

StackExchange Code Review Q#48479, answer score: 3

Revisions (0)

No revisions yet.