[MediaWiki-commits] [Gerrit] wikimedia...polloi[master]: Fix a bug in function 'compress' when number is between 0 and 1

2017-08-08 Thread Bearloga (Code Review)
Bearloga has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/370756 )

Change subject: Fix a bug in function 'compress' when number is between 0 and 1
..


Fix a bug in function 'compress' when number is between 0 and 1

Change-Id: I25ef7d1332d0bacafbd07d7ee64c6c22e3dd7bbd
---
M R/maths.R
M tests/testthat/test-maths.R
2 files changed, 3 insertions(+), 2 deletions(-)

Approvals:
  Bearloga: Verified; Looks good to me, approved



diff --git a/R/maths.R b/R/maths.R
index aab5b7f..5cfd017 100644
--- a/R/maths.R
+++ b/R/maths.R
@@ -22,5 +22,6 @@
 #' @export
 compress <- function(x, round_by = 2) {
   div <- findInterval(x, c(1, 1e3, 1e6, 1e9, 1e12))
-  return(paste0(round( x / 10 ^ (3 * (div - 1)), round_by), c("", "", "K", 
"M", "B", "T")[div + 1]))
+  return(paste0(round( x / 10 ^ (3 * ifelse(div - 1 < 0, 0, div - 1)), 
round_by),
+c("", "", "K", "M", "B", "T")[div + 1]))
 }
diff --git a/tests/testthat/test-maths.R b/tests/testthat/test-maths.R
index 9a84d57..2455e10 100644
--- a/tests/testthat/test-maths.R
+++ b/tests/testthat/test-maths.R
@@ -7,7 +7,7 @@
 })
 
 test_that("suffixes", {
-  expect_equal(compress(c(0, 1, 10, 100)), c("0", "1", "10", "100"))
+  expect_equal(compress(c(0, 0.5, 1, 10, 100)), c("0", "0.5", "1", "10", 
"100"))
   expect_equal(compress(1.642e3, round_by = 1), "1.6K")
   expect_equal(compress(c(10, 1e6, 1e12, 1e9)), c("100K", "1M", "1T", 
"1B"))
   expect_equal(compress(c(0, 1, 1e6, 1e3)), c("0", "1", "1M", "1K"))

-- 
To view, visit https://gerrit.wikimedia.org/r/370756
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I25ef7d1332d0bacafbd07d7ee64c6c22e3dd7bbd
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/discovery/polloi
Gerrit-Branch: master
Gerrit-Owner: Chelsyx 
Gerrit-Reviewer: Bearloga 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits


[MediaWiki-commits] [Gerrit] wikimedia...polloi[master]: Fix a bug in function 'compress' when number is between 0 and 1

2017-08-08 Thread Chelsyx (Code Review)
Chelsyx has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/370756 )

Change subject: Fix a bug in function 'compress' when number is between 0 and 1
..

Fix a bug in function 'compress' when number is between 0 and 1

Change-Id: I25ef7d1332d0bacafbd07d7ee64c6c22e3dd7bbd
---
M R/maths.R
M tests/testthat/test-maths.R
2 files changed, 3 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/discovery/polloi 
refs/changes/56/370756/1

diff --git a/R/maths.R b/R/maths.R
index aab5b7f..5cfd017 100644
--- a/R/maths.R
+++ b/R/maths.R
@@ -22,5 +22,6 @@
 #' @export
 compress <- function(x, round_by = 2) {
   div <- findInterval(x, c(1, 1e3, 1e6, 1e9, 1e12))
-  return(paste0(round( x / 10 ^ (3 * (div - 1)), round_by), c("", "", "K", 
"M", "B", "T")[div + 1]))
+  return(paste0(round( x / 10 ^ (3 * ifelse(div - 1 < 0, 0, div - 1)), 
round_by),
+c("", "", "K", "M", "B", "T")[div + 1]))
 }
diff --git a/tests/testthat/test-maths.R b/tests/testthat/test-maths.R
index 9a84d57..2455e10 100644
--- a/tests/testthat/test-maths.R
+++ b/tests/testthat/test-maths.R
@@ -7,7 +7,7 @@
 })
 
 test_that("suffixes", {
-  expect_equal(compress(c(0, 1, 10, 100)), c("0", "1", "10", "100"))
+  expect_equal(compress(c(0, 0.5, 1, 10, 100)), c("0", "0.5", "1", "10", 
"100"))
   expect_equal(compress(1.642e3, round_by = 1), "1.6K")
   expect_equal(compress(c(10, 1e6, 1e12, 1e9)), c("100K", "1M", "1T", 
"1B"))
   expect_equal(compress(c(0, 1, 1e6, 1e3)), c("0", "1", "1M", "1K"))

-- 
To view, visit https://gerrit.wikimedia.org/r/370756
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I25ef7d1332d0bacafbd07d7ee64c6c22e3dd7bbd
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/discovery/polloi
Gerrit-Branch: master
Gerrit-Owner: Chelsyx 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits