From b84c299e2026471a5b04a07f11a671494eb82c56 Mon Sep 17 00:00:00 2001 From: Keita Kobayashi Date: Wed, 23 Oct 2024 10:38:15 +0900 Subject: [PATCH] Remove extra data from metadata --- src/lib/utils.ts | 32 ++++++++++++++++++++++++++++++++ src/normalize.ts | 12 ++++++++---- src/types.ts | 2 +- test/main/metadata.test.ts | 2 +- 4 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 src/lib/utils.ts diff --git a/src/lib/utils.ts b/src/lib/utils.ts new file mode 100644 index 0000000000..2a3d72c399 --- /dev/null +++ b/src/lib/utils.ts @@ -0,0 +1,32 @@ +import { + SingleCity, + SingleMachiAza, + SinglePrefecture, +} from '@geolonia/japanese-addresses-v2' + +export function removeCitiesFromPrefecture( + pref: SinglePrefecture | undefined, +): Omit | undefined { + if (!pref) { + return undefined + } + + const newPref: Omit & { cities?: SingleCity[] } = + { + ...pref, + } + delete newPref.cities + return newPref +} + +export function removeExtraFromMachiAza( + machiAza: SingleMachiAza | undefined, +): Omit | undefined { + if (!machiAza) { + return undefined + } + + const newMachiAza: SingleMachiAza = { ...machiAza } + delete newMachiAza.csv_ranges + return newMachiAza +} diff --git a/src/normalize.ts b/src/normalize.ts index b569ccf984..0af6f1cd15 100755 --- a/src/normalize.ts +++ b/src/normalize.ts @@ -34,6 +34,10 @@ import { rsdtOrChibanToResultPoint, upgradePoint, } from './types' +import { + removeCitiesFromPrefecture, + removeExtraFromMachiAza, +} from './lib/utils' export type TransformRequestQuery = { level: number // level = -1 は旧 API。 transformRequestFunction を設定しても無視する @@ -366,9 +370,9 @@ export const normalize: Normalizer = async ( point, metadata: { input: address, - prefecture: pref, + prefecture: removeCitiesFromPrefecture(pref), city: city, - machiAza: town, + machiAza: removeExtraFromMachiAza(town), }, } return result @@ -409,9 +413,9 @@ export const normalize: Normalizer = async ( other, metadata: { input: address, - prefecture: pref, + prefecture: removeCitiesFromPrefecture(pref), city: city, - machiAza: town, + machiAza: removeExtraFromMachiAza(town), rsdt: normalizedAddrPart.rsdt, chiban: normalizedAddrPart.chiban, }, diff --git a/src/types.ts b/src/types.ts index 8230470ac9..60c6aeb109 100644 --- a/src/types.ts +++ b/src/types.ts @@ -109,7 +109,7 @@ export type NormalizeResultMetadata = { input: string /** 都道府県 */ - prefecture?: SinglePrefecture + prefecture?: Omit /** 市区町村 */ city?: SingleCity /** 町字 */ diff --git a/test/main/metadata.test.ts b/test/main/metadata.test.ts index ac244c74ef..7288e61870 100644 --- a/test/main/metadata.test.ts +++ b/test/main/metadata.test.ts @@ -11,7 +11,7 @@ describe(`metadata tests`, () => { assert.strictEqual(res.metadata.prefecture?.pref, '東京都') assert.strictEqual(res.metadata.prefecture?.pref_k, 'トウキョウト') assert.strictEqual(res.metadata.prefecture?.pref_r, 'Tokyo') - assert.ok(res.metadata.prefecture?.cities.length > 0) + assert.ok(!('cities' in res.metadata.prefecture)) assert.strictEqual(res.metadata.city?.code, 131130) assert.strictEqual(res.metadata.city?.city, '渋谷区')