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

release/v0.1.0 beta.4をmainブランチにマージ #137

Merged
merged 36 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6d0741a
update: JS側のRegExpを使用する: `js-sys`の依存関係を追加
YuukiToriyama Jan 13, 2024
cffc2f7
update: JS側のRegExpを使用する: `regex::Regex`をuse文を使わないで直接使うように修正
YuukiToriyama Jan 13, 2024
7114ecc
update: JS側のRegExpを使用する: `NonKanjiBlockNumberFilter.apply()`の中身を関数に切り出した
YuukiToriyama Jan 13, 2024
e94ce40
update: JS側のRegExpを使用する: `filter_with_js_sys_regexp()`を定義
YuukiToriyama Jan 13, 2024
5a4883e
update: JS側のRegExpを使用する: ターゲットが`wasm32`の場合は`filter_with_js_sys_regexp…
YuukiToriyama Jan 13, 2024
2723ebf
update: JS側のRegExpを使用する: `filter_with_regex()`に対してテストコードを追加
YuukiToriyama Jan 13, 2024
a3c65a6
update: JS側のRegExpを使用する: `filter_with_js_sys_regexp()`に対してテストコードを追加
YuukiToriyama Jan 13, 2024
3d0c763
update: JS側のRegExpを使用する: `filter_with_regex()`で文字列をIntに変換する処理にエラーハンドリ…
YuukiToriyama Jan 13, 2024
7161427
update: JS側のRegExpを使用する: `filter_with_js_sys_regexp()`内の処理のエラーハンドリングを…
YuukiToriyama Jan 13, 2024
31bcd0b
Merge pull request #130 from YuukiToriyama/feature/use-js-sys/non_kan…
YuukiToriyama Jan 13, 2024
9bad04a
update: JS側のRegExpを使用する: `regex::Regex`をuse文を使わないで直接使うように修正
YuukiToriyama Jan 13, 2024
17c1e0a
rename: JS側のRegExpを使用する: `extract_town_name()` -> `extract_town_name_…
YuukiToriyama Jan 13, 2024
0f817a0
rename: JS側のRegExpを使用する: `invalid_town_name_format_test` -> `tests`
YuukiToriyama Jan 13, 2024
0066765
update: JS側のRegExpを使用する: `extract_town_name_with_js_sys_regexp()`を定義
YuukiToriyama Jan 13, 2024
76c5d60
update: JS側のRegExpを使用する: ターゲットが`wasm32`の場合は`extract_town_with_js_sys_…
YuukiToriyama Jan 13, 2024
614c381
update: JS側のRegExpを使用する: InvalidTownNameFormatFilterではなくextract_town…
YuukiToriyama Jan 13, 2024
7df85ee
update: JS側のRegExpを使用する: `wasm_tests`を追加
YuukiToriyama Jan 13, 2024
cbc6db1
Merge pull request #131 from YuukiToriyama/feature/use-js-sys/invalid…
YuukiToriyama Jan 13, 2024
b7f3596
rename: JS側のRegExpを使用する: `read_house_number()` -> `read_house_number_…
YuukiToriyama Jan 13, 2024
dbf76b1
rename: JS側のRegExpを使用する: `read_house_number_tests` -> `tests`
YuukiToriyama Jan 13, 2024
3b66817
update: JS側のRegExpを使用する: `read_house_number_with_regex()`をprivateに変更
YuukiToriyama Jan 13, 2024
50eea0c
update: JS側のRegExpを使用する: `read_house_number_with_js_sys_regexp()`を定義
YuukiToriyama Jan 13, 2024
a7fecb0
update: JS側のRegExpを使用する: ターゲット設定を追加
YuukiToriyama Jan 13, 2024
e0d860b
update: JS側のRegExpを使用する: `wasm_tests`を追加
YuukiToriyama Jan 13, 2024
e572e73
package.versionを変更
YuukiToriyama Jan 13, 2024
cf42113
Merge pull request #132 from YuukiToriyama/feature/use-js-sys/read_ho…
YuukiToriyama Jan 13, 2024
8d4c69a
update: JS側のRegExpを使用する: `extract_town_name_with_js_sys_regexp()`がNon…
YuukiToriyama Jan 20, 2024
fed4226
update: JS側のRegExpを使用する: `unwrap()`を`ok()?`に書き換えた
YuukiToriyama Jan 20, 2024
f2cd308
update: JS側のRegExpを使用する: match式を`?`で書き換えた
YuukiToriyama Jan 20, 2024
aaf362f
update: JS側のRegExpを使用する: 正規表現を簡略化
YuukiToriyama Jan 20, 2024
ec3cd3b
update: JS側のRegExpを使用する: `filter_with_js_sys_regexp()`に対するテスト関数の命名を修正
YuukiToriyama Jan 20, 2024
60e1534
update: JS側のRegExpを使用する: `filter_with_js_sys_regexp()`に対するテストにケースを追加
YuukiToriyama Jan 20, 2024
7fbb8f2
update: JS側のRegExpを使用する: `to_japanese_form()`の呼び出し箇所にエラーハンドリングを追加
YuukiToriyama Jan 20, 2024
0d24d81
update: JS側のRegExpを使用する: 正規表現を簡略化
YuukiToriyama Jan 20, 2024
6eacce3
Merge pull request #134 from YuukiToriyama/feature/use-js-sys/refacto…
YuukiToriyama Jan 20, 2024
62187c2
Merge pull request #135 from YuukiToriyama/feature/use-js-sys/master
YuukiToriyama Jan 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "japanese-address-parser"
version = "0.1.0-beta.3"
version = "0.1.0-beta.4"
edition = "2021"
description = "A Rust Library to parse japanses addresses."
authors = ["Yuuki Toriyama <github@toriyama.dev>"]
Expand All @@ -23,6 +23,7 @@ debug = []
[dependencies]
console_error_panic_hook = "0.1.7"
itertools = "0.12.0"
js-sys = "0.3.67"
nom = "7.1.3"
regex = "1.10.2"
reqwest = { version = "0.11.23", default-features = false, features = ["json", "rustls-tls", "blocking"] }
Expand Down
89 changes: 70 additions & 19 deletions src/parser/filter/invalid_town_name_format.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
use crate::parser::filter::Filter;
use crate::util::converter::JapaneseNumber;
use regex::Regex;

pub struct InvalidTownNameFormatFilter {}

impl Filter for InvalidTownNameFormatFilter {
#[cfg(not(target_arch = "wasm32"))]
fn apply(self, input: String) -> String {
extract_town_name(&input).unwrap_or(input)
extract_town_name_with_regex(&input).unwrap_or(input)
}
#[cfg(target_arch = "wasm32")]
fn apply(self, input: String) -> String {
extract_town_name_with_js_sys_regexp(&input).unwrap_or(input)
}
}

fn extract_town_name(input: &str) -> Option<String> {
#[cfg(not(target_arch = "wasm32"))]
fn extract_town_name_with_regex(input: &str) -> Option<String> {
let expression =
Regex::new(r"^(?<town_name>\D+)(?<block_number>\d+)[-ー-]*(?<rest>.*)$").unwrap();
regex::Regex::new(r"^(?<town_name>\D+)(?<block_number>\d+)[-ー-]*(?<rest>.*)$").unwrap();
let captures = expression.captures(input)?;
let town_name = if let Some(matched) = captures.name("town_name") {
matched.as_str()
Expand All @@ -36,26 +41,72 @@ fn extract_town_name(input: &str) -> Option<String> {
Some(format!("{}{}丁目{}", town_name, block_number, rest))
}

#[cfg(test)]
mod invalid_town_name_format_test {
use crate::parser::filter::invalid_town_name_format::InvalidTownNameFormatFilter;
use crate::parser::filter::Filter;
#[cfg(target_arch = "wasm32")]
fn extract_town_name_with_js_sys_regexp(input: &str) -> Option<String> {
let expression = js_sys::RegExp::new(r"^(\D+)(\d+)[-ー-]*(.*)$", "");
let captures = expression.exec(input)?;
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()
.unwrap_or_else(|| "".to_string());
Some(format!("{}{}丁目{}", town_name, block_number, rest))
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
use crate::parser::filter::invalid_town_name_format::extract_town_name_with_regex;

#[test]
fn 有楽町一丁目() {
let result = InvalidTownNameFormatFilter {}.apply("有楽町1".to_string());
assert_eq!(result, "有楽町一丁目")
fn extract_town_name_with_regex_success() {
let result = extract_town_name_with_regex("有楽町1");
assert!(result.is_some());
assert_eq!(result.unwrap(), "有楽町一丁目");

let result = extract_town_name_with_regex("有楽町1-1");
assert!(result.is_some());
assert_eq!(result.unwrap(), "有楽町一丁目1");

let result = extract_town_name_with_regex("有楽町1-1-2");
assert!(result.is_some());
assert_eq!(result.unwrap(), "有楽町一丁目1-2");
}
}

#[test]
fn 有楽町一丁目1番() {
let result = InvalidTownNameFormatFilter {}.apply("有楽町1-1".to_string());
assert_eq!(result, "有楽町一丁目1")
#[cfg(all(test, target_arch = "wasm32"))]
mod wasm_tests {
use crate::parser::filter::invalid_town_name_format::extract_town_name_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 extract_town_name_with_js_sys_regexp_success() {
let result = extract_town_name_with_js_sys_regexp("有楽町1");
assert!(result.is_some());
assert_eq!(result.unwrap(), "有楽町一丁目");

let result = extract_town_name_with_js_sys_regexp("有楽町1-1");
assert!(result.is_some());
assert_eq!(result.unwrap(), "有楽町一丁目1");

let result = extract_town_name_with_js_sys_regexp("有楽町1-1-2");
assert!(result.is_some());
assert_eq!(result.unwrap(), "有楽町一丁目1-2");
}

#[test]
fn 有楽町一丁目1番2() {
let result = InvalidTownNameFormatFilter {}.apply("有楽町1-1-2".to_string());
assert_eq!(result, "有楽町一丁目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());
}
}
105 changes: 91 additions & 14 deletions src/parser/filter/non_kanji_block_number.rs
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丁目");
}
}
49 changes: 40 additions & 9 deletions src/parser/read_house_number.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use regex::Regex;

#[allow(dead_code)]
pub fn read_house_number(input: &str) -> Option<(String, String)> {
let expression = Regex::new(r"(?<house_number>\d+)\D*(?<rest>.*)$").unwrap();
#[cfg(not(target_arch = "wasm32"))]
fn read_house_number_with_regex(input: &str) -> Option<(String, String)> {
let expression = regex::Regex::new(r"(?<house_number>\d+)\D*(?<rest>.*)$").unwrap();
let captures = expression.captures(input)?;
let house_number = if let Some(matched) = captures.name("house_number") {
matched.as_str()
Expand All @@ -17,20 +16,52 @@ pub fn read_house_number(input: &str) -> Option<(String, String)> {
Some((rest.to_string(), format!("{}番", house_number)))
}

#[cfg(test)]
mod read_house_number_tests {
use crate::parser::read_house_number::read_house_number;
#[allow(dead_code)]
#[cfg(target_arch = "wasm32")]
fn read_house_number_with_js_sys_regexp(input: &str) -> Option<(String, String)> {
let expression = js_sys::RegExp::new(r"(?<house_number>\d+)\D*(?<rest>.*)$", "");
let captures = expression.exec(input)?;
let house_number = captures.get(1).as_string()?;
let rest = captures
.get(2)
.as_string()
.unwrap_or_else(|| "".to_string());
Some((rest, format!("{}番", house_number)))
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
use crate::parser::read_house_number::read_house_number_with_regex;

#[test]
fn read_house_number_1番() {
let (rest, house_number) = read_house_number("1").unwrap();
let (rest, house_number) = read_house_number_with_regex("1").unwrap();
assert_eq!(house_number, "1番");
assert_eq!(rest, "");
}

#[test]
fn read_house_number_3番2() {
let (rest, house_number) = read_house_number("3-2").unwrap();
let (rest, house_number) = read_house_number_with_regex("3-2").unwrap();
assert_eq!(house_number, "3番");
assert_eq!(rest, "2");
}
}

#[cfg(all(test, target_arch = "wasm32"))]
mod wasm_tests {
use crate::parser::read_house_number::read_house_number_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 read_house_number_with_js_sys_regexp_success() {
let (rest, house_number) = read_house_number_with_js_sys_regexp("1").unwrap();
assert_eq!(house_number, "1番");
assert_eq!(rest, "");

let (rest, house_number) = read_house_number_with_js_sys_regexp("3-2").unwrap();
assert_eq!(house_number, "3番");
assert_eq!(rest, "2");
}
Expand Down
Loading