<commit_before>/*
 * Node Pact Publisher
 * https://github.com/alexcu-/node-pact-publisher
 *
 * Copyright (c) 2015 Alex Cummaudo.
 * Licensed under the MIT license.
 */
var request = require('request');
var fs      = require('fs');
var path    = require('path');
var log     = require('color-log');
var Q       = require('q');
var _       = require('underscore');

/**
 * A pact publisher
 * @param  {Object|String}  configOrVersion Config object or version number.
 *                                          If config object is provided, it's
 *                                          just an object containing:
 *                                          - {String} appVersion
 *                                          - {String} brokerBaseUrl
 *                                          - {Array|String} pact
 *                                          - {Boolean} logging (if set to
 *                                            false, no logs will be output)
 * @param  {String}         brokerBaseUrl   Base url of the remote pact broker.
 * @param  {Array|String}   pacts           Where pact contracts are located.
 *                                          Optional parameter. If array is
 *                                          provided, then it's expected that
 *                                          this will contain absolute paths
 *                                          of every pact JSON file. If string
 *                                          then it's expected that this is a
 *                                          directory containing pact JSON
 *                                          contracts and will publish every
 *                                          JSON file in that directory.
 */
function PactPublisher (configOrVersion, brokerBaseUrl, pacts) {
  var _version, _brokerBaseUrl, _pacts;
  if (!_.contains(['object', 'string'], typeof configOrVersion)) {
    throw new TypeError('Invalid first parameter provided constructing Pact Publisher. Expected a config object or version string for first parameter.');
  }
  _version        = configOrVersion.appVersion || configOrVersion;
  _brokerBaseUrl  = configOrVersion.brokerBaseUrl || brokerBaseUrl;
  _pacts          = configOrVersion.pacts || pacts;
  _logging        = _.isBoolean(configOrVersion.logging) ? configOrVersion.logging : true;
  if (!_.isString(_version)) {
    throw new Error("Expected a string for version number parameter.");
  }
  if (!_.isString(_brokerBaseUrl)) {
    throw new Error("Expected a string for broker base URL parameter.");
  }
  if (_pacts !== undefined && !(_.isString(_pacts) || _.isArray(_pacts))) {
    throw new Error("Expected a string or array for pacts parameter.");
  }
  this._appVersion          = _version;
  this._pactBrokerBaseUrl   = _brokerBaseUrl;
  this._pactUrl             = "{pactBaseUrl}/pacts/provider/{provider}/consumer/{consumer}/version/{version}".replace("{pactBaseUrl}", this._pactBrokerBaseUrl).replace('{version}', this._appVersion);
  this._logging             = _logging;
  if (_.isString(_pacts)) {
    if (!fs.existsSync(_pacts)) {
      throw new Error("Pact directory " + _pacts + " does not exist");
    }
    this._enqueuedPactFiles = fs.readdirSync(_pacts).filter(function (file) {
      return path.extname(file) === '.json';
    }).map(function (file) {
      return _pacts + '/' + file;
    });
  } else if (_.isArray(_pacts)) {
    this._enqueuedPactFiles = _pacts;
  } else {
    this._enqueuedPactFiles = [];
  }
};

/**
 * Logs a message if logging was enabled
 * @param  {String} msg  Message to log
 * @param  {String} type Optional type of message. Defaults to info.
 */
PactPublisher.prototype._log = function (msg, type) {
  if (this._logging) {
    log[type || 'info'](msg);
  }
};

/**
 * Gets all pacticipants registered in the pact broker
 * @return {Promise}  Promise to the pacts
 */
PactPublisher.prototype.getPacticipantNames = function () {
  var self = this;
  return Q.Promise(function (resolve, reject) {
    request({
      uri: self._pactBrokerBaseUrl + "/pacticipants",
      method: 'GET'
    }, function (err, res, body) {
      body = JSON.parse(body);
      if (err || res.statusCode !== 200 || body.pacticipants === undefined) {
        reject(err || body);
      } else {
        resolve(_.pluck(body.pacticipants, 'name'));
      }
    });
  });
};

/**
 * Checks if a pacticipant with the given name has been registered
 * @param  {String} name Pact name to check
 * @return {Promise}     Promise to if the pact is registered
 */
PactPublisher.prototype.isPacticipantRegistered = function (name) {
  var self = this;
  return Q.Promise(function (resolve, reject) {
    self.getPacticipantNames().then(function (names) {
      resolve(_.contains(names, name))
    }, function (err) {
      reject(err);
    });
  });
};

/**
 * Registers a pacticipant with the broker
 * @param  {String} name Pacticipant name to check
 * @return {Promise}     Promise to if the pact was registered
 */
PactPublisher.prototype.registerPacticipant = function (name) {
  var self = this;
  return Q.Promise(function (resolve, reject) {
    request({
      uri: self._pactBrokerBaseUrl + "/pacticipants",
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        name: name
      })
    }, function (err, res, body) {
      body = JSON.parse(body);
      if (err || res.statusCode !== 201 || body.name === undefined) {
        reject(err || body);
      } else {
        resolve(body);
      }
    });
  });
};

/**
 * Loads a pact from file
 * @param  {String}   pactFileName JSON contract
 * @return {Promise}               The pact loaded, or exception message
 */
PactPublisher.prototype._loadPactFile = function (pactFileName) {
  return Q.Promise(function (resolve, reject) {
    try {
      var pact = require(pactFileName);
      pact.pactFileName = pactFileName;
      resolve(pact);
      self._log('Loaded pact contract \'' + pactFileName + '\'');
    } catch (e) {
      reject(e.message);
    }
  });
};

/**
 * Publishes a pact
 * @param  {Object}   pact  The pact to publish
 * @return {Promise}        Promise to whether or not the pact was published
 *                          If rejected, then the reason is given
 */
PactPublisher.prototype._publish = function (pact) {
  var d = Q.defer();
  var self = this;

  this._log('Publishing \'' + pact.pactFileName + '\'...');

  var uri = this._pactUrl.replace("{provider}", pact.provider.name)
                         .replace("{consumer}", pact.consumer.name);
  request({
    uri: uri,
    method: 'PUT',
    json: true,
    headers: {
      "Content-Type": "application/json; charset=utf-8"
    },
    body: pact
  }, function (err, res, body) {
    if (err || !_.contains([200, 201], res.statusCode)) {
      var msg = 'Error uploading pact file ' + pact.pactFileName + ' to broker';
      if (!_.contains([200, 201], res.statusCode)) {
        msg += '\n\tStatus Code: ' + res.statusCode + ' - ' + this.httpModule.STATUS_CODES[res.statusCode];
      }
      if (err) {
        msg += '\n\tError: ' + JSON.stringify(err);
      }
      if (body) {
        msg += '\n\tContent: ' + JSON.stringify(body);
      }
      self._log('Failed to publish \'' + pact.pactFileName + '\': ' + err, 'error');
      d.reject(msg);
    } else {
      self._log('\'' + pact.pactFileName + '\' published to broker!', 'mark');
      d.resolve();
    }
  });

  return d.promise;
};

/**
 * Publishes a pact. If called without any paramaters, will used enqueued
 * parameters
 * @param  {String}   pactFileName  The pact file to publish. Optional
 *                                  parameter.
 * @return {Promise}                Promise to the number of pacts published
 */
PactPublisher.prototype.publish = function (pactFileName) {
  var self = this;
  var d = Q.defer();

  if (_.isString(pactFileName)) {
    this._enqueuedPactFiles.push(pactFileName);
  }
  if (this._enqueuedPactFiles.length === 0) {
    this._log('No pacts to publish', 'warn');
    d.resolve(0);
  }

  // Load in each of our pacts
  var fileLoadPromises = this._enqueuedPactFiles.map(function (pactFileName) {
    return self._loadPactFile(pactFileName);
  });
  Q.all(fileLoadPromises).then(function (pacts) {
    // Reset enqueued files
    self._enqueuedPactFiles = [];
    // Queue up a whole bunch of promises to be published
    var publishPromises = pacts.map(function (pact) {
      return self._publish(pact);
    });
    Q.all(publishPromises).then(function (responses) {
      self._log('Successfully published ' + responses.length + ' pact(s)!', 'mark');
      d.resolve(responses.length);
    }, function (responses) {
      self._log('Failed to publish all pact contracts. Only ' + responses.length + ' pact(s) were published.', 'error');
      d.reject(responses.length);
    });
  }, function (err) {
    self._log('Failed to load pact contract' + err, 'error');
  });

  return d.promise;
};

module.exports = PactPublisher;<commit_msg>ENHANCE: Improve invalid JSON response handling with reject errors<commit_after>/*
 * Node Pact Publisher
 * https://github.com/alexcu-/node-pact-publisher
 *
 * Copyright (c) 2015 Alex Cummaudo.
 * Licensed under the MIT license.
 */
var request = require('request');
var fs      = require('fs');
var path    = require('path');
var log     = require('color-log');
var Q       = require('q');
var _       = require('underscore');

/**
 * A pact publisher
 * @param  {Object|String}  configOrVersion Config object or version number.
 *                                          If config object is provided, it's
 *                                          just an object containing:
 *                                          - {String} appVersion
 *                                          - {String} brokerBaseUrl
 *                                          - {Array|String} pact
 *                                          - {Boolean} logging (if set to
 *                                            false, no logs will be output)
 * @param  {String}         brokerBaseUrl   Base url of the remote pact broker.
 * @param  {Array|String}   pacts           Where pact contracts are located.
 *                                          Optional parameter. If array is
 *                                          provided, then it's expected that
 *                                          this will contain absolute paths
 *                                          of every pact JSON file. If string
 *                                          then it's expected that this is a
 *                                          directory containing pact JSON
 *                                          contracts and will publish every
 *                                          JSON file in that directory.
 */
function PactPublisher (configOrVersion, brokerBaseUrl, pacts) {
  var _version, _brokerBaseUrl, _pacts;
  if (!_.contains(['object', 'string'], typeof configOrVersion)) {
    throw new TypeError('Invalid first parameter provided constructing Pact Publisher. Expected a config object or version string for first parameter.');
  }
  _version        = configOrVersion.appVersion || configOrVersion;
  _brokerBaseUrl  = configOrVersion.brokerBaseUrl || brokerBaseUrl;
  _pacts          = configOrVersion.pacts || pacts;
  _logging        = _.isBoolean(configOrVersion.logging) ? configOrVersion.logging : true;
  if (!_.isString(_version)) {
    throw new Error("Expected a string for version number parameter.");
  }
  if (!_.isString(_brokerBaseUrl)) {
    throw new Error("Expected a string for broker base URL parameter.");
  }
  if (_pacts !== undefined && !(_.isString(_pacts) || _.isArray(_pacts))) {
    throw new Error("Expected a string or array for pacts parameter.");
  }
  this._appVersion          = _version;
  this._pactBrokerBaseUrl   = _brokerBaseUrl;
  this._pactUrl             = "{pactBaseUrl}/pacts/provider/{provider}/consumer/{consumer}/version/{version}".replace("{pactBaseUrl}", this._pactBrokerBaseUrl).replace('{version}', this._appVersion);
  this._logging             = _logging;
  if (_.isString(_pacts)) {
    if (!fs.existsSync(_pacts)) {
      throw new Error("Pact directory " + _pacts + " does not exist");
    }
    this._enqueuedPactFiles = fs.readdirSync(_pacts).filter(function (file) {
      return path.extname(file) === '.json';
    }).map(function (file) {
      return _pacts + '/' + file;
    });
  } else if (_.isArray(_pacts)) {
    this._enqueuedPactFiles = _pacts;
  } else {
    this._enqueuedPactFiles = [];
  }
};

/**
 * Logs a message if logging was enabled
 * @param  {String} msg  Message to log
 * @param  {String} type Optional type of message. Defaults to info.
 */
PactPublisher.prototype._log = function (msg, type) {
  if (this._logging) {
    log[type || 'info'](msg);
  }
};

/**
 * Gets all pacticipants registered in the pact broker
 * @return {Promise}  Promise to the pacts
 */
PactPublisher.prototype.getPacticipantNames = function () {
  var self = this;
  return Q.Promise(function (resolve, reject) {
    request({
      uri: self._pactBrokerBaseUrl + "/pacticipants",
      method: 'GET'
    }, function (err, res, body) {
      try {
        body = JSON.parse(body);
      } catch (e) {
        return reject("Error parsing expected JSON pacticipant response: " + e.message);
      }
      if (err || res.statusCode !== 200 || body.pacticipants === undefined) {
        reject(err || body);
      } else {
        resolve(_.pluck(body.pacticipants, 'name'));
      }
    });
  });
};

/**
 * Checks if a pacticipant with the given name has been registered
 * @param  {String} name Pact name to check
 * @return {Promise}     Promise to if the pact is registered
 */
PactPublisher.prototype.isPacticipantRegistered = function (name) {
  var self = this;
  return Q.Promise(function (resolve, reject) {
    self.getPacticipantNames().then(function (names) {
      resolve(_.contains(names, name));
    }, function (err) {
      reject(err);
    });
  });
};

/**
 * Registers a pacticipant with the broker
 * @param  {String} name Pacticipant name to check
 * @return {Promise}     Promise to if the pact was registered
 */
PactPublisher.prototype.registerPacticipant = function (name) {
  var self = this;
  return Q.Promise(function (resolve, reject) {
    request({
      uri: self._pactBrokerBaseUrl + "/pacticipants",
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        name: name
      })
    }, function (err, res, body) {
      try {
        body = JSON.parse(body);
      } catch (e) {
        return reject("Error parsing expected JSON pacticipant response: " + e.message);
      }
      if (err || res.statusCode !== 201 || body.name === undefined) {
        reject(err || body);
      } else {
        resolve(body);
      }
    });
  });
};

/**
 * Loads a pact from file
 * @param  {String}   pactFileName JSON contract
 * @return {Promise}               The pact loaded, or exception message
 */
PactPublisher.prototype._loadPactFile = function (pactFileName) {
  return Q.Promise(function (resolve, reject) {
    try {
      var pact = require(pactFileName);
      pact.pactFileName = pactFileName;
      resolve(pact);
      self._log('Loaded pact contract \'' + pactFileName + '\'');
    } catch (e) {
      reject(e.message);
    }
  });
};

/**
 * Publishes a pact
 * @param  {Object}   pact  The pact to publish
 * @return {Promise}        Promise to whether or not the pact was published
 *                          If rejected, then the reason is given
 */
PactPublisher.prototype._publish = function (pact) {
  var d = Q.defer();
  var self = this;

  this._log('Publishing \'' + pact.pactFileName + '\'...');

  var uri = this._pactUrl.replace("{provider}", pact.provider.name)
                         .replace("{consumer}", pact.consumer.name);
  request({
    uri: uri,
    method: 'PUT',
    json: true,
    headers: {
      "Content-Type": "application/json; charset=utf-8"
    },
    body: pact
  }, function (err, res, body) {
    if (err || !_.contains([200, 201], res.statusCode)) {
      var msg = 'Error uploading pact file ' + pact.pactFileName + ' to broker';
      if (!_.contains([200, 201], res.statusCode)) {
        msg += '\n\tStatus Code: ' + res.statusCode + ' - ' + this.httpModule.STATUS_CODES[res.statusCode];
      }
      if (err) {
        msg += '\n\tError: ' + JSON.stringify(err);
      }
      if (body) {
        msg += '\n\tContent: ' + JSON.stringify(body);
      }
      self._log('Failed to publish \'' + pact.pactFileName + '\': ' + err, 'error');
      d.reject(msg);
    } else {
      self._log('\'' + pact.pactFileName + '\' published to broker!', 'mark');
      d.resolve();
    }
  });

  return d.promise;
};

/**
 * Publishes a pact. If called without any paramaters, will used enqueued
 * parameters
 * @param  {String}   pactFileName  The pact file to publish. Optional
 *                                  parameter.
 * @return {Promise}                Promise to the number of pacts published
 */
PactPublisher.prototype.publish = function (pactFileName) {
  var self = this;
  var d = Q.defer();

  if (_.isString(pactFileName)) {
    this._enqueuedPactFiles.push(pactFileName);
  }
  if (this._enqueuedPactFiles.length === 0) {
    this._log('No pacts to publish', 'warn');
    d.resolve(0);
  }

  // Load in each of our pacts
  var fileLoadPromises = this._enqueuedPactFiles.map(function (pactFileName) {
    return self._loadPactFile(pactFileName);
  });
  Q.all(fileLoadPromises).then(function (pacts) {
    // Reset enqueued files
    self._enqueuedPactFiles = [];
    // Queue up a whole bunch of promises to be published
    var publishPromises = pacts.map(function (pact) {
      return self._publish(pact);
    });
    Q.all(publishPromises).then(function (responses) {
      self._log('Successfully published ' + responses.length + ' pact(s)!', 'mark');
      d.resolve(responses.length);
    }, function (responses) {
      self._log('Failed to publish all pact contracts. Only ' + responses.length + ' pact(s) were published.', 'error');
      d.reject(responses.length);
    });
  }, function (err) {
    self._log('Failed to load pact contract' + err, 'error');
  });

  return d.promise;
};

module.exports = PactPublisher;<|endoftext|>


<commit_before>/* jshint node: true */
'use strict';

var TranslateHelperName = require('./lib/plugins/translate-helper-name');

module.exports = {
  name: 'ember-page-title',

  setupPreprocessorRegistry: function (type, registry) {
    this._super.setupPreprocessorRegistry.apply(this, arguments);

    app.registry.add('htmlbars-ast-plugin', {
      name: 'translate-title-helper-to-page-title-helper',
      plugin: TranslateHelperName,
      baseDir: function() {
        return __dirname;
      }
    });
  }
};
<commit_msg>remove _super method since it doesn't have one<commit_after>/* jshint node: true */
'use strict';

var TranslateHelperName = require('./lib/plugins/translate-helper-name');

module.exports = {
  name: 'ember-page-title',

  setupPreprocessorRegistry: function (type, registry) {
    app.registry.add('htmlbars-ast-plugin', {
      name: 'translate-title-helper-to-page-title-helper',
      plugin: TranslateHelperName,
      baseDir: function() {
        return __dirname;
      }
    });
  }
};
<|endoftext|>


<commit_before>'use strict';

const
  fs = require('fs'),
  path = require('path');

module.exports = (configName, options) => {
  const configNameSeparator = options.configNameSeparator || '-';
  const configDirName = options.configDirName || '';
  const configFileName = options.configFileName || 'config';
  const workingDir = options.workingDir || process.cwd();
  const loaders = options.loaders || [require('./jsLoader'), require('./jsonLoader')];
  const transform = typeof options.transform === 'function' ? options.transform : _ => _;
  const merge = typeof options.merge === 'function' ? options.merge : (a, b) => Object.assign({}, a, b);

  function getConfigNameParts(configName) {
    return configName.split(configNameSeparator);
  }

  const
    originalConfigName = configName,
    originalConfigNameParts = getConfigNameParts(configName);

  function load(config) {
    const parentConfig = config.extends === undefined
      ? (config.nameParts.length > 0
        ? resolve(config.nameParts.slice(0, config.nameParts.length - 1))
        : undefined)
      : resolve(config.extends);
    const loadedParentConfig = parentConfig ? load(parentConfig) : {};
    const info = {
      config: {
        name: originalConfigName,
        nameParts: originalConfigNameParts
      },
      currentConfig: {
        dir: config.dir,
        name: config.name,
        nameParts: config.nameParts
      }
    };
    if (config.file) {
      info.currentConfig.file = config.file;
    }
    if (parentConfig) {
      info.parentConfig = {
        dir: parentConfig.dir,
        name: parentConfig.name,
        nameParts: parentConfig.nameParts
      };
      if (parentConfig.file) {
        info.parentConfig.file = parentConfig.file;
      }
    }
    const loadedConfig = config.load(loadedParentConfig, info);
    return transform(config.merge ? merge(loadedParentConfig, loadedConfig, info) : loadedConfig, info);
  }

  function resolve(configName) {
    let config = {};
    if (typeof configName === 'string') {
      config.name = configName;
      config.nameParts = getConfigNameParts(configName);
    } else {
      config.nameParts = configName;
      config.name = config.nameParts.join(configNameSeparator);
    }

    config.dir = path.resolve(workingDir, configDirName, ...config.nameParts.map(i => path.join(i, configDirName)));

    function resolveConfigItemPath(...parts) {
      return path.resolve(config.dir, ...parts);
    }

    function resolveConfigFile(extension) {
      return resolveConfigItemPath(configFileName + extension);
    }

    let configFile;

    for (let loader of loaders) {
      configFile = resolveConfigFile(loader.extension);
      if (fs.existsSync(configFile)) {
        config.file = configFile;
        config = Object.assign(loader.loader(configFile), config);
        break;
      }
    }

    if (config.load === undefined) {
      config.load = () => ({});
    }
    if (config.merge === undefined) {
      config.merge = true;
    }
    return config;
  }

  return load(resolve(configName));
};
<commit_msg>fix(*): search only config.js in configDirName<commit_after>'use strict';

const
  fs = require('fs'),
  path = require('path');

module.exports = (configName, options) => {
  const configNameSeparator = options.configNameSeparator || '-';
  const configDirName = options.configDirName || '';
  const configFileName = options.configFileName || 'config';
  const workingDir = options.workingDir || process.cwd();
  const loaders = options.loaders || [require('./jsLoader'), require('./jsonLoader')];
  const transform = typeof options.transform === 'function' ? options.transform : _ => _;
  const merge = typeof options.merge === 'function' ? options.merge : (a, b) => Object.assign({}, a, b);

  function getConfigNameParts(configName) {
    return configName.split(configNameSeparator);
  }

  const
    originalConfigName = configName,
    originalConfigNameParts = getConfigNameParts(configName);

  function load(config) {
    const parentConfig = config.extends === undefined
      ? (config.nameParts.length > 0
        ? resolve(config.nameParts.slice(0, config.nameParts.length - 1))
        : undefined)
      : resolve(config.extends);
    const loadedParentConfig = parentConfig ? load(parentConfig) : {};
    const info = {
      config: {
        name: originalConfigName,
        nameParts: originalConfigNameParts
      },
      currentConfig: {
        dir: config.dir,
        name: config.name,
        nameParts: config.nameParts
      }
    };
    if (config.file) {
      info.currentConfig.file = config.file;
    }
    if (parentConfig) {
      info.parentConfig = {
        dir: parentConfig.dir,
        name: parentConfig.name,
        nameParts: parentConfig.nameParts
      };
      if (parentConfig.file) {
        info.parentConfig.file = parentConfig.file;
      }
    }
    const loadedConfig = config.load(loadedParentConfig, info);
    return transform(config.merge ? merge(loadedParentConfig, loadedConfig, info) : loadedConfig, info);
  }

  function resolve(configName) {
    let config = {};
    if (typeof configName === 'string') {
      config.name = configName;
      config.nameParts = getConfigNameParts(configName);
    } else {
      config.nameParts = configName;
      config.name = config.nameParts.join(configNameSeparator);
    }

    config.dir = path.resolve(workingDir, ...config.nameParts.map(i => path.join(configDirName, i)));

    function resolveConfigItemPath(...parts) {
      return path.resolve(config.dir, ...parts);
    }

    function resolveConfigFile(extension) {
      return resolveConfigItemPath(configDirName, configFileName + extension);
    }

    let configFile;

    for (let loader of loaders) {
      configFile = resolveConfigFile(loader.extension);
      if (fs.existsSync(configFile)) {
        config.file = configFile;
        config = Object.assign(loader.loader(configFile), config);
        break;
      }
    }

    if (config.load === undefined) {
      config.load = () => ({});
    }
    if (config.merge === undefined) {
      config.merge = true;
    }
    return config;
  }

  return load(resolve(configName));
};
<|endoftext|>


<commit_before>var inside = require('turf-inside');

/**
 * Calculates the maximum value of a field for a set of {@link Point} features within a set of {@link Polygon} features.
 *
 * @module turf/max
 * @param {FeatureCollection} polygons a FeatureCollection of {@link Polygon} features
 * @param {FeatureCollection} points a FeatureCollection of {@link Point} features
 * @param {string} inField the field in input data to analyze
 * @param {string} outField the field in which to store results
 * @return {FeatureCollection} a FeatureCollection of {@link Polygon} features
 * with properties listed as `outField` values
 * @example
 * var polygons = {
 *   "type": "FeatureCollection",
 *   "features": [
 *     {
 *       "type": "Feature",
 *       "properties": {},
 *       "geometry": {
 *         "type": "Polygon",
 *         "coordinates": [[
 *           [101.551437, 3.150114],
 *           [101.551437, 3.250208],
 *           [101.742324, 3.250208],
 *           [101.742324, 3.150114],
 *           [101.551437, 3.150114]
 *         ]]
 *       }
 *     }, {
 *       "type": "Feature",
 *       "properties": {},
 *       "geometry": {
 *         "type": "Polygon",
 *         "coordinates": [[
 *           [101.659927, 3.011612],
 *           [101.659927, 3.143944],
 *           [101.913986, 3.143944],
 *           [101.913986, 3.011612],
 *           [101.659927, 3.011612]
 *         ]]
 *       }
 *     }
 *   ]
 * };
 * var points = {
 *   "type": "FeatureCollection",
 *   "features": [
 *     {
 *       "type": "Feature",
 *       "properties": {
 *         "population": 200
 *       },
 *       "geometry": {
 *         "type": "Point",
 *         "coordinates": [101.56105, 3.213874]
 *       }
 *     }, {
 *       "type": "Feature",
 *       "properties": {
 *         "population": 600
 *       },
 *       "geometry": {
 *         "type": "Point",
 *         "coordinates": [101.709365, 3.211817]
 *       }
 *     }, {
 *       "type": "Feature",
 *       "properties": {
 *         "population": 100
 *       },
 *       "geometry": {
 *         "type": "Point",
 *         "coordinates": [101.645507, 3.169311]
 *       }
 *     }, {
 *       "type": "Feature",
 *       "properties": {
 *         "population": 200
 *       },
 *       "geometry": {
 *         "type": "Point",
 *         "coordinates": [101.708679, 3.071266]
 *       }
 *     }, {
 *       "type": "Feature",
 *       "properties": {
 *         "population": 300
 *       },
 *       "geometry": {
 *         "type": "Point",
 *         "coordinates": [101.826782, 3.081551]
 *       }
 *     }
 *   ]
 * };
 *
 * var aggregated = turf.max(
 *   polygons, points, 'population', 'max');
 *
 * var resultFeatures = points.features.concat(
 *   aggregated.features);
 * var result = {
 *   "type": "FeatureCollection",
 *   "features": resultFeatures
 * };
 *
 * //=result
 */
module.exports = function(polyFC, ptFC, inField, outField){
  polyFC.features.forEach(function(poly){
    if(!poly.properties){
      poly.properties = {};
    }
    var values = [];
    ptFC.features.forEach(function(pt){
      if (inside(pt, poly)) {
        values.push(pt.properties[inField]);
      }
    });
    poly.properties[outField] = max(values);
  });

  return polyFC;
}

function max(x) {
    var value;
    for (var i = 0; i < x.length; i++) {
        // On the first iteration of this loop, max is
        // undefined and is thus made the maximum element in the array
        if (x[i] > value || value === undefined) value = x[i];
    }
    return value;
}
<commit_msg>Add category<commit_after>var inside = require('turf-inside');

/**
 * Calculates the maximum value of a field for a set of {@link Point} features within a set of {@link Polygon} features.
 *
 * @module turf/max
 * @category aggregation
 * @param {FeatureCollection} polygons a FeatureCollection of {@link Polygon} features
 * @param {FeatureCollection} points a FeatureCollection of {@link Point} features
 * @param {string} inField the field in input data to analyze
 * @param {string} outField the field in which to store results
 * @return {FeatureCollection} a FeatureCollection of {@link Polygon} features
 * with properties listed as `outField` values
 * @example
 * var polygons = {
 *   "type": "FeatureCollection",
 *   "features": [
 *     {
 *       "type": "Feature",
 *       "properties": {},
 *       "geometry": {
 *         "type": "Polygon",
 *         "coordinates": [[
 *           [101.551437, 3.150114],
 *           [101.551437, 3.250208],
 *           [101.742324, 3.250208],
 *           [101.742324, 3.150114],
 *           [101.551437, 3.150114]
 *         ]]
 *       }
 *     }, {
 *       "type": "Feature",
 *       "properties": {},
 *       "geometry": {
 *         "type": "Polygon",
 *         "coordinates": [[
 *           [101.659927, 3.011612],
 *           [101.659927, 3.143944],
 *           [101.913986, 3.143944],
 *           [101.913986, 3.011612],
 *           [101.659927, 3.011612]
 *         ]]
 *       }
 *     }
 *   ]
 * };
 * var points = {
 *   "type": "FeatureCollection",
 *   "features": [
 *     {
 *       "type": "Feature",
 *       "properties": {
 *         "population": 200
 *       },
 *       "geometry": {
 *         "type": "Point",
 *         "coordinates": [101.56105, 3.213874]
 *       }
 *     }, {
 *       "type": "Feature",
 *       "properties": {
 *         "population": 600
 *       },
 *       "geometry": {
 *         "type": "Point",
 *         "coordinates": [101.709365, 3.211817]
 *       }
 *     }, {
 *       "type": "Feature",
 *       "properties": {
 *         "population": 100
 *       },
 *       "geometry": {
 *         "type": "Point",
 *         "coordinates": [101.645507, 3.169311]
 *       }
 *     }, {
 *       "type": "Feature",
 *       "properties": {
 *         "population": 200
 *       },
 *       "geometry": {
 *         "type": "Point",
 *         "coordinates": [101.708679, 3.071266]
 *       }
 *     }, {
 *       "type": "Feature",
 *       "properties": {
 *         "population": 300
 *       },
 *       "geometry": {
 *         "type": "Point",
 *         "coordinates": [101.826782, 3.081551]
 *       }
 *     }
 *   ]
 * };
 *
 * var aggregated = turf.max(
 *   polygons, points, 'population', 'max');
 *
 * var resultFeatures = points.features.concat(
 *   aggregated.features);
 * var result = {
 *   "type": "FeatureCollection",
 *   "features": resultFeatures
 * };
 *
 * //=result
 */
module.exports = function(polyFC, ptFC, inField, outField){
  polyFC.features.forEach(function(poly){
    if(!poly.properties){
      poly.properties = {};
    }
    var values = [];
    ptFC.features.forEach(function(pt){
      if (inside(pt, poly)) {
        values.push(pt.properties[inField]);
      }
    });
    poly.properties[outField] = max(values);
  });

  return polyFC;
}

function max(x) {
    var value;
    for (var i = 0; i < x.length; i++) {
        // On the first iteration of this loop, max is
        // undefined and is thus made the maximum element in the array
        if (x[i] > value || value === undefined) value = x[i];
    }
    return value;
}
<|endoftext|>


<commit_before>
/**
 * Module dependencies.
 */

var Analytics = require('analytics-node');
var thunkify = require('thunkify-wrap');
var storage = require('./lib/storage');
var urlRegex = require('url-regex');
var Botkit = require('Botkit');
var fmt = require('node-fmt');
var url = require('url');
var _ = require('lodash');
var co = require('co');

/**
 * Assign environmental variables.
 */

var clientId = process.env.clientId;
var clientSecret = process.env.clientSecret;
var port = process.env.PORT || 3000;
if (!clientId || !clientSecret || !port) {
  console.log('Error: clientId, clientSecret, and port are undefined in environment');
  process.exit(1);
}

/**
 * Setup `analytics`.
 */

var analytics = new Analytics(process.env.segmentKey);

/**
 * Configure the controller.
 */

var controller = Botkit.slackbot({
  storage: thunkatron(storage())
}).configureSlackApp({
  clientId: clientId,
  clientSecret: clientSecret,
  redirectUri: 'http://slack-channels-fomo-bot.herokuapp.com/oauth',
  scopes: ['bot']
});

/**
 * Setup web server.
 */

controller.setupWebserver(port, function(err, webserver) {
  controller.createWebhookEndpoints(controller.webserver);
  controller.createOauthEndpoints(controller.webserver, function(err, req, res) {
    if (err) return res.status(500).send('ERROR: ' + err);
    res.send('Success!');
  });
});

/**
 * Setup bots.
 */

var _bots = {};
function trackBot(bot) {
  _bots[bot.config.token] = bot;
}

/**
 * Create bots.
 */

controller.on('create_bot', function(bot, config) {
  // Thunkify the api.
  bot.api = thunkatron(bot.api);

  // If already configured, leave.
  if (_bots[bot.config.token]) return;

  // Otherwise, start RTM.
  bot.startRTM(co.wrap(function *(err) {
    if (err) return err;
    trackBot(bot);

    /**
     * Add team to database.
     */

    var team = {
      id: bot.team_info.id,
      domain: bot.team_info.domain,
      email_domain: bot.team_info.email_domain,
      feed_id: ''
    };
    yield controller.storage.teams.save(team);

    /**
     * Analytics.
     */

    analytics.identify({
      userId: config.createdBy,
      traits: {
        team_id: bot.config[0].id,
        domain: bot.config[0].domain,
        email_domain: bot.config[0].email_domain
      }
    });

    analytics.track({
      userId: config.createdBy,
      event: 'Bot Installed'
    });

    /**
     * Initial conversation with bot's creator.
     */

    bot.startPrivateConversation({ user: config.createdBy }, co.wrap(function *(err, convo) {
      if (err) return console.log(err);
      convo.say('Hello, I\'m Channel FOMO Bot! I just joined your team. I\'m here to let you know when a new channel is created!');
      convo.say('Whenever someone creates a new channel, I\'ll publish it to a channel of your choosing.');
      convo.ask('What channel should I publish to? Respond with the channel name or skip by saying \'no\' (you can set this later by mentioning me and saying \'publish to <channel-name>\').', co.wrap(function *(response, convo) {
        if (response.text.toLowerCase() === 'no') {
          convo.say('No problem! See you around :)');
          convo.next();
        }
        var res = yield bot.api.channels.list({});
        var channel = _.filter(res.channels, function(c) {
          return c.name.indexOf(response.text) >= 0;
        });
        if (channel.length > 0) {
          team.feed_id = channel[0].id;
          yield controller.storage.teams.save(team);
          convo.say('Great! New channels will be posted there.');
          convo.next();
        } else {
          convo.say('Hmmm, that doesn\'t look like a valid channel name.');
          convo.repeat();
          convo.next();
        }
      }));
    }));
  }));
});

/**
 * Open connection.
 */

controller.on('channel_created', co.wrap(function *(bot, res) {
  var team = yield controller.storage.teams.get(bot.team_info.id);
  if (!team[0].feed_id) return;
  var text = fmt('<#%s> was just created by <@%s>', res.channel.id, res.channel.creator);

  bot.say({
    text: text,
    channel: team[0].feed_id
  });

  analytics.identify({
    userId: res.channel.creator
    traits: {
      team_id: team[0].id
    }
  });

  analytics.track({
    userId: res.channel.creator,
    event: 'Bot Installed'
  });
}));

/**
 * Open connection.
 */

controller.on('rtm_open', function(bot) {
  console.log('** The RTM api just connected!');
});

/**
 * Close connection.
 */

controller.on('rtm_close', function(bot) {
  console.log('** The RTM api just closed');
  // you may want to attempt to re-open
});

/**
 * Connect all bots.
 */

controller.storage.teams.all(function(err, teams) {
  if (err) throw new Error(err);
  for (var t in teams) {
    if (teams[t].bot) {
      var bot = controller.spawn(teams[t]).startRTM(function(err) {
        if (err) return console.log('Error connecting bot to Slack: ', err);
        trackBot(bot);
      });
    }
  }
});

/**
 * Hear URL(s) in chat.
 */

controller.hears('publish', ['direct_message', 'direct_mention', 'mention', 'ambient'], function(bot, message) {
  // publish to <channel-name>
});

/**
 * Help.
 */

controller.hears('help', ['direct_message', 'direct_mention', 'mention', 'ambient'], function(bot, message) {
  var text = '';
  bot.reply(message, text);
});

/**
 * Private function to deep nest thunkify.
 */

function thunkatron(obj) {
  Object.keys(obj).forEach(function(key) {
    if (typeof obj[key] === 'function') obj[key] = thunkify(obj[key]);
    if (typeof obj[key] === 'object' && obj[key] !== null) obj[key] = thunkatron(obj[key]);
  });
  return obj;
}
<commit_msg>updated index.js<commit_after>
/**
 * Module dependencies.
 */

var Analytics = require('analytics-node');
var thunkify = require('thunkify-wrap');
var storage = require('./lib/storage');
var urlRegex = require('url-regex');
var Botkit = require('Botkit');
var fmt = require('node-fmt');
var url = require('url');
var _ = require('lodash');
var co = require('co');

/**
 * Assign environmental variables.
 */

var clientId = process.env.clientId;
var clientSecret = process.env.clientSecret;
var port = process.env.PORT || 3000;
if (!clientId || !clientSecret || !port) {
  console.log('Error: clientId, clientSecret, and port are undefined in environment');
  process.exit(1);
}

/**
 * Setup `analytics`.
 */

var analytics = new Analytics(process.env.segmentKey);

/**
 * Configure the controller.
 */

var controller = Botkit.slackbot({
  storage: thunkatron(storage())
}).configureSlackApp({
  clientId: clientId,
  clientSecret: clientSecret,
  redirectUri: 'http://slack-channel-fomo-bot.herokuapp.com/oauth',
  scopes: ['bot']
});

/**
 * Setup web server.
 */

controller.setupWebserver(port, function(err, webserver) {
  controller.createWebhookEndpoints(controller.webserver);
  controller.createOauthEndpoints(controller.webserver, function(err, req, res) {
    if (err) return res.status(500).send('ERROR: ' + err);
    res.send('Success!');
  });
});

/**
 * Setup bots.
 */

var _bots = {};
function trackBot(bot) {
  _bots[bot.config.token] = bot;
}

/**
 * Create bots.
 */

controller.on('create_bot', function(bot, config) {
  // Thunkify the api.
  bot.api = thunkatron(bot.api);

  // If already configured, leave.
  if (_bots[bot.config.token]) return;

  // Otherwise, start RTM.
  bot.startRTM(co.wrap(function *(err) {
    if (err) return err;
    trackBot(bot);

    /**
     * Add team to database.
     */

    var team = {
      id: bot.team_info.id,
      domain: bot.team_info.domain,
      email_domain: bot.team_info.email_domain,
      feed_id: ''
    };
    yield controller.storage.teams.save(team);

    /**
     * Analytics.
     */

    analytics.identify({
      userId: config.createdBy,
      traits: {
        team_id: bot.config[0].id,
        domain: bot.config[0].domain,
        email_domain: bot.config[0].email_domain
      }
    });

    analytics.track({
      userId: config.createdBy,
      event: 'Bot Installed'
    });

    /**
     * Initial conversation with bot's creator.
     */

    bot.startPrivateConversation({ user: config.createdBy }, co.wrap(function *(err, convo) {
      if (err) return console.log(err);
      convo.say('Hello, I\'m Channel FOMO Bot! I just joined your team. I\'m here to let you know when a new channel is created!');
      convo.say('Whenever someone creates a new channel, I\'ll publish it to a channel of your choosing.');
      convo.ask('What channel should I publish to? Respond with the channel name or skip by saying \'no\' (you can set this later by mentioning me and saying \'publish to <channel-name>\').', co.wrap(function *(response, convo) {
        if (response.text.toLowerCase() === 'no') {
          convo.say('No problem! See you around :)');
          convo.next();
        }
        var res = yield bot.api.channels.list({});
        var channel = _.filter(res.channels, function(c) {
          return c.name.indexOf(response.text) >= 0;
        });
        if (channel.length > 0) {
          team.feed_id = channel[0].id;
          yield controller.storage.teams.save(team);
          convo.say('Great! New channels will be posted there.');
          convo.next();
        } else {
          convo.say('Hmmm, that doesn\'t look like a valid channel name.');
          convo.repeat();
          convo.next();
        }
      }));
    }));
  }));
});

/**
 * Open connection.
 */

controller.on('channel_created', co.wrap(function *(bot, res) {
  var team = yield controller.storage.teams.get(bot.team_info.id);
  if (!team[0].feed_id) return;
  var text = fmt('<#%s> was just created by <@%s>', res.channel.id, res.channel.creator);

  bot.say({
    text: text,
    channel: team[0].feed_id
  });

  analytics.identify({
    userId: res.channel.creator
    traits: {
      team_id: team[0].id
    }
  });

  analytics.track({
    userId: res.channel.creator,
    event: 'Bot Installed'
  });
}));

/**
 * Open connection.
 */

controller.on('rtm_open', function(bot) {
  console.log('** The RTM api just connected!');
});

/**
 * Close connection.
 */

controller.on('rtm_close', function(bot) {
  console.log('** The RTM api just closed');
  // you may want to attempt to re-open
});

/**
 * Connect all bots.
 */

controller.storage.teams.all(function(err, teams) {
  if (err) throw new Error(err);
  for (var t in teams) {
    if (teams[t].bot) {
      var bot = controller.spawn(teams[t]).startRTM(function(err) {
        if (err) return console.log('Error connecting bot to Slack: ', err);
        trackBot(bot);
      });
    }
  }
});

/**
 * Hear URL(s) in chat.
 */

controller.hears('publish', ['direct_message', 'direct_mention', 'mention', 'ambient'], function(bot, message) {
  // publish to <channel-name>
});

/**
 * Help.
 */

controller.hears('help', ['direct_message', 'direct_mention', 'mention', 'ambient'], function(bot, message) {
  var text = '';
  bot.reply(message, text);
});

/**
 * Private function to deep nest thunkify.
 */

function thunkatron(obj) {
  Object.keys(obj).forEach(function(key) {
    if (typeof obj[key] === 'function') obj[key] = thunkify(obj[key]);
    if (typeof obj[key] === 'object' && obj[key] !== null) obj[key] = thunkatron(obj[key]);
  });
  return obj;
}
<|endoftext|>


<commit_before>'use strict'

var toArray = require('stream-to-array')
var Promise = require('bluebird')

module.exports = streamToPromise

function streamToPromise (stream) {
  var promise
  if (stream.readable) {
    promise = fromReadable(stream)
  } else if (stream.writable) {
    promise = fromWritable(stream)
  } else {
    promise = Promise.resolve()
  }
  return promise
}

function fromReadable (stream) {
  var promise = toArray(stream)

  // Ensure stream is in flowing mode
  stream.resume()

  return promise
    .then(function (parts) {
      var buffers = []
      for (var i = 0, l = parts.length; i < l; ++i) {
        var part = parts[i]
        buffers.push((part instanceof Buffer) ? part : new Buffer(part))
      }
      return Buffer.concat(buffers)
    })
}

function fromWritable (stream) {
  return new Promise(function (resolve, reject) {
    stream.once('finish', resolve)
    stream.once('error', reject)
  })
}
<commit_msg>Simplify readable concat w/ Buffer.isBuffer and [].map<commit_after>'use strict'

var toArray = require('stream-to-array')
var Promise = require('bluebird')

module.exports = streamToPromise

function streamToPromise (stream) {
  var promise
  if (stream.readable) {
    promise = fromReadable(stream)
  } else if (stream.writable) {
    promise = fromWritable(stream)
  } else {
    promise = Promise.resolve()
  }
  return promise
}

function fromReadable (stream) {
  var promise = toArray(stream)

  // Ensure stream is in flowing mode
  stream.resume()

  return promise
    .then(function concat (parts) {
      return Buffer.concat(parts.map(bufferize))
    })
}

function fromWritable (stream) {
  return new Promise(function (resolve, reject) {
    stream.once('finish', resolve)
    stream.once('error', reject)
  })
}

function bufferize (chunk) {
  return Buffer.isBuffer(chunk) ? chunk : new Buffer(chunk)
}
<|endoftext|>


<commit_before>var url = require('url');
var http = require('http');
var https = require('https');
var through = require('through2');
var duplexer = require('duplexer2');

module.exports = hyperquest;

function bind (obj, fn) {
  var args = Array.prototype.slice.call(arguments, 2);
  return function () {
    var argv = args.concat(Array.prototype.slice.call(arguments));
    return fn.apply(obj, argv);
  }
}

function hyperquest (uri, opts, cb, extra) {
    if (typeof uri === 'object') {
        cb = opts;
        opts = uri;
        uri = undefined;
    }
    if (typeof opts === 'function') {
      cb = opts;
      opts = undefined;
    }
    if (!opts) opts = {};
    if (uri !== undefined) opts.uri = uri;
    if (extra) opts.method = extra.method;

    var req = new Req(opts);
    var ws = req.duplex && through();
    var rs = through();

    var dup = req.duplex ? duplexer(ws, rs) : rs;
    if (!req.duplex) {
        rs.writable = false;
    }
    dup.request = req;
    dup.setHeader = bind(req, req.setHeader);
    dup.setLocation = bind(req, req.setLocation);

    var closed = false;
    dup.on('close', function () { closed = true });

    process.nextTick(function () {
        if (closed) return;
        dup.on('close', function () { r.destroy() });

        var r = req._send();
        r.on('error', bind(dup, dup.emit, 'error'));

        r.on('response', function (res) {
            dup.response = res;
            dup.emit('response', res);
            if (req.duplex) res.pipe(rs)
            else {
                res.on('data', function (buf) { rs.push(buf) });
                res.on('end', function () { rs.push(null) });
            }
        });

        if (req.duplex) {
            ws.pipe(r);
        }
        else r.end();
    });

    if (cb) {
        dup.on('error', cb);
        dup.on('response', bind(dup, cb, null));
    }
    return dup;
}

hyperquest.get = hyperquest;

hyperquest.post = function (uri, opts, cb) {
    return hyperquest(uri, opts, cb, { method: 'POST' });
};

hyperquest.put = function (uri, opts, cb) {
    return hyperquest(uri, opts, cb, { method: 'PUT' });
};

hyperquest['delete'] = function (uri, opts, cb) {
    return hyperquest(uri, opts, cb, { method: 'DELETE' });
};

function Req (opts) {
    this.headers = opts.headers || {};

    var method = (opts.method || 'GET').toUpperCase();
    this.method = method;
    this.duplex = !(method === 'GET' || method === 'DELETE'
        || method === 'HEAD');
    this.auth = opts.auth;

    this.options = opts;

    if (opts.uri) this.setLocation(opts.uri);
}

Req.prototype._send = function () {
    this._sent = true;

    var headers = this.headers || {};
    var u = url.parse(this.uri);
    var au = u.auth || this.auth;
    if (au) {
        headers.authorization = 'Basic ' + Buffer(au).toString('base64');
    }

    var protocol = u.protocol || '';
    var iface = protocol === 'https:' ? https : http;
    var opts = {
        scheme: protocol.replace(/:$/, ''),
        method: this.method,
        host: u.hostname,
        port: Number(u.port) || (protocol === 'https:' ? 443 : 80),
        path: u.path,
        agent: false,
        headers: headers,
        withCredentials: this.options.withCredentials
    };
    if (protocol === 'https:') {
        opts.pfx = this.options.pfx;
        opts.key = this.options.key;
        opts.cert = this.options.cert;
        opts.ca = this.options.ca;
        opts.ciphers = this.options.ciphers;
        opts.rejectUnauthorized = this.options.rejectUnauthorized;
        opts.secureProtocol = this.options.secureProtocol;
    }
    var req = iface.request(opts);

    if (req.setTimeout) req.setTimeout(Math.pow(2, 32) * 1000);
    return req;
};

Req.prototype.setHeader = function (key, value) {
    if (this._sent) throw new Error('request already sent');
    this.headers[key] = value;
    return this;
};

Req.prototype.setLocation = function (uri) {
    this.uri = uri;
    return this;
};
<commit_msg>indent<commit_after>var url = require('url');
var http = require('http');
var https = require('https');
var through = require('through2');
var duplexer = require('duplexer2');

module.exports = hyperquest;

function bind (obj, fn) {
    var args = Array.prototype.slice.call(arguments, 2);
    return function () {
        var argv = args.concat(Array.prototype.slice.call(arguments));
        return fn.apply(obj, argv);
    }
}

function hyperquest (uri, opts, cb, extra) {
    if (typeof uri === 'object') {
        cb = opts;
        opts = uri;
        uri = undefined;
    }
    if (typeof opts === 'function') {
      cb = opts;
      opts = undefined;
    }
    if (!opts) opts = {};
    if (uri !== undefined) opts.uri = uri;
    if (extra) opts.method = extra.method;

    var req = new Req(opts);
    var ws = req.duplex && through();
    var rs = through();

    var dup = req.duplex ? duplexer(ws, rs) : rs;
    if (!req.duplex) {
        rs.writable = false;
    }
    dup.request = req;
    dup.setHeader = bind(req, req.setHeader);
    dup.setLocation = bind(req, req.setLocation);

    var closed = false;
    dup.on('close', function () { closed = true });

    process.nextTick(function () {
        if (closed) return;
        dup.on('close', function () { r.destroy() });

        var r = req._send();
        r.on('error', bind(dup, dup.emit, 'error'));

        r.on('response', function (res) {
            dup.response = res;
            dup.emit('response', res);
            if (req.duplex) res.pipe(rs)
            else {
                res.on('data', function (buf) { rs.push(buf) });
                res.on('end', function () { rs.push(null) });
            }
        });

        if (req.duplex) {
            ws.pipe(r);
        }
        else r.end();
    });

    if (cb) {
        dup.on('error', cb);
        dup.on('response', bind(dup, cb, null));
    }
    return dup;
}

hyperquest.get = hyperquest;

hyperquest.post = function (uri, opts, cb) {
    return hyperquest(uri, opts, cb, { method: 'POST' });
};

hyperquest.put = function (uri, opts, cb) {
    return hyperquest(uri, opts, cb, { method: 'PUT' });
};

hyperquest['delete'] = function (uri, opts, cb) {
    return hyperquest(uri, opts, cb, { method: 'DELETE' });
};

function Req (opts) {
    this.headers = opts.headers || {};

    var method = (opts.method || 'GET').toUpperCase();
    this.method = method;
    this.duplex = !(method === 'GET' || method === 'DELETE'
        || method === 'HEAD');
    this.auth = opts.auth;

    this.options = opts;

    if (opts.uri) this.setLocation(opts.uri);
}

Req.prototype._send = function () {
    this._sent = true;

    var headers = this.headers || {};
    var u = url.parse(this.uri);
    var au = u.auth || this.auth;
    if (au) {
        headers.authorization = 'Basic ' + Buffer(au).toString('base64');
    }

    var protocol = u.protocol || '';
    var iface = protocol === 'https:' ? https : http;
    var opts = {
        scheme: protocol.replace(/:$/, ''),
        method: this.method,
        host: u.hostname,
        port: Number(u.port) || (protocol === 'https:' ? 443 : 80),
        path: u.path,
        agent: false,
        headers: headers,
        withCredentials: this.options.withCredentials
    };
    if (protocol === 'https:') {
        opts.pfx = this.options.pfx;
        opts.key = this.options.key;
        opts.cert = this.options.cert;
        opts.ca = this.options.ca;
        opts.ciphers = this.options.ciphers;
        opts.rejectUnauthorized = this.options.rejectUnauthorized;
        opts.secureProtocol = this.options.secureProtocol;
    }
    var req = iface.request(opts);

    if (req.setTimeout) req.setTimeout(Math.pow(2, 32) * 1000);
    return req;
};

Req.prototype.setHeader = function (key, value) {
    if (this._sent) throw new Error('request already sent');
    this.headers[key] = value;
    return this;
};

Req.prototype.setLocation = function (uri) {
    this.uri = uri;
    return this;
};
<|endoftext|>


<commit_before>// dependencies -------------------------------------------------------------------

var JSHINT    = require('jshint').JSHINT;
var async     = require('async');
var fileUtils = require('./files');

// public API ---------------------------------------------------------------------

var validate = {
    BIDS: BIDS,
    JSON: JSON,
    TSV: TSV
};

exports = validate;

// implementations ----------------------------------------------------------------

function BIDS (fileList, callback) {
    let errors = [];
    async.forEachOf(fileList, function (file, key, cb) {

        // validate tsv
        if (file.name && file.name.indexOf('.tsv') > -1) {
            validate.TSV(file, function () {
                cb();
            });
            return;
        }

        // validate json
        if (file.name && file.name.indexOf('.json') > -1) {
            validate.JSON(file, function (err) {
                if (err) {
                    errors.push({file: file, errors: err})
                }
                cb();
            });
        } else {
            cb();
        }

    }, function () {
        callback(errors);
    });
}

function JSON (file, callback) {
    fileUtils.read(file, function (contents) {
        if (!JSHINT(contents)) {
            let out = JSHINT.data(),
            errors = out.errors;
            callback(errors);
        } else {
            callback(null);
        }
    });
}

function TSV (file, callback) {
    fileUtils.read(file, function (contents) {
        let rows = contents.split('\n');
        async.each(rows, function (row) {
            let columnsInRow = row.split('\t');
        });
        callback();
    });
}
<commit_msg>Changed remaining lets to vars<commit_after>// dependencies -------------------------------------------------------------------

var JSHINT    = require('jshint').JSHINT;
var async     = require('async');
var fileUtils = require('./files');

// public API ---------------------------------------------------------------------

var validate = {
    BIDS: BIDS,
    JSON: JSON,
    TSV: TSV
};

exports = validate;

// implementations ----------------------------------------------------------------

function BIDS (fileList, callback) {
    var errors = [];
    async.forEachOf(fileList, function (file, key, cb) {

        // validate tsv
        if (file.name && file.name.indexOf('.tsv') > -1) {
            validate.TSV(file, function () {
                cb();
            });
            return;
        }

        // validate json
        if (file.name && file.name.indexOf('.json') > -1) {
            validate.JSON(file, function (err) {
                if (err) {
                    errors.push({file: file, errors: err})
                }
                cb();
            });
        } else {
            cb();
        }

    }, function () {
        callback(errors);
    });
}

function JSON (file, callback) {
    fileUtils.read(file, function (contents) {
        if (!JSHINT(contents)) {
            var out = JSHINT.data(),
            errors = out.errors;
            callback(errors);
        } else {
            callback(null);
        }
    });
}

function TSV (file, callback) {
    fileUtils.read(file, function (contents) {
        var rows = contents.split('\n');
        async.each(rows, function (row) {
            var columnsInRow = row.split('\t');
        });
        callback();
    });
}
<|endoftext|>


<commit_before>#! /usr/bin/env node

console.log('Starting update_ics_portfolios');
const request = require('request');

const urls = ['http://www.google.com', 'http://philipmjohnson.org'];
const urlBodies = [];

const urlBodyPromises = urls.map(function (url) {
  return new Promise(function (resolve) {
    request.get(url, function processUrl(error, response, body) {
      if (!error && response.statusCode === 200) {
        urlBodies.push(body);
      } else {
        console.log(`Failed to get body of ${url}.`);
      }
      resolve();
    });
  });
});

Promise.all(urlBodyPromises)
    .then(function () { console.log(`finished, got ${urlBodies.length} bodies`); })
    .catch(console.error);
<commit_msg>continue fiddling<commit_after>#! /usr/bin/env node

// 1. Create a profile-data.json file.
//    [{ domain: 'philipmjohnson.github.io', techfolio: true, bio-overrides: { name: 'Philip Johnson' }}]
// 2. Read the profiles.json file in as text, convert to JSON, store as profileData.
// 3. Add a default field called bio with name, homepage, picture, interests, summary fields.
// 2  If techfolio, update bio field based on bio.json file data.
// 4. Override bio fields with bio-overrides.
// 3. Write out a data file to this _data directory with info for Jekyll.

console.log('Starting update_ics_portfolios');
const request = require('request');

// URL: https://raw.githubusercontent.com/philipmjohnson/philipmjohnson.github.io/master/_data/bio.json

function getBioJsonUrl(domain) {
  const username = domain.split('.')[0];
  return `https://raw.githubusercontent.com/${username}/${domain}/master/_data/bio.json`;
}

const domains = ['philipmjohnson.github.io'];

const bioJsons = [];

/**
 * Returns a Promise which, when resolved, results in pushing the bio.json onto bioJsons.
 * If an error occurs, prints out a message to the console with the problematic URL.
 * @param url The URL to get the body of.
 * @returns {Promise} A Promise to push that body onto bioJsons.
 */
function getBioFiles(domain) {
  return new Promise(function (resolve) {
    request.get(getBioJsonUrl(domain), function processUrl(error, response, body) {
      if (!error && response.statusCode === 200) {
        try {
          bioJsons.push(JSON.parse(body));
        } catch (e) {
          console.log(`Failed to parse bio.json for ${domain}.`);
        }
      } else {
        console.log(`Failed to get bio.json for ${domain}.`);
      }
      resolve();
    });
  });
}

const urlBodyPromises = domains.map(getBioFiles);

Promise.all(urlBodyPromises)
    .then(function () {
      console.log(bioJsons);
    })
    .catch(console.error);
<|endoftext|>


<commit_before>var TENSION = 800;
var FRICTION = 90;

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  TouchableWithoutFeedback,
  Text,
  Image,
  View,
  Animated,
  AlertIOS,
  PanResponder,
  Dimensions, TouchableOpacity
} from 'react-native';
var SCREEN_HEIGHT = Dimensions.get('window').height;
var DraggableDrawerHelper = require('./helper')(SCREEN_HEIGHT);

class component extends Component {
  constructor (props) {
    super(props)
    // naming it initialX clearly indicates that the only purpose
    // of the passed down prop is to initialize something internally
    var initialDrawerSize = DraggableDrawerHelper.calculateInitialPosition(this.props.initialDrawerSize);
    var finalDrawerSize = this.props.finalDrawerHeight;
    // console.log(initialDrawerSize, 'Initila size');
    this.state = {
      touched: 'FALSE',
      position: new Animated.Value(initialDrawerSize),
      initialPositon: initialDrawerSize,
      finalPosition: finalDrawerSize,
    };
  }

  onUpdatePosition (position) {
    // console.log('UPDATE_POSITION', position);
    position = position -50;
    this.state.position.setValue(position);
    this._previousTop = position;
    // console.log('Position ', position);
    var initialPosition = DraggableDrawerHelper.getInitialPosition();

    if (initialPosition === position) {
      this.props.onInitialPositionReached && this.props.onInitialPositionReached();
    }
  }

  componentWillMount () {
    // Initialize the DraggableDrawerHelper that will drive animations
    DraggableDrawerHelper.setupAnimation(TENSION, FRICTION,
      (position) => {
        if (!this.center) return;
        this.onUpdatePosition(position.value);
      }
    );

    this._panGesture = PanResponder.create({
      onMoveShouldSetPanResponder: (evt, gestureState) => {
        return DraggableDrawerHelper.isAValidMovement(gestureState.dx, gestureState.dy) && this.state.touched == 'TRUE';
      },
      onPanResponderMove: (evt, gestureState) => {
        this.moveDrawerView(gestureState);
      },
      onPanResponderRelease: (evt, gestureState) => {
        this.moveFinished(gestureState);
      },
    });
  }

  moveDrawerView (gestureState) {
    // console.log('GESTURE', gestureState);
    if (!this.center) return;
    var currentValue = Math.abs(gestureState.moveY / SCREEN_HEIGHT);
    var isGoingToUp = (gestureState.vy < 0);
    //Here, I'm subtracting %5 of screen size from edge drawer position to be closer as possible to finger location when dragging the drawer view
    var position = gestureState.moveY - SCREEN_HEIGHT * 0.05;
    //Send to callback function the current drawer position when drag down the drawer view component
    //   if(!isGoingToUp) this.props.onDragDown(1-currentValue);
    this.onUpdatePosition(position);
  }

  moveFinished (gestureState) {
    var isGoingToUp = (gestureState.vy < 0);
    if (!this.center) return;
    DraggableDrawerHelper.startAnimation(gestureState.vy, gestureState.moveY, this.state.initialPositon, gestureState.stateId, this.state.finalPosition);
    this.props.onRelease && this.props.onRelease(isGoingToUp);
  }

  render() {
    var containerView = this.props.renderContainerView ? this.props.renderContainerView() : null;
    var drawerView = this.props.renderDrawerView ? this.props.renderDrawerView() : null;
    var initDrawerView = this.props.renderInitDrawerView ? this.props.renderInitDrawerView() : null;
    var drawerPosition = {
      top: this.state.position
    };

    return (
      <View style={styles.viewport}>
        <View style={styles.container}>
          {containerView}
        </View>
        <Animated.View
          style={[drawerPosition, styles.drawer,
            {backgroundColor: this.props.drawerBg ? this.props.drawerBg : 'transparent' }]}
          ref={(center) => this.center = center}
          {...this._panGesture.panHandlers}>
          <TouchableWithoutFeedback
            onPressIn={() => { this.setState({ touched: 'TRUE' }); }}
            onPressOut={() => { this.setState({ touched: 'FALSE' }); }}>
            {initDrawerView}

          </TouchableWithoutFeedback>
          {drawerView}
        </Animated.View>

      </View>
    );
  }
};

var styles = StyleSheet.create({
  viewport: {
    flex: 1,
  },
  drawer: {
    flex: 1,
  },
  container: {
    position: 'absolute',
    top: 0,
    left: 0,
    bottom: 0,
    right: 0,
  },
});

module.exports = component;
<commit_msg>add background color & final drawer height<commit_after>var TENSION = 800;
var FRICTION = 90;

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  TouchableWithoutFeedback,
  Text,
  Image,
  View,
  Animated,
  AlertIOS,
  PanResponder,
  Dimensions, TouchableOpacity
} from 'react-native';
var SCREEN_HEIGHT = Dimensions.get('window').height;
var DraggableDrawerHelper = require('./helper')(SCREEN_HEIGHT);

class component extends Component {
  constructor (props) {
    super(props)
    // naming it initialX clearly indicates that the only purpose
    // of the passed down prop is to initialize something internally
    var initialDrawerSize = DraggableDrawerHelper.calculateInitialPosition(this.props.initialDrawerSize);
    var finalDrawerSize = this.props.finalDrawerHeight ? this.props.finalDrawerHeight : 0;
    // console.log(initialDrawerSize, 'Initila size');
    this.state = {
      touched: 'FALSE',
      position: new Animated.Value(initialDrawerSize),
      initialPositon: initialDrawerSize,
      finalPosition: finalDrawerSize,
    };
  }

  onUpdatePosition (position) {
    // console.log('UPDATE_POSITION', position);
    position = position -50;
    this.state.position.setValue(position);
    this._previousTop = position;
    // console.log('Position ', position);
    var initialPosition = DraggableDrawerHelper.getInitialPosition();

    if (initialPosition === position) {
      this.props.onInitialPositionReached && this.props.onInitialPositionReached();
    }
  }

  componentWillMount () {
    // Initialize the DraggableDrawerHelper that will drive animations
    DraggableDrawerHelper.setupAnimation(TENSION, FRICTION,
      (position) => {
        if (!this.center) return;
        this.onUpdatePosition(position.value);
      }
    );

    this._panGesture = PanResponder.create({
      onMoveShouldSetPanResponder: (evt, gestureState) => {
        return DraggableDrawerHelper.isAValidMovement(gestureState.dx, gestureState.dy) && this.state.touched == 'TRUE';
      },
      onPanResponderMove: (evt, gestureState) => {
        this.moveDrawerView(gestureState);
      },
      onPanResponderRelease: (evt, gestureState) => {
        this.moveFinished(gestureState);
      },
    });
  }

  moveDrawerView (gestureState) {
    // console.log('GESTURE', gestureState);
    if (!this.center) return;
    var currentValue = Math.abs(gestureState.moveY / SCREEN_HEIGHT);
    var isGoingToUp = (gestureState.vy < 0);
    //Here, I'm subtracting %5 of screen size from edge drawer position to be closer as possible to finger location when dragging the drawer view
    var position = gestureState.moveY - SCREEN_HEIGHT * 0.05;
    //Send to callback function the current drawer position when drag down the drawer view component
    //   if(!isGoingToUp) this.props.onDragDown(1-currentValue);
    this.onUpdatePosition(position);
  }

  moveFinished (gestureState) {
    var isGoingToUp = (gestureState.vy < 0);
    if (!this.center) return;
    DraggableDrawerHelper.startAnimation(gestureState.vy, gestureState.moveY, this.state.initialPositon, gestureState.stateId, this.state.finalPosition);
    this.props.onRelease && this.props.onRelease(isGoingToUp);
  }

  render() {
    var containerView = this.props.renderContainerView ? this.props.renderContainerView() : null;
    var drawerView = this.props.renderDrawerView ? this.props.renderDrawerView() : null;
    var initDrawerView = this.props.renderInitDrawerView ? this.props.renderInitDrawerView() : null;
    var drawerPosition = {
      top: this.state.position
    };

    return (
      <View style={styles.viewport}>
        <View style={styles.container}>
          {containerView}
        </View>
        <Animated.View
          style={[drawerPosition, styles.drawer,
            {backgroundColor: this.props.drawerBg ? this.props.drawerBg : 'white' }]}
          ref={(center) => this.center = center}
          {...this._panGesture.panHandlers}>
          <TouchableWithoutFeedback
            onPressIn={() => { this.setState({ touched: 'TRUE' }); }}
            onPressOut={() => { this.setState({ touched: 'FALSE' }); }}>
            {initDrawerView}

          </TouchableWithoutFeedback>
          {drawerView}
        </Animated.View>

      </View>
    );
  }
};

var styles = StyleSheet.create({
  viewport: {
    flex: 1,
  },
  drawer: {
    flex: 1,
  },
  container: {
    position: 'absolute',
    top: 0,
    left: 0,
    bottom: 0,
    right: 0,
  },
});

module.exports = component;
<|endoftext|>


<commit_before>module.exports = {
  env: {
    node: true,
    mocha: true,
    es6: true
  },
  extends: 'eslint:recommended',
  parserOptions: {
    ecmaVersion: 9
  },
  rules: {
    /* Strict Mode */
    strict: 0,

    /* Possible Errors are covered in "eslint:recommended" */
    'no-extra-parens': 0,
    'no-prototype-builtins': 1,
    'no-template-curly-in-string': 1,
    'no-unsafe-negation': 1,

    /* Best Practices */
    'block-scoped-var': 1,
    complexity: 1,
    'consistent-return': 1,
    curly: 1,
    'default-case': 1,
    'dot-location': [1, 'property'],
    'dot-notation': 1,
    eqeqeq: 1,
    'guard-for-in': 1,
    'no-alert': 1,
    'no-caller': 1,
    'no-div-regex': 1,
    'no-else-return': 1,
    'no-eval': 1,
    'no-extend-native': 1,
    'no-extra-bind': 1,
    'no-extra-label': 1,
    'no-floating-decimal': 1,
    'no-implicit-coercion': 1,
    'no-implicit-globals': 1,
    'no-multi-spaces': 1,
    'no-useless-call': 1,
    radix: 1,
    'vars-on-top': 1,
    yoda: [1, 'never', { exceptRange: true }],

    /* Stylistic Issues */
    'array-bracket-spacing': [1, 'never'],
    'array-callback-return': 1,
    'block-spacing': 1,
    'brace-style': [1, '1tbs', { allowSingleLine: true }],
    'comma-dangle': 1,
    'comma-spacing': 1,
    'comma-style': 1,
    'computed-property-spacing': 1,
    'eol-last': 1,
    'func-call-spacing': 1,
    indent: [1, 2, { VariableDeclarator: { var: 2, let: 2, const: 3 }, SwitchCase: 1, ObjectExpression: 'first', ImportDeclaration: 'first' }],
    'jsx-quotes': [1, 'prefer-single'],
    'key-spacing': 1,
    'keyword-spacing': 1,
    'linebreak-style': [1, 'unix'],
    'newline-after-var': 1,
    'newline-before-return': 1,
    'no-lonely-if': 1,
    'no-multiple-empty-lines': 1,
    'no-tabs': 1,
    'no-trailing-spaces': 1,
    'no-underscore-dangle': 1,
    'object-curly-spacing': [1, 'always'],
    'quote-props': [1, 'as-needed'],
    quotes: [1, 'single', { avoidEscape: true, allowTemplateLiterals: true }],
    'semi-spacing': 1,
    semi: [1, 'always'],
    'one-var': 1,
    'sort-vars': 0,
    'space-before-blocks': 1,
    'space-before-function-paren': [1, { anonymous: 'always', named: 'never' }],
    'space-in-parens': 1,
    'space-infix-ops': 1,
    'space-unary-ops': 1,
    'spaced-comment': 1,
    'wrap-regex': 1,

    /* Variables */
    'init-declarations': 1,
    'no-catch-shadow': 0,
    'no-label-var': 1,
    'no-restricted-globals': 1,
    'no-shadow': 0,
    'no-shadow-restricted-names': 1,
    'no-undef-init': 1,
    'no-use-before-define': 1,

    /* Node.js and CommonJS */
    'callback-return': 1,
    'handle-callback-err': 1,
    // "no-mixed-requires": 1,
    'no-new-require': 1,
    'no-path-concat': 1,
    // "no-process-env": 1,
    'no-process-exit': 1,
    'no-restricted-modules': 1,
    'no-sync': 1,

    /* ES6 */
    'arrow-body-style': 1,
    'arrow-parens': [1, 'as-needed'],
    'arrow-spacing': 1,
    'no-confusing-arrow': [1, { allowParens: true }],
    'no-useless-computed-key': 1,
    'no-var': 1,
    'object-shorthand': [1, 'properties'],
    'prefer-const': 1,
    'prefer-template': 1,
    'template-curly-spacing': 1
  }
};
<commit_msg>Switch to year based versioning.<commit_after>module.exports = {
  env: {
    node: true,
    mocha: true,
    es6: true
  },
  extends: 'eslint:recommended',
  parserOptions: {
    ecmaVersion: 2018
  },
  rules: {
    /* Strict Mode */
    strict: 0,

    /* Possible Errors are covered in "eslint:recommended" */
    'no-extra-parens': 0,
    'no-prototype-builtins': 1,
    'no-template-curly-in-string': 1,
    'no-unsafe-negation': 1,

    /* Best Practices */
    'block-scoped-var': 1,
    complexity: 1,
    'consistent-return': 1,
    curly: 1,
    'default-case': 1,
    'dot-location': [1, 'property'],
    'dot-notation': 1,
    eqeqeq: 1,
    'guard-for-in': 1,
    'no-alert': 1,
    'no-caller': 1,
    'no-div-regex': 1,
    'no-else-return': 1,
    'no-eval': 1,
    'no-extend-native': 1,
    'no-extra-bind': 1,
    'no-extra-label': 1,
    'no-floating-decimal': 1,
    'no-implicit-coercion': 1,
    'no-implicit-globals': 1,
    'no-multi-spaces': 1,
    'no-useless-call': 1,
    radix: 1,
    'vars-on-top': 1,
    yoda: [1, 'never', { exceptRange: true }],

    /* Stylistic Issues */
    'array-bracket-spacing': [1, 'never'],
    'array-callback-return': 1,
    'block-spacing': 1,
    'brace-style': [1, '1tbs', { allowSingleLine: true }],
    'comma-dangle': 1,
    'comma-spacing': 1,
    'comma-style': 1,
    'computed-property-spacing': 1,
    'eol-last': 1,
    'func-call-spacing': 1,
    indent: [1, 2, { VariableDeclarator: { var: 2, let: 2, const: 3 }, SwitchCase: 1, ObjectExpression: 'first', ImportDeclaration: 'first' }],
    'jsx-quotes': [1, 'prefer-single'],
    'key-spacing': 1,
    'keyword-spacing': 1,
    'linebreak-style': [1, 'unix'],
    'newline-after-var': 1,
    'newline-before-return': 1,
    'no-lonely-if': 1,
    'no-multiple-empty-lines': 1,
    'no-tabs': 1,
    'no-trailing-spaces': 1,
    'no-underscore-dangle': 1,
    'object-curly-spacing': [1, 'always'],
    'quote-props': [1, 'as-needed'],
    quotes: [1, 'single', { avoidEscape: true, allowTemplateLiterals: true }],
    'semi-spacing': 1,
    semi: [1, 'always'],
    'one-var': 1,
    'sort-vars': 0,
    'space-before-blocks': 1,
    'space-before-function-paren': [1, { anonymous: 'always', named: 'never' }],
    'space-in-parens': 1,
    'space-infix-ops': 1,
    'space-unary-ops': 1,
    'spaced-comment': 1,
    'wrap-regex': 1,

    /* Variables */
    'init-declarations': 1,
    'no-catch-shadow': 0,
    'no-label-var': 1,
    'no-restricted-globals': 1,
    'no-shadow': 0,
    'no-shadow-restricted-names': 1,
    'no-undef-init': 1,
    'no-use-before-define': 1,

    /* Node.js and CommonJS */
    'callback-return': 1,
    'handle-callback-err': 1,
    // "no-mixed-requires": 1,
    'no-new-require': 1,
    'no-path-concat': 1,
    // "no-process-env": 1,
    'no-process-exit': 1,
    'no-restricted-modules': 1,
    'no-sync': 1,

    /* ES6 */
    'arrow-body-style': 1,
    'arrow-parens': [1, 'as-needed'],
    'arrow-spacing': 1,
    'no-confusing-arrow': [1, { allowParens: true }],
    'no-useless-computed-key': 1,
    'no-var': 1,
    'object-shorthand': [1, 'properties'],
    'prefer-const': 1,
    'prefer-template': 1,
    'template-curly-spacing': 1
  }
};
<|endoftext|>


<commit_before>#!/usr/bin/env node
process.title = 'pcp'

var argv = require('yargs')
    .usage('Usage: $0 -p [str] file.json')
    .demand(['p', 1])
    .argv;
var transformer = require('postman-collection-transformer');
var pad = require('left-pad');
var jsonfile = require('jsonfile');

var prefix = argv.p;
var collection = require('./' + argv._[0]);

var options = {
    inputVersion: '1.0.0',
    outputVersion: '2.0.0'
};

function addNum(str, num) {
  return prefix + pad(num, 2, 0) + ' - ' + str;
}

function addNumPoint(str, num, point) {
  return prefix + pad(num, 2, 0) + '.' + pad(point, 2, 0) + ' - ' + str;
}

function prefixIt(collection) {
    collection.info.name = prefix + ' - ' + collection.info.name
    console.log(collection.info.name);

    var i = 1;
    for (let item of collection.item) {
        item.name = addNum(item.name, i++);
        console.log(item.name);
        var j = 1;
        if (item.item) {
            for (let item2 of item.item) {
                item2.name = addNumPoint(item2.name, i, j++);
                console.log(item2.name);
            }
        }
    }
}

transformer.convert(collection, options, function (error, result) {
    if (error) {
        return console.error(error);
    }

    prefixIt(result);

    var options = {
        inputVersion: '2.0.0',
        outputVersion: '1.0.0'
    };

    transformer.convert(result, options, function (error2, result2) {
        if (error) {
            return console.error(error);
        }

        jsonfile.writeFile('./' + argv._[0], result2, {spaces: 2}, function (err) {
            if (err) {
                console.error(err)
            }
        });
    });
});
<commit_msg>fix path<commit_after>#!/usr/bin/env node
process.title = 'pcp'

var path = require('path');

var argv = require('yargs')
    .usage('Usage: $0 -p [str] file.json')
    .demand(['p', 1])
    .argv;
var transformer = require('postman-collection-transformer');
var pad = require('left-pad');
var jsonfile = require('jsonfile');

var prefix = argv.p;
var collection = require(path.resolve('.', argv._[0]));

var options = {
    inputVersion: '1.0.0',
    outputVersion: '2.0.0'
};

function addNum(str, num) {
  return prefix + pad(num, 2, 0) + ' - ' + str;
}

function addNumPoint(str, num, point) {
  return prefix + pad(num, 2, 0) + '.' + pad(point, 2, 0) + ' - ' + str;
}

function prefixIt(collection) {
    collection.info.name = prefix + ' - ' + collection.info.name
    console.log(collection.info.name);

    var i = 1;
    for (let item of collection.item) {
        item.name = addNum(item.name, i++);
        console.log(item.name);
        var j = 1;
        if (item.item) {
            for (let item2 of item.item) {
                item2.name = addNumPoint(item2.name, i, j++);
                console.log(item2.name);
            }
        }
    }
}

transformer.convert(collection, options, function (error, result) {
    if (error) {
        return console.error(error);
    }

    prefixIt(result);

    var options = {
        inputVersion: '2.0.0',
        outputVersion: '1.0.0'
    };

    transformer.convert(result, options, function (error2, result2) {
        if (error) {
            return console.error(error);
        }

        jsonfile.writeFile(path.resolve('.', argv._[0]), result2, {spaces: 2}, function (err) {
            if (err) {
                console.error(err)
            }
        });
    });
});
<|endoftext|>


<commit_before>// jshint esnext:true
// Started as a PDF.js example file, originally licensed public domain

const _ = require('highland'),
    dsv = require('d3-dsv'),
    fs = require('fs'),
    vfs = require('vinyl-fs');

global.DOMParser = require('./lib/domparsermock.js').DOMParserMock;

var pdfjs = require('pdfjs-dist');

const filingsPath = `${__dirname}/test/data/`; // make these configurable
    dataPath = `${__dirname}/test/data/`;

let skipHeaders = {}; // hmmm

function getBoldFont(content) {
    let boldFont = '';

    const styleKeys = Object.keys(content.styles);
    styleKeys.forEach(styleKey => {
        if (content.styles[styleKey].fontFamily == 'sans-serif' &&
            content.styles[styleKey].descent < -0.295) { // can we use a better metric than this?
            boldFont = styleKey;
        }
    });

    return boldFont;
}

function saveFiling(file, tables) {
    const fileName = file.basename
        .replace('.pdf', '')
        .replace('.PDF', '');

    return new Promise((resolve, reject) => {
        tables.forEach(table => {
            const csvFile = `${dataPath + table.name.toLowerCase().replace(/[ ,']+/g, '-')}.csv`;

            table.rows.forEach(row => {
                row.file = fileName;
            });

            if (table.cols.length > 0) {
                const columns = ['file'].concat(table.cols.map(col => col.slug));
                let csvString = dsv.csvFormat(table.rows, columns);
                if (skipHeaders[table.name]) {
                    csvString = csvString.substring(csvString.indexOf('\n') + 1);
                } else {
                    skipHeaders[table.name] = true;
                }
                fs.appendFileSync(csvFile, `${csvString}\n`);
            }
        });

        resolve();
    });
}

function processFiling(file) {

    console.log(file.relative);

    let tables = [];
    let ignoreRest = false;

    function processPage(page) {
        const viewport = page.getViewport(1.0); // scale

        return page.getTextContent()
            .then(content => {
                return new Promise((resolve, reject) => {
                    let boldFont = getBoldFont(content);
                    let ignorePage = false;
                    let rowY = 0;

                    content.items.sort((a, b) => {
                        if (a.transform[4] - b.transform[4] !== 0) {
                            return a.transform[4] - b.transform[4];
                        } else {
                            return a.transform[5] - b.transform[5];
                        }
                    }).forEach((item, i) => {
                        if (item.str.includes('Data Revised') ||
                            item.str === 'U.S. Office of Government Ethics Certification') {
                            ignorePage = true;
                        }

                        if (!ignorePage && !ignoreRest) {
                            if (item.height == 14) {
                                if (item.str == 'Filer\'s Information') {
                                    ignorePage = true;
                                } else if (item.str == 'Summary of Contents') {
                                    ignoreRest = true;
                                } else if (item.str.trim().length > 2) {
                                    const headerParts = item.str.split('. ');
                                    let part = null;
                                    let name = null;

                                    if (headerParts.length > 1) {
                                        part = parseInt(headerParts[0]);
                                        name = headerParts.slice(1).join('. ');
                                    } else {
                                        name = item.str;
                                    }

                                    tables.push({
                                        part,
                                        name,
                                        cols: [],
                                        rows: []
                                    });
                                }
                            } else if (item.height == 10) {
                                const curTable = tables[tables.length - 1];
                                if (curTable) {

                                    if (item.fontName == boldFont) {
                                        if (item.str == 'PART' || (item.str == '#' && curTable.cols.length > 1)) {
                                            curTable.cols = [];
                                        }

                                        let append = true;

                                        curTable.cols.forEach(col => {
                                            if (col.x == item.transform[5]) {
                                                col.name += ` ${item.str}`;
                                                col.slug = col.name.toLowerCase().replace(/[ ,]+/g, '-');
                                                append = false;
                                            }
                                        });

                                        if (append) {
                                            curTable.cols.push({
                                                name: item.str,
                                                slug: item.str.toLowerCase().replace(/[ ,]+/g, '-'),
                                                x: item.transform[5],
                                                y: item.transform[4]
                                            });
                                        }
                                    } else {
                                        curTable.cols.forEach((col, i2) => {
                                            if (col.x == item.transform[5]) {
                                                if (i2 === 0 && item.transform[4] - rowY > 12) {
                                                    curTable.rows.push({});
                                                    rowY = item.transform[4];
                                                }
                                                const curRow = curTable.rows[curTable.rows.length - 1];

                                                if (col.slug in curRow) {
                                                    if (col.slug !== '#') {
                                                        curRow[col.slug] += ' ';
                                                    }
                                                    curRow[col.slug] += item.str;
                                                } else {
                                                    curRow[col.slug] = item.str;
                                                }
                                            }
                                        });
                                    }
                                }
                            }
                        }
                    });

                    resolve();
                });

            });
    }

    const data = new Uint8Array(file.contents);

    // parse PDF document
    return new Promise((resolve,reject) => {
        _(pdfjs.getDocument(data))
            // get each page in the document
            .flatMap(doc => {
                let pages = [];

                for (let i = 1; i <= doc.numPages; i++) {
                    pages.push(i);
                }

                return _(pages)
                    .map(page => _(doc.getPage(page)));
            })
            .flatten()
            .map(processPage)
            .done(() => {
                saveFiling(file,tables)
                    .then(resolve,reject);
            });
    });
}

function processFilings(path) {
    // process all PDFs in the directory
    _(vfs.src(path + '**/*.pdf'))
        .map(processFiling)
        .done(() => {
            console.log('done');
        });
}

processFilings(filingsPath);
<commit_msg>Correctly handle PDFs with uppercase file extensions<commit_after>// jshint esnext:true
// Started as a PDF.js example file, originally licensed public domain

const _ = require('highland'),
    dsv = require('d3-dsv'),
    fs = require('fs'),
    vfs = require('vinyl-fs');

global.DOMParser = require('./lib/domparsermock.js').DOMParserMock;

var pdfjs = require('pdfjs-dist');

const filingsPath = `${__dirname}/test/data/`; // make these configurable
    dataPath = `${__dirname}/test/data/`;

let skipHeaders = {}; // hmmm

function getBoldFont(content) {
    let boldFont = '';

    const styleKeys = Object.keys(content.styles);
    styleKeys.forEach(styleKey => {
        if (content.styles[styleKey].fontFamily == 'sans-serif' &&
            content.styles[styleKey].descent < -0.295) { // can we use a better metric than this?
            boldFont = styleKey;
        }
    });

    return boldFont;
}

function saveFiling(file, tables) {
    const fileName = file.basename
        .replace('.pdf', '')
        .replace('.PDF', '');

    return new Promise((resolve, reject) => {
        tables.forEach(table => {
            const csvFile = `${dataPath + table.name.toLowerCase().replace(/[ ,']+/g, '-')}.csv`;

            table.rows.forEach(row => {
                row.file = fileName;
            });

            if (table.cols.length > 0) {
                const columns = ['file'].concat(table.cols.map(col => col.slug));
                let csvString = dsv.csvFormat(table.rows, columns);
                if (skipHeaders[table.name]) {
                    csvString = csvString.substring(csvString.indexOf('\n') + 1);
                } else {
                    skipHeaders[table.name] = true;
                }
                fs.appendFileSync(csvFile, `${csvString}\n`);
            }
        });

        resolve();
    });
}

function processFiling(file) {

    console.log(file.relative);

    let tables = [];
    let ignoreRest = false;

    function processPage(page) {
        const viewport = page.getViewport(1.0); // scale

        return page.getTextContent()
            .then(content => {
                return new Promise((resolve, reject) => {
                    let boldFont = getBoldFont(content);
                    let ignorePage = false;
                    let rowY = 0;

                    content.items.sort((a, b) => {
                        if (a.transform[4] - b.transform[4] !== 0) {
                            return a.transform[4] - b.transform[4];
                        } else {
                            return a.transform[5] - b.transform[5];
                        }
                    }).forEach((item, i) => {
                        if (item.str.includes('Data Revised') ||
                            item.str === 'U.S. Office of Government Ethics Certification') {
                            ignorePage = true;
                        }

                        if (!ignorePage && !ignoreRest) {
                            if (item.height == 14) {
                                if (item.str == 'Filer\'s Information') {
                                    ignorePage = true;
                                } else if (item.str == 'Summary of Contents') {
                                    ignoreRest = true;
                                } else if (item.str.trim().length > 2) {
                                    const headerParts = item.str.split('. ');
                                    let part = null;
                                    let name = null;

                                    if (headerParts.length > 1) {
                                        part = parseInt(headerParts[0]);
                                        name = headerParts.slice(1).join('. ');
                                    } else {
                                        name = item.str;
                                    }

                                    tables.push({
                                        part,
                                        name,
                                        cols: [],
                                        rows: []
                                    });
                                }
                            } else if (item.height == 10) {
                                const curTable = tables[tables.length - 1];
                                if (curTable) {

                                    if (item.fontName == boldFont) {
                                        if (item.str == 'PART' || (item.str == '#' && curTable.cols.length > 1)) {
                                            curTable.cols = [];
                                        }

                                        let append = true;

                                        curTable.cols.forEach(col => {
                                            if (col.x == item.transform[5]) {
                                                col.name += ` ${item.str}`;
                                                col.slug = col.name.toLowerCase().replace(/[ ,]+/g, '-');
                                                append = false;
                                            }
                                        });

                                        if (append) {
                                            curTable.cols.push({
                                                name: item.str,
                                                slug: item.str.toLowerCase().replace(/[ ,]+/g, '-'),
                                                x: item.transform[5],
                                                y: item.transform[4]
                                            });
                                        }
                                    } else {
                                        curTable.cols.forEach((col, i2) => {
                                            if (col.x == item.transform[5]) {
                                                if (i2 === 0 && item.transform[4] - rowY > 12) {
                                                    curTable.rows.push({});
                                                    rowY = item.transform[4];
                                                }
                                                const curRow = curTable.rows[curTable.rows.length - 1];

                                                if (col.slug in curRow) {
                                                    if (col.slug !== '#') {
                                                        curRow[col.slug] += ' ';
                                                    }
                                                    curRow[col.slug] += item.str;
                                                } else {
                                                    curRow[col.slug] = item.str;
                                                }
                                            }
                                        });
                                    }
                                }
                            }
                        }
                    });

                    resolve();
                });

            });
    }

    const data = new Uint8Array(file.contents);

    // parse PDF document
    return new Promise((resolve,reject) => {
        _(pdfjs.getDocument(data))
            // get each page in the document
            .flatMap(doc => {
                let pages = [];

                for (let i = 1; i <= doc.numPages; i++) {
                    pages.push(i);
                }

                return _(pages)
                    .map(page => _(doc.getPage(page)));
            })
            .flatten()
            .map(processPage)
            .done(() => {
                saveFiling(file,tables)
                    .then(resolve,reject);
            });
    });
}

function processFilings(path) {
    // process all PDFs in the directory
    _(vfs.src(path + '**/*.@(pdf|PDF)'))
        .map(processFiling)
        .done(() => {
            console.log('done');
        });
}

processFilings(filingsPath);
<|endoftext|>