japanese_address_parser 4.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +67 -6
  3. data/CHANGELOG.md +17 -0
  4. data/Gemfile.lock +104 -58
  5. data/Steepfile +1 -7
  6. data/japanese_address_parser.gemspec +2 -0
  7. data/lib/japanese_address_parser/address.rb +35 -34
  8. data/lib/japanese_address_parser/cache_regexes.rb +22 -14
  9. data/lib/japanese_address_parser/city.rb +14 -13
  10. data/lib/japanese_address_parser/data/api_meta.rb +8 -7
  11. data/lib/japanese_address_parser/data/machi_aza_api.rb +8 -7
  12. data/lib/japanese_address_parser/data/prefecture_api.rb +8 -7
  13. data/lib/japanese_address_parser/data/single_chiban.rb +17 -16
  14. data/lib/japanese_address_parser/data/single_city.rb +25 -24
  15. data/lib/japanese_address_parser/data/single_machi_aza.rb +28 -27
  16. data/lib/japanese_address_parser/data/single_prefecture.rb +13 -12
  17. data/lib/japanese_address_parser/data/single_rsdt.rb +17 -16
  18. data/lib/japanese_address_parser/dictionaries/convert.rb +1 -0
  19. data/lib/japanese_address_parser/fetcher.rb +13 -12
  20. data/lib/japanese_address_parser/japanese_numeral.rb +2 -0
  21. data/lib/japanese_address_parser/normalize_result_point.rb +21 -21
  22. data/lib/japanese_address_parser/prefecture.rb +15 -14
  23. data/lib/japanese_address_parser/town.rb +23 -22
  24. data/lib/japanese_address_parser/version.rb +1 -1
  25. data/sig/address.rbs +2 -5
  26. data/sig/cache_regexes.rbs +22 -25
  27. data/sig/config.rbs +0 -2
  28. data/sig/data.rbs +2 -5
  29. data/sig/dict.rbs +1 -3
  30. data/sig/dictionaries.rbs +1 -3
  31. data/sig/fetcher.rbs +5 -6
  32. data/sig/japanese_address_parser.rbs +6 -3
  33. data/sig/japanese_numeral.rbs +12 -15
  34. data/sig/kan2num.rbs +1 -3
  35. data/sig/lru_redux.rbs +9 -0
  36. data/sig/normalize.rbs +4 -7
  37. data/sig/normalize_helpers.rbs +1 -3
  38. data/sig/normalize_result.rbs +0 -2
  39. data/sig/normalize_result_point.rbs +8 -8
  40. data/sig/patch_addr.rbs +1 -3
  41. data/sig/utils.rbs +2 -4
  42. data/sig/zen2han.rbs +1 -3
  43. metadata +30 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc60cb10d4d82995816196dffbb29519d6e8d6c7088dca5da2dfcd2d73c22b1a
4
- data.tar.gz: a27154a347951759bef645e81b94559f6d66cc93393651607a045daa9359de93
3
+ metadata.gz: 4b19df70505de62d54e53148e4987925f9fef8054550e5c1d48c8b2048814dda
4
+ data.tar.gz: cca05c04ce8cbddad73903dc8d07f5f8867e03f1c687c70620cec0954073b3b5
5
5
  SHA512:
6
- metadata.gz: e396466caac9d0d76c294a147625d6f012439b97823840f8f350fa395446dc768d788c2e139503a971d1056a6dc16a9f8647a9c5ae33da67090b79092bb401bd
7
- data.tar.gz: 8a9cd3d54cc50c8133535099e037ada4957dbafdb3a07bf2f65119abdea0edf114155bb389569fc5030b571e693bbc27a06effd734ce8ef964ccee7d318be1b9
6
+ metadata.gz: bb15346ae7129d17d418ec239092592587e60fe65ffe1fa30aaee6eae4b5b5a4a0c217f4485600d60df46c917de42a79bad86a142378d5403128720571b6af69
7
+ data.tar.gz: 5aae0a4aa7d390b1fbaa1212f29e8743c9bf1d7f1562bd9cb1a481d7f18d903b2a2e4f184b9ac35b49a48bbaac2c332339a6b51159b6190e48105c6854244c2b
data/.rubocop.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  inherit_from: .rubocop_todo.yml
2
2
 
3
- require:
3
+ plugins:
4
4
  - rubocop-rake
5
5
  - rubocop-rspec
6
6
 
@@ -15,7 +15,7 @@ Metrics/BlockLength:
15
15
  # v4 の逐語移植では上流 JS の巨大なコールバック(getTownRegexPatterns 等)をそのまま写す。
16
16
  - lib/japanese_address_parser/**/*.rb
17
17
 
18
- # v4 リアーキの lib は上流 JS の逐語移植(working_agreement §3)。メソッド構成・
18
+ # v4 lib は上流 JS(@geolonia/normalize-japanese-addresses)の逐語移植。メソッド構成・
19
19
  # 複雑度・モジュール長は移植元 JS に合わせるため、これらの Metrics を v4 では緩める。
20
20
  # (from_json のフィールドマッピングや japanese_numeral の漢数字変換ロジック等。)
21
21
  Metrics/MethodLength:
@@ -58,12 +58,61 @@ Naming/AccessorMethodName:
58
58
  Exclude:
59
59
  - lib/japanese_address_parser/**/*.rb
60
60
 
61
- # v4 は正規表現を文字列レベルで逐語コピーする(§3-1)。上流が文字クラス内に
61
+ # v4 は上流 JS の正規表現を文字列レベルで逐語コピーする。上流が文字クラス内に
62
62
  # リテラル `|` を含める癖(例: /[都|道|府|県]$/)もそのまま移植するため許容する。
63
63
  Lint/DuplicateRegexpCharacterClassElement:
64
64
  Exclude:
65
65
  - lib/japanese_address_parser/**/*.rb
66
66
 
67
+ # 同じく正規表現を逐語コピーするため、上流の冗長なエスケープ(例: 文字クラス内の `\-`)も
68
+ # そのまま写すのを許容する。
69
+ Style/RedundantRegexpEscape:
70
+ Exclude:
71
+ - lib/japanese_address_parser/**/*.rb
72
+
73
+ # v4 は JS の `array[0]` / `a && a.b` / import 順もそのまま写すため lib では緩める。
74
+ Style/ArrayFirstLast:
75
+ Exclude:
76
+ - lib/japanese_address_parser/**/*.rb
77
+
78
+ Style/SafeNavigation:
79
+ Exclude:
80
+ - lib/japanese_address_parser/**/*.rb
81
+
82
+ Style/RequireOrder:
83
+ Exclude:
84
+ - lib/japanese_address_parser/**/*.rb
85
+ - lib/japanese_address_parser.rb
86
+
87
+ # jis_dai2 の異体字辞書は上流 dict.ts のデータの逐語移植なので許容する。
88
+ Metrics/CollectionLiteralLength:
89
+ Exclude:
90
+ - lib/japanese_address_parser/**/*.rb
91
+
92
+ # Data.define したクラスの再オープン(Steep 対応。クラスメソッドだけを追加するケース)を
93
+ # 「クラスメソッドのみのクラス」と誤認して module 化を提案するため lib では緩める。
94
+ Style/StaticClass:
95
+ Exclude:
96
+ - lib/japanese_address_parser/**/*.rb
97
+
98
+ # Data.define したクラスの再オープンにも別途のドキュメントコメントを要求するため lib では
99
+ # 緩める(VO の説明は定数代入側にある)。merge 指定は .rubocop_todo.yml 側の Exclude を
100
+ # 上書きせず追記するため。
101
+ Style/Documentation:
102
+ inherit_mode:
103
+ merge:
104
+ - Exclude
105
+ Exclude:
106
+ - lib/japanese_address_parser/**/*.rb
107
+
108
+ # キー欠落を黙って nil にせず例外にしたい箇所で Hash#fetch を意図的に使うため無効化する。
109
+ Style/HashLookupMethod:
110
+ Enabled: false
111
+
112
+ # 開発依存は従来どおり gemspec で管理する(Gemfile への移行はしない)。
113
+ Gemspec/DevelopmentDependencies:
114
+ Enabled: false
115
+
67
116
  # contextの記述を日本語で記述するのを許容する。
68
117
  RSpec/ContextWording:
69
118
  Enabled: false
@@ -74,12 +123,24 @@ RSpec/ExampleLength:
74
123
  RSpec/MultipleExpectations:
75
124
  Enabled: false
76
125
 
77
- # v4 リアーキの spec は隔離のため spec/v4 配下に置く(クラスの名前空間を
78
- # そのままパスに写すと spec/japanese_address_parser/v4/... となり隔離できない)。
79
- RSpec/FilePath:
126
+ # spec は上流テストファイル・ドメイン単位の命名(addresses_spec 等)で、クラスの
127
+ # 名前空間をパスに写さない(spec/japanese_address_parser/... 形式にしない)ため無効化。
128
+ RSpec/SpecFilePathFormat:
80
129
  Exclude:
81
130
  - spec/**/*_spec.rb
82
131
 
132
+ # addresses_spec は上流 addresses.test.ts の逐語移植で、CSV の行ごとに describe 直下で
133
+ # 期待値を組み立てて example を生成するデータ駆動形式のため、この cop の警告対象になる。
134
+ # 各変数はイテレーションごとに束縛され example 実行前に変更されないのでリークしない。
135
+ RSpec/LeakyLocalVariable:
136
+ Exclude:
137
+ - spec/addresses_spec.rb
138
+
139
+ # level1 / level3 の数字は位置情報レベル(level: 1 / 3)というドメインの値を指すため許容する。
140
+ RSpec/IndexedLet:
141
+ Exclude:
142
+ - spec/normalize_result_point_spec.rb
143
+
83
144
  # filesystem_api_spec は CDN から tmpdir へ一度だけ DL するため before/after(:context) を使う。
84
145
  # 状態(Config / モジュールキャッシュ)は around で example ごとに復元・reset し、tmpdir は
85
146
  # after(:context) で削除するので、本 cop が警告する状態リークは管理済み。
data/CHANGELOG.md CHANGED
@@ -18,6 +18,23 @@ Change Log の形式は [Keep a Changelog](http://keepachangelog.com/) に従い
18
18
 
19
19
  ### Security
20
20
 
21
+ ## [4.0.1] - 2026-08-22
22
+
23
+ ### Added
24
+
25
+ - [#149](https://github.com/yamat47/japanese_address_parser/pull/149) Add Trusted Publishing release workflow([@yamat47](https://github.com/yamat47))
26
+
27
+ ### Changed
28
+
29
+ - [#143](https://github.com/yamat47/japanese_address_parser/pull/143) Bump actions/checkout from 4 to 7([@yamat47](https://github.com/yamat47))
30
+ - [#150](https://github.com/yamat47/japanese_address_parser/pull/150) Group Dependabot updates per ecosystem([@yamat47](https://github.com/yamat47))
31
+ - [#151](https://github.com/yamat47/japanese_address_parser/pull/151) Bump actions/checkout in the github-actions-dependencies group([@yamat47](https://github.com/yamat47))
32
+ - [#152](https://github.com/yamat47/japanese_address_parser/pull/152) Bump the bundler-dependencies group (8 updates) and migrate to RuboCop 1.89 / Steep 2.0([@yamat47](https://github.com/yamat47))
33
+
34
+ ### Security
35
+
36
+ - [#142](https://github.com/yamat47/japanese_address_parser/pull/142) Update vulnerable development dependencies (activesupport / concurrent-ruby / rexml)([@yamat47](https://github.com/yamat47))
37
+
21
38
  ## [4.0.0]
22
39
 
23
40
  ### Changed
data/Gemfile.lock CHANGED
@@ -1,100 +1,144 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- japanese_address_parser (4.0.0)
4
+ japanese_address_parser (4.0.1)
5
5
  csv
6
6
  lru_redux
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activesupport (7.0.7.1)
12
- concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ activesupport (8.1.3.1)
12
+ base64
13
+ bigdecimal
14
+ concurrent-ruby (~> 1.0, >= 1.3.1)
15
+ connection_pool (>= 2.2.5)
16
+ drb
13
17
  i18n (>= 1.6, < 2)
18
+ json
19
+ logger (>= 1.4.2)
14
20
  minitest (>= 5.1)
15
- tzinfo (~> 2.0)
21
+ securerandom (>= 0.3)
22
+ tzinfo (~> 2.0, >= 2.0.5)
23
+ uri (>= 0.13.1)
16
24
  addressable (2.9.0)
17
25
  public_suffix (>= 2.0.2, < 8.0)
18
- ast (2.4.2)
26
+ ast (2.4.3)
19
27
  base64 (0.3.0)
28
+ benchmark (0.5.0)
20
29
  bigdecimal (4.1.2)
21
- concurrent-ruby (1.2.2)
30
+ concurrent-ruby (1.3.8)
31
+ connection_pool (3.0.2)
22
32
  crack (1.0.1)
23
33
  bigdecimal
24
34
  rexml
25
- csv (3.3.2)
26
- diff-lcs (1.5.0)
27
- ffi (1.15.5)
35
+ csv (3.3.6)
36
+ diff-lcs (1.6.2)
37
+ drb (2.2.3)
38
+ ffi (1.17.4-aarch64-linux-gnu)
39
+ ffi (1.17.4-aarch64-linux-musl)
40
+ ffi (1.17.4-arm64-darwin)
41
+ ffi (1.17.4-x86_64-darwin)
42
+ ffi (1.17.4-x86_64-linux-gnu)
43
+ fileutils (1.8.0)
28
44
  hashdiff (1.2.1)
29
- i18n (1.14.1)
45
+ i18n (1.15.2)
30
46
  concurrent-ruby (~> 1.0)
31
- json (2.6.2)
32
- language_server-protocol (3.17.0.1)
33
- listen (3.7.1)
47
+ json (2.21.2)
48
+ language_server-protocol (3.17.0.6)
49
+ lint_roller (1.1.0)
50
+ listen (3.10.0)
51
+ logger
34
52
  rb-fsevent (~> 0.10, >= 0.10.3)
35
53
  rb-inotify (~> 0.9, >= 0.9.10)
54
+ logger (1.7.0)
36
55
  lru_redux (1.1.0)
37
- minitest (5.19.0)
38
- parallel (1.22.1)
39
- parser (3.2.2.3)
56
+ minitest (6.0.6)
57
+ drb (~> 2.0)
58
+ prism (~> 1.5)
59
+ ostruct (0.6.3)
60
+ parallel (1.28.0)
61
+ parser (3.3.12.0)
40
62
  ast (~> 2.4.1)
41
63
  racc
64
+ prism (1.9.0)
42
65
  public_suffix (7.0.5)
43
- racc (1.7.1)
66
+ racc (1.8.1)
44
67
  rainbow (3.1.1)
45
- rake (13.0.6)
68
+ rake (13.4.2)
46
69
  rb-fsevent (0.11.2)
47
- rb-inotify (0.10.1)
70
+ rb-inotify (0.11.1)
48
71
  ffi (~> 1.0)
49
- rbs (2.6.0)
50
- regexp_parser (2.5.0)
51
- rexml (3.2.5)
52
- rspec (3.11.0)
53
- rspec-core (~> 3.11.0)
54
- rspec-expectations (~> 3.11.0)
55
- rspec-mocks (~> 3.11.0)
56
- rspec-core (3.11.0)
57
- rspec-support (~> 3.11.0)
58
- rspec-expectations (3.11.1)
72
+ rbs (4.1.3)
73
+ logger
74
+ prism (>= 1.6.0)
75
+ tsort
76
+ regexp_parser (2.12.0)
77
+ rexml (3.4.4)
78
+ rspec (3.13.2)
79
+ rspec-core (~> 3.13.0)
80
+ rspec-expectations (~> 3.13.0)
81
+ rspec-mocks (~> 3.13.0)
82
+ rspec-core (3.13.6)
83
+ rspec-support (~> 3.13.0)
84
+ rspec-expectations (3.13.5)
59
85
  diff-lcs (>= 1.2.0, < 2.0)
60
- rspec-support (~> 3.11.0)
61
- rspec-mocks (3.11.1)
86
+ rspec-support (~> 3.13.0)
87
+ rspec-mocks (3.13.8)
62
88
  diff-lcs (>= 1.2.0, < 2.0)
63
- rspec-support (~> 3.11.0)
64
- rspec-support (3.11.1)
65
- rubocop (1.36.0)
89
+ rspec-support (~> 3.13.0)
90
+ rspec-support (3.13.7)
91
+ rubocop (1.89.0)
66
92
  json (~> 2.3)
67
- parallel (~> 1.10)
68
- parser (>= 3.1.2.1)
93
+ language_server-protocol (~> 3.17.0.2)
94
+ lint_roller (~> 1.1.0)
95
+ parallel (>= 1.10)
96
+ parser (>= 3.3.0.2)
69
97
  rainbow (>= 2.2.2, < 4.0)
70
- regexp_parser (>= 1.8, < 3.0)
71
- rexml (>= 3.2.5, < 4.0)
72
- rubocop-ast (>= 1.20.1, < 2.0)
98
+ regexp_parser (>= 2.9.3, < 3.0)
99
+ rubocop-ast (>= 1.49.0, < 2.0)
73
100
  ruby-progressbar (~> 1.7)
74
- unicode-display_width (>= 1.4.0, < 3.0)
75
- rubocop-ast (1.21.0)
76
- parser (>= 3.1.1.0)
77
- rubocop-rake (0.6.0)
78
- rubocop (~> 1.0)
79
- rubocop-rspec (2.13.2)
80
- rubocop (~> 1.33)
81
- ruby-progressbar (1.11.0)
82
- steep (1.1.1)
83
- activesupport (>= 5.1)
84
- language_server-protocol (>= 3.15, < 4.0)
101
+ unicode-display_width (>= 2.4.0, < 4.0)
102
+ rubocop-ast (1.50.0)
103
+ parser (>= 3.3.7.2)
104
+ prism (~> 1.7)
105
+ rubocop-rake (0.7.1)
106
+ lint_roller (~> 1.1)
107
+ rubocop (>= 1.72.1)
108
+ rubocop-rspec (3.10.2)
109
+ lint_roller (~> 1.1)
110
+ regexp_parser (>= 2.0)
111
+ rubocop (~> 1.86, >= 1.86.2)
112
+ ruby-progressbar (1.13.0)
113
+ securerandom (0.4.1)
114
+ steep (2.0.0)
115
+ concurrent-ruby (>= 1.1.10)
116
+ csv (>= 3.0.9)
117
+ fileutils (>= 1.1.0)
118
+ json (>= 2.1.0)
119
+ language_server-protocol (>= 3.17.0.4, < 4.0)
85
120
  listen (~> 3.0)
86
- parallel (>= 1.0.0)
87
- parser (>= 3.1)
121
+ logger (>= 1.3.0)
122
+ parser (>= 3.2)
123
+ prism (>= 0.25.0)
88
124
  rainbow (>= 2.2.2, < 4.0)
89
- rbs (>= 2.3.2)
90
- terminal-table (>= 2, < 4)
91
- terminal-table (3.0.2)
92
- unicode-display_width (>= 1.1.1, < 3)
93
- typeprof (0.21.3)
125
+ rbs (~> 4.0)
126
+ securerandom (>= 0.1)
127
+ strscan (>= 1.0.0)
128
+ terminal-table (>= 2, < 5)
129
+ uri (>= 0.12.0)
130
+ strscan (3.1.8)
131
+ terminal-table (4.0.0)
132
+ unicode-display_width (>= 1.1.1, < 4)
133
+ tsort (0.2.0)
134
+ typeprof (0.21.11)
94
135
  rbs (>= 1.8.1)
95
136
  tzinfo (2.0.6)
96
137
  concurrent-ruby (~> 1.0)
97
- unicode-display_width (2.3.0)
138
+ unicode-display_width (3.2.0)
139
+ unicode-emoji (~> 4.1)
140
+ unicode-emoji (4.2.0)
141
+ uri (1.1.1)
98
142
  webmock (3.26.2)
99
143
  addressable (>= 2.8.0)
100
144
  crack (>= 0.3.2)
@@ -114,7 +158,9 @@ PLATFORMS
114
158
  DEPENDENCIES
115
159
  activesupport
116
160
  base64
161
+ benchmark
117
162
  japanese_address_parser!
163
+ ostruct
118
164
  rake
119
165
  rspec
120
166
  rubocop
data/Steepfile CHANGED
@@ -5,13 +5,7 @@ target :lib do
5
5
 
6
6
  check 'lib'
7
7
 
8
- configure_code_diagnostics(::Steep::Diagnostic::Ruby.strict)
9
- end
10
-
11
- target :test do
12
- signature 'sig'
13
-
14
- check 'spec'
8
+ library 'csv', 'json', 'net-http', 'socket', 'uri'
15
9
 
16
10
  configure_code_diagnostics(::Steep::Diagnostic::Ruby.strict)
17
11
  end
@@ -34,6 +34,8 @@ require_relative 'lib/japanese_address_parser/version'
34
34
  spec.add_dependency('lru_redux')
35
35
  spec.add_development_dependency('activesupport')
36
36
  spec.add_development_dependency('base64')
37
+ spec.add_development_dependency('benchmark')
38
+ spec.add_development_dependency('ostruct')
37
39
  spec.add_development_dependency('rake')
38
40
  spec.add_development_dependency('rspec')
39
41
  spec.add_development_dependency('rubocop')
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Public value object returned by the v4 API (M6).
3
+ # Public value object returned by the v4 API.
4
4
  # Ruby-original enrichment layer over the faithful NormalizeResult (working_agreement §1-3 / rearchitecture §5.2)。
5
5
  # JS の NormalizeResult(文字列 pref/city/town)をネストしたリッチ VO に組み替える。
6
6
 
@@ -11,39 +11,40 @@ require 'japanese_address_parser/town'
11
11
  module JapaneseAddressParser
12
12
  # 正規化結果の公開 VO。prefecture/city/town は未判別なら nil(JS 同様、未マッチは失敗ではない)。
13
13
  # metadata は VO に昇格しない生データ(rsdt/chiban 等)の逃がし道(working_agreement §1-3)。
14
- Address =
15
- ::Data.define(:full_address, :prefecture, :city, :town, :addr, :other, :point, :level, :metadata) do
16
- # 内部 NormalizeResult(M5)から公開 Address を組み立てる。
17
- def self.from_normalize_result(result)
18
- metadata = result.metadata
19
- new(
20
- full_address: metadata.input,
21
- prefecture: Prefecture.from_metadata(metadata.prefecture),
22
- city: City.from_metadata(metadata.city),
23
- town: Town.from_metadata(metadata.machi_aza),
24
- addr: result.addr,
25
- other: result.other,
26
- point: result.point,
27
- level: result.level,
28
- metadata:
29
- )
30
- end
14
+ Address = ::Data.define(:full_address, :prefecture, :city, :town, :addr, :other, :point, :level, :metadata)
15
+ public_constant :Address
31
16
 
32
- # ネストした VO・座標も含めて Hash 化する Ruby 独自 API。metadata は生データの逃がし道として
33
- # shallow Hash 化する(内部の SingleCity 等は VO のまま)。
34
- def to_h
35
- {
36
- full_address:,
37
- prefecture: prefecture&.to_h,
38
- city: city&.to_h,
39
- town: town&.to_h,
40
- addr:,
41
- other:,
42
- point: point&.to_h,
43
- level:,
44
- metadata: metadata.to_h
45
- }
46
- end
17
+ class Address
18
+ # 内部 NormalizeResult から公開 Address を組み立てる。
19
+ def self.from_normalize_result(result)
20
+ metadata = result.metadata
21
+ new(
22
+ full_address: metadata.input,
23
+ prefecture: Prefecture.from_metadata(metadata.prefecture),
24
+ city: City.from_metadata(metadata.city),
25
+ town: Town.from_metadata(metadata.machi_aza),
26
+ addr: result.addr,
27
+ other: result.other,
28
+ point: result.point,
29
+ level: result.level,
30
+ metadata:
31
+ )
47
32
  end
48
- public_constant :Address
33
+
34
+ # ネストした VO・座標も含めて Hash 化する Ruby 独自 API。metadata は生データの逃がし道として
35
+ # shallow に Hash 化する(内部の SingleCity 等は VO のまま)。
36
+ def to_h
37
+ {
38
+ full_address:,
39
+ prefecture: prefecture&.to_h,
40
+ city: city&.to_h,
41
+ town: town&.to_h,
42
+ addr:,
43
+ other:,
44
+ point: point&.to_h,
45
+ level:,
46
+ metadata: metadata.to_h
47
+ }
48
+ end
49
+ end
49
50
  end
@@ -2,10 +2,8 @@
2
2
 
3
3
  # Port of: https://github.com/geolonia/normalize-japanese-addresses/blob/49c1ae4be9d2ba353b86eaf40fd7eb12a1269f3e/src/lib/cacheRegexes.ts
4
4
  # Upstream: @geolonia/normalize-japanese-addresses v3.1.3
5
- # level 0-3 部分のみ。rsdt/chiban(getRsdt/getChiban/fetchSubresource/parseSubresource)は M8 で実装する。
6
5
 
7
6
  require 'csv'
8
- require 'set'
9
7
  require 'lru_redux'
10
8
  require 'japanese_address_parser/config'
11
9
  require 'japanese_address_parser/fetcher'
@@ -24,15 +22,16 @@ module JapaneseAddressParser
24
22
  module CacheRegexes
25
23
  # 「○○町」の省略エイリアス。JS では SingleMachiAza に originalTown を生やした部分オブジェクト。
26
24
  # machi_aza_name は oaza_cho(町を除いた名前)を返す(chome/koaza は持たない)。
27
- TownAlias =
28
- ::Data.define(:machiaza_id, :point, :oaza_cho, :original_town) do
29
- # JS: machiAzaName(town) 相当。エイリアスは oaza_cho(町を除いた名前)のみを持つ。
30
- def machi_aza_name
31
- oaza_cho
32
- end
33
- end
25
+ TownAlias = ::Data.define(:machiaza_id, :point, :oaza_cho, :original_town)
34
26
  public_constant :TownAlias
35
27
 
28
+ class TownAlias
29
+ # JS: machiAzaName(town) 相当。エイリアスは oaza_cho(町を除いた名前)のみを持つ。
30
+ def machi_aza_name
31
+ oaza_cho
32
+ end
33
+ end
34
+
36
35
  module_function
37
36
 
38
37
  # JS: const cache = new LRUCache({ max: currentConfig.cacheSize })
@@ -67,7 +66,8 @@ module JapaneseAddressParser
67
66
 
68
67
  # JS: getPrefectures()
69
68
  def get_prefectures
70
- return @cached_prefectures unless @cached_prefectures.nil?
69
+ cached = @cached_prefectures
70
+ return cached unless cached.nil?
71
71
 
72
72
  response = Fetcher.fetch('.json') # ja.json
73
73
  cache_prefectures(Data::PrefectureApi.from_json(response.json))
@@ -80,7 +80,8 @@ module JapaneseAddressParser
80
80
 
81
81
  # JS: getPrefectureRegexPatterns(api)
82
82
  def get_prefecture_regex_patterns(api)
83
- return @cached_prefecture_patterns if @cached_prefecture_patterns
83
+ cached = @cached_prefecture_patterns
84
+ return cached if cached
84
85
 
85
86
  @cached_prefecture_patterns =
86
87
  api.data.map do |pref|
@@ -137,6 +138,7 @@ module JapaneseAddressParser
137
138
  api = get_towns(pref, city, api_version)
138
139
  pre_towns = api.data
139
140
  town_set = ::Set.new(pre_towns.map(&:machi_aza_name))
141
+ # @type var towns: ::Array[Data::SingleMachiAza | TownAlias]
140
142
  towns = []
141
143
 
142
144
  is_kyoto = city.city == '京都市'
@@ -163,6 +165,8 @@ module JapaneseAddressParser
163
165
  # JS: towns.sort((a, b) => bLen - aLen)(大字始まりは優先度を下げる)— 文字数の降順に安定ソート
164
166
  towns = stable_sort_by(towns) { |town| -town_sort_length(town) }
165
167
 
168
+ # 後段の丁目ループは TownAlias を元 VO に剥がさずそのまま追加する(JS と同挙動)。
169
+ # @type var patterns: ::Array[[Data::SingleMachiAza | TownAlias, ::String]]
166
170
  patterns =
167
171
  towns.map do |town|
168
172
  pattern = Dict.to_regex_pattern(town_pattern_source(town.machi_aza_name))
@@ -190,12 +194,14 @@ module JapaneseAddressParser
190
194
 
191
195
  # JS: getSameNamedPrefectureCityRegexPatterns(prefApi)
192
196
  def get_same_named_prefecture_city_regex_patterns(pref_api)
193
- return @cached_same_named_prefecture_city_regex_patterns unless @cached_same_named_prefecture_city_regex_patterns.nil?
197
+ cached = @cached_same_named_prefecture_city_regex_patterns
198
+ return cached unless cached.nil?
194
199
 
195
200
  pref_list = pref_api.data
196
201
  # JS: pref.pref.replace(/[都|道|府|県]$/, '') — 文字クラス(| も含む)は上流のまま($ → \z)
197
202
  prefs = pref_list.map { |pref| pref.pref.sub(/[都|道|府|県]\z/, '') }
198
203
 
204
+ # @type var patterns: ::Array[[::String, ::String]]
199
205
  patterns = []
200
206
  pref_list.each do |pref|
201
207
  pref.cities.each do |city|
@@ -248,7 +254,8 @@ module JapaneseAddressParser
248
254
  # JS: firstLineEnd = data.indexOf('\n')(未検出は -1); rest = data.slice(firstLineEnd + 1)
249
255
  first_line_end = data.index("\n") || -1
250
256
  rest = data[(first_line_end + 1)..].to_s
251
- ::CSV.parse(rest, headers: true).filter_map do |row|
257
+ rows = _ = ::CSV.parse(rest, headers: true)
258
+ rows.filter_map do |row|
252
259
  point = point_from(row['lng'], row['lat'])
253
260
  if row.headers.include?('blk_num') # JS: 'blk_num' in line
254
261
  Data::SingleRsdt.new(blk_num: row['blk_num'], rsdt_num: row['rsdt_num'], rsdt_num2: row['rsdt_num2'], point:)
@@ -296,7 +303,7 @@ module JapaneseAddressParser
296
303
  x_cho = target_town_name.scan(/.町/) # JS: match(/.町/g)
297
304
  return false if x_cho.empty?
298
305
 
299
- !JapaneseNumeral.find_kanji_numbers(x_cho[0]).empty?
306
+ !JapaneseNumeral.find_kanji_numbers(x_cho[0].to_s).empty?
300
307
  end
301
308
 
302
309
  # JS: getTownRegexPatterns 内の machiAzaName(town).replace(...) チェーン(toRegexPattern へ渡す前段)。
@@ -314,6 +321,7 @@ module JapaneseAddressParser
314
321
  # JS: getTownRegexPatterns 内の数字パターン展開コールバック。
315
322
  def expand_town_number(match)
316
323
  suffix = /(丁目?|番(町|丁)|条|軒|線|(の|ノ)町|地割|号)/
324
+ # @type var patterns: ::Array[::String]
317
325
  patterns = []
318
326
  patterns << match.sub(suffix, '') # 漢数字(接尾辞を除く)
319
327
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Rich value object exposed by the v4 public API (M6).
3
+ # Rich value object exposed by the v4 public API.
4
4
  # Ruby-original enrichment layer over the faithful NormalizeResult (working_agreement §1-3).
5
5
  # 供給元: NormalizeResult#metadata.city(SingleCity VO)。
6
6
 
@@ -8,19 +8,20 @@ require 'japanese_address_parser/normalize_result_point'
8
8
 
9
9
  module JapaneseAddressParser
10
10
  # 市区町村のリッチ VO。name は cityName(郡+市+政令区)。point は代表点(NormalizeResultPoint, level 2)。
11
- City =
12
- ::Data.define(:name, :code, :county, :ward, :name_kana, :name_romaji, :point) do
13
- # metadata.city(SingleCity VO)から VO を作る。city 未判別なら nil。
14
- def self.from_metadata(city)
15
- return if city.nil?
11
+ City = ::Data.define(:name, :code, :county, :ward, :name_kana, :name_romaji, :point)
12
+ public_constant :City
16
13
 
17
- new(name: city.city_name, code: city.code, county: city.county, ward: city.ward, name_kana: city.city_k, name_romaji: city.city_r, point: ResultPoint.city_to_result_point(city))
18
- end
14
+ class City
15
+ # metadata.city(SingleCity VO)から VO を作る。city 未判別なら nil。
16
+ def self.from_metadata(city)
17
+ return if city.nil?
19
18
 
20
- # 緯度経度をネストも含めて Hash 化する Ruby 独自 API。
21
- def to_h
22
- { name:, code:, county:, ward:, name_kana:, name_romaji:, point: point.to_h }
23
- end
19
+ new(name: city.city_name, code: city.code, county: city.county, ward: city.ward, name_kana: city.city_k, name_romaji: city.city_r, point: ResultPoint.city_to_result_point(city))
24
20
  end
25
- public_constant :City
21
+
22
+ # 緯度経度をネストも含めて Hash 化する Ruby 独自 API。
23
+ def to_h
24
+ { name:, code:, county:, ward:, name_kana:, name_romaji:, point: point.to_h }
25
+ end
26
+ end
26
27
  end
@@ -7,13 +7,14 @@ module JapaneseAddressParser
7
7
  module Data
8
8
  # API レスポンスの meta。`updated` はデータの更新時刻で、町字/サブリソース取得時の
9
9
  # `?v={apiVersion}` に使う(キャッシュバスティング)。
10
- ApiMeta =
11
- ::Data.define(:updated) do
12
- # JSON(パース済み Hash・文字列キー)から VO を生成する Ruby 独自ヘルパ。
13
- def self.from_json(hash)
14
- new(updated: hash['updated'])
15
- end
16
- end
10
+ ApiMeta = ::Data.define(:updated)
17
11
  public_constant :ApiMeta
12
+
13
+ class ApiMeta
14
+ # JSON(パース済み Hash・文字列キー)から VO を生成する Ruby 独自ヘルパ。
15
+ def self.from_json(hash)
16
+ new(updated: hash['updated'])
17
+ end
18
+ end
18
19
  end
19
20
  end
@@ -9,13 +9,14 @@ require 'japanese_address_parser/data/single_machi_aza'
9
9
  module JapaneseAddressParser
10
10
  module Data
11
11
  # api/ja/{県}/{市}.json のレスポンス全体(町字一覧)。JS の MachiAzaApi。
12
- MachiAzaApi =
13
- ::Data.define(:meta, :data) do
14
- # JSON(パース済み Hash・文字列キー)から VO を生成する Ruby 独自ヘルパ。
15
- def self.from_json(hash)
16
- new(meta: ApiMeta.from_json(hash['meta']), data: (hash['data'] || []).map { |machi_aza| SingleMachiAza.from_json(machi_aza) })
17
- end
18
- end
12
+ MachiAzaApi = ::Data.define(:meta, :data)
19
13
  public_constant :MachiAzaApi
14
+
15
+ class MachiAzaApi
16
+ # JSON(パース済み Hash・文字列キー)から VO を生成する Ruby 独自ヘルパ。
17
+ def self.from_json(hash)
18
+ new(meta: ApiMeta.from_json(hash['meta']), data: (hash['data'] || []).map { |machi_aza| SingleMachiAza.from_json(machi_aza) })
19
+ end
20
+ end
20
21
  end
21
22
  end