Skip to content

Commit

Permalink
Merge pull request #566 from YuukiToriyama/feature/refactor-codes/cho…
Browse files Browse the repository at this point in the history
…me-with-arabic-numerals

コードのリファクタ: `chome_with_arabic_numerals.rs`
  • Loading branch information
YuukiToriyama authored Dec 30, 2024
2 parents d6dee29 + fc4f0f0 commit 189d8f4
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions core/src/formatter/chome_with_arabic_numerals.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
use crate::util::converter::JapaneseNumber;

pub(crate) fn format_chome_with_arabic_numerals(target: &str) -> Option<String> {
let chome = if cfg!(target_arch = "wasm32") {
let chome = extract_chome(target)?;
let chome_int = chome.parse::<i8>().ok()?;
Some(target.replacen(&chome, chome_int.to_japanese_form()?.as_str(), 1))
}

fn extract_chome(target: &str) -> Option<String> {
if cfg!(target_arch = "wasm32") {
js_sys::RegExp::new(r"\D+(\d+)丁目", "")
.exec(target)?
.get(1)
.as_string()?
.as_string()
} else {
regex::Regex::new(r"\D+(?<chome>\d+)丁目")
.unwrap()
.captures(target)?
.name("chome")?
.as_str()
.to_string()
};
let chome_int = chome.parse::<i8>().ok()?;
Some(target.replacen(&chome, chome_int.to_japanese_form()?.as_str(), 1))
.name("chome")
.map(|m| m.as_str().to_string())
}
}

#[cfg(all(test, not(target_arch = "wasm32")))]
Expand Down

0 comments on commit 189d8f4

Please sign in to comment.