Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extra data from metadata #247

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
SingleCity,
SingleMachiAza,
SinglePrefecture,
} from '@geolonia/japanese-addresses-v2'

export function removeCitiesFromPrefecture(
pref: SinglePrefecture | undefined,
): Omit<SinglePrefecture, 'cities'> | undefined {
if (!pref) {
return undefined
}

const newPref: Omit<SinglePrefecture, 'cities'> & { cities?: SingleCity[] } =
{
...pref,
}
delete newPref.cities
return newPref
}

export function removeExtraFromMachiAza(
machiAza: SingleMachiAza | undefined,
): Omit<SingleMachiAza, 'csv_ranges'> | undefined {
if (!machiAza) {
return undefined
}

const newMachiAza: SingleMachiAza = { ...machiAza }
delete newMachiAza.csv_ranges
return newMachiAza
}
12 changes: 8 additions & 4 deletions src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import {
rsdtOrChibanToResultPoint,
upgradePoint,
} from './types'
import {
removeCitiesFromPrefecture,
removeExtraFromMachiAza,
} from './lib/utils'

export type TransformRequestQuery = {
level: number // level = -1 は旧 API。 transformRequestFunction を設定しても無視する
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export type NormalizeResultMetadata = {
input: string

/** 都道府県 */
prefecture?: SinglePrefecture
prefecture?: Omit<SinglePrefecture, 'cities'>
/** 市区町村 */
city?: SingleCity
/** 町字 */
Expand Down
2 changes: 1 addition & 1 deletion test/main/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '渋谷区')
Expand Down