Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS側のRegExpを使用する: リファクタリング #134

Merged
Merged
29 changes: 17 additions & 12 deletions src/parser/filter/invalid_town_name_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,15 @@ fn extract_town_name_with_regex(input: &str) -> Option<String> {

#[cfg(target_arch = "wasm32")]
fn extract_town_name_with_js_sys_regexp(input: &str) -> Option<String> {
let expression = js_sys::RegExp::new(
r"^(?<town_name>\D+)(?<block_number>\d+)[-ー-]*(?<rest>.*)$",
"",
);
let expression = js_sys::RegExp::new(r"^(\D+)(\d+)[-ー-]*(.*)$", "");
let captures = expression.exec(input)?;
let town_name = match captures.get(1).as_string() {
Some(matched) => matched,
None => return None,
};
let block_number = match captures.get(2).as_string() {
Some(matched) => matched.parse::<i32>().unwrap().to_japanese_form()?,
None => return None,
};
let town_name = captures.get(1).as_string()?;
let block_number = captures
.get(2)
.as_string()?
.parse::<i32>()
.ok()?
.to_japanese_form()?;
let rest = captures
.get(3)
.as_string()
Expand Down Expand Up @@ -104,4 +100,13 @@ mod wasm_tests {
assert!(result.is_some());
assert_eq!(result.unwrap(), "有楽町一丁目1-2");
}

#[wasm_bindgen_test]
fn extract_town_name_with_js_sys_regexp_fail() {
let result = extract_town_name_with_js_sys_regexp("1-1");
assert!(result.is_none());

let result = extract_town_name_with_js_sys_regexp("有楽町");
assert!(result.is_none());
}
}
26 changes: 16 additions & 10 deletions src/parser/filter/non_kanji_block_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn filter_with_regex(input: String) -> String {

#[cfg(target_arch = "wasm32")]
fn filter_with_js_sys_regexp(input: String) -> String {
let expression = js_sys::RegExp::new(r"\D+(?<block_number>\d+)丁目", "");
let expression = js_sys::RegExp::new(r"\D+(\d+)丁目", "");
match expression.exec(&input) {
Some(result) => {
let capture_block_number = match result.get(1).as_string() {
Expand All @@ -47,11 +47,11 @@ fn filter_with_js_sys_regexp(input: String) -> String {
Ok(x) => x,
Err(_) => return input,
};
input.replacen(
&capture_block_number,
block_number.to_japanese_form().unwrap().as_str(),
1,
)
let block_number_in_japanese_form = match block_number.to_japanese_form() {
Some(x) => x,
None => return input,
};
input.replacen(&capture_block_number, &block_number_in_japanese_form, 1)
}
None => input,
}
Expand Down Expand Up @@ -82,14 +82,20 @@ mod wasm_tests {
wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn filter_with_js_sys_regexp_success() {
fn filter_with_js_sys_regexp_input_value_will_be_filtered() {
let result = filter_with_js_sys_regexp("銀座1丁目".to_string());
assert_eq!(result, "銀座一丁目");

let result = filter_with_js_sys_regexp("銀座1丁目1-1".to_string());
assert_eq!(result, "銀座一丁目1-1");
}

#[wasm_bindgen_test]
fn filter_with_js_sys_regexp_fail() {
let result = filter_with_js_sys_regexp("銀座1丁目".to_string());
assert_ne!(result, "銀座一丁目");
fn filter_with_js_sys_regexp_return_original_value() {
let result = filter_with_js_sys_regexp("銀座A丁目".to_string());
assert_eq!(result, "銀座A丁目");

let result = filter_with_js_sys_regexp("銀座2147483648丁目".to_string());
assert_eq!(result, "銀座2147483648丁目");
}
}
Loading