Created
December 29, 2021 14:36
-
-
Save hideokamoto/875a4adf1d5027e27c8601da9468f2c6 to your computer and use it in GitHub Desktop.
Stripe Connectむけの住所フォーマットに成形するサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { normalizeForStripeConnect } from './format-address.ts' | |
test.each([ | |
['大阪府堺市北区新金岡町4丁1−8', { | |
state: "大阪府", | |
city: "堺市北区", | |
town: "新金岡町四丁", | |
line1: "1-8" | |
}], | |
["神奈川県横浜市中区本町6丁目50−10", { | |
"city": "横浜市中区", | |
"line1": "50-10", | |
"state": "神奈川県", | |
"town": "本町六丁目", | |
}], | |
['岩手県九戸郡洋野町種市第35地割102−2', { | |
"city": "九戸郡洋野町", | |
"line1": "102-2", | |
"state": "岩手県", | |
"town": "種市第35地割", | |
}], | |
['京都府 京都市 中京区 御幸町通三条上る 丸屋町326', { | |
"city": "京都市中京区", | |
"line1": "326", | |
"state": "京都府", | |
"town": "丸屋町", | |
}], | |
['宮城県仙台市太白区鈎取一本杉64−1', { | |
"city": "仙台市太白区", | |
"line1": "一本杉64-1", | |
"state": "宮城県", | |
"town": "鈎取", | |
}], | |
['北海道札幌市豊平区平岸5条6丁目1−24', { | |
"city": "札幌市豊平区", | |
"line1": "六丁目1-24", | |
"state": "北海道", | |
"town": "平岸五条", | |
}], | |
['愛知県名古屋市熱田区二番2丁目20−2', { | |
"city": "名古屋市熱田区", | |
"line1": "20-2", | |
"state": "愛知県", | |
"town": "二番二丁目", | |
}] | |
])("%p", async (address, expectedResult) => { | |
const res = await normalizeForStripeConnect(address) | |
expect(res).toEqual(expectedResult) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { normalize, NormalizeResult } from '@geolonia/normalize-japanese-addresses'; | |
interface StripeConnectAddress { | |
state: string; | |
city: string; | |
town: string; | |
line1: string; | |
line2?: string; | |
} | |
const convertToStripeConnect = (address: NormalizeResult): StripeConnectAddress => { | |
return { | |
state: address.pref, | |
city: address.city, | |
town: address.town, | |
line1: address.addr | |
} | |
} | |
export const normalizeForStripeConnect = async (address: string): Promise<StripeConnectAddress> => { | |
/** | |
* 京都市の通り名は、city の最後につける -> 一律でDropする | |
* 町域よりも小さな区画がある場合、line1 の先頭につける | |
*/ | |
const res = await normalize(address) | |
const { addr, town, pref } = res | |
// 岩手県の「第◯地割」は、town の最後につける | |
if (pref === '岩手県') { | |
if (/地割/.test(addr)) { | |
const chiwari = (() => { | |
const reg1 = addr.match(/第.\d地割/) | |
if (reg1) return reg1[0] | |
const reg2 = addr.match(/.\d地割/) | |
if (reg2) return reg2[0] | |
return '' | |
})() | |
res.addr = addr.replace(/第.\d地割/, '').replace(/.\d地割/, '') | |
res.town = `${town}${chiwari}` | |
} | |
} | |
// 札幌市の「◯条」は、town の最後につける | |
if (pref === '北海道') { | |
if (/条/.test(town)) { | |
res.addr = `${town.substring(town.indexOf('条') + 1)}${addr}` | |
res.town = town.substring(0, town.indexOf('条') + 1) | |
} | |
} | |
return convertToStripeConnect(res) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment