-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from YuukiToriyama/feature/use-js-sys/master
JS側のRegExpを使用するをrelease/v0.1.0-beta.4にマージ
- Loading branch information
Showing
4 changed files
with
202 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,101 @@ | ||
use regex::Regex; | ||
|
||
use crate::parser::filter::Filter; | ||
use crate::util::converter::JapaneseNumber; | ||
|
||
pub struct NonKanjiBlockNumberFilter {} | ||
|
||
impl Filter for NonKanjiBlockNumberFilter { | ||
#[cfg(not(target_arch = "wasm32"))] | ||
fn apply(self, input: String) -> String { | ||
filter_with_regex(input) | ||
} | ||
#[cfg(target_arch = "wasm32")] | ||
fn apply(self, input: String) -> String { | ||
let expression = Regex::new(r"\D+(?<block_number>\d+)丁目").unwrap(); | ||
match expression.captures(&input) { | ||
Some(captures) => { | ||
let capture_block_number = &captures.name("block_number").unwrap().as_str(); | ||
let block_number = capture_block_number.parse::<i32>().unwrap(); | ||
input.replacen( | ||
capture_block_number, | ||
block_number.to_japanese_form().unwrap().as_str(), | ||
1, | ||
) | ||
} | ||
None => input, | ||
filter_with_js_sys_regexp(input) | ||
} | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
fn filter_with_regex(input: String) -> String { | ||
let expression = regex::Regex::new(r"\D+(?<block_number>\d+)丁目").unwrap(); | ||
match expression.captures(&input) { | ||
Some(captures) => { | ||
let capture_block_number = &captures.name("block_number").unwrap().as_str(); | ||
let block_number = match capture_block_number.parse::<i32>() { | ||
Ok(x) => x, | ||
Err(_) => return input, | ||
}; | ||
input.replacen( | ||
capture_block_number, | ||
block_number.to_japanese_form().unwrap().as_str(), | ||
1, | ||
) | ||
} | ||
None => input, | ||
} | ||
} | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
fn filter_with_js_sys_regexp(input: String) -> String { | ||
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() { | ||
Some(x) => x, | ||
None => return input, | ||
}; | ||
let block_number = match capture_block_number.parse::<i32>() { | ||
Ok(x) => x, | ||
Err(_) => return input, | ||
}; | ||
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, | ||
} | ||
} | ||
|
||
#[cfg(all(test, not(target_arch = "wasm32")))] | ||
mod tests { | ||
use crate::parser::filter::non_kanji_block_number::filter_with_regex; | ||
|
||
#[test] | ||
fn filter_with_regex_成功() { | ||
let result = filter_with_regex("銀座1丁目".to_string()); | ||
assert_eq!(result, "銀座一丁目"); | ||
} | ||
|
||
#[test] | ||
fn filter_with_regex_失敗() { | ||
let result = filter_with_regex("銀座1丁目".to_string()); | ||
assert_ne!(result, "銀座一丁目"); | ||
} | ||
} | ||
|
||
#[cfg(all(test, target_arch = "wasm32"))] | ||
mod wasm_tests { | ||
use crate::parser::filter::non_kanji_block_number::filter_with_js_sys_regexp; | ||
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure}; | ||
|
||
wasm_bindgen_test_configure!(run_in_browser); | ||
|
||
#[wasm_bindgen_test] | ||
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_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丁目"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters