Skip to content

Commit

Permalink
fix floor parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kane50613 committed Apr 27, 2024
1 parent 4e1c5ba commit 3e2edf9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/utils/translate-address-to-english.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,21 @@ const numberMatchingPatterns: {
render: (match) => `No. ${match[1]}`,
},
{
regex: /((\d+)(-\d+)?) */,
render: (match) => `${match[1]}F.`,
regex: /((\d+)(-(\d+))?) */,
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]}`;

return `${match[1]}F.`;
},
},
{
regex: /(\d+) *[]/,
render: (match) => `Rm. ${match[1]}`,
},
{
regex: /(\d+) */,
render: (match) => `${nth(Number(match[1]))} Neighborhood`,
render: (match) => `${match[1]}${nth(Number(match[1]))} Neighborhood`,
},
];

Expand Down Expand Up @@ -131,14 +136,14 @@ export function translateAddressToEnglish(
}
}

// decode all numbers back to arabic numbers,
// format ${number}之${extra} to ${number}-${extra}${type} (e.g. 11號之1 => 11-1號)
// decode all numbers back to arabic numbers
mutableAddress = mutableAddress
.replace(/[]+/g, (ch) =>
nzhInstance.decodeS(ch, {
tenMin: true,
}),
)
// 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}`),
);
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 @@ -24,6 +24,10 @@ const testCases = [
"105台灣台北市松山區南京東路四段1號6樓",
"6F., No. 1, Sec. 4, Nanjing E. Rd., Songshan Dist., Taipei City 105, Taiwan (R.O.C.)",
],
[
"105台灣台北市松山區南京東路四段六鄰1號6樓之2",
"6th Neighborhood, 6F.-2, No. 1, Sec. 4, Nanjing E. Rd., Songshan Dist., Taipei City 105, Taiwan (R.O.C.)",
],
] as const;

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

0 comments on commit 3e2edf9

Please sign in to comment.