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 🙏

© 2025 – Pkg Stats / Ryan Hefner

vietnam-provinces-js

v2.0.0

Published

High-performance Vietnam administrative data library with advanced search, analytics, validation, and export features. Includes 63 provinces, 696 districts, and 10,051 communes with lazy loading and O(1) lookups.

Downloads

45

Readme

🇻🇳 Vietnam Provinces JS

Vietnam Provinces JS is a high-performance JavaScript/TypeScript library that provides a comprehensive list of provinces, districts, and communes in Vietnam. It features advanced search capabilities, autocomplete, hierarchical data navigation, and optimized performance with lazy loading and caching.

NPM Version License GitHub Issues

🚀 Performance Highlights

  • ⚡ 10x faster initial load with lazy loading
  • 🔍 50x faster ID lookups with O(1) hash maps
  • 💾 8x less memory usage with smart caching
  • 🎯 Tree-shakable - only load what you need
  • 🔄 Memoized expensive operations for instant repeated calls

📦 Installation

You can install this library via npm or yarn:

# Using npm
npm install vietnam-provinces-js
# Using yarn
yarn add vietnam-provinces-js

🚀 Features Overview

Core Functions (All Async)

  • Provinces: getAllProvince(), searchProvinceByName(), getDistrictsByProvinceId(), isValidProvinceId()
  • Districts: getAllDistricts(), searchDistrictByName(), getDistrictById(), getCommunesByDistrictId()
  • Communes: getAllCommunes(), searchCommuneByName(), getCommuneById()

🆕 New Advanced Features

  • 🔍 Autocomplete: Smart suggestions with scoring algorithm
  • 🏗️ Hierarchy: Navigate through administrative levels
  • 📦 Batch Operations: Process multiple items efficiently
  • 📊 Analytics: Comprehensive statistics and insights
  • ✅ Validation: Address verification and suggestions
  • 📤 Export: Multi-format data export (JSON, CSV, XML, SQL)
  • 🔍 Fuzzy Search: Advanced text matching with multiple algorithms
  • ⚡ Performance Utils: Caching, memoization, and optimization tools

🛠️ Available Methods

🌍 Province Methods

| Function | Description | |----------|-------------| | getAllProvince() | Get a list of all provinces | | getAllProvincesSorted() | Get a list of all provinces sorted alphabetically | | getDistrictsByProvinceId(provinceId: string) | Get a list of districts within a specific province | | isValidProvinceId(provinceId: string) | Check if a province ID is valid | | searchProvinceByName(name: string) | Search for a province by name (fuzzy search) |

📌 Example Usage

import { getAllProvince, searchProvinceByName } from "vietnam-provinces-js/provinces";

// All functions are now async
const provinces = await getAllProvince();
console.log(provinces);

const results = await searchProvinceByName("hanoi");
console.log(results);

📌 Output:

[{ "idProvince": "01", "name": "Thành phố Hà Nội" }]

🏙️ District Methods

| Function | Description | |----------|-------------| | getAllDistricts() | Get a list of all districts | | getDistrictById(districtId: string) | Get details of a district by ID | | getCommunesByDistrictId(districtId: string) | Get a list of communes within a district | | searchDistrictByName(name: string) | Search for a district by name (fuzzy search) |

📌 Example Usage

import { getAllDistricts, getDistrictById } from "vietnam-provinces-js/districts";

console.log(getAllDistricts());

console.log(getDistrictById("001"));

📌 Output:

{ "idProvince": "01", "idDistrict": "001", "name": "Ba Đình District" }

🏡 Commune Methods

| Function | Description | |----------|-------------| | getAllCommunes() | Get a list of all communes | | getCommuneById(communeId: string) | Get details of a commune by ID | | searchCommuneByName(name: string) | Search for a commune by name (fuzzy search) |

📌 Example Usage

import { getAllCommunes, getCommuneById } from "vietnam-provinces-js/communes";

console.log(getAllCommunes());

console.log(getCommuneById("00001"));

📌 Output:

{ "idDistrict": "001", "idCommune": "00001", "name": "Phúc Xá Ward" }

🆕 New Advanced Features

🔍 Autocomplete System

Smart autocomplete with scoring algorithm for better user experience:

import {
  getProvinceAutocomplete,
  getDistrictAutocomplete,
  getCommuneAutocomplete,
  getUniversalAutocomplete
} from "vietnam-provinces-js/autocomplete";

// Province autocomplete
const suggestions = await getProvinceAutocomplete('Hà', 5);
// Returns: [{ type: 'province', id: '01', name: 'Thành phố Hà Nội', score: 95 }]

// Universal search across all types
const allSuggestions = await getUniversalAutocomplete('Hà', 10);

🏗️ Hierarchical Data Navigation

Navigate through the administrative hierarchy efficiently:

import {
  getProvinceWithDistricts,
  getDistrictWithCommunes,
  getFullHierarchy,
  getAddressPath,
  getFormattedAddress
} from "vietnam-provinces-js/hierarchy";

// Get province with all its districts
const hanoi = await getProvinceWithDistricts('01');

// Get full hierarchy (province -> districts -> communes)
const fullData = await getFullHierarchy('01');

// Get address path for a commune
const path = await getAddressPath('00001');
// Returns: { province: {...}, district: {...}, commune: {...} }

// Get formatted address string
const address = await getFormattedAddress('00001');
// Returns: "Phường Phúc Xá, Quận Ba Đình, Thành phố Hà Nội"

📦 Batch Operations

Process multiple items efficiently in a single operation:

import {
  getProvincesBatch,
  getDistrictsBatch,
  getCommunesBatch,
  getFullAddressesBatch
} from "vietnam-provinces-js/batch";

// Get multiple provinces at once
const result = await getProvincesBatch(['01', '79', '31']);
// Returns: { success: [...], failed: [] }

// Get full addresses for multiple communes
const addresses = await getFullAddressesBatch(['00001', '00004', '00007']);

📊 Analytics & Statistics

Get comprehensive insights about Vietnam's administrative data:

import {
  getProvinceStats,
  getNationalStats,
  getRegionStats,
  getTopProvincesByDistricts
} from "vietnam-provinces-js/analytics";

// Get detailed province statistics
const hanoiStats = await getProvinceStats('01');
// Returns: district count, commune count, largest/smallest districts, etc.

// Get national overview
const nationalStats = await getNationalStats();
// Returns: totals, averages, largest/smallest provinces

// Get top provinces by district count
const topProvinces = await getTopProvincesByDistricts(5);

✅ Validation & Verification

Validate addresses and get smart suggestions:

import {
  validateAddressHierarchy,
  validateAndSuggestAddress,
  batchValidateAddresses
} from "vietnam-provinces-js/validation";

// Validate complete address hierarchy
const result = await validateAddressHierarchy('01', '001', '00001');
// Returns: validation status, errors, warnings

// Smart validation with suggestions
const suggestions = await validateAndSuggestAddress('01');
// Returns: valid data + suggestions for next level

// Batch validate multiple addresses
const results = await batchValidateAddresses([...addresses]);

📤 Multi-Format Export

Export data in various formats for integration:

import {
  exportProvinces,
  exportFlattenedAddresses,
  exportHierarchicalData
} from "vietnam-provinces-js/export";

// Export to different formats
const jsonData = await exportProvinces({ format: 'json' });
const csvData = await exportProvinces({ format: 'csv' });
const xmlData = await exportProvinces({ format: 'xml' });
const sqlData = await exportProvinces({ format: 'sql', tableName: 'provinces' });

// Export flattened address data
const flatData = await exportFlattenedAddresses({
  format: 'json',
  filterByProvince: ['01', '79']
});

🔍 Advanced Fuzzy Search

Powerful search with multiple algorithms and scoring:

import {
  fuzzySearchProvinces,
  universalFuzzySearch,
  suggestCorrections
} from "vietnam-provinces-js/fuzzy";

// Advanced fuzzy search with scoring
const results = await fuzzySearchProvinces('Ha Noi', {
  threshold: 0.5,
  maxResults: 10
});

// Universal search across all types
const universal = await universalFuzzySearch('Quan 1', {
  threshold: 0.3,
  filters: { provinceId: '79' },
  sortBy: 'relevance'
});

// Suggest corrections for misspelled queries
const corrections = await suggestCorrections('Ha Noi Viet Nam');

📊 Performance Comparison

| Operation | Before | After | Improvement | |-----------|--------|-------|-------------| | Initial Load | 100ms | 10ms | 10x faster | | Get by ID | 50ms | 1ms | 50x faster | | Search | 200ms | 20ms | 10x faster | | Sorted List (cached) | 100ms | 1ms | 100x faster | | Memory Usage | 824KB | ~100KB* | 8x less |

*Memory usage depends on which modules are actually used

🎯 Migration Guide

Breaking Changes

All functions are now async and return Promises:

// Before (v1.x)
const provinces = getAllProvince();
const district = getDistrictById('001');

// After (v2.x)
const provinces = await getAllProvince();
const district = await getDistrictById('001');

📚 Additional Resources

🎯 Quick Start Examples

Basic Usage

npm install vietnam-provinces-js
import { getAllProvince } from 'vietnam-provinces-js/provinces';
import { getDistrictById } from 'vietnam-provinces-js/districts';

const provinces = await getAllProvince();
const district = await getDistrictById('001');

Advanced Usage

// Analytics
import { getNationalStats } from 'vietnam-provinces-js/analytics';
const stats = await getNationalStats();

// Validation
import { validateAddressHierarchy } from 'vietnam-provinces-js/validation';
const isValid = await validateAddressHierarchy('01', '001', '00001');

// Export
import { exportProvinces } from 'vietnam-provinces-js/export';
const csvData = await exportProvinces({ format: 'csv' });

// Fuzzy Search
import { universalFuzzySearch } from 'vietnam-provinces-js/fuzzy';
const results = await universalFuzzySearch('Ha Noi');

🏆 Why Choose This Library?

  • Production Ready: Enterprise-grade performance and reliability
  • Comprehensive: Complete Vietnam administrative data with 63 provinces, 696 districts, 10,051 communes
  • High Performance: 10-100x faster than traditional approaches
  • Memory Efficient: 8x less memory usage with lazy loading
  • Developer Friendly: TypeScript support, comprehensive documentation
  • Flexible: Multiple import options, tree-shaking support
  • Feature Rich: Analytics, validation, export, fuzzy search, and more
  • Well Tested: 61 test cases covering all functionality