Javascript Helper Functions

Date: 2016-06-24
var forIn = function(obj, fn) {
    for (var p in obj) {
        if (obj.hasOwnProperty(p)) {
            if (fn(p, obj[p], obj) === false) {
                break;
            }
        }
    }
};

var forEach = function(arr, fn) {
    for (var i = 0; i < arr.length; i += 1) {
        if (fn(arr[i], i, arr) === false) {
            break;
        }
    }
};

var forTimes = function(count, fn) {
    for (var i = 0; i < count; i += 1) {
        if (fn(i) === false) {
            break;
        }
    }
};

var concat = function() {
 return [].concat.apply([], arguments);
};

var deduplicate = function(arr, equals) {
    return arr.reduce(function(acc, val) {
        if (acc.filter(function(fi) { return equals(fi, val); }).length === 0) {
            acc.push(val);
        }
        return acc;
    }, []);
};

var shuffle = function(array) {
    var tmp, current, top = array.length;
    if (top) {
        while (--top) {
            current = Math.floor(Math.random() * (top + 1));
            tmp = array[current];
            array[current] = array[top];
            array[top] = tmp;
        }
    }
    return array;
};

var any = function(arr, fn) {
    for (var i = 0; i < arr.length; i += 1) {
        if (fn(arr[i], i, arr) === true) {
            return true;
        }
    }
};


var groupBy = function(array, fn) {
    var groups = {};
    array.forEach(function(item) {
        var result = fn(item);
        var group = JSON.stringify(result.group);
        groups[group] = groups[group] || [];
        groups[group].push(result.data || result);
    });
    return Object.keys(groups).map(function(group) {
        return groups[group];
    });
};

var limit = function(v, min, max) {
    v = Number(v) || 0;
    return Math.max(min, Math.min(max, v));
};

var map = function (v, in_min, in_max, out_min, out_max) {
  v = limit(v, in_min, in_max);
  return (v - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
};

var inRange = function(val, min, max) {            
  return !!val && (!min || val >= min) && (!max || val <= max);      
};

var rangeOverlap = function(min1, max1, min2, max2) {
  return (max2 === null || min1 <= max2) && (max1 === null || max1 >= min2);
};

// strings

var format = function(str) {
    var i;
    for (i = 1; i < arguments.length; i += 1) {
        str = str.replace('{' + (i - 1) + '}', arguments[i]);
    }
    return str;
};

var lpad = function (str, pad, length) {
    while (str.length < length)
        str = pad + str;
    return str;
};


// dates

var today = function() {
  var t = new Date(); 
  t.setHours(0, 0, 0, 0);
  return t;
};
var dateAdd = function (date, ms) {
    return new Date(date.getTime() + ms);
};

var parseHourMinute = function (timeStr) {
    var hm = ('' + timeStr).replace(':', '');
    var h = 0;
    var m = parseInt(hm) || 0;
    if (hm.length > 2) {
        h = parseInt(hm.slice(0, -2)) || 0;
        m = parseInt(hm.slice(-2)) || 0
    }
    h = Math.min(h, 23);
    m = Math.min(m, 59);
    return { h: h, m: m };
};

var formatTime = function (minutes) {
    var minutesPerDay = 60 * 24;
    while (minutes < 0)
        minutes += minutesPerDay;
    while (minutes >= minutesPerDay) 
        minutes -= minutesPerDay;
    var h = Math.floor(minutes / 60);
    var m = minutes % 60;
    return lpad('' + h, '0', 2) + ':' + lpad('' + m, '0', 2);
};
    
var timeUp = function (time) {
    var hm = parseHourMinute(time());
    var hm2 = (hm.h * 60) + hm.m;
    hm2 += 30;
    time(formatTime(hm2));
};

// urls

var createQuery = function(qry) {
    var arr = [];
    exports.forIn(qry, function(k, v) {

        // Handle js dates
        if (v && v.toISOString) {
            v = v.toISOString();
        }

        arr.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
    });
    return arr.join('&');
};
3190cookie-checkJavascript Helper Functions