Skip to content

Commit

Permalink
「和歌山県」を「和歌山」のように「都道府県」の文字列を入れないケースに対応 (#116)
Browse files Browse the repository at this point in the history
Co-authored-by: Takayuki Miyauchi <miya0001@users.noreply.github.com>
  • Loading branch information
champierre and miya0001 authored Sep 4, 2021
1 parent bdb8470 commit 9313c3a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/lib/cacheRegexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const getPrefectureRegexes = (prefs: string[]) => {

cachedPrefectureRegexes = prefs.map((pref) => {
const _pref = pref.replace(/(|||)$/, '') // `東京` の様に末尾の `都府県` が抜けた住所に対応
const reg = new RegExp(`^${_pref}(都|道|府|県)`)
const reg = new RegExp(`^${_pref}(都|道|府|県)?`)
return [pref, reg]
})

Expand Down
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ export const normalize: (

for (let i = 0; i < prefRegexes.length; i++) {
const [_pref, reg] = prefRegexes[i]
if (addr.match(reg)) {
const match = addr.match(reg)
if (match) {
pref = _pref
addr = addr.substring(pref.length) // 都道府県名以降の住所
addr = addr.substring(match[0].length) // 都道府県名以降の住所
break
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,3 +726,9 @@ test('岩手県花巻市12丁目704', async () => {
const res = await normalize('岩手県花巻市12丁目704')
expect(res).toStrictEqual({"pref": "岩手県", "city": "花巻市", "town": "十二丁目", "addr": "704", "lat": 39.358268, "lng": 141.122331, "level": 3})
})

// 「都道府県」の文字列を省略した場合
test('岩手花巻市12丁目704', async () => {
const res = await normalize('岩手花巻市12丁目704')
expect(res).toStrictEqual({"pref": "岩手県", "city": "花巻市", "town": "十二丁目", "addr": "704", "lat": 39.358268, "lng": 141.122331, "level": 3})
})

0 comments on commit 9313c3a

Please sign in to comment.