chilean-territorial-divisions 1.0.0
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.
- package/README.md +196 -0
- package/dist/index.cjs +1963 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +151 -0
- package/dist/index.d.ts +151 -0
- package/dist/index.global.js +1947 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +1922 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
# Chilean Territorial Divisions
|
|
2
|
+
|
|
3
|
+
Political and administrative divisions of Chile: regions, provinces, and communes.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/chilean-territorial-divisions)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install chilean-territorial-divisions
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
### ESM (recommended)
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import {
|
|
20
|
+
getRegiones,
|
|
21
|
+
getRegionByNumber,
|
|
22
|
+
getComunaByCode,
|
|
23
|
+
searchComunas,
|
|
24
|
+
} from 'chilean-territorial-divisions';
|
|
25
|
+
|
|
26
|
+
// Get all regions
|
|
27
|
+
const regiones = getRegiones(); // 16 regions
|
|
28
|
+
|
|
29
|
+
// Find region by number
|
|
30
|
+
const rm = getRegionByNumber('XIII');
|
|
31
|
+
console.log(rm?.region); // "Región Metropolitana de Santiago"
|
|
32
|
+
|
|
33
|
+
// Find commune by CUT code
|
|
34
|
+
const santiago = getComunaByCode('13101');
|
|
35
|
+
console.log(santiago?.comuna.name); // "Santiago"
|
|
36
|
+
console.log(santiago?.provincia); // "Santiago"
|
|
37
|
+
console.log(santiago?.region); // "Región Metropolitana de Santiago"
|
|
38
|
+
|
|
39
|
+
// Search communes by name
|
|
40
|
+
const results = searchComunas('viña');
|
|
41
|
+
console.log(results[0].comuna.name); // "Viña del Mar"
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### CommonJS
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
const { getRegiones, getComunaByCode } = require('chilean-territorial-divisions');
|
|
48
|
+
|
|
49
|
+
const regiones = getRegiones();
|
|
50
|
+
console.log(regiones.length); // 16
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## API
|
|
54
|
+
|
|
55
|
+
### Region Functions
|
|
56
|
+
|
|
57
|
+
| Function | Description |
|
|
58
|
+
|----------|-------------|
|
|
59
|
+
| `getRegiones()` | Returns all 16 regions of Chile |
|
|
60
|
+
| `getRegionByNumber(number)` | Find region by Roman numeral (e.g., "XIII", "V", "XV") |
|
|
61
|
+
| `getRegionByISO(iso)` | Find by ISO 3166-2 code (e.g., "CL-RM", "CL-AP") |
|
|
62
|
+
| `getRegionByName(name)` | Find by name (partial, case-insensitive) |
|
|
63
|
+
|
|
64
|
+
### Province Functions
|
|
65
|
+
|
|
66
|
+
| Function | Description |
|
|
67
|
+
|----------|-------------|
|
|
68
|
+
| `getProvincias(regionNumber)` | Returns provinces of a region |
|
|
69
|
+
| `getAllProvincias()` | Returns all provinces of Chile |
|
|
70
|
+
|
|
71
|
+
### Commune Functions
|
|
72
|
+
|
|
73
|
+
| Function | Description |
|
|
74
|
+
|----------|-------------|
|
|
75
|
+
| `getComunas(regionNumber, provinciaName?)` | Communes of a region or province |
|
|
76
|
+
| `getComunaByCode(code)` | Find commune by CUT code |
|
|
77
|
+
| `searchComunas(query)` | Text search (case-insensitive) |
|
|
78
|
+
| `getAllComunas()` | Returns all communes of Chile |
|
|
79
|
+
|
|
80
|
+
### Select/Dropdown Helpers
|
|
81
|
+
|
|
82
|
+
| Function | Description |
|
|
83
|
+
|----------|-------------|
|
|
84
|
+
| `getRegionOptions()` | Regions as `{label, value}` for selects |
|
|
85
|
+
| `getProvinciaOptions(regionNumber)` | Provinces of a region for selects |
|
|
86
|
+
| `getComunaOptions(regionNumber, provinciaName?)` | Communes for selects (value = CUT code) |
|
|
87
|
+
| `getAllComunaOptions()` | All communes for autocomplete |
|
|
88
|
+
|
|
89
|
+
#### Example: Cascading Select (React)
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
import { useState } from 'react';
|
|
93
|
+
import {
|
|
94
|
+
getRegionOptions,
|
|
95
|
+
getProvinciaOptions,
|
|
96
|
+
getComunaOptions,
|
|
97
|
+
} from 'chilean-territorial-divisions';
|
|
98
|
+
|
|
99
|
+
function AddressForm() {
|
|
100
|
+
const [region, setRegion] = useState('');
|
|
101
|
+
const [provincia, setProvincia] = useState('');
|
|
102
|
+
const [comuna, setComuna] = useState('');
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<>
|
|
106
|
+
<select value={region} onChange={(e) => { setRegion(e.target.value); setProvincia(''); setComuna(''); }}>
|
|
107
|
+
<option value="">Select region</option>
|
|
108
|
+
{getRegionOptions().map((o) => <option key={o.value} value={o.value}>{o.label}</option>)}
|
|
109
|
+
</select>
|
|
110
|
+
|
|
111
|
+
<select value={provincia} onChange={(e) => { setProvincia(e.target.value); setComuna(''); }} disabled={!region}>
|
|
112
|
+
<option value="">Select province</option>
|
|
113
|
+
{getProvinciaOptions(region).map((o) => <option key={o.value} value={o.value}>{o.label}</option>)}
|
|
114
|
+
</select>
|
|
115
|
+
|
|
116
|
+
<select value={comuna} onChange={(e) => setComuna(e.target.value)} disabled={!region}>
|
|
117
|
+
<option value="">Select commune</option>
|
|
118
|
+
{getComunaOptions(region, provincia || undefined).map((o) => <option key={o.value} value={o.value}>{o.label}</option>)}
|
|
119
|
+
</select>
|
|
120
|
+
</>
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### Example: Region → Commune Only (no province)
|
|
126
|
+
|
|
127
|
+
```tsx
|
|
128
|
+
import { useState } from 'react';
|
|
129
|
+
import { getRegionOptions, getComunaOptions } from 'chilean-territorial-divisions';
|
|
130
|
+
|
|
131
|
+
function SimpleAddressForm() {
|
|
132
|
+
const [region, setRegion] = useState('');
|
|
133
|
+
const [comuna, setComuna] = useState('');
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<>
|
|
137
|
+
<select value={region} onChange={(e) => { setRegion(e.target.value); setComuna(''); }}>
|
|
138
|
+
<option value="">Select region</option>
|
|
139
|
+
{getRegionOptions().map((o) => <option key={o.value} value={o.value}>{o.label}</option>)}
|
|
140
|
+
</select>
|
|
141
|
+
|
|
142
|
+
<select value={comuna} onChange={(e) => setComuna(e.target.value)} disabled={!region}>
|
|
143
|
+
<option value="">Select commune</option>
|
|
144
|
+
{getComunaOptions(region).map((o) => <option key={o.value} value={o.value}>{o.label}</option>)}
|
|
145
|
+
</select>
|
|
146
|
+
</>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## TypeScript Types
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
interface Region {
|
|
155
|
+
region: string; // Full name
|
|
156
|
+
region_number: string; // Roman numeral (I, II, ..., XV, XVI, XIII)
|
|
157
|
+
region_iso_3166_2: string; // ISO code (CL-AP, CL-RM, etc.)
|
|
158
|
+
provincias: Provincia[];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
interface Provincia {
|
|
162
|
+
name: string;
|
|
163
|
+
comunas: Comuna[];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
interface Comuna {
|
|
167
|
+
name: string;
|
|
168
|
+
code: string; // Unique Territorial Code (CUT)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
interface ComunaSearchResult {
|
|
172
|
+
comuna: Comuna;
|
|
173
|
+
provincia: string;
|
|
174
|
+
region: string;
|
|
175
|
+
region_number: string;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
interface SelectOption {
|
|
179
|
+
label: string;
|
|
180
|
+
value: string;
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Data
|
|
185
|
+
|
|
186
|
+
- **16 regions** of Chile
|
|
187
|
+
- **56 provinces**
|
|
188
|
+
- **346 communes**
|
|
189
|
+
|
|
190
|
+
Includes official codes:
|
|
191
|
+
- **CUT** (Código Único Territorial) for each commune
|
|
192
|
+
- **ISO 3166-2:CL** for each region
|
|
193
|
+
|
|
194
|
+
## License
|
|
195
|
+
|
|
196
|
+
MIT © Hans Steven Vergara Chamorro
|