Skip to content

Commit 6e49d6b

Browse files
committed
Optimize GetAddressCodeTimeline
1 parent 16fb4ea commit 6e49d6b

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

data/address_code_timeline.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,13 @@ var addressCodeTimelineProvincePluck = map[uint8]func() map[uint32][]map[string]
5050
83: addressCodeTimelineTaiWan,
5151
}
5252

53-
func GetAddressCodeTimeline(code uint32) ([]map[string]string, bool) {
54-
if code < 110000 || code > 830000 {
55-
return []map[string]string{}, false
53+
func GetAddressCodeTimeline(code uint32) []map[string]string {
54+
f := addressCodeTimelineProvincePluck[uint8(math.Floor(float64(code)/10000))]
55+
if f == nil {
56+
return []map[string]string{}
5657
}
5758

58-
f, ok := addressCodeTimelineProvincePluck[uint8(math.Floor(float64(code)/10000))]
59-
if !ok {
60-
return []map[string]string{}, false
61-
}
62-
63-
timeline, ok := f()[code]
64-
return timeline, ok
59+
return f()[code]
6560
}
6661

6762
func addressCodeTimelineBeiJing() map[uint32][]map[string]string {

generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func generatorBirthdayCode(addressCode string, address string, birthday string)
116116
month := datePipeHandle(datePad(substr(birthday, 4, 6), "month"), "month")
117117
day := datePipeHandle(datePad(substr(birthday, 6, 8), "day"), "day")
118118

119-
if timeline, ok := data.GetAddressCodeTimeline(cast.ToUint32(addressCode)); ok {
119+
if timeline := data.GetAddressCodeTimeline(cast.ToUint32(addressCode)); len(timeline) > 0 {
120120
for _, val := range timeline {
121121
if val["address"] == address {
122122
if val["start_year"] != "" {

helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func getAddressInfo(addressCode string, birthdayCode string, strict bool) map[st
4242
// 获取省市区地址码
4343
func getAddress(addressCode string, birthdayCode string, strict bool) string {
4444
address := ""
45-
timeline, ok := data.GetAddressCodeTimeline(cast.ToUint32(addressCode))
46-
if !ok {
45+
timeline := data.GetAddressCodeTimeline(cast.ToUint32(addressCode))
46+
if len(timeline) == 0 {
4747
// 修复 \d\d\d\d01、\d\d\d\d02、\d\d\d\d11 和 \d\d\d\d20 的历史遗留问题
4848
// 以上四种地址码,现实身份证真实存在,但民政部历年公布的官方地址码中可能没有查询到
4949
// 如:440401 450111 等

0 commit comments

Comments
 (0)