Code coverage report for build/index.js

Statements: 97.56% (40 / 41)      Branches: 75% (9 / 12)      Functions: 100% (5 / 5)      Lines: 100% (38 / 38)      Ignored: none     

All files » build/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101    1   1   1     1   1   1   1   1   1   1   1   1                                         1 1   1 1   1 1 1000     1 1 1 1   1 1000     1000 1000     1000     1000 3224 3224     3224     2224 2224   724       724     1000           1      
'use strict';
 
var _Promise = require('babel-runtime/core-js/promise')['default'];
 
var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default')['default'];
 
Object.defineProperty(exports, '__esModule', {
  value: true
});
exports.findMatches = findMatches;
 
var _rbush = require('rbush');
 
var _rbush2 = _interopRequireDefault(_rbush);
 
var _turfDestination = require('turf-destination');
 
var _turfDestination2 = _interopRequireDefault(_turfDestination);
 
var _turfDistance = require('turf-distance');
 
var _turfDistance2 = _interopRequireDefault(_turfDistance);
 
var _turfPoint = require('turf-point');
 
var _turfPoint2 = _interopRequireDefault(_turfPoint);
 
/**
 * Make a SOAP request to [Commuter Connections](http://www.commuterconnections.org/) to get the number of carpools available for a given starting, ending location, and search radius.
 *
 * @param {Object} opts Options object
 * @returns {Promise} promise
 * @example
 * import {findMatches} from 'commuter-connections'
 * findMatches({
 *   commuters: [{
 *     _id: 1,
 *     coordinates: [-77.4875, 39.0436]
 *   }], {
 *     radius: .5,
 *     units: 'miles'
 * }}).then((matches) => {
 *     console.log(matches) // map of commuter id's to matching commuter id's
 * }, handleError)
 */
 
function findMatches(commuters) {
  var opts = arguments[1] === undefined ? {} : arguments[1];
 
  return new _Promise(function (resolve, reject) {
    Iif (!commuters) return reject('No commuters.');
 
    var tree = (0, _rbush2['default'])();
    tree.load(commuters.map(function (c) {
      return [c.coordinates[0], c.coordinates[1], c.coordinates[0], c.coordinates[1], c];
    }));
 
    var responses = [];
    var RADIUS = opts.radius || 0.25;
    var DIST = RADIUS * Math.sqrt(2);
    var UNITS = opts.units || 'miles';
 
    commuters.forEach(function (commuter) {
      var fromPoint = (0, _turfPoint2['default'])(commuter.coordinates);
 
      // construct bbox
      var bottomLeft = (0, _turfDestination2['default'])(fromPoint, DIST, -135, UNITS);
      var topRight = (0, _turfDestination2['default'])(fromPoint, DIST, 45, UNITS);
 
      // do the initial bbox search
      var results = tree.search(bottomLeft.geometry.coordinates.concat(topRight.geometry.coordinates));
 
      // filter the matches
      var matches = results.reduce(function (matches, result) {
        var match = result[4]._id;
        var matchPoint = (0, _turfPoint2['default'])([result[0], result[1]]);
 
        // ignore self match
        if (match === commuter._id) return matches;
 
        // ignore matches where distance exceeds search radius
        var distance = (0, _turfDistance2['default'])(fromPoint, matchPoint);
        if (distance > RADIUS) return matches;
 
        matches.push({
          _id: match,
          distance: distance
        });
        return matches;
      }, []);
 
      responses.push({
        _id: commuter._id,
        matches: matches
      });
    });
 
    resolve(responses);
  });
}