Skip to content

Commit

Permalink
fix dash type matching
Browse files Browse the repository at this point in the history
  • Loading branch information
kane50613 committed Apr 28, 2024
1 parent 3e2edf9 commit 54db283
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/utils/translate-address-to-english.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const numberMatchingPatterns: {
render: (match) => `No. ${match[1]}`,
},
{
regex: /((\d+)(-(\d+))?) */,
regex: /((\d+)(-(\d+))?) *[樓F]/,
render(match) {
// if extra number is found, format it to 1F.-2 (e.g. 1-1樓 => 1F.-1)
if (match[4]) return `${match[2]}F.-${match[4]}`;
Expand Down Expand Up @@ -144,8 +144,9 @@ export function translateAddressToEnglish(
}),
)
// format ${number}之${extra} to ${number}-${extra}${type} (e.g. 11號之1 => 11-1號)
.replace(/(\d+)(.)?之(\d+)/g, (ch, number, type = "", extra) =>
ch.replace(`${number}${type}${extra}`, `${number}-${extra}${type}`),
.replace(
/(\d+)(.)?[之-](\d+)/g,
(_, number, type = "", extra) => `${number}-${extra}${type}`,
);

let roundSuccess = false;
Expand Down
4 changes: 4 additions & 0 deletions test/address-to-english.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const testCases = [
"105台灣台北市松山區南京東路四段六鄰1號6樓之2",
"6th Neighborhood, 6F.-2, No. 1, Sec. 4, Nanjing E. Rd., Songshan Dist., Taipei City 105, Taiwan (R.O.C.)",
],
[
"臺中市西區公益路367號10F-2",
"10F.-2, No. 367, Gongyi Rd., West Dist., Taichung City 403, Taiwan (R.O.C.)",
],
] as const;

test("address-to-english", async () => {
Expand Down

0 comments on commit 54db283

Please sign in to comment.