npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

brazil-cities

v0.3.1

Published

A TypeScript library for querying Brazilian states and cities with utilities for filtering and searching.

Readme

🇧🇷 brazil-cities

A lightweight TypeScript/JavaScript utility for handling Brazilian states and cities with simple, typed access. Includes fast lookup, filtering, sorting, grouping, and analytics functions.


📦 Installation

Using npm:

npm install brazil-cities

Using yarn:

yarn add brazil-cities

Using pnpm:

pnpm add brazil-cities

🧩 Basic Usage

import { State, City, utils } from "brazil-cities";

// Find a state by abbreviation
const sp = utils.findStateByAbbr("SP");
console.log(sp?.name); // "São Paulo"

// Find a state by name
const rj = utils.findStateByName("Rio de Janeiro");

// Get all states
const allStates = utils.getAllStates();

// Find a city by name
const campinas = utils.findCityByName("Campinas");

// Filter cities by prefix across all states
const citiesWithSan = utils.filterCitiesByPrefix("San");

// Filter cities within a specific state by prefix
const citiesInSP = utils.filterCitiesInStateByPrefix(sp!, "Camp");

// Filter cities by substring ignoring accents
const citiesNormalized = utils.filterCitiesByNormalizedSubstring("sao");

// Filter cities by suffix
const citiesWithSuffix = utils.filterCitiesBySuffix("ópolis");

// Filter cities by word count
const twoWordCities = utils.filterCitiesByWordCount(2);

// Filter cities using regex
const citiesMatchingRegex = utils.filterCitiesByRegex(/^S/);

// Filter cities by state abbreviation
const citiesInState = utils.filterCitiesByStateAbbr("SP");

// Filter cities by ID range
const citiesInIdRange = utils.filterCitiesByIdRange(5300108, 5301000);

// Get states sorted alphabetically
const sortedStates = utils.getStatesSortedByName();

// Get cities sorted alphabetically
const sortedCities = utils.getCitiesSortedByName();

// Group cities alphabetically
const citiesGroupedByLetter = utils.groupCitiesByInitialLetter();

// Group cities by state
const citiesGroupedByState = utils.groupCitiesByState();

// Get top states by city count
const topStates = utils.getMostPopulatedStatesByCityCount(5);

// Get city name statistics
const stats = utils.getCityNameStats();
console.log(stats.total, stats.avgLength, stats.maxLength, stats.minLength);

🗺️ Data Structures

State

interface State {
  name: string; // e.g. "São Paulo"
  abbr: string; // e.g. "SP"
  cities: City[]; // List of cities in that state
}

City

interface City {
  id: number; // e.g. 3509502
  name: string; // e.g. "Campinas"
  stateAbbr: string; // e.g. "SP"
  stateName: string; // e.g. "São Paulo"
}

⚙️ API Reference

| Function | Description | Returns | | -------------------------------------------------- | ------------------------------------------------------ | -------------------------------------------- | | findStateByAbbr(abbr) | Finds a state by its abbreviation | State \| undefined | | findStateByName(name) | Finds a state by its full name | State \| undefined | | getAllStates() | Returns all states | State[] | | getAllStateAbbrs() | Returns all state abbreviations | string[] | | getAllStateNames() | Returns all state names | string[] | | getStatesWithoutCities() | Returns states without city lists | Omit<State, "cities">[] | | getStatesWithCities() | Returns states with their cities | State[] | | findCityByName(name) | Finds a city by its name | City \| undefined | | getAllCities() | Returns all cities in Brazil | City[] | | getCitiesFromState(identifier) | Returns all cities from a given state | City[] | | filterCitiesByPrefix(prefix) | Filters all cities starting with a given prefix | City[] | | filterCitiesInStateByPrefix(state, prefix) | Filters cities within a given state by prefix | City[] | | filterCitiesByInitialLetter(letter) | Filters cities by their first letter | City[] | | filterCitiesBySubstring(substring) | Filters cities containing a substring | City[] | | filterCitiesByNormalizedSubstring(substring) | Filters cities containing a substring ignoring accents | City[] | | filterCitiesBySuffix(suffix) | Filters cities ending with a specific suffix | City[] | | filterCitiesByWordCount(count) | Filters cities by number of words in their name | City[] | | filterCitiesByRegex(pattern) | Filters cities matching a regular expression | City[] | | filterCitiesByStateAbbr(abbr) | Filters cities from a specific state | City[] | | filterCitiesByIdRange(minId, maxId?) | Filters cities by ID range | City[] | | filterCitiesInStateBySubstring(state, substring) | Filters cities within a given state by substring | City[] | | getStatesSortedByName() | Returns all states sorted alphabetically | State[] | | getCitiesSortedByName() | Returns all cities sorted alphabetically | City[] | | getCitiesSortedByNameLength() | Returns all cities sorted by name length | City[] | | groupCitiesByInitialLetter() | Groups cities alphabetically by first letter | Record<string, City[]> | | groupCitiesByState() | Groups cities by state abbreviation | Record<string, City[]> | | getMostPopulatedStatesByCityCount(limit) | Returns top states by number of cities | State[] | | getCityNameStats() | Returns statistics about city names | { total, avgLength, maxLength, minLength } |


🧠 TypeScript Support

Fully written in TypeScript, with clear type exports and all utility functions accessible via utils:

import { State, City, utils } from "brazil-cities";

// Example usage:
const sp: State | undefined = utils.findStateByAbbr("SP");
const campinas: City | undefined = utils.findCityByName("Campinas");

All types (State, City) and functions (utils) are fully typed for autocomplete and safety.


🪶 License

MIT © 2025 — brazil-cities