Example #1
0
 return function(units, destinations, onePerDestination) {
     var distancesByUnit = {},
         destinationsByUnit = {},
         minDistanceByUnit,
         dis2des,
         destinationsLeft,
         unitsLeft;
     if (!units || !destinations) {
         return {};
     }
     if (onePerDestination === undefined) {
         onePerDestination = true;
     }
     destinationsLeft = destinations.length;
     unitsLeft = units.length;
     if (destinationsLeft === 0 || unitsLeft === 0) {
         return {};
     }
     //Get all distances
     _.each(units, function(u) {
         distancesByUnit[u.id] = [];
         _.each(destinations, function(d) {
             distancesByUnit[u.id].push({
                 destination: d,
                 unit: u,
                 distance: getDistance(u, d)
             });
         });
     });
     minDistanceByUnit = getMinDistanceByUnit(distancesByUnit);
     while (unitsLeft > 0 &&
             (!onePerDestination || destinationsLeft > 0)) {
         dis2des = _.min(minDistanceByUnit, 'distance');
         destinationsByUnit[dis2des.unit.id] = dis2des.destination;
         delete distancesByUnit[dis2des.unit.id];
         unitsLeft--;
         if (onePerDestination) {
             _.each(distancesByUnit,
                 removeDestination(dis2des.destination));
             destinationsLeft--;
         }
         minDistanceByUnit = getMinDistanceByUnit(distancesByUnit);
     }
     return destinationsByUnit;
 };
Example #2
0
			work: function() {

				winston.info('manager:composeResponse');

				// Update the expiration date on the bundle
				var tout = {
					expires: _.min( thisResponse, function( val ) { return val.expires } ).expires,
					lastModified: now
				};

				if (_.has( thisResponse, 'redirect')) {
					tout.redirect = thisResponse.redirect,
					tout.guid = thisResponse.guid,
					tout.authBundle = thisResponse.authBundle,
					tout.authPart = thisResponse.authPart
				};

				// Insert api responses into bundle
				_.each( thisResponse, function( val, idx ) {
					tout[val.cname] = val;
				});

				// Perform cleanup function on bundle
			  	if (_.has(bundle, 'cleanup')) {
				  	tout = bundle.cleanup(tout);
				}

				// Determine the seconds left before expiry
				if ( 'expires' in tout && _.isDate(tout.expires) ) {
					tout.secleft = tout.expires.getSecondsBetween( now ) * -1;
				} else {
					tout.secleft = 3600;
				}

        // Save cached bundle in Redis
        client.set('bid'+bid, JSON.stringify(tout));

				manager.enqueue('sendResponse', tout);
				this.finished = true;

			}
Example #3
0
// invoke: 在list里的每个元素上调用名为methodName的函数,任何附加的函数传入,invoke将会转给调用的函数
var v = _.invoke([[2,1,3], [4,5,2]], 'sort')
console.log("v", v);

// pluck: 提取一个集合里指定的属性值
var users = [
    {name:'moe', age: 40},
    {name:'larry', age: 50}
];
console.log(_.pluck(users, 'name'));
// max, min, sortBy:取list中的最大,最小元素,自定义比较器
console.log(_.max(users, function(stooge){
    return stooge.age
}));
console.log(_.min(users, function(stooge){
    return stooge.age //{ name: 'moe', age: 40 }
}));
console.log(_.sortBy([3,4,1,2,6], function(num){
    return Math.max(num);//[ 1, 2, 3, 4, 6 ]
}));

// groupBy 一个集合分成多个集合
console.log(_.groupBy(['one', 'two', 'three'], 'length'));//{ '3': [ 'one', 'two' ], '5': [ 'three' ] }
// size 得到list中元素个数
console.log(_.size({one:1, two:2, three: 3})); //3
// shuffle:随机打乱一个数据
console.log(_.shuffle([1,2,3,4,5,6,7]));
// countBy 把一个数据分组后计数
console.log(_.countBy([1,2,3,4,5], function(num){
    return num % 2 == 0 ? 'even':'odd'; //{ odd: 3, even: 2 }
}));
Example #4
0
Helpers.prototype.getRange = function(array) {
	return _.max(array) - _.min(array);
}
Example #5
0
 _.each(distancesByUnit, function(dis2desArray, unitID) {
     minDistanceByUnit[unitID] = _.min(dis2desArray, 'distance');
 });