<commit_before>var fs = require('fs');
var geolib = require('geolib');
var path = require('path');
var sets = require('simplesets');
// Create a Timezone function that knows about all the world's time zones. Add a
// custom `strftime` format specifier `%+` that returns the time zone offset in
// milliseconds.
var tz = require('timezone')(require('timezone/zones'), function () {
  this["+"] = function () { return this.entry.offset + this.entry.save }
});

// The "shortcut" iterates the timezone polygons when they are read in, and
// determines the minimum/maximum longitude of each.  Because there are what
// we professionals refer to as "a shitload" of polygons, and because the
// naive method I use for determining which timezone contains a given point
// could in the worst case require calculations on the order of O(shitload),
// I take advantage of the fact that my particular dataset clusters very
// heavily by degrees longitude.
// TODO cache this with file and read from cached file.
var SHORTCUT_DEGREES_LATITUDE = 1;
var SHORTCUT_DEGREES_LONGITUDE = 1;
// Maybe you only care about one region of the earth.  Exclude "America" to
// discard timezones that start with "America/", such as "America/Los Angeles"
// and "America/Chicago", etc.
var EXCLUDE_REGIONS = [];

var timezoneNamesToPolygons = null;
var timezoneLongitudeShortcuts = null;
var timezoneLatitudeShortcuts = null;
var currentTzWorld;
var constructedShortcutFilePath = path.join(__dirname, 'shortcuts.json');

// Init
// by default using the provided tz_world.json file, alternativly a similar file
// can be passed ie. for debugging and testing
var init = function(tzWorldFile){
  if(!tzWorldFile) tzWorldFile = path.join(__dirname, 'tz_world.json');

  if(currentTzWorld !== tzWorldFile) {
    currentTzWorld = tzWorldFile;
    // reinit on change
    timezoneNamesToPolygons = null;
    timezoneLongitudeShortcuts = null;
    timezoneLatitudeShortcuts = null;
    constructShortcuts(tzWorldFile);
    return true;
  } else {
    return false
  }
};

var constructShortcuts = function (tzWorldFile) {
  // Construct once
  if ((timezoneNamesToPolygons === null) || (timezoneLongitudeShortcuts === null)) {
    // Try to read from cache first
    if (false) {
      // TODO read from cached shortcut file
    } else {
      var now = Date.now();
      var featureCollection = JSON.parse(fs.readFileSync(tzWorldFile, 'utf-8'));
      timezoneNamesToPolygons = {};<commit_msg>added async suppport with callback for init<commit_after>var fs = require('fs');
var geolib = require('geolib');
var path = require('path');
var sets = require('simplesets');
// Create a Timezone function that knows about all the world's time zones. Add a
// custom `strftime` format specifier `%+` that returns the time zone offset in
// milliseconds.
var tz = require('timezone')(require('timezone/zones'), function () {
  this["+"] = function () { return this.entry.offset + this.entry.save }
});

// The "shortcut" iterates the timezone polygons when they are read in, and
// determines the minimum/maximum longitude of each.  Because there are what
// we professionals refer to as "a shitload" of polygons, and because the
// naive method I use for determining which timezone contains a given point
// could in the worst case require calculations on the order of O(shitload),
// I take advantage of the fact that my particular dataset clusters very
// heavily by degrees longitude.
// TODO cache this with file and read from cached file.
var SHORTCUT_DEGREES_LATITUDE = 1;
var SHORTCUT_DEGREES_LONGITUDE = 1;
// Maybe you only care about one region of the earth.  Exclude "America" to
// discard timezones that start with "America/", such as "America/Los Angeles"
// and "America/Chicago", etc.
var EXCLUDE_REGIONS = [];

var timezoneNamesToPolygons = null;
var timezoneLongitudeShortcuts = null;
var timezoneLatitudeShortcuts = null;
var currentTzWorld;
var constructedShortcutFilePath = path.join(__dirname, 'shortcuts.json');

// Init
// by default using the provided tz_world.json file, alternativly a similar file
// can be passed ie. for debugging and testing
var init = function(tzWorldFile, callback){
  if(!tzWorldFile) tzWorldFile = path.join(__dirname, 'tz_world.json');

  if(currentTzWorld !== tzWorldFile) {
    currentTzWorld = tzWorldFile;
    // reinit on change
    timezoneNamesToPolygons = null;
    timezoneLongitudeShortcuts = null;
    timezoneLatitudeShortcuts = null;
    if (callback) {

      fs.readFile(tzWorldFile, 'utf-8', function(error, data) {
        if (error) {
          callback(error);
        } else {
          try {
            constructShortcuts(JSON.parse(data));
            callback(null);
          } catch(e){
            callback(e);
          }
        }
      });

    } else {

      var featureCollection = JSON.parse(fs.readFileSync(tzWorldFile, 'utf-8'));
      constructShortcuts(featureCollection);
    }
    return true;
  } else {
    return false
  }
};

var constructShortcuts = function (featureCollection) {
  // Construct once
  if ((timezoneNamesToPolygons === null) || (timezoneLongitudeShortcuts === null)) {
    // Try to read from cache first
    if (false) {
      // TODO read from cached shortcut file
    } else {
      var now = Date.now();
      timezoneNamesToPolygons = {};
{'fraction_non_alphanumeric': 0.10312382386149793, 'fraction_numerical': 0.0013172751223184042, 'mean_word_length': 3.6745822339489886, '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': '2038090', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1511, 'n_words': 543}