1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! A Rust library for formatting ordinal numbers.
//!
//! # Example
//!
//! ```
//!
//! fn main() {
//!     println!("{}", ordnl::of_u8(0u8)); // "0th"
//!     println!("{}", ordnl::of_u8(1u8)); // "1st"
//!     println!("{}", ordnl::of_u8(2u8)); // "2nd"
//!     println!("{}", ordnl::of_u8(3u8)); // "3rd"
//!     println!("{}", ordnl::of_u8(4u8)); // "4th"
//!
//!     println!("{}", ordnl::of_u8(0u8)); // "0th"
//!     println!("{}", ordnl::of_u16(1u16)); // "1st"
//!     println!("{}", ordnl::of_u32(2u32)); // "2nd"
//!     println!("{}", ordnl::of_u64(3u64)); // "3rd"
//!     println!("{}", ordnl::of_u128(4u128)); // "4th"
//! }

/// Ordinalizes a `u8` integer.
///
/// ```
/// let first = ordnl::of_u8(1u8);
/// let second = ordnl::of_u8(2u8);
/// let third = ordnl::of_u8(3u8);
/// let fourth = ordnl::of_u8(4u8);
///
/// assert_eq!(first, "1st");
/// assert_eq!(second, "2nd");
/// assert_eq!(third, "3rd");
/// assert_eq!(fourth, "4th");
///
/// ```
pub fn of_u8(n: u8) -> String {
    match (n % 10, (n % 100) < 11 || (n % 100) > 13) {
        (1, true) => format!("{}st", n),
        (2, true) => format!("{}nd", n),
        (3, true) => format!("{}rd", n),
        _ => format!("{}th", n),
    }
}

/// Ordinalizes a `u16` integer.
///
/// ```
/// let first = ordnl::of_u16(1u16);
/// let second = ordnl::of_u16(2u16);
/// let third = ordnl::of_u16(3u16);
/// let fourth = ordnl::of_u16(4u16);
///
/// assert_eq!(first, "1st");
/// assert_eq!(second, "2nd");
/// assert_eq!(third, "3rd");
/// assert_eq!(fourth, "4th");
///
/// ```
pub fn of_u16(n: u16) -> String {
    match (n % 10, (n % 100) < 11 || (n % 100) > 13) {
        (1, true) => format!("{}st", n),
        (2, true) => format!("{}nd", n),
        (3, true) => format!("{}rd", n),
        _ => format!("{}th", n),
    }
}

/// Ordinalizes a `u32` integer.
///
/// ```
/// let first = ordnl::of_u32(1u32);
/// let second = ordnl::of_u32(2u32);
/// let third = ordnl::of_u32(3u32);
/// let fourth = ordnl::of_u32(4u32);
///
/// assert_eq!(first, "1st");
/// assert_eq!(second, "2nd");
/// assert_eq!(third, "3rd");
/// assert_eq!(fourth, "4th");
///
/// ```
pub fn of_u32(n: u32) -> String {
    match (n % 10, (n % 100) < 11 || (n % 100) > 13) {
        (1, true) => format!("{}st", n),
        (2, true) => format!("{}nd", n),
        (3, true) => format!("{}rd", n),
        _ => format!("{}th", n),
    }
}

/// Ordinalizes a `u64` integer.
///
/// ```
/// let first = ordnl::of_u64(1u64);
/// let second = ordnl::of_u64(2u64);
/// let third = ordnl::of_u64(3u64);
/// let fourth = ordnl::of_u64(4u64);
///
/// assert_eq!(first, "1st");
/// assert_eq!(second, "2nd");
/// assert_eq!(third, "3rd");
/// assert_eq!(fourth, "4th");
///
/// ```
pub fn of_u64(n: u64) -> String {
    match (n % 10, (n % 100) < 11 || (n % 100) > 13) {
        (1, true) => format!("{}st", n),
        (2, true) => format!("{}nd", n),
        (3, true) => format!("{}rd", n),
        _ => format!("{}th", n),
    }
}

/// Ordinalizes a `u128` integer.
///
/// ```
/// let first = ordnl::of_u128(1u128);
/// let second = ordnl::of_u128(2u128);
/// let third = ordnl::of_u128(3u128);
/// let fourth = ordnl::of_u128(4u128);
///
/// assert_eq!(first, "1st");
/// assert_eq!(second, "2nd");
/// assert_eq!(third, "3rd");
/// assert_eq!(fourth, "4th");
///
/// ```
pub fn of_u128(n: u128) -> String {
    match (n % 10, (n % 100) < 11 || (n % 100) > 13) {
        (1, true) => format!("{}st", n),
        (2, true) => format!("{}nd", n),
        (3, true) => format!("{}rd", n),
        _ => format!("{}th", n),
    }
}