[Pkg-javascript-commits] [science.js] 72/87: Return correct mode for 1-element arrays.

bhuvan krishna bhuvan-guest at moszumanska.debian.org
Thu Dec 8 06:12:04 UTC 2016


This is an automated email from the git hooks/post-receive script.

bhuvan-guest pushed a commit to branch master
in repository science.js.

commit 67768a39935742c6b010655a076df46b98052147
Author: Jason Davies <jason at jasondavies.com>
Date:   Fri Dec 14 16:34:23 2012 +0000

    Return correct mode for 1-element arrays.
    
    This also modifies the behaviour slightly to return undefined if there
    is no single mode e.g. for [1, 2, 3].
    
    Fixes #7.
---
 science.v1.js           | 5 +++--
 science.v1.min.js       | 2 +-
 src/stats/mode.js       | 5 +++--
 test/stats/mode-test.js | 5 +++--
 4 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/science.v1.js b/science.v1.js
index 7fa24d1..83d4226 100644
--- a/science.v1.js
+++ b/science.v1.js
@@ -1629,12 +1629,13 @@ science.stats.median = function(x) {
   return science.stats.quantiles(x, [.5])[0];
 };
 science.stats.mode = function(x) {
+  if ((n = x.length) === 1) return x[0];
   x = x.slice().sort(science.ascending);
   var mode,
-      n = x.length,
+      n,
       i = -1,
       l = i,
-      last = null,
+      last,
       max = 0,
       tmp,
       v;
diff --git a/science.v1.min.js b/science.v1.min.js
index 71fd801..728ee6b 100644
--- a/science.v1.min.js
+++ b/science.v1.min.js
@@ -1 +1 @@
-(function(a){(function(a){science={version:"1.9.1"},science.ascending=function(a,b){return a-b},science.EULER=.5772156649015329,science.expm1=function(a){return a<1e-5&&a>-0.00001?a+.5*a*a:Math.exp(a)-1},science.functor=function(a){return typeof a=="function"?a:function(){return a}},science.hypot=function(a,b){a=Math.abs(a),b=Math.abs(b);var c,d;a>b?(c=a,d=b):(c=b,d=a);var e=d/c;return c*Math.sqrt(1+e*e)},science.quadratic=function(){function b(b,c,d){var e=c*c-4*b*d;return e>0?(e=Math.s [...]
\ No newline at end of file
+(function(a){(function(a){science={version:"1.9.1"},science.ascending=function(a,b){return a-b},science.EULER=.5772156649015329,science.expm1=function(a){return a<1e-5&&a>-0.00001?a+.5*a*a:Math.exp(a)-1},science.functor=function(a){return typeof a=="function"?a:function(){return a}},science.hypot=function(a,b){a=Math.abs(a),b=Math.abs(b);var c,d;a>b?(c=a,d=b):(c=b,d=a);var e=d/c;return c*Math.sqrt(1+e*e)},science.quadratic=function(){function b(b,c,d){var e=c*c-4*b*d;return e>0?(e=Math.s [...]
\ No newline at end of file
diff --git a/src/stats/mode.js b/src/stats/mode.js
index 0452382..97677f9 100644
--- a/src/stats/mode.js
+++ b/src/stats/mode.js
@@ -1,10 +1,11 @@
 science.stats.mode = function(x) {
+  if ((n = x.length) === 1) return x[0];
   x = x.slice().sort(science.ascending);
   var mode,
-      n = x.length,
+      n,
       i = -1,
       l = i,
-      last = null,
+      last,
       max = 0,
       tmp,
       v;
diff --git a/test/stats/mode-test.js b/test/stats/mode-test.js
index 36c7ed9..366187d 100644
--- a/test/stats/mode-test.js
+++ b/test/stats/mode-test.js
@@ -9,9 +9,10 @@ var suite = vows.describe("science.stats.mode");
 suite.addBatch({
   "mode": {
     "returns correct modes": function() {
-      assert.isTrue(isNaN(science.stats.mode([])));
-      assert.equal(science.stats.mode([1, 2, 3, 4, 5]), null);
       assert.equal(science.stats.mode([1, 2, 2, 3, 4, 7, 9]), 2);
+      assert.equal(science.stats.mode([1]), 1);
+      assert.isUndefined(science.stats.mode([1, 2, 3, 4, 5]));
+      assert.isUndefined(science.stats.mode([]));
     }
   }
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/science.js.git



More information about the Pkg-javascript-commits mailing list