Skip to content

Commit

Permalink
version 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gerrymanoim authored and cran-robot committed Apr 4, 2018
1 parent 56043c7 commit 548192e
Show file tree
Hide file tree
Showing 17 changed files with 351 additions and 26 deletions.
9 changes: 6 additions & 3 deletions DESCRIPTION
@@ -1,5 +1,5 @@
Package: humanize
Version: 0.1.0
Version: 0.2.0
Title: Create Values for Human Consumption
Description: An almost direct port of the 'python' 'humanize' package <https://github.com/jmoiron/humanize>.
This package contains utilities to convert values into human readable forms.
Expand All @@ -11,9 +11,12 @@ RoxygenNote: 6.0.1
Suggests: testthat, purrr
Imports: lubridate, assertthat, glue
License: MIT + file LICENSE
BugReports: https://github.com/newtux/humanize/issues
URL: https://newtux.github.io/humanize/index.html,
https://github.com/newtux/humanize
NeedsCompilation: no
Packaged: 2018-03-14 23:45:17 UTC; g
Packaged: 2018-04-04 01:45:59 UTC; g
Author: Gerry Manoim [aut, cre]
Maintainer: Gerry Manoim <gerrymanoim@gmail.com>
Repository: CRAN
Date/Publication: 2018-03-15 16:08:45 UTC
Date/Publication: 2018-04-04 04:16:58 UTC
27 changes: 16 additions & 11 deletions MD5
@@ -1,18 +1,23 @@
7f28148db3b2982bde661e8f353cb94e *DESCRIPTION
717550edb9a4c28df3f0c0481db17400 *DESCRIPTION
a07c6138c94c34449bf526709f630608 *LICENSE
f01cc332a8e8806b1996d39ac1fde77b *NAMESPACE
5e8bba1890c7cc91888edd9fafe8678e *NEWS.md
a0d1cf9a23854c6f3c34d203e5c42cb6 *R/assertions.R
2f060416aab46f017e62dd49b0a454f2 *NAMESPACE
1ae1e352545673fc4cb7f318243ff76b *NEWS.md
3dda89562df2d985996bec7279620c5a *R/assertions.R
5a995c67567f66368a439baa2357519c *R/filesize.R
d41d8cd98f00b204e9800998ecf8427e *R/number.R
5a8127f07beb4a90eb2a0bc8882c0aef *R/time.R
27512b890cc625beaa9dd8c7f8e96377 *README.md
f7161b6874a41f2d37469dfe0d8722e4 *R/number.R
0e3b3707c1798c9d5e1341706710a570 *R/time.R
0bb0ddfdb3c4805cbfd4064e07a9a34c *README.md
213f6ce850cc2f6a8536881b43151bd4 *man/count_as_ap.Rd
3e41fbc63d067769d7537b1425c80fec *man/count_as_ordinal.Rd
1ff6e6cf6abc07cbf0f0a9fe5d1fbcb8 *man/count_as_word.Rd
38cb81b0a60f348d403356f1d3ca3c35 *man/natural_date.Rd
f756a05ba9c8f4944b9072a835c1f1ae *man/natural_day.Rd
8a3159ee4c4b4e4f9ca44a7dcf16ad6d *man/natural_day.Rd
a214be85f4f44244d44913af0e229a45 *man/natural_size.Rd
22db532b0cf0b840f093a5ff7a79b43a *man/natural_time.Rd
a10d8ae05a6fe0039b6556d129e4bf32 *man/reexports.Rd
b6b60d56e11b9173238e71296afa85f7 *man/number_as_comma.Rd
76701a869ca8d1ddc56b961f3511f76e *man/reexports.Rd
1953704f71af8288a84faebad83b022f *man/seconds_to_natural_delta.Rd
9ecc77040e0736daad22f876c5462224 *tests/testthat.R
c8a10baee8e6bd53751bf6bd5a938dd4 *tests/testthat/test_filesize.R
56d822a384f68bbfbf18d8996d2e1224 *tests/testthat/test_time.R
e2fe90798dbb22df7c9673fe657a15c0 *tests/testthat/test_filesize.R
4c7c3ceba54f496c6afe7c1df237a2f8 *tests/testthat/test_number.R
fe029a07bb8452670ebe25d80d8e1058 *tests/testthat/test_time.R
5 changes: 5 additions & 0 deletions NAMESPACE
@@ -1,11 +1,16 @@
# Generated by roxygen2: do not edit by hand

export(count_as_ap)
export(count_as_ordinal)
export(count_as_word)
export(natural_date)
export(natural_day)
export(natural_size)
export(natural_time)
export(number_as_comma)
export(seconds_to_natural_delta)
importFrom(assertthat,assert_that)
importFrom(assertthat,is.count)
importFrom(assertthat,is.date)
importFrom(lubridate,now)
importFrom(lubridate,today)
6 changes: 6 additions & 0 deletions NEWS.md
@@ -1,3 +1,9 @@
# humanize 0.2.0

* Ported over `number` functions
* Clean up arguments to be consistent across library (format -> fmt)
* Fix bugs with tests

# humanize 0.1.0

* Ported over `time` functions
Expand Down
3 changes: 3 additions & 0 deletions R/assertions.R
Expand Up @@ -3,3 +3,6 @@ assertthat::assert_that

#' @importFrom assertthat is.date
assertthat::is.date

#' @importFrom assertthat is.count
assertthat::is.count
117 changes: 117 additions & 0 deletions R/number.R
@@ -0,0 +1,117 @@

ordinals <- list(
"0" = "th",
"1" = "st",
"2" = "nd",
"3" = "rd",
"4" = "th",
"5" = "th",
"6" = "th",
"7" = "th",
"8" = "th",
"9" = "th"
)

#' Transform a count to an ordinal string
#'
#' @param value A single positive integer
#'
#' @return A string with the ordinal representation of a number
#' @export
#'
#' @examples
#' count_as_ordinal(1)
#' count_as_ordinal(111)
count_as_ordinal <- function(value) {
assert_that(is.count(value))

if (value %% 100 %in% c(11, 12, 13)) {
return(paste0(value, ordinals[["0"]]))
}
paste0(value, ordinals[[as.character(value %% 10)]])
}

#' Convert an number to a string with comma separation
#'
#' Just a wrapper around `format` with defaults for full digits
#'
#' @param value A numeric
#'
#' @return A string with comma separation every three digits
#' @export
#'
#' @examples
#' number_as_comma(1000)
#' number_as_comma(10000)
number_as_comma <- function(value) {
assert_that(is.numeric(value))
format(value, big.mark = ",", scientific = FALSE, digits = 22)
}

human_powers <- 10^c(6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 100)
names(human_powers) <- c(
'million','billion','trillion','quadrillion',
'quintillion','sextillion','septillion',
'octillion','nonillion','decillion','googol'
)

#' Convert Large Counts into Friendly Text
#'
#' Note - currently limited to `.Machine$integer.max`.
#'
#' @param value A single positive integer
#' @param fmt Extra number formatting supplied to sprintf
#'
#' @return Returns a string with the power of a number replaced by the appropriate word.
#' @export
#'
#' @examples
#' count_as_word(100)
#' count_as_word(1000000)
#' count_as_word(1200000000)
count_as_word <- function(value, fmt='%.1f') {
assert_that(is.count(value))

if (value < human_powers[1]) {
return(format(value, scientific = FALSE))
}
# Drop the first value, but shift our index
for (i in seq_along(human_powers[-1]) + 1) {
if (value < human_powers[[i]]) {
chopped <- value / human_powers[[i - 1]]
human_name <- names(human_powers[i - 1])
return(paste(sprintf(fmt, chopped), human_name))
}
}
# gigantic values
# R does not seem to have percision here. TODO?
return(format(value, scientific = FALSE))
}


ap_numbers <- c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")


#' Convert to AP Number
#'
#' @param value A single positive integer
#'
#' @return For numbers 1-9, returns the number spelled out. Otherwise, returns
#' the number as a string.
#' @export
#'
#' @examples
#' count_as_ap(3)
#' count_as_ap(20)
count_as_ap <- function(value) {
assert_that(is.count(value))
if (value >= 10) {
return(format(value, scientific = FALSE))
}

ap_numbers[value]
}

fractional <- function(value) {

}
8 changes: 4 additions & 4 deletions R/time.R
Expand Up @@ -123,18 +123,18 @@ natural_time <- function(value, use_months=TRUE) {
#'
#' For date values that are tomorrow, today or yesterday compared to present day
#' returns representing string. Otherwise, returns a string formatted according
#' to `format`
#' to `fmt`
#'
#' @param value A date value
#' @param format Optional formatting string for dates not yesterday, today, tomorrow
#' @param fmt Optional formatting string for dates not yesterday, today, tomorrow
#'
#' @return A nicely formatted date
#' @export
#'
#' @examples
#' natural_day(Sys.Date())
#' natural_day(Sys.Date()-10)
natural_day <- function(value, format='%b %d') {
natural_day <- function(value, fmt='%b %d') {
assert_that(is.date(value))
delta <- value - today()

Expand All @@ -145,7 +145,7 @@ natural_day <- function(value, format='%b %d') {
} else if (delta == -1) {
out <- 'yesterday'
} else {
out <- format(value, format)
out <- format(value, fmt)
}

out
Expand Down
55 changes: 52 additions & 3 deletions README.md
Expand Up @@ -3,15 +3,21 @@
humanize
========

[![Travis-CI Build Status](https://travis-ci.org/newtux/humanize.svg?branch=master)](https://travis-ci.org/newtux/humanize)
[![Travis-CI Build Status](https://travis-ci.org/newtux/humanize.svg?branch=master)](https://travis-ci.org/newtux/humanize) [![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/humanize)](https://cran.r-project.org/package=humanize)

Humanize is an almost direct port of the python [humanize package](https://github.com/jmoiron/humanize).

The goal of humanize is to provide some utlities in order to turn values (so far times and file sizes) into human readable forms
The goal of humanize is to provide some utlities in order to turn values (so far times, file sizes, and numbers) into human readable forms.

Installation
------------

You can install the latest CRAN version with:

``` r
install.packages("humanize")
```

You can install humanize from github with:

``` r
Expand All @@ -22,6 +28,8 @@ devtools::install_github("newtux/humanize")
Examples
--------

### Times

Convert times:

``` r
Expand Down Expand Up @@ -53,6 +61,8 @@ natural_time(Sys.time() + lubridate::ddays(1))
#> [1] "23 hours from now"
```

### File Sizes

Convert file sizes:

``` r
Expand All @@ -70,6 +80,46 @@ natural_size(10**26 * 30)
#> 3000.0 YB
```

### Numbers

Ordinals:

``` r
count_as_ordinal(1)
#> [1] "1st"
count_as_ordinal(111)
#> [1] "111th"
```

Comma Seperation:

``` r
number_as_comma(1000)
#> [1] "1,000"
number_as_comma(10000)
#> [1] "10,000"
```

Words:

``` r
count_as_word(100)
#> [1] "100"
count_as_word(1000000)
#> [1] "1.0 million"
count_as_word(1200000000)
#> [1] "1.2 billion"
```

AP Format:

``` r
count_as_ap(3)
#> [1] "three"
count_as_ap(20)
#> [1] "20"
```

Todo
----

Expand All @@ -78,4 +128,3 @@ This is still a very early cut of the package.
- Better support in times? For diff time?
- Maybe add times relative to other times?
- Export helper functions used in tests?
- Port over the numbers code
22 changes: 22 additions & 0 deletions man/count_as_ap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions man/count_as_ordinal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 548192e

Please sign in to comment.