Skip to content

Commit

Permalink
Merge pull request #220 from geolonia/add-more-tests
Browse files Browse the repository at this point in the history
[bug fix] 3より大きい正規化レベルを要求したが3未満に終わった場合に、不要に番地号レベルの正規化をしようとするバグの修正
  • Loading branch information
Kamata, Ryo committed Aug 1, 2023
2 parents f3030fc + 3919aae commit 828c446
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,10 @@ export const normalize: Normalizer = async (
const prefectures = await getPrefectures()
const prefs = Object.keys(prefectures)
const prefPatterns = getPrefectureRegexPatterns(prefs)
const sameNamedPrefectureCityRegexPatterns =
getSameNamedPrefectureCityRegexPatterns(prefs, prefectures)
const sameNamedPrefectureCityRegexPatterns = getSameNamedPrefectureCityRegexPatterns(
prefs,
prefectures,
)

// 県名が省略されており、かつ市の名前がどこかの都道府県名と同じ場合(例.千葉県千葉市)、
// あらかじめ県名を補完しておく。
Expand Down Expand Up @@ -480,7 +482,7 @@ export const normalize: Normalizer = async (
if (city) level = level + 1
if (town) level = level + 1

if (option.level <= 3) {
if (option.level <= 3 || level < 3) {
return { pref, city, town, addr, level, lat, lng }
}

Expand Down
33 changes: 33 additions & 0 deletions test/advanced/interface_v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { normalize, config } from '../../src/main-node'

config.interfaceVersion = 2
config.transformRequest = async (url, query) => {
console.log(url, query)
const { level, pref = '', city = '', town = '' } = query

switch (level) {
Expand Down Expand Up @@ -46,7 +47,10 @@ config.transformRequest = async (url, query) => {
return [
{ addr: '1-1', lat: '30.1', lng: '135.1' },
{ addr: '1-2', lat: '30.2', lng: '135.2' },
{ addr: '2-3', lat: null, lng: null },
]
} else {
return []
}
}
default:
Expand Down Expand Up @@ -86,3 +90,32 @@ test('リクエスト変形テスト 2', async () => {
lat: 30.2,
})
})

test('リクエスト変形テスト - レベル8 で緯度経度が null の時はレベル3の緯度経度を使う', async () => {
const res = await normalize('A県 X市 あああ 2の3 こんばんはビル', {
level: 8,
})
expect(res).toStrictEqual({
pref: 'A県',
city: 'X市',
town: 'あああ',
addr: '2-3',
other: 'こんばんはビル',
lng: 135,
level: 8,
lat: 30,
})
})

test('リクエスト変形テスト - 全く正規化できないケース', async () => {
const res = await normalize('こんにちはこんにちは', { level: 8 })
expect(res).toStrictEqual({
pref: '',
city: '',
town: '',
addr: 'こんにちはこんにちは',
lat: null,
lng: null,
level: 0,
})
})

0 comments on commit 828c446

Please sign in to comment.