<commit_before>'use strict';

var request = require('request')
  , allGateways
  ;

request.get('http://tel-carrier.coolaj86.com/gateways', function (err, req, data) {
  try {
    allGateways = JSON.parse(data);
  } catch(e) {
    // ignore
    allGateways = {};
  }
});

function updateRegistry(number, map, opts) {
  opts = opts || {};

  if (!allGateways) {
    setTimeout(function () {
      updateRegistry(number, map, opts);
    }, 500);
    return;
  }

  var data = { gateways: {}, numbers: {} }
    ;

  if ((map.smsGateway && !(allGateways[map.carrier]||{}).sms) || (map.mmsGateway && !(allGateways[map.carrier]||{}).mms)) {
    data.gateways[map.carrier] = { sms: (map.smsGateway||'').replace(/.*@/, ''), mms: (map.mmsGateway||'').replace(/.*@/, '') };
  }

  data.numbers[number] = { wireless: map.wireless, carrier: map.carrier };

  if (false === opts.authoritatize) {
    delete data.gateways;
    delete data.numbers[number].carrier;
    delete data.numbers[number].wireless;
  }

  allGateways[map.carrier] = data.gateways[map.carrier];
  request.post('http://tel-carrier.coolaj86.com/analytics', { json: data }, function (/*err, req, data*/) {
    // ignore
  });
}

module.exports.create = function (opts) {
  var jar = request.jar()
    , services
    , service
    ;

  services = require('./services');
  service = services[opts.service];

  // TODO move
  function normalize(number, map/*, opts*/) {
    map.number = '+1' + /(?=\+?1)?(\d{10})$/.exec(String(map.number))[1];
    if (map.smsGateway) {
      map.smsGateway = map.number.replace(/^\+1/, '') + map.smsGateway.replace(/.*@/, '@');
    }
    if (map.mmsGateway) {
      map.mmsGateway = map.number.replace(/^\+1/, '') + map.mmsGateway.replace(/.*@/, '@');
    }
    return map;
  }
  
  return {
    lookup: function (number, fn) {
      service(request, jar, number, opts, function (err, map, opts) {
        if (map) {
          map = normalize(number, map, opts);
        }
        updateRegistry(number, map, opts);
        fn(err, map);
      });
    }
  };
};
<commit_msg>normalize between +1, 1, and area-code only formats<commit_after>'use strict';

var request = require('request')
  , allGateways
  ;

request.get('http://tel-carrier.coolaj86.com/gateways', function (err, req, data) {
  try {
    allGateways = JSON.parse(data);
  } catch(e) {
    // ignore
    allGateways = {};
  }
});

function updateRegistry(number, map, opts) {
  opts = opts || {};

  if (!allGateways) {
    setTimeout(function () {
      updateRegistry(number, map, opts);
    }, 500);
    return;
  }

  var data = { gateways: {}, numbers: {} }
    ;

  if ((map.smsGateway && !(allGateways[map.carrier]||{}).sms) || (map.mmsGateway && !(allGateways[map.carrier]||{}).mms)) {
    data.gateways[map.carrier] = { sms: (map.smsGateway||'').replace(/.*@/, ''), mms: (map.mmsGateway||'').replace(/.*@/, '') };
  }

  data.numbers[number] = { wireless: map.wireless, carrier: map.carrier };

  if (false === opts.authoritatize) {
    delete data.gateways;
    delete data.numbers[number].carrier;
    delete data.numbers[number].wireless;
  }

  allGateways[map.carrier] = data.gateways[map.carrier];
  request.post('http://tel-carrier.coolaj86.com/analytics', { json: data }, function (/*err, req, data*/) {
    // ignore
  });
}

module.exports.create = function (opts) {
  var jar = request.jar()
    , services
    , service
    ;

  services = require('./services');
  service = services[opts.service];

  // TODO move
  function normalize(number, map/*, opts*/) {
    map.number = '+1' + /(?=\+?1)?(\d{10})$/.exec(String(map.number))[1];
    if (map.smsGateway) {
      map.smsGateway = map.number.replace(/^\+1/, '') + map.smsGateway.replace(/.*@/, '@');
    }
    if (map.mmsGateway) {
      map.mmsGateway = map.number.replace(/^\+1/, '') + map.mmsGateway.replace(/.*@/, '@');
    }
    return map;
  }

  function normalizeNum(number) {
    return String(number).replace(/(?=\+?1)?(\d{10})$/, '$1');
  }
  
  return {
    lookup: function (number, fn) {
      service(request, jar, normalizeNum(number), opts, function (err, map, opts) {
        if (map && map.carrier) {
          map = normalize(number, map, opts);
          updateRegistry(number, map, opts);
        }
        fn(err, map);
      });
    }
  };
};

{'fraction_non_alphanumeric': 0.20894479385045422, 'fraction_numerical': 0.00791986955508968, 'mean_word_length': 2.9000908265213443, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1015059', 'n_tokens_mistral': 1554, 'n_tokens_neox': 1505, 'n_words': 265}