var jsonfile = './browscap.preprocessed.json';

exports.setJson = function (filename) {
  jsonfile = filename;
};

exports.getBrowser = function (userAgent) {
  patterns = require(jsonfile).patterns;
  browsers = require(jsonfile).browsers;

  // Test user agent against each browser regex
  for (var pattern in patterns) {
    pattern = pattern.replace('@', '');
    re = new RegExp(pattern, 'i');

    if (re.test(userAgent)) {
      key     = patterns[pattern];
      found   = false;
      matches = userAgent.match(re);

      if (matches.length === 1) {
        browsersindex = key;
        found = true;
      } else {
        matchString = '@' + matches.join('|');

        if (key[matchString]) {
          browsersindex = key[matchString];
          found = true;
        }
      }

      if (found && browsers[browsersindex]) {
        browser = [userAgent, pattern.toLowerCase().trim()];
        browserData = JSON.parse(browsers[browsersindex]);

        for (var property in browserData) {
          browser[property] = browserData[property];
        }

        while (browserData[3]) {
          browserData = JSON.parse(browsers[browserData[3]]);

          for (var property in browserData) {
            if (!browser[property]) {
              browser[property] = browserData[property];
            }
          }
        }

        if (browser[3]) {
          userAgents = require(jsonfile).userAgents;
          browser[3] = userAgents[browser[3]];
        }

        ua = {};
        properties = require(jsonfile).properties;

        for (var propertyNumber in browser) {
          propertyName = properties[propertyNumber];
          ua[propertyName] = browser[propertyNumber];
        }

        return ua;
      }
    }
  }

  return null;
};

@@ -1088,17 +1088,19 @@
 serData%5B
-3
+'3'
 %5D) %7B%0A   
@@ -1357,17 +1357,19 @@
 browser%5B
-3
+'3'
 %5D) %7B%0A   
@@ -1436,17 +1436,19 @@
 browser%5B
-3
+'3'
 %5D = user
@@ -1462,17 +1462,19 @@
 browser%5B
-3
+'3'
 %5D%5D;%0A    

fix index