Skip to content

Commit 9a904ee

Browse files
committed
fix(get-municipality-by-cep): refresh the CEP ranges with the other datasets
CONTRIBUTING.md says the municipality CEP ranges are generated "the same way" as the other datasets, but scripts/data.ts never ran municipality-cep-ranges.ts, so the Update datasets workflow never refreshed, linted or formatted its output, and the data-summary entry for it could never be reached. It now runs after the other generators, since it is joined against the municipality table cities.ts rewrites, and its output is in the list the run lints and formats. The generator also stops reimplementing normalizeMunicipalityName: it loads the helper through the same extensionless import hook it already registers for the dataset, so the table is built with exactly the normalization it is read with. The OTHER_NAMES JSDoc said the variants were recent official renames, citing the IBGE; most are hyphen and accent spellings of the Correios CEP range mirror, which is now what it says and cites. The getting-started tables said 5574 ranges; the table holds 5573. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RLkm9YrtAifc6XCLFVEsdH
1 parent 3c85a01 commit 9a904ee

7 files changed

Lines changed: 26 additions & 28 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ release-please turns merged commits into releases.
9494

9595
## Datasets
9696

97-
The tables under `src/_internals/constants/` fall in two groups, and only the first refreshes
98-
itself:
97+
The tables under `src/_internals/constants/` fall in three groups, and only the first two refresh
98+
themselves:
9999

100100
- **Generated from an official source** by a script in `scripts/` (`npm run build:data`, run
101101
every Monday by the `Update datasets` workflow): banks (Banco Central, `banks.ts`), CBO
@@ -104,7 +104,7 @@ itself:
104104
`ncm.ts`). When a run changes a file, the workflow opens a pull request whose description, written
105105
by `scripts/data-summary.ts`, lists per table how many entries were added and removed, with a
106106
sample of each. Never edit these files by hand.
107-
- **Generated from a fetchable mirror of an official source**, the same way, but pinned to a
107+
- **Generated from a fetchable mirror of an official source**, by the same `npm run build:data` run (after the municipalities, which it is joined against), but pinned to a
108108
specific revision of the mirror because the official source itself cannot be fetched by a
109109
script: the CEP range of each municipality (Correios "Busca Faixa de CEP", via a community CSV
110110
mirror of its answers, `municipality-cep-ranges.ts`, writing `src/get-municipality-by-cep/constants.ts`).

‎docs/getting-started.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ A few utils embed an official dataset and weigh far more than everything else co
7070
| --- | --- | --- | --- |
7171
| `getMunicipalities` · `getMunicipalityByCode` · `getMunicipality` | 5571 IBGE municipalities, with names and codes | 154.9 - 156.5 KB | 50.3 - 50.4 KB |
7272
| `getCities` | 5571 IBGE municipality names | 154.3 KB | 49.9 KB |
73-
| `getMunicipalityByCep` | The IBGE municipality table above, plus 5574 Correios CEP ranges | 393.3 KB | 98.8 KB |
73+
| `getMunicipalityByCep` | The IBGE municipality table above, plus 5573 Correios CEP ranges | 393.3 KB | 98.8 KB |
7474
| `isValidNcm` | NCM (Nomenclatura Comum do Mercosul) codes | 114.2 KB | 24.6 KB |
7575
| `isValidCbo` · `getCbo` | CBO 2002 occupation titles | 119.1 KB | 30.6 KB |
7676
| `isValidCnae` · `getCnae` | CNAE-Subclasses 2.3 | 93.9 KB | 21.2 KB |

‎docs/pt-br/getting-started.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Alguns utilitários embutem uma base de dados oficial e pesam muito mais que tod
7070
| --- | --- | --- | --- |
7171
| `getMunicipalities` · `getMunicipalityByCode` · `getMunicipality` | 5571 municípios do IBGE, com nomes e códigos | 154,9 - 156,5 KB | 50,3 - 50,4 KB |
7272
| `getCities` | nomes dos 5571 municípios do IBGE | 154,3 KB | 49,9 KB |
73-
| `getMunicipalityByCep` | a tabela de municípios do IBGE acima, mais 5574 faixas de CEP dos Correios | 393,3 KB | 98,8 KB |
73+
| `getMunicipalityByCep` | a tabela de municípios do IBGE acima, mais 5573 faixas de CEP dos Correios | 393,3 KB | 98,8 KB |
7474
| `isValidNcm` | códigos NCM (Nomenclatura Comum do Mercosul) | 114,2 KB | 24,6 KB |
7575
| `isValidCbo` · `getCbo` | títulos das ocupações da CBO 2002 | 119,1 KB | 30,6 KB |
7676
| `isValidCnae` · `getCnae` | CNAE-Subclasses 2.3 | 93,9 KB | 21,2 KB |

‎scripts/cities.ts‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ const isCity = (value: unknown): value is City =>
5757
"regiao-imediata" in value;
5858

5959
/**
60-
* Alternative spellings kept for a handful of municipalities whose name changed at the IBGE
61-
* more recently than a third party dataset this library cross-checks against, keyed by the
62-
* 7-digit IBGE code. Curated by hand from the mismatches found joining
60+
* Alternative spellings the Correios CEP range mirror uses for a handful of municipalities,
61+
* mostly hyphen and accent variants and a couple of older names, keyed by the 7-digit IBGE code. Curated by hand from the mismatches found joining
6362
* https://gist.githubusercontent.com/hugosenari/ec1a7d88f5bdd01844424dbc9aff9590/raw/9aeb90ef777131ffaf1a6f5381d1f163c9c79b09/ceps.csv
6463
* against the IBGE municipalities below (see `scripts/municipality-cep-ranges.ts`); `main`
6564
* below fails the build if a code here stops matching a real municipality, so a future IBGE
@@ -180,12 +179,12 @@ ${body}
180179
};
181180
182181
/**
183-
* Alternative spellings of a municipality name, keyed by its 7-digit IBGE code, for the
184-
* municipalities a source outside the IBGE still spells differently, usually because of a
185-
* recent official rename. A municipality with no variant is simply absent, at no extra cost.
186-
* Curated by \`scripts/cities.ts\`.
182+
* Alternative spellings of a municipality name, keyed by its 7-digit IBGE code: the spellings
183+
* the Correios CEP range mirror uses where it differs from the IBGE, mostly hyphen and accent
184+
* variants (Biritiba Mirim, Grão-Pará) and a couple of older names. A municipality with no
185+
* variant is simply absent, at no extra cost. Curated by \`scripts/cities.ts\`.
187186
*
188-
* @see Official: https://servicodados.ibge.gov.br/api/docs/localidades
187+
* @see Based on: https://gist.github.com/hugosenari/ec1a7d88f5bdd01844424dbc9aff9590
189188
*/
190189
export const OTHER_NAMES: Readonly<Record<string, readonly string[]>> = {
191190
${otherNamesBody}

‎scripts/data.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const generatedFiles = [
3737
"./src/_internals/constants/cnae.ts",
3838
"./src/_internals/constants/municipalities.ts",
3939
"./src/_internals/constants/states.ts",
40+
"./src/get-municipality-by-cep/constants.ts",
4041
"./src/is-valid-legal-nature/constants.ts",
4142
"./src/is-valid-ncm/constants.ts",
4243
];
@@ -45,6 +46,10 @@ const results = await Promise.all(
4546
generators.map((generator) => run("node", [resolve(scriptsDir, generator)])),
4647
);
4748

49+
// The CEP ranges are joined against the municipality table cities.ts has just rewritten, so
50+
// they are generated after it rather than alongside it.
51+
results.push(await run("node", [resolve(scriptsDir, "municipality-cep-ranges.ts")]));
52+
4853
// Lint and format before checking the generators, so a failing generator never leaves
4954
// unformatted files behind in the working tree. `vp fmt` runs last because `vp lint --fix`
5055
// rewrites code without reformatting it.

‎scripts/municipality-cep-ranges.ts‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { register } from "node:module";
55
import { resolve } from "node:path";
66

77
import { fetchWithRetry } from "../src/_internals/fetch-with-retry/fetch-with-retry.ts";
8-
import { removeAccents } from "../src/remove-accents/remove-accents.ts";
98

109
const scriptsDir = import.meta.dirname;
1110

@@ -37,15 +36,10 @@ const { DATA, OTHER_NAMES } = await import("../src/_internals/constants/municipa
3736

3837
type StateCode = keyof typeof DATA;
3938

40-
const WHITESPACE_RUN_REGEX = /\s+/g;
41-
42-
// Same composition as `normalizeMunicipalityName`, reimplemented from the `removeAccents`
43-
// primitive it is itself built from: that helper's own import of `removeAccents` omits the
44-
// file extension (the convention every `src/` module follows, resolved by the bundler), which
45-
// a plain `node` process run from `scripts/` cannot resolve, unlike every other `scripts/`
46-
// import, which always carries its extension.
47-
const normalizeMunicipalityName = (value: string): string =>
48-
removeAccents(value).replaceAll(WHITESPACE_RUN_REGEX, " ").trim().toUpperCase();
39+
// The same helper getMunicipalityByCep matches names with, loaded through the hook above like
40+
// the dataset, so the table is built with exactly the normalization it is read with.
41+
const { normalizeMunicipalityName } =
42+
await import("../src/_internals/normalize-municipality-name/normalize-municipality-name.ts");
4943

5044
// Pinned to a specific commit of the gist, so a re-run always reads the exact CSV this table
5145
// was cross-checked against; bumping it is a deliberate, reviewed change.

‎src/_internals/constants/municipalities.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5643,12 +5643,12 @@ export const DATA: Record<StateCode, readonly (readonly [string, string])[]> = {
56435643
};
56445644

56455645
/**
5646-
* Alternative spellings of a municipality name, keyed by its 7-digit IBGE code, for the
5647-
* municipalities a source outside the IBGE still spells differently, usually because of a
5648-
* recent official rename. A municipality with no variant is simply absent, at no extra cost.
5649-
* Curated by `scripts/cities.ts`.
5646+
* Alternative spellings of a municipality name, keyed by its 7-digit IBGE code: the spellings
5647+
* the Correios CEP range mirror uses where it differs from the IBGE, mostly hyphen and accent
5648+
* variants (Biritiba Mirim, Grão-Pará) and a couple of older names. A municipality with no
5649+
* variant is simply absent, at no extra cost. Curated by `scripts/cities.ts`.
56505650
*
5651-
* @see Official: https://servicodados.ibge.gov.br/api/docs/localidades
5651+
* @see Based on: https://gist.github.com/hugosenari/ec1a7d88f5bdd01844424dbc9aff9590
56525652
*/
56535653
export const OTHER_NAMES: Readonly<Record<string, readonly string[]>> = {
56545654
"1400605": ["São Luiz"],

0 commit comments

Comments
 (0)