diff --git a/package.json b/package.json index 702f9b44c4..c40470dd1f 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "test:addresses": "jest test/addresses.test.ts", "test:advanced": "jest test/advanced --runInBand", "test:util": "jest test/util.test.ts", - "test:node": "curl -sL https://github.com/geolonia/japanese-addresses/archive/refs/heads/master.tar.gz | tar xvfz - -C ./test > nul 2>&1 && jest test/fs.test.ts", + "test:node": "curl -sL https://github.com/geolonia/japanese-addresses/archive/refs/heads/master.tar.gz | tar xvfz - -C ./test > /dev/null 2>&1 && jest test/fs.test.ts", "test:node-win": "curl -sL https://github.com/geolonia/japanese-addresses/archive/refs/heads/master.tar.gz -o master.tar.gz && 7z x master.tar.gz -y -o./ && 7z x ./master.tar -y -o./test && del master.tar.gz master.tar && jest test/fs.test.ts", "test:generate-test-data": "npx ts-node -O '{\"module\":\"commonjs\"}' test/build-test-data.ts > test/addresses.csv", "lint": "eslint \"src/**/*.ts\" \"test/**/*.test.ts\" --fix", diff --git a/src/lib/cacheRegexes.ts b/src/lib/cacheRegexes.ts index 66a08aeace..d192fb80c9 100644 --- a/src/lib/cacheRegexes.ts +++ b/src/lib/cacheRegexes.ts @@ -46,8 +46,9 @@ const cachedTowns: { [key: string]: TownList } = {} const cachedGaikuListItem: { [key: string]: GaikuListItem[] } = {} const cachedResidentials: { [key: string]: ResidentialList } = {} const cachedAddrs: { [key: string]: AddrList } = {} // TODO: use LRU -let cachedSameNamedPrefectureCityRegexPatterns: [string, string][] | undefined = - undefined +let cachedSameNamedPrefectureCityRegexPatterns: + | [string, string][] + | undefined = undefined export const getPrefectures = async () => { if (typeof cachedPrefectures !== 'undefined') { @@ -208,7 +209,9 @@ export const getAddrs = async (pref: string, city: string, town: string) => { addrs = [] } - addrs.sort((res1, res2) => res1.addr.length - res2.addr.length) + // 文字数が多い順に並び替えします。 + // 長い順に並び替えしないと、短い住所がマッチしてしまう。 + addrs.sort((res1, res2) => res2.addr.length - res1.addr.length) return (cachedAddrs[cacheKey] = addrs) } diff --git a/test/advanced/interface_v2.test.ts b/test/advanced/interface_v2.test.ts index 3418c19264..8d2895cd15 100644 --- a/test/advanced/interface_v2.test.ts +++ b/test/advanced/interface_v2.test.ts @@ -1,4 +1,5 @@ import { normalize, config } from '../../src/main-node' +import { NormalizeResult } from '../../src/normalize' config.interfaceVersion = 2 config.transformRequest = async (url, query) => { @@ -48,6 +49,11 @@ config.transformRequest = async (url, query) => { { 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 }, + { addr: '3-1', lat: '30.25', lng: '135.25' }, + { addr: '3-19', lat: '30.3', lng: '135.3' }, + { addr: '13-1', lat: '30.4', lng: '135.4' }, + { addr: '13-14', lat: '30.45', lng: '135.45' }, + { addr: '13-19', lat: '30.5', lng: '135.5' }, ] } else { return [] @@ -107,6 +113,77 @@ test('リクエスト変形テスト - レベル8 で緯度経度が null の時 }) }) +const expectedCommon = { pref: 'A県', city: 'X市', town: 'あああ', level: 8 } +const cases: { input: string; expected: NormalizeResult }[] = [ + { + input: 'A県 X市 あああ1-1 こんばんはビル', + expected: { + ...expectedCommon, + addr: '1-1', + other: 'こんばんはビル', + lng: 135.1, + lat: 30.1, + }, + }, + { + input: 'A県 X市 あああ3-1 こんばんはビル', + expected: { + ...expectedCommon, + addr: '3-1', + other: 'こんばんはビル', + lng: 135.25, + lat: 30.25, + }, + }, + { + input: 'A県 X市 あああ3-19 こんばんはビル', + expected: { + ...expectedCommon, + addr: '3-19', + other: 'こんばんはビル', + lng: 135.3, + lat: 30.3, + }, + }, + { + input: 'A県 X市 あああ13-1 こんばんはビル', + expected: { + ...expectedCommon, + addr: '13-1', + other: 'こんばんはビル', + lng: 135.4, + lat: 30.4, + }, + }, + { + input: 'A県 X市 あああ13-14 こんばんはビル', + expected: { + ...expectedCommon, + addr: '13-14', + other: 'こんばんはビル', + lng: 135.45, + lat: 30.45, + }, + }, + { + input: 'A県 X市 あああ13-19 こんばんはビル', + expected: { + ...expectedCommon, + addr: '13-19', + other: 'こんばんはビル', + lng: 135.5, + lat: 30.5, + }, + }, +] + +for (const { input, expected } of cases) { + test(`リクエスト変形テスト - ${input}`, async () => { + const res = await normalize(input, { level: 8 }) + expect(res).toStrictEqual(expected) + }) +} + test('リクエスト変形テスト - 全く正規化できないケース', async () => { const res = await normalize('こんにちはこんにちは', { level: 8 }) expect(res).toStrictEqual({