use std::sync::LazyLock;
use js_sys::Intl;
use perspective_js::json;
use perspective_js::utils::global::navigator;
use wasm_bindgen::JsValue;
struct UnsafeNumberFormat(Intl::NumberFormat);
unsafe impl Send for UnsafeNumberFormat {}
unsafe impl Sync for UnsafeNumberFormat {}
static NUMBER_FORMAT: LazyLock<UnsafeNumberFormat> = LazyLock::new(|| {
let locale = navigator().languages();
let opts = json!({});
let number_format = Intl::NumberFormat::new(&locale, &opts);
UnsafeNumberFormat(number_format)
});
impl UnsafeNumberFormat {}
#[extend::ext]
pub impl u32 {
fn to_formatted_string(&self) -> String {
NUMBER_FORMAT
.0
.format()
.call1(&NUMBER_FORMAT.0, &JsValue::from_f64(*self as f64))
.unwrap()
.as_string()
.unwrap()
}
}