From f63533dd9ca49fb01fc9ede5a24ea2c6402267c3 Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Thu, 17 Sep 2015 16:29:07 -0400 Subject: [PATCH] Switch to res.data instead of req.results.data --- middleware/confidenceScoreReverse.js | 58 ++++--- .../unit/middleware/confidenceScoreReverse.js | 160 ++++++++---------- 2 files changed, 103 insertions(+), 115 deletions(-) diff --git a/middleware/confidenceScoreReverse.js b/middleware/confidenceScoreReverse.js index 384f1aa1..462ea9d9 100644 --- a/middleware/confidenceScoreReverse.js +++ b/middleware/confidenceScoreReverse.js @@ -3,19 +3,23 @@ var _ = require('lodash'); // these are subjective terms, but wanted to add shortcuts to denote something // about importance -var EXACT = 1.0; -var EXCELLENT = 0.9; -var GOOD = 0.8; -var OKAY = 0.7; -var POOR = 0.6; -var NONE = 0.5; -var INVALID = 0.0; +var SCORES = { + EXACT: 1.0, + EXCELLENT: 0.9, + GOOD: 0.8, + OKAY: 0.7, + POOR: 0.6, + NONE: 0.5, + INVALID: 0.0 +}; -var _1_METER = 1; -var _10_METERS = 10; -var _100_METERS = 100; -var _250_METERS = 250; -var _1_KILOMETER = 1000; +var BUCKETS = { + _1_METER: 1, + _10_METERS: 10, + _100_METERS: 100, + _250_METERS: 250, + _1_KILOMETER: 1000 +}; function setup() { return computeScores; @@ -23,38 +27,38 @@ function setup() { function computeScores(req, res, next) { // do nothing if no result data set - if (!req.results || !req.results.data) { + if (!res.data || !res.data) { return next(); } // loop through data items and determine confidence scores - req.results.data = req.results.data.map(computeConfidenceScore.bind(null, req)); + res.data = res.data.map(computeConfidenceScore); next(); } -function computeConfidenceScore(req, hit) { +function computeConfidenceScore(hit) { // non-number or invalid distance should be given confidence 0.0 if (!_.isFinite(hit.distance) || hit.distance < 0) { - hit.confidence = INVALID; + hit.confidence = SCORES.INVALID; return hit; } var distance = hit.distance * 1000.0; // figure out which range the distance lies in and assign confidence accordingly - if (distance < _1_METER) { - hit.confidence = EXACT; - } else if (distance < _10_METERS) { - hit.confidence = EXCELLENT; - } else if (distance < _100_METERS) { - hit.confidence = GOOD; - } else if (distance < _250_METERS) { - hit.confidence = OKAY; - } else if (distance < _1_KILOMETER) { - hit.confidence = POOR; + if (distance < BUCKETS._1_METER) { + hit.confidence = SCORES.EXACT; + } else if (distance < BUCKETS._10_METERS) { + hit.confidence = SCORES.EXCELLENT; + } else if (distance < BUCKETS._100_METERS) { + hit.confidence = SCORES.GOOD; + } else if (distance < BUCKETS._250_METERS) { + hit.confidence = SCORES.OKAY; + } else if (distance < BUCKETS._1_KILOMETER) { + hit.confidence = SCORES.POOR; } else { - hit.confidence = NONE; + hit.confidence = SCORES.NONE; } return hit; diff --git a/test/unit/middleware/confidenceScoreReverse.js b/test/unit/middleware/confidenceScoreReverse.js index bad7d93b..450fd0db 100644 --- a/test/unit/middleware/confidenceScoreReverse.js +++ b/test/unit/middleware/confidenceScoreReverse.js @@ -3,11 +3,11 @@ var confidenceScoreReverse = require('../../../middleware/confidenceScoreReverse module.exports.tests = {}; module.exports.tests.confidenceScoreReverse = function(test, common) { - test('req without results should not throw exception', function(t) { - var req = {}; + test('res without results should not throw exception', function(t) { + var res = {}; try { - confidenceScoreReverse(req, null, function() {}); + confidenceScoreReverse(null, res, function() {}); } catch (e) { t.fail('an exception should not have been thrown'); @@ -18,13 +18,13 @@ module.exports.tests.confidenceScoreReverse = function(test, common) { }); - test('req.results without data should not throw exception', function(t) { - var req = { + test('res.results without data should not throw exception', function(t) { + var res = { results: {} }; try { - confidenceScoreReverse(req, null, function() {}); + confidenceScoreReverse(null, res, function() {}); } catch (e) { t.fail('an exception should not have been thrown'); @@ -36,146 +36,130 @@ module.exports.tests.confidenceScoreReverse = function(test, common) { }); test('0m <= distance < 1m should be given score 1.0', function(t) { - var req = { - results: { - data: [ - { distance: 0.0000 / 1000.0 }, - { distance: 0.9999 / 1000.0 } - ] - } + var res = { + data: [ + { distance: 0.0000 / 1000.0 }, + { distance: 0.9999 / 1000.0 } + ] }; - confidenceScoreReverse(req, null, function() { - t.equal(req.results.data[0].confidence, 1.0, 'score should be exact confidence'); - t.equal(req.results.data[1].confidence, 1.0, 'score should be exact confidence'); + confidenceScoreReverse(null, res, function() { + t.equal(res.data[0].confidence, 1.0, 'score should be exact confidence'); + t.equal(res.data[1].confidence, 1.0, 'score should be exact confidence'); t.end(); }); }); test('1m <= distance < 10m should be given score 0.9', function(t) { - var req = { - results: { - data: [ - { distance: 1.0000 / 1000.0 }, - { distance: 9.9999 / 1000.0 } - ] - } + var res = { + data: [ + { distance: 1.0000 / 1000.0 }, + { distance: 9.9999 / 1000.0 } + ] }; - confidenceScoreReverse(req, null, function() { - t.equal(req.results.data[0].confidence, 0.9, 'score should be excellent confidence'); - t.equal(req.results.data[1].confidence, 0.9, 'score should be excellent confidence'); + confidenceScoreReverse(null, res, function() { + t.equal(res.data[0].confidence, 0.9, 'score should be excellent confidence'); + t.equal(res.data[1].confidence, 0.9, 'score should be excellent confidence'); t.end(); }); }); test('10m <= distance < 100m should be given score 0.8', function(t) { - var req = { - results: { - data: [ - { distance: 10.0000 / 1000.0 }, - { distance: 99.9999 / 1000.0 } - ] - } + var res = { + data: [ + { distance: 10.0000 / 1000.0 }, + { distance: 99.9999 / 1000.0 } + ] }; - confidenceScoreReverse(req, null, function() { - t.equal(req.results.data[0].confidence, 0.8, 'score should be good confidence'); - t.equal(req.results.data[1].confidence, 0.8, 'score should be good confidence'); + confidenceScoreReverse(null, res, function() { + t.equal(res.data[0].confidence, 0.8, 'score should be good confidence'); + t.equal(res.data[1].confidence, 0.8, 'score should be good confidence'); t.end(); }); }); test('100m <= distance < 250m should be given score 0.7', function(t) { - var req = { - results: { - data: [ - { distance: 100.0000 / 1000.0 }, - { distance: 249.9999 / 1000.0 } - ] - } + var res = { + data: [ + { distance: 100.0000 / 1000.0 }, + { distance: 249.9999 / 1000.0 } + ] }; - confidenceScoreReverse(req, null, function() { - t.equal(req.results.data[0].confidence, 0.7, 'score should be okay confidence'); - t.equal(req.results.data[1].confidence, 0.7, 'score should be okay confidence'); + confidenceScoreReverse(null, res, function() { + t.equal(res.data[0].confidence, 0.7, 'score should be okay confidence'); + t.equal(res.data[1].confidence, 0.7, 'score should be okay confidence'); t.end(); }); }); test('250m <= distance < 1000m should be given score 0.6', function(t) { - var req = { - results: { - data: [ - { distance: 250.0000 / 1000.0 }, - { distance: 999.9999 / 1000.0 } - ] - } + var res = { + data: [ + {distance: 250.0000 / 1000.0}, + {distance: 999.9999 / 1000.0} + ] }; - confidenceScoreReverse(req, null, function() { - t.equal(req.results.data[0].confidence, 0.6, 'score should be poor confidence'); - t.equal(req.results.data[1].confidence, 0.6, 'score should be poor confidence'); + confidenceScoreReverse(null, res, function() { + t.equal(res.data[0].confidence, 0.6, 'score should be poor confidence'); + t.equal(res.data[1].confidence, 0.6, 'score should be poor confidence'); t.end(); }); }); test('distance >= 1000m should be given score 0.5', function(t) { - var req = { - results: { - data: [ - { distance: 1000.0 / 1000.0 }, - { distance: 2000.0 / 1000.0 } - ] - } + var res = { + data: [ + {distance: 1000.0 / 1000.0}, + {distance: 2000.0 / 1000.0} + ] }; - confidenceScoreReverse(req, null, function() { - t.equal(req.results.data[0].confidence, 0.5, 'score should be least confidence'); - t.equal(req.results.data[1].confidence, 0.5, 'score should be least confidence'); + confidenceScoreReverse(null, res, function() { + t.equal(res.data[0].confidence, 0.5, 'score should be least confidence'); + t.equal(res.data[1].confidence, 0.5, 'score should be least confidence'); t.end(); }); }); test('distance < 0 (invalid) should be given score 0.0', function(t) { - var req = { - results: { - data: [ - { distance: -1.0000 / 1000.0 } - ] - } + var res = { + data: [ + { distance: -1.0000 / 1000.0 } + ] }; - confidenceScoreReverse(req, null, function() { - t.equal(req.results.data[0].confidence, 0.0, 'score should be 0.0 confidence'); + confidenceScoreReverse(null, res, function() { + t.equal(res.data[0].confidence, 0.0, 'score should be 0.0 confidence'); t.end(); }); }); test('non-number-type (invalid) distance should be given score 0.0', function(t) { - var req = { - results: { - data: [ - {}, - { distance: [] }, - { distance: {} }, - { distance: 'this is not a number' } - ] - } + var res = { + data: [ + {}, + {distance: []}, + {distance: {}}, + {distance: 'this is not a number'} + ] }; - confidenceScoreReverse(req, null, function() { - t.equal(req.results.data[0].confidence, 0.0, 'score should be 0.0 confidence'); - t.equal(req.results.data[1].confidence, 0.0, 'score should be 0.0 confidence'); - t.equal(req.results.data[2].confidence, 0.0, 'score should be 0.0 confidence'); - t.equal(req.results.data[3].confidence, 0.0, 'score should be 0.0 confidence'); + confidenceScoreReverse(null, res, function() { + t.equal(res.data[0].confidence, 0.0, 'score should be 0.0 confidence'); + t.equal(res.data[1].confidence, 0.0, 'score should be 0.0 confidence'); + t.equal(res.data[2].confidence, 0.0, 'score should be 0.0 confidence'); + t.equal(res.data[3].confidence, 0.0, 'score should be 0.0 confidence'); t.end(); });