diff --git a/README.md b/README.md index f68cdfb..ed138c9 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,9 @@ var result = percentile( console.log(result); // 8 ``` +## Notes + +Non-numeric (NaN) values are treated as the smallest values, Eg `percentile(50, [ 5, 2, NaN]) === 2` ## Author diff --git a/lib/index.js b/lib/index.js index a283f9f..dce505d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -61,6 +61,9 @@ function percentile(p, list, fn) { b = fn(b); } + a = Number.isNaN(a) ? Number.NEGATIVE_INFINITY : a; + b = Number.isNaN(b) ? Number.NEGATIVE_INFINITY : b; + if (a > b) return 1; if (a < b) return -1; diff --git a/test/index.js b/test/index.js index f19fdf2..de471f1 100644 --- a/test/index.js +++ b/test/index.js @@ -24,6 +24,8 @@ const stubsSimple = [ { percentile: 75, list: shuffleArray(generateArraySimple(100)), result: 75 }, { percentile: 100, list: shuffleArray(generateArraySimple(100)), result: 100 }, + { percentile: 75, list: [NaN, NaN, 1, 100], result: 1 }, + { percentile: 75, list: [1, 100, NaN, NaN], result: 1 }, { percentile: 75, list: shuffleArray([].concat(generateArraySimple(100), generateArraySimple(30))), result: 68 } ];