-
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 #382 from YuukiToriyama/feature/format-house-numbe…
…r/master 住居番号の正規化オプションをrelease/v0.1.11にマージ
- Loading branch information
Showing
14 changed files
with
298 additions
and
93 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod house_number; |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#[cfg(not(target_arch = "wasm32"))] | ||
pub(crate) fn format_house_number(input: &str) -> Result<String, &'static str> { | ||
let captures = regex::Regex::new(r"(?<block_number>\d+)\D+(?<house_number>\d+)(?<rest>.*)$") | ||
.unwrap() | ||
.captures(input) | ||
.ok_or("マッチするものがありませんでした")?; | ||
let block_number = captures | ||
.name("block_number") | ||
.ok_or("街区符号を検出できませんでした")?; | ||
let house_number = captures | ||
.name("house_number") | ||
.ok_or("住居番号を検出できませんでした")?; | ||
let rest = match captures.name("rest") { | ||
Some(matched) => matched.as_str(), | ||
None => "", | ||
}; | ||
Ok(format!( | ||
"{}番{}号{}", | ||
block_number.as_str(), | ||
house_number.as_str(), | ||
rest | ||
)) | ||
} | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
pub(crate) fn format_house_number(input: &str) -> Result<String, &'static str> { | ||
let captures = js_sys::RegExp::new( | ||
r"(?<block_number>\d+)\D+(?<house_number>\d+)(?<rest>.*)$", | ||
"", | ||
) | ||
.exec(input) | ||
.ok_or("マッチするものがありませんでした")?; | ||
let block_number = captures | ||
.get(1) | ||
.as_string() | ||
.ok_or("街区符号を検出できませんでした")?; | ||
let house_number = captures | ||
.get(2) | ||
.as_string() | ||
.ok_or("住居番号を検出できませんでした")?; | ||
let rest = captures | ||
.get(3) | ||
.as_string() | ||
.unwrap_or_else(|| "".to_string()); | ||
Ok(format!("{}番{}号{}", block_number, house_number, rest)) | ||
} | ||
|
||
#[cfg(all(test, not(target_arch = "wasm32")))] | ||
mod tests { | ||
use crate::formatter::house_number::format_house_number; | ||
|
||
#[test] | ||
fn format_house_number_1番1号() { | ||
let result = format_house_number("1-1"); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), "1番1号"); | ||
} | ||
|
||
#[test] | ||
fn format_house_number_3番2号レジデンシャルマンション101号室() { | ||
let result = format_house_number("3-2レジデンシャルマンション101号室"); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), "3番2号レジデンシャルマンション101号室"); | ||
} | ||
} | ||
|
||
#[cfg(all(test, target_arch = "wasm32"))] | ||
mod wasm_tests { | ||
use crate::formatter::house_number::format_house_number; | ||
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure}; | ||
|
||
wasm_bindgen_test_configure!(run_in_browser); | ||
|
||
#[wasm_bindgen_test] | ||
fn format_house_number_success() { | ||
let result = format_house_number("1-1"); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), "1番1号"); | ||
|
||
let result = format_house_number("3-2レジデンシャルマンション101号室"); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), "3番2号レジデンシャルマンション101号室"); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<!DOCTYPE html> | ||
<html lang="ja"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Demo | japanese-address-parser</title> | ||
<link rel="stylesheet" href="./style.css" type="text/css"> | ||
</head> | ||
<body> | ||
<div class="ribbon"> | ||
<span class="ribbon-label">Debug</span> | ||
</div> | ||
<h2>YuukiToriyama/japanese-address-parser</h2> | ||
<p>Rust製の住所パーサーです</p> | ||
|
||
<h3>住所を入力してください</h3> | ||
<div class="input"> | ||
<input class="address" id="input" type="text" placeholder="例) 東京都中央区日本橋一丁目1-1"/> | ||
<button class="button" id="exec">パースを実行</button> | ||
</div> | ||
|
||
<h3>処理結果</h3> | ||
<table class="output"> | ||
<thead> | ||
<tr> | ||
<th>入力値</th> | ||
<th>ステータス</th> | ||
<th>address.prefecture</th> | ||
<th>address.city</th> | ||
<th>address.town</th> | ||
<th>address.rest</th> | ||
<th>JSON</th> | ||
</tr> | ||
</thead> | ||
<tbody id="result"> | ||
<tr> | ||
<td><p>東京都中央区日本橋一丁目1-1</p></td> | ||
<td><p>成功</p></td> | ||
<td><p>東京都</p></td> | ||
<td><p>中央区</p></td> | ||
<td><p>日本橋一丁目</p></td> | ||
<td><p>1-1</p></td> | ||
<td><code>{"address":{"prefecture":"東京都","city":"中央区","town":"日本橋一丁目","rest":"1-1"}}</code> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
<script src="table_util.js"></script> | ||
<script type="module"> | ||
import init, {Parser} from "../pkg/japanese_address_parser_debug.js" | ||
|
||
const inputTextArea = document.getElementById("input") | ||
|
||
init().then(() => { | ||
document.getElementById("exec").addEventListener("click", () => { | ||
const input = inputTextArea.value | ||
alert("input: " + input) | ||
const parser = new Parser() | ||
parser.parse(input).then(result => { | ||
document.getElementById("result").appendChild( | ||
createRow(input, result) | ||
) | ||
}) | ||
}) | ||
}) | ||
</script> | ||
|
||
</body> | ||
</html> |
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
Oops, something went wrong.