Skip to content

Commit

Permalink
Merge pull request #1 from ali77gh/abbreviate-options
Browse files Browse the repository at this point in the history
feat: separator option for abbreviate
  • Loading branch information
alirezasariri78 authored Apr 21, 2024
2 parents 7561e63 + 5bfa1f0 commit a4a9fad
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 97 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Program Will Automaticly Remove Leading Zero (001 Will Become 1 and etc)
```[tasklist]
- [ ] Optimize Code
- [ ] Create Custom Symbols
- [ ] Create Padding For Symbols
- [x] Custom separator pass by option
- [ ] Create Unit Conversion
```

Expand Down
10 changes: 6 additions & 4 deletions src/calculation.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::AbbreviationOptions;

const SYMBOLS: [&str; 21] = [
"q", "r", "y", "z", "a", "f", "p", "n", "μ", "m", "", "K", "M", "G", "T", "P", "E", "Z", "Y",
"R", "Q",
];
const FRACTION_COUNT: usize = 10;

pub fn abbreviate_number(num: f64) -> String {
pub fn abbreviate_number(num: f64, options: &AbbreviationOptions) -> String {
let base: f64 = 1000.0;
let mut number = num;
let max_legal_index = SYMBOLS.len() - 1;
Expand All @@ -15,7 +17,7 @@ pub fn abbreviate_number(num: f64) -> String {
let symbol = *SYMBOLS.get(index_of_symbol).unwrap();
let pw: f64 = base.powf((index_of_symbol - FRACTION_COUNT) as f64);
number /= pw;
format!("{}{}", remove_floating_zero(number), symbol)
format!("{}{}{}", remove_floating_zero(number), options.separator, symbol)
}

pub fn unabbreviate_number(number: &str) -> f64 {
Expand Down Expand Up @@ -50,7 +52,7 @@ pub fn unabbreviate_number(number: &str) -> f64 {
}
}

pub fn abbreviate_fraction_number(num: f64) -> String {
pub fn abbreviate_fraction_number(num: f64, options: &AbbreviationOptions) -> String {
let mut number = num.abs();
let mut ten_th_counter = 0;
const THOUSAND: f64 = 1000.0;
Expand All @@ -67,7 +69,7 @@ pub fn abbreviate_fraction_number(num: f64) -> String {
let symbol = *SYMBOLS.get(symbol_index as usize).unwrap_or(&"");
let result = (num * THOUSAND.powf(thousand_raise)) as i128;

format!("{result}{symbol}")
format!("{}{}{}", result, options.separator, symbol)
}

fn remove_floating_zero(number: f64) -> String {
Expand Down
74 changes: 42 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ pub trait NumericAbbreviate {
/// ```
/// use rs_abbreviation_number::*;
/// let number=110_000;
/// let result=number.abbreviate_number();
/// let result=number.abbreviate_number(&Default::default());
/// assert_eq!("110K",result);
///
/// let number=0.000_1;
/// let result=number.abbreviate_number();
/// let result=number.abbreviate_number(&Default::default());
/// assert_eq!("100μ",result);
/// ```
fn abbreviate_number(&self) -> String;
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String;
}

pub trait NumericUnAbbreviate {
Expand All @@ -35,80 +35,80 @@ pub trait NumericUnAbbreviate {
}

impl NumericAbbreviate for f64 {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self,options)
}
}

impl NumericAbbreviate for f32 {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self as f64)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self as f64,options)
}
}

impl NumericAbbreviate for i128 {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self as f64)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self as f64,options)
}
}

impl NumericAbbreviate for i64 {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self as f64)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self as f64,options)
}
}

impl NumericAbbreviate for i32 {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self as f64)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self as f64,options)
}
}

impl NumericAbbreviate for i16 {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self as f64)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self as f64,options)
}
}

impl NumericAbbreviate for i8 {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self as f64)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self as f64,options)
}
}

impl NumericAbbreviate for usize {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self as f64)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self as f64,options)
}
}

impl NumericAbbreviate for u128 {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self as f64)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self as f64,options)
}
}

impl NumericAbbreviate for u64 {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self as f64)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self as f64,options)
}
}

impl NumericAbbreviate for u32 {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self as f64)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self as f64,options)
}
}

impl NumericAbbreviate for u16 {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self as f64)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self as f64,options)
}
}

impl NumericAbbreviate for u8 {
fn abbreviate_number(&self) -> String {
handle_abbreviation(*self as f64)
fn abbreviate_number(&self, options: &AbbreviationOptions) -> String {
handle_abbreviation(*self as f64,options)
}
}

Expand All @@ -124,10 +124,20 @@ impl NumericUnAbbreviate for &str {
}
}

fn handle_abbreviation(number: f64) -> String {
fn handle_abbreviation(number: f64, options: &AbbreviationOptions) -> String {
if number.abs() >= 1.0 || number == 0.0 {
abbreviate_number(number)
abbreviate_number(number,options)
} else {
abbreviate_fraction_number(number)
abbreviate_fraction_number(number,options)
}
}

pub struct AbbreviationOptions{
pub separator: String
}

impl Default for AbbreviationOptions {
fn default() -> Self {
Self { separator: "".to_string() }
}
}
110 changes: 58 additions & 52 deletions tests/abbreviate_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,88 @@ mod abbreviate_tests {

#[test]
fn mix_floating_test() {
assert_eq!("1.43K", 1435.549.abbreviate_number());
assert_eq!("1.43K", 1435.549.abbreviate_number(&Default::default()));
}

#[test]
fn mix_negative_floating_test() {
assert_eq!("-1.43K", (-1435.549).abbreviate_number());
assert_eq!("-1.43K", (-1435.549).abbreviate_number(&Default::default()));
}

#[test]
fn abbreviate_number_less_then_thousand_test() {
assert_eq!("1", 1.abbreviate_number());
assert_eq!("0", 0.abbreviate_number());
assert_eq!("123", 123.0.abbreviate_number());
assert_eq!("1", 1.abbreviate_number(&Default::default()));
assert_eq!("0", 0.abbreviate_number(&Default::default()));
assert_eq!("123", 123.0.abbreviate_number(&Default::default()));
}

#[test]
fn abbreviate_number_ceil_test() {
assert_eq!("1K", 1_000.0.abbreviate_number());
assert_eq!("1M", 1_000_000.0.abbreviate_number());
assert_eq!("16G", 16_000_000_000.0.abbreviate_number());
assert_eq!("1T", (1_000_000_000_000.0).abbreviate_number());
assert_eq!("54P", (54_000_000_000_000_000.0).abbreviate_number());
assert_eq!("123E", 123_000_000_000_000_000_000.0.abbreviate_number());
assert_eq!("1K", 1_000.0.abbreviate_number(&Default::default()));
assert_eq!("1M", 1_000_000.0.abbreviate_number(&Default::default()));
assert_eq!("16G", 16_000_000_000.0.abbreviate_number(&Default::default()));
assert_eq!("1T", (1_000_000_000_000.0).abbreviate_number(&Default::default()));
assert_eq!("54P", (54_000_000_000_000_000.0).abbreviate_number(&Default::default()));
assert_eq!("123E", 123_000_000_000_000_000_000.0.abbreviate_number(&Default::default()));
}

#[test]
fn abbreviate_number_randoms_test() {
assert_eq!("5K", 5_000.0.abbreviate_number());
assert_eq!("12M", 12_000_430.0.abbreviate_number());
assert_eq!("1G", 1_000_345_000.0.abbreviate_number());
assert_eq!("133T", 133_000_000_055_000.0.abbreviate_number());
assert_eq!("166P", 166_000_643_300_000_000.0.abbreviate_number());
assert_eq!("999E", 999_000_123_123_000_000_123.0.abbreviate_number());
assert_eq!("5K", 5_000.0.abbreviate_number(&Default::default()));
assert_eq!("12M", 12_000_430.0.abbreviate_number(&Default::default()));
assert_eq!("1G", 1_000_345_000.0.abbreviate_number(&Default::default()));
assert_eq!("133T", 133_000_000_055_000.0.abbreviate_number(&Default::default()));
assert_eq!("166P", 166_000_643_300_000_000.0.abbreviate_number(&Default::default()));
assert_eq!("999E", 999_000_123_123_000_000_123.0.abbreviate_number(&Default::default()));
}

#[test]
fn abbreviate_number_randoms_floating_test() {
assert_eq!("5.1K", 5_100.0.abbreviate_number());
assert_eq!("5.09K", 5_090.0.abbreviate_number());
assert_eq!("12.01M", 12_010_430.0.abbreviate_number());
assert_eq!("1.1G", 1_100_345_000.0.abbreviate_number());
assert_eq!("133.09T", 133_090_000_055_000.0.abbreviate_number());
assert_eq!("166.05P", 166_050_643_300_000_000.0.abbreviate_number());
assert_eq!("999.5E", 999_500_123_123_000_000_123.0.abbreviate_number());
assert_eq!("5.1K", 5_100.0.abbreviate_number(&Default::default()));
assert_eq!("5.09K", 5_090.0.abbreviate_number(&Default::default()));
assert_eq!("12.01M", 12_010_430.0.abbreviate_number(&Default::default()));
assert_eq!("1.1G", 1_100_345_000.0.abbreviate_number(&Default::default()));
assert_eq!("133.09T", 133_090_000_055_000.0.abbreviate_number(&Default::default()));
assert_eq!("166.05P", 166_050_643_300_000_000.0.abbreviate_number(&Default::default()));
assert_eq!("999.5E", 999_500_123_123_000_000_123.0.abbreviate_number(&Default::default()));
}

#[test]
fn abbreviate_number_negative_test() {
assert_eq!("-5.1K", (-5_100.0).abbreviate_number());
assert_eq!("-5.09K", (-5_090.0).abbreviate_number());
assert_eq!("-12.01M", (-12_010_430.0).abbreviate_number());
assert_eq!("-1.1G", (-1_100_345_000.0).abbreviate_number());
assert_eq!("-133.09T", (-133_090_000_055_000.0).abbreviate_number());
assert_eq!("-166.05P", (-166_050_643_300_000_000.0).abbreviate_number());
assert_eq!("-5.1K", (-5_100.0).abbreviate_number(&Default::default()));
assert_eq!("-5.09K", (-5_090.0).abbreviate_number(&Default::default()));
assert_eq!("-12.01M", (-12_010_430.0).abbreviate_number(&Default::default()));
assert_eq!("-1.1G", (-1_100_345_000.0).abbreviate_number(&Default::default()));
assert_eq!("-133.09T", (-133_090_000_055_000.0).abbreviate_number(&Default::default()));
assert_eq!("-166.05P", (-166_050_643_300_000_000.0).abbreviate_number(&Default::default()));
assert_eq!(
"-999.5E",
(-999_500_123_123_000_000_123.0).abbreviate_number()
(-999_500_123_123_000_000_123.0).abbreviate_number(&Default::default())
);
}

#[test]
fn abbreviate_number_big_negative_number_test() {
assert_eq!(
"-999.5Q",
(-999_509_999_999_999_000_123_123_000_000_123.0).abbreviate_number()
(-999_509_999_999_999_000_123_123_000_000_123.0).abbreviate_number(&Default::default())
);

let big_num: i128 = -999_509_999_999_999_000_123_123_000_000_123;
assert_eq!("-999.5Q", big_num.abbreviate_number());
assert_eq!("-999.5Q", big_num.abbreviate_number(&Default::default()));
}

#[test]
fn abbreviate_number_big_number_test() {
let big_num: i128 = 999_509_999_999_999_000_123_123_000_000_123;

assert_eq!("999.5Q", big_num.abbreviate_number());
assert_eq!("999.5Q", big_num.abbreviate_number(&Default::default()));
}

#[test]
fn abbreviate_number_with_option_test() {
assert_eq!("1-K", 1_000.0.abbreviate_number(&AbbreviationOptions { separator: "-".to_string() }));
assert_eq!("1 - K", 1_000.0.abbreviate_number(&AbbreviationOptions { separator: " - ".to_string() }));
}
}

Expand All @@ -89,30 +95,30 @@ mod integer_abbreviate_tests {
use rs_abbreviation_number::*;
#[test]
fn abbreviate_inumber_less_then_thousand_test() {
assert_eq!("1", 1.abbreviate_number());
assert_eq!("0", 0.abbreviate_number());
assert_eq!("123", 123.abbreviate_number());
assert_eq!("1", 1.abbreviate_number(&Default::default()));
assert_eq!("0", 0.abbreviate_number(&Default::default()));
assert_eq!("123", 123.abbreviate_number(&Default::default()));
}

#[test]
fn abbreviate_number_ceil_test() {
assert_eq!("1K", 1_000.abbreviate_number());
assert_eq!("1M", 1_000_000.abbreviate_number());
assert_eq!("1K", 1_000.abbreviate_number(&Default::default()));
assert_eq!("1M", 1_000_000.abbreviate_number(&Default::default()));
}

#[test]
fn abbreviate_number_randoms_test() {
assert_eq!("5K", 5_000.abbreviate_number());
assert_eq!("12M", 12_000_430.abbreviate_number());
assert_eq!("1G", 1_000_345_000.abbreviate_number());
assert_eq!("5K", 5_000.abbreviate_number(&Default::default()));
assert_eq!("12M", 12_000_430.abbreviate_number(&Default::default()));
assert_eq!("1G", 1_000_345_000.abbreviate_number(&Default::default()));
}

#[test]
fn abbreviate_number_negative_test() {
assert_eq!("-5.1K", (-5_100).abbreviate_number());
assert_eq!("-5.09K", (-5_090).abbreviate_number());
assert_eq!("-12.01M", (-12_010_430).abbreviate_number());
assert_eq!("-1.1G", (-1_100_345_000).abbreviate_number());
assert_eq!("-5.1K", (-5_100).abbreviate_number(&Default::default()));
assert_eq!("-5.09K", (-5_090).abbreviate_number(&Default::default()));
assert_eq!("-12.01M", (-12_010_430).abbreviate_number(&Default::default()));
assert_eq!("-1.1G", (-1_100_345_000).abbreviate_number(&Default::default()));
}
}

Expand All @@ -122,21 +128,21 @@ mod utype_tests {
#[test]
fn abbreviate_number_randoms_test() {
let num128: u128 = 999_509_999_999_999_000_123_123_000_000_123;
assert_eq!("999.5Q", num128.abbreviate_number());
assert_eq!("999.5Q", num128.abbreviate_number(&Default::default()));

let num64: u64 = 999_509_999_999_999_000;
assert_eq!("999.5P", num64.abbreviate_number());
assert_eq!("999.5P", num64.abbreviate_number(&Default::default()));

let num32: u32 = 999_509_999;
assert_eq!("999.5M", num32.abbreviate_number());
assert_eq!("999.5M", num32.abbreviate_number(&Default::default()));

let num16: u16 = 9999;
assert_eq!("9.99K", num16.abbreviate_number());
assert_eq!("9.99K", num16.abbreviate_number(&Default::default()));

let num8: u8 = 99;
assert_eq!("99", num8.abbreviate_number());
assert_eq!("99", num8.abbreviate_number(&Default::default()));

let num: usize = 10_000_000_000_000_000_000;
assert_eq!("10E", num.abbreviate_number());
assert_eq!("10E", num.abbreviate_number(&Default::default()));
}
}
Loading

0 comments on commit a4a9fad

Please sign in to comment.