|
1 | 1 | //! Some custom filters for templates.
|
2 | 2 |
|
| 3 | +use std::sync::LazyLock; |
| 4 | + |
3 | 5 | use chrono::{DateTime, NaiveDate, Utc};
|
| 6 | +use human_format::{Formatter, Scales}; |
4 | 7 | use tracing::error;
|
5 | 8 |
|
| 9 | +/// Salary formatter. |
| 10 | +static SALARY_FORMATTER: LazyLock<Formatter> = LazyLock::new(|| { |
| 11 | + let mut scales = Scales::new(); |
| 12 | + scales |
| 13 | + .with_base(1000) |
| 14 | + .with_suffixes(vec!["", "K", "M", "B", "T", "P", "E", "Z", "Y"]); |
| 15 | + |
| 16 | + let mut formatter = Formatter::new(); |
| 17 | + formatter.with_scales(scales).with_decimals(0).with_separator(""); |
| 18 | + |
| 19 | + formatter |
| 20 | +}); |
| 21 | + |
6 | 22 | /// Return the value if it is some, otherwise return an empty string.
|
7 | 23 | #[allow(clippy::unnecessary_wraps, clippy::ref_option)]
|
8 | 24 | pub(crate) fn display_some<T>(value: &Option<T>) -> askama::Result<String>
|
@@ -76,18 +92,15 @@ where
|
76 | 92 | }
|
77 | 93 | }
|
78 | 94 |
|
79 |
| -/// Return the number in humanized format. |
| 95 | +/// Return the salary amount in humanized format. |
80 | 96 | #[allow(
|
81 | 97 | clippy::unnecessary_wraps,
|
82 | 98 | clippy::ref_option,
|
83 | 99 | clippy::trivially_copy_pass_by_ref,
|
84 | 100 | clippy::cast_precision_loss
|
85 | 101 | )]
|
86 |
| -pub(crate) fn humanize_number(value: &i64) -> askama::Result<String> { |
87 |
| - Ok(human_format::Formatter::new() |
88 |
| - .with_decimals(0) |
89 |
| - .with_separator("") |
90 |
| - .format(*value as f64)) |
| 102 | +pub(crate) fn humanize_salary(amount: &i64) -> askama::Result<String> { |
| 103 | + Ok(SALARY_FORMATTER.format(*amount as f64)) |
91 | 104 | }
|
92 | 105 |
|
93 | 106 | /// Filter to convert markdown to html.
|
|
0 commit comments