var _ = require("lodash");

var getEr = function(iDielectricConst, iImpedanceHz) {
    iDielectricConst = _.toString(iDielectricConst);
    if (iDielectricConst == '') {
        return 0;
    }
    if (!/=/.test(iDielectricConst)) {
        return _.toNumber(iDielectricConst);
    }
    var _hz = function(v) {
        if (/g/i.test(v)) {
            return  _.toNumber(v.replace(/g/i, '')) * 1000 * 1000;
        } else if (/m/i.test(v)) {v
            return _.toNumber(v.replace(/m/i, '')) * 1000;
        } else {
            return _.toNumber(v);
        }
    };
    var options = iDielectricConst.split(';');
    return _.chain(options)
        .filter(function(item){ return item.split('=').length == 2; })
        .reduce(function(prev, curr) {
            var temp = curr.replace(/^\s+|\s+$/g, '').split('=');
            var diff = Math.abs(_hz(iImpedanceHz) - _hz(temp[0]));
            if (prev.diff == -1 || diff < prev.diff) {
                prev.diff = diff;
                prev.value = _.toNumber(temp[1]);
            }
            return prev;
        }, { diff: -1 })
        .value().value;
}

var r = getEr("1G=4.15;2G=4.5", "1.49G");
print("r = ", r);
r = getEr("4.3", "2G");
print("r = ", r);