jp-local-gov-id

app-1.2.0data-1.0.0

API

Overview of the client returned by createLocalGovClient.

createLocalGovClient(options)

OptionDescription
datanpm dataset (includes searchNgramShards)
urlVersioned index.json URL (resolves sibling .bin.br paths)
cachelocalStorage cache for url mode via @b4moss/cachian (default true). Keys use prefix jp-local-gov-id:
cacheTtlSecondsCache TTL in seconds (default 31536000)

Exactly one of data or url is required.

Data & search highlights

  • On-wire payloads are Brotli (.bin.br); the client decompresses then decodes
  • Nationwide string search uses hybrid JLIX (hot 2-gram regions + cold 3-gram shards)
  • After normalize: <2 → empty / 2 → 2-gram only / ≥3 → merge both
  • Index fetch: concurrency 3 with 100ms start stagger; candidate pref bins: concurrency 6
  • localStorage stores minified decoded JSON (not raw Brotli). Nationwide pref loads + JLIX are memory-only
  • schemaVersion is 2 (prefecture code is 6-digit; no prefecture* fields on prefectures)
  • 1.2.0: createLocalGovClient initial graph keeps search and @b4moss/cachian behind dynamic import (initial minify ≈ 24339 bytes)

Prefecture / Municipality

FieldPrefectureMunicipality
code6-digit local-gov code6-digit local-gov code
name / nameKanayesyes
prefectureCode / prefectureName / prefectureNameKananobelonging prefecture (2-digit + names)
municipalityCounts?{ both, city, ward }no

LocalGov = Prefecture | Municipality

Methods

MethodReturnsDescription
listPrefectures()Prefecture[]All prefectures (each code is 6-digit)
getPrefectureByCode(code)Prefecture | null2-digit org code or 6-digit entity code
getPrefectureCodeByName(name)string | nullOfficial name → 2-digit prefecture code
getMunicipalityCountByPrefecture(pref, options?)number | nullSync counts
listMunicipalitiesByPrefecture(pref, options?)Promise<Municipality[]>Lazy-load municipalities
getMunicipalityByCode(code)Promise<Municipality | null>Municipality 6-digit only
getByCode(code)Promise<LocalGov | null>2- or 6-digit (6-digit prefers prefecture entity)
searchByText(text, options?)Promise<LocalGov[]>Partial match
getLocalGovCodeByName(name, options?)Promise<string | null>Exact name → 6-digit local-gov code
purgeCache(options)Promise<void>Clear URL-mode localStorage cache ({ all: true } / { keys } / etc. — cachian CachePurgeOptions)

designatedCity

"both" (default) / "city" / "ward". Tokyo special wards are unaffected.

Search options

KeyDefaultDescription
prefecture—Scope to one prefecture (skips JLIX)
target'all''all' | 'prefectures' | 'cities'
matchField'both''name' | 'nameKana' | 'both'
designatedCity'both'Designated-city filter

Errors & empty results

  • Schema / invalid payload → LocalGovSchemaError
  • Network / HTTP → normal fetch errors
  • Missing / ambiguous → null / [] (no throw)