@aprestmo/norway-geodata 0.1.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/LICENSE +22 -0
- package/README.md +288 -0
- package/data/fylker-2025.json +77 -0
- package/data/kommuner-2025.json +24802 -0
- package/data/postal-codes-2025.json +25687 -0
- package/dist/esm/index.d.ts +207 -0
- package/dist/esm/index.js +397 -0
- package/dist/esm/types.d.ts +111 -0
- package/dist/esm/types.js +2 -0
- package/dist/index.d.ts +207 -0
- package/dist/index.js +433 -0
- package/dist/types.d.ts +111 -0
- package/dist/types.js +3 -0
- package/package.json +95 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import type { Municipality, County, PostalCode, PopulationDensityStats, LanguageStatus, MunicipalitySearchOptions, MunicipalityFilterOptions, PostalCodeSearchOptions } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Get the library version following semantic versioning.
|
|
4
|
+
* @returns Version string (e.g., "1.0.0")
|
|
5
|
+
*/
|
|
6
|
+
export declare function getVersion(): string;
|
|
7
|
+
/**
|
|
8
|
+
* Get all municipalities with metadata.
|
|
9
|
+
* @returns Array of all municipalities
|
|
10
|
+
*/
|
|
11
|
+
export declare function getMunicipalities(): readonly Municipality[];
|
|
12
|
+
/**
|
|
13
|
+
* Get a municipality by municipality ID.
|
|
14
|
+
* @param id - 4-digit municipality ID (e.g., "0301" for Oslo)
|
|
15
|
+
* @returns Municipality object or undefined if not found
|
|
16
|
+
* @throws {TypeError} If id is not a string
|
|
17
|
+
*/
|
|
18
|
+
export declare function getMunicipalityById(id: string): Municipality | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Get municipalities by name (case-insensitive partial match by default).
|
|
21
|
+
* @param name - Municipality name or partial name to search for
|
|
22
|
+
* @param options - Search options
|
|
23
|
+
* @returns Array of matching municipalities
|
|
24
|
+
* @throws {TypeError} If name is not a string
|
|
25
|
+
*/
|
|
26
|
+
export declare function getMunicipalitiesByName(name: string, options?: MunicipalitySearchOptions): Municipality[];
|
|
27
|
+
/**
|
|
28
|
+
* Get all counties (fylker) with metadata.
|
|
29
|
+
* @returns Array of all counties
|
|
30
|
+
*/
|
|
31
|
+
export declare function getCounties(): readonly County[];
|
|
32
|
+
/**
|
|
33
|
+
* Get a county by county ID.
|
|
34
|
+
* @param id - 2-digit county ID (e.g., "03" for Oslo)
|
|
35
|
+
* @returns County object or undefined if not found
|
|
36
|
+
* @throws {TypeError} If id is not a string
|
|
37
|
+
*/
|
|
38
|
+
export declare function getCountyById(id: string): County | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Get county by name (case-insensitive partial match).
|
|
41
|
+
* @param name - County name or partial name to search for
|
|
42
|
+
* @param exactMatch - Whether to match exactly (default: false)
|
|
43
|
+
* @returns County object or undefined if not found
|
|
44
|
+
* @throws {TypeError} If name is not a string
|
|
45
|
+
*/
|
|
46
|
+
export declare function getCountyByName(name: string, exactMatch?: boolean): County | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Get all municipalities in a given county.
|
|
49
|
+
* @param countyId - 2-digit county ID (first two digits of municipality k_id)
|
|
50
|
+
* @returns Array of municipalities in the county
|
|
51
|
+
* @throws {TypeError} If countyId is not a string
|
|
52
|
+
*/
|
|
53
|
+
export declare function getMunicipalitiesByCounty(countyId: string): Municipality[];
|
|
54
|
+
/**
|
|
55
|
+
* Get municipality by postal code.
|
|
56
|
+
* @param postalCode - Postal code to search for
|
|
57
|
+
* @returns Municipality object or undefined if not found
|
|
58
|
+
* @throws {TypeError} If postal code is not a valid number
|
|
59
|
+
*/
|
|
60
|
+
export declare function getMunicipalityByPostalCode(postalCode: string | number): Municipality | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Get all postal codes for a specific municipality.
|
|
63
|
+
* @param municipalityId - 4-digit municipality ID
|
|
64
|
+
* @returns Array of postal codes or undefined if municipality not found
|
|
65
|
+
*/
|
|
66
|
+
export declare function getPostalCodesByMunicipality(municipalityId: string): readonly string[] | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Get all postal codes from all municipalities.
|
|
69
|
+
* @param includeDetails - If true, returns objects with postal code and place name. If false, returns only postal codes (default: false)
|
|
70
|
+
* @returns Array of postal codes or postal code objects
|
|
71
|
+
*/
|
|
72
|
+
export declare function getAllPostalCodes(includeDetails?: boolean): readonly string[] | readonly {
|
|
73
|
+
zip: string;
|
|
74
|
+
place: string;
|
|
75
|
+
municipalityId: string;
|
|
76
|
+
municipalityName: string;
|
|
77
|
+
}[];
|
|
78
|
+
/**
|
|
79
|
+
* Get municipalities sorted by population.
|
|
80
|
+
* @param ascending - Sort in ascending order if true (default: false for descending)
|
|
81
|
+
* @returns Array of municipalities sorted by population
|
|
82
|
+
*/
|
|
83
|
+
export declare function getMunicipalitiesByPopulation(ascending?: boolean): Municipality[];
|
|
84
|
+
/**
|
|
85
|
+
* Get municipalities sorted by area.
|
|
86
|
+
* @param ascending - Sort in ascending order if true (default: false for descending)
|
|
87
|
+
* @returns Array of municipalities sorted by area
|
|
88
|
+
*/
|
|
89
|
+
export declare function getMunicipalitiesByArea(ascending?: boolean): Municipality[];
|
|
90
|
+
/**
|
|
91
|
+
* Get municipalities by language status.
|
|
92
|
+
* @param language - Language status
|
|
93
|
+
* @returns Array of municipalities with the specified language status
|
|
94
|
+
* @throws {TypeError} If language is not a string
|
|
95
|
+
*/
|
|
96
|
+
export declare function getMunicipalitiesByLanguage(language: LanguageStatus): Municipality[];
|
|
97
|
+
/**
|
|
98
|
+
* Get municipalities with advanced filtering options.
|
|
99
|
+
* @param options - Filter options
|
|
100
|
+
* @returns Array of municipalities matching the filter criteria
|
|
101
|
+
*/
|
|
102
|
+
export declare function getMunicipalitiesFiltered(options: MunicipalityFilterOptions): Municipality[];
|
|
103
|
+
/**
|
|
104
|
+
* Get total population of all municipalities.
|
|
105
|
+
* @returns Total population
|
|
106
|
+
*/
|
|
107
|
+
export declare function getTotalPopulation(): number;
|
|
108
|
+
/**
|
|
109
|
+
* Get total area of all municipalities.
|
|
110
|
+
* @returns Total area in square kilometers
|
|
111
|
+
*/
|
|
112
|
+
export declare function getTotalArea(): number;
|
|
113
|
+
/**
|
|
114
|
+
* Get population density statistics.
|
|
115
|
+
* @returns Object with min, max, and average population density
|
|
116
|
+
*/
|
|
117
|
+
export declare function getPopulationDensityStats(): PopulationDensityStats;
|
|
118
|
+
/**
|
|
119
|
+
* Get municipalities by population density range.
|
|
120
|
+
* @param minDensity - Minimum population density (people per km²)
|
|
121
|
+
* @param maxDensity - Maximum population density (people per km²)
|
|
122
|
+
* @returns Array of municipalities within the density range
|
|
123
|
+
*/
|
|
124
|
+
export declare function getMunicipalitiesByPopulationDensity(minDensity?: number, maxDensity?: number): Municipality[];
|
|
125
|
+
/**
|
|
126
|
+
* Get the largest municipalities by population.
|
|
127
|
+
* @param count - Number of municipalities to return (default: 10)
|
|
128
|
+
* @returns Array of largest municipalities by population
|
|
129
|
+
*/
|
|
130
|
+
export declare function getLargestMunicipalities(count?: number): Municipality[];
|
|
131
|
+
/**
|
|
132
|
+
* Get the smallest municipalities by population.
|
|
133
|
+
* @param count - Number of municipalities to return (default: 10)
|
|
134
|
+
* @returns Array of smallest municipalities by population
|
|
135
|
+
*/
|
|
136
|
+
export declare function getSmallestMunicipalities(count?: number): Municipality[];
|
|
137
|
+
/**
|
|
138
|
+
* Get all postal codes with metadata.
|
|
139
|
+
* @returns Array of all postal codes
|
|
140
|
+
*/
|
|
141
|
+
export declare function getPostalCodes(): readonly PostalCode[];
|
|
142
|
+
/**
|
|
143
|
+
* Get postal code information by postal code.
|
|
144
|
+
* @param code - 4-digit postal code (e.g., "0001")
|
|
145
|
+
* @returns PostalCode object or undefined if not found
|
|
146
|
+
* @throws {TypeError} If code is not a string
|
|
147
|
+
*/
|
|
148
|
+
export declare function getPostalCodeByCode(code: string): PostalCode | undefined;
|
|
149
|
+
/**
|
|
150
|
+
* Get postal codes by postal place (case-insensitive partial match by default).
|
|
151
|
+
* @param place - Postal place name or partial name to search for
|
|
152
|
+
* @param options - Search options
|
|
153
|
+
* @returns Array of matching postal codes
|
|
154
|
+
* @throws {TypeError} If place is not a string
|
|
155
|
+
*/
|
|
156
|
+
export declare function getPostalCodesByPlace(place: string, options?: PostalCodeSearchOptions): PostalCode[];
|
|
157
|
+
/**
|
|
158
|
+
* Get postal codes by municipality ID.
|
|
159
|
+
* @param municipalityId - 4-digit municipality ID (e.g., "0301" for Oslo)
|
|
160
|
+
* @returns Array of postal codes for the municipality
|
|
161
|
+
* @throws {TypeError} If municipalityId is not a string
|
|
162
|
+
*/
|
|
163
|
+
export declare function getPostalCodesByMunicipalityId(municipalityId: string): PostalCode[];
|
|
164
|
+
/**
|
|
165
|
+
* Get unique postal places.
|
|
166
|
+
* @returns Array of unique postal place names sorted alphabetically
|
|
167
|
+
*/
|
|
168
|
+
export declare function getUniquePostalPlaces(): readonly string[];
|
|
169
|
+
/**
|
|
170
|
+
* Get postal codes within a range.
|
|
171
|
+
* @param startCode - Starting postal code (inclusive)
|
|
172
|
+
* @param endCode - Ending postal code (inclusive)
|
|
173
|
+
* @returns Array of postal codes within the range
|
|
174
|
+
* @throws {TypeError} If start or end codes are not strings
|
|
175
|
+
*/
|
|
176
|
+
export declare function getPostalCodesInRange(startCode: string, endCode: string): PostalCode[];
|
|
177
|
+
/**
|
|
178
|
+
* Get postal codes statistics.
|
|
179
|
+
* @returns Object with total count and unique places count
|
|
180
|
+
*/
|
|
181
|
+
export declare function getPostalCodesStats(): {
|
|
182
|
+
readonly totalPostalCodes: number;
|
|
183
|
+
readonly uniquePlaces: number;
|
|
184
|
+
readonly uniqueMunicipalities: number;
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Get postal codes sorted by code.
|
|
188
|
+
* @param ascending - Sort in ascending order if true (default: true)
|
|
189
|
+
* @returns Array of postal codes sorted by postal code
|
|
190
|
+
*/
|
|
191
|
+
export declare function getPostalCodesSorted(ascending?: boolean): PostalCode[];
|
|
192
|
+
/**
|
|
193
|
+
* Check if a postal code exists.
|
|
194
|
+
* @param code - 4-digit postal code to check
|
|
195
|
+
* @returns True if postal code exists, false otherwise
|
|
196
|
+
*/
|
|
197
|
+
export declare function isValidPostalCode(code: string): boolean;
|
|
198
|
+
/**
|
|
199
|
+
* Get postal codes by municipality name.
|
|
200
|
+
* Note: This function combines data from municipalities and postal codes.
|
|
201
|
+
* @param municipalityName - Municipality name to search for
|
|
202
|
+
* @param exactMatch - Whether to match exactly (default: false)
|
|
203
|
+
* @returns Array of postal codes for municipalities matching the name
|
|
204
|
+
*/
|
|
205
|
+
export declare function getPostalCodesByMunicipalityName(municipalityName: string, exactMatch?: boolean): PostalCode[];
|
|
206
|
+
export type { Municipality, County, PostalCode, PopulationDensityStats, LanguageStatus, MunicipalitySearchOptions, MunicipalityFilterOptions, PostalCodeSearchOptions };
|
|
207
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import municipalitiesData from '../data/kommuner-2025.json';
|
|
2
|
+
import countiesData from '../data/fylker-2025.json';
|
|
3
|
+
import postalCodesData from '../data/postal-codes-2025.json';
|
|
4
|
+
import packageJson from '../package.json';
|
|
5
|
+
// Type-safe data imports
|
|
6
|
+
const municipalities = municipalitiesData;
|
|
7
|
+
const counties = countiesData;
|
|
8
|
+
const postalCodes = postalCodesData;
|
|
9
|
+
/**
|
|
10
|
+
* Get the library version following semantic versioning.
|
|
11
|
+
* @returns Version string (e.g., "1.0.0")
|
|
12
|
+
*/
|
|
13
|
+
export function getVersion() {
|
|
14
|
+
return packageJson.version;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Get all municipalities with metadata.
|
|
18
|
+
* @returns Array of all municipalities
|
|
19
|
+
*/
|
|
20
|
+
export function getMunicipalities() {
|
|
21
|
+
return municipalities;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get a municipality by municipality ID.
|
|
25
|
+
* @param id - 4-digit municipality ID (e.g., "0301" for Oslo)
|
|
26
|
+
* @returns Municipality object or undefined if not found
|
|
27
|
+
* @throws {TypeError} If id is not a string
|
|
28
|
+
*/
|
|
29
|
+
export function getMunicipalityById(id) {
|
|
30
|
+
if (typeof id !== 'string') {
|
|
31
|
+
throw new TypeError('Municipality ID must be a string');
|
|
32
|
+
}
|
|
33
|
+
return municipalities.find(m => m.k_id === id);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Get municipalities by name (case-insensitive partial match by default).
|
|
37
|
+
* @param name - Municipality name or partial name to search for
|
|
38
|
+
* @param options - Search options
|
|
39
|
+
* @returns Array of matching municipalities
|
|
40
|
+
* @throws {TypeError} If name is not a string
|
|
41
|
+
*/
|
|
42
|
+
export function getMunicipalitiesByName(name, options = {}) {
|
|
43
|
+
if (typeof name !== 'string') {
|
|
44
|
+
throw new TypeError('Municipality name must be a string');
|
|
45
|
+
}
|
|
46
|
+
const { includeAllNames = true, caseSensitive = false, exactMatch = false } = options;
|
|
47
|
+
const searchTerm = caseSensitive ? name : name.toLowerCase();
|
|
48
|
+
return municipalities.filter(m => {
|
|
49
|
+
const namesToSearch = includeAllNames
|
|
50
|
+
? [m.k_name, m.k_name_no]
|
|
51
|
+
: [m.k_name];
|
|
52
|
+
return namesToSearch.some(municipalityName => {
|
|
53
|
+
const compareString = caseSensitive
|
|
54
|
+
? municipalityName
|
|
55
|
+
: municipalityName.toLowerCase();
|
|
56
|
+
return exactMatch
|
|
57
|
+
? compareString === searchTerm
|
|
58
|
+
: compareString.includes(searchTerm);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get all counties (fylker) with metadata.
|
|
64
|
+
* @returns Array of all counties
|
|
65
|
+
*/
|
|
66
|
+
export function getCounties() {
|
|
67
|
+
return counties;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Get a county by county ID.
|
|
71
|
+
* @param id - 2-digit county ID (e.g., "03" for Oslo)
|
|
72
|
+
* @returns County object or undefined if not found
|
|
73
|
+
* @throws {TypeError} If id is not a string
|
|
74
|
+
*/
|
|
75
|
+
export function getCountyById(id) {
|
|
76
|
+
if (typeof id !== 'string') {
|
|
77
|
+
throw new TypeError('County ID must be a string');
|
|
78
|
+
}
|
|
79
|
+
return counties.find(f => f.f_id === id);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get county by name (case-insensitive partial match).
|
|
83
|
+
* @param name - County name or partial name to search for
|
|
84
|
+
* @param exactMatch - Whether to match exactly (default: false)
|
|
85
|
+
* @returns County object or undefined if not found
|
|
86
|
+
* @throws {TypeError} If name is not a string
|
|
87
|
+
*/
|
|
88
|
+
export function getCountyByName(name, exactMatch = false) {
|
|
89
|
+
if (typeof name !== 'string') {
|
|
90
|
+
throw new TypeError('County name must be a string');
|
|
91
|
+
}
|
|
92
|
+
const searchTerm = name.toLowerCase();
|
|
93
|
+
return counties.find(f => {
|
|
94
|
+
const countyName = f.f_name.toLowerCase();
|
|
95
|
+
return exactMatch ? countyName === searchTerm : countyName.includes(searchTerm);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Get all municipalities in a given county.
|
|
100
|
+
* @param countyId - 2-digit county ID (first two digits of municipality k_id)
|
|
101
|
+
* @returns Array of municipalities in the county
|
|
102
|
+
* @throws {TypeError} If countyId is not a string
|
|
103
|
+
*/
|
|
104
|
+
export function getMunicipalitiesByCounty(countyId) {
|
|
105
|
+
if (typeof countyId !== 'string') {
|
|
106
|
+
throw new TypeError('County ID must be a string');
|
|
107
|
+
}
|
|
108
|
+
return municipalities.filter(m => m.k_id.startsWith(countyId));
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Get municipality by postal code.
|
|
112
|
+
* @param postalCode - Postal code to search for
|
|
113
|
+
* @returns Municipality object or undefined if not found
|
|
114
|
+
* @throws {TypeError} If postal code is not a valid number
|
|
115
|
+
*/
|
|
116
|
+
export function getMunicipalityByPostalCode(postalCode) {
|
|
117
|
+
const code = String(postalCode);
|
|
118
|
+
if (!code.match(/^\d{4}$/)) {
|
|
119
|
+
throw new TypeError('Postal code must be a valid 4-digit number');
|
|
120
|
+
}
|
|
121
|
+
return municipalities.find(m => m.k_postal_codes.some(pc => pc.postal_code === code));
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Get all postal codes for a specific municipality.
|
|
125
|
+
* @param municipalityId - 4-digit municipality ID
|
|
126
|
+
* @returns Array of postal codes or undefined if municipality not found
|
|
127
|
+
*/
|
|
128
|
+
export function getPostalCodesByMunicipality(municipalityId) {
|
|
129
|
+
const municipality = getMunicipalityById(municipalityId);
|
|
130
|
+
return municipality ? municipality.k_postal_codes.map(pc => pc.postal_code) : undefined;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Get all postal codes from all municipalities.
|
|
134
|
+
* @param includeDetails - If true, returns objects with postal code and place name. If false, returns only postal codes (default: false)
|
|
135
|
+
* @returns Array of postal codes or postal code objects
|
|
136
|
+
*/
|
|
137
|
+
export function getAllPostalCodes(includeDetails = false) {
|
|
138
|
+
if (includeDetails) {
|
|
139
|
+
const allPostalCodes = [];
|
|
140
|
+
municipalities.forEach(municipality => {
|
|
141
|
+
municipality.k_postal_codes.forEach(pc => {
|
|
142
|
+
allPostalCodes.push({
|
|
143
|
+
zip: pc.postal_code,
|
|
144
|
+
place: pc.postal_place,
|
|
145
|
+
municipalityId: municipality.k_id,
|
|
146
|
+
municipalityName: municipality.k_name
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
return allPostalCodes.sort((a, b) => a.zip.localeCompare(b.zip));
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
const allPostalCodes = new Set();
|
|
154
|
+
municipalities.forEach(municipality => {
|
|
155
|
+
municipality.k_postal_codes.forEach(pc => {
|
|
156
|
+
allPostalCodes.add(pc.postal_code);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
return Array.from(allPostalCodes).sort();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Get municipalities sorted by population.
|
|
164
|
+
* @param ascending - Sort in ascending order if true (default: false for descending)
|
|
165
|
+
* @returns Array of municipalities sorted by population
|
|
166
|
+
*/
|
|
167
|
+
export function getMunicipalitiesByPopulation(ascending = false) {
|
|
168
|
+
const sorted = [...municipalities].sort((a, b) => ascending ? a.k_population - b.k_population : b.k_population - a.k_population);
|
|
169
|
+
return sorted;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Get municipalities sorted by area.
|
|
173
|
+
* @param ascending - Sort in ascending order if true (default: false for descending)
|
|
174
|
+
* @returns Array of municipalities sorted by area
|
|
175
|
+
*/
|
|
176
|
+
export function getMunicipalitiesByArea(ascending = false) {
|
|
177
|
+
const sorted = [...municipalities].sort((a, b) => ascending ? a.k_area - b.k_area : b.k_area - a.k_area);
|
|
178
|
+
return sorted;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Get municipalities by language status.
|
|
182
|
+
* @param language - Language status
|
|
183
|
+
* @returns Array of municipalities with the specified language status
|
|
184
|
+
* @throws {TypeError} If language is not a string
|
|
185
|
+
*/
|
|
186
|
+
export function getMunicipalitiesByLanguage(language) {
|
|
187
|
+
if (typeof language !== 'string') {
|
|
188
|
+
throw new TypeError('Language must be a string');
|
|
189
|
+
}
|
|
190
|
+
return municipalities.filter(m => m.k_language === language);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Get municipalities with advanced filtering options.
|
|
194
|
+
* @param options - Filter options
|
|
195
|
+
* @returns Array of municipalities matching the filter criteria
|
|
196
|
+
*/
|
|
197
|
+
export function getMunicipalitiesFiltered(options) {
|
|
198
|
+
return municipalities.filter(m => {
|
|
199
|
+
if (options.minPopulation !== undefined && m.k_population < options.minPopulation)
|
|
200
|
+
return false;
|
|
201
|
+
if (options.maxPopulation !== undefined && m.k_population > options.maxPopulation)
|
|
202
|
+
return false;
|
|
203
|
+
if (options.minArea !== undefined && m.k_area < options.minArea)
|
|
204
|
+
return false;
|
|
205
|
+
if (options.maxArea !== undefined && m.k_area > options.maxArea)
|
|
206
|
+
return false;
|
|
207
|
+
if (options.language !== undefined && m.k_language !== options.language)
|
|
208
|
+
return false;
|
|
209
|
+
if (options.countyId !== undefined && !m.k_id.startsWith(options.countyId))
|
|
210
|
+
return false;
|
|
211
|
+
return true;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Get total population of all municipalities.
|
|
216
|
+
* @returns Total population
|
|
217
|
+
*/
|
|
218
|
+
export function getTotalPopulation() {
|
|
219
|
+
return municipalities.reduce((total, m) => total + m.k_population, 0);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get total area of all municipalities.
|
|
223
|
+
* @returns Total area in square kilometers
|
|
224
|
+
*/
|
|
225
|
+
export function getTotalArea() {
|
|
226
|
+
return municipalities.reduce((total, m) => total + m.k_area, 0);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Get population density statistics.
|
|
230
|
+
* @returns Object with min, max, and average population density
|
|
231
|
+
*/
|
|
232
|
+
export function getPopulationDensityStats() {
|
|
233
|
+
const densities = municipalities.map(m => m.k_population / m.k_area);
|
|
234
|
+
return {
|
|
235
|
+
min: Math.min(...densities),
|
|
236
|
+
max: Math.max(...densities),
|
|
237
|
+
average: densities.reduce((sum, d) => sum + d, 0) / densities.length
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Get municipalities by population density range.
|
|
242
|
+
* @param minDensity - Minimum population density (people per km²)
|
|
243
|
+
* @param maxDensity - Maximum population density (people per km²)
|
|
244
|
+
* @returns Array of municipalities within the density range
|
|
245
|
+
*/
|
|
246
|
+
export function getMunicipalitiesByPopulationDensity(minDensity = 0, maxDensity = Infinity) {
|
|
247
|
+
return municipalities.filter(m => {
|
|
248
|
+
const density = m.k_population / m.k_area;
|
|
249
|
+
return density >= minDensity && density <= maxDensity;
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Get the largest municipalities by population.
|
|
254
|
+
* @param count - Number of municipalities to return (default: 10)
|
|
255
|
+
* @returns Array of largest municipalities by population
|
|
256
|
+
*/
|
|
257
|
+
export function getLargestMunicipalities(count = 10) {
|
|
258
|
+
return getMunicipalitiesByPopulation(false).slice(0, count);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Get the smallest municipalities by population.
|
|
262
|
+
* @param count - Number of municipalities to return (default: 10)
|
|
263
|
+
* @returns Array of smallest municipalities by population
|
|
264
|
+
*/
|
|
265
|
+
export function getSmallestMunicipalities(count = 10) {
|
|
266
|
+
return getMunicipalitiesByPopulation(true).slice(0, count);
|
|
267
|
+
}
|
|
268
|
+
// ========================
|
|
269
|
+
// POSTAL CODE FUNCTIONS
|
|
270
|
+
// ========================
|
|
271
|
+
/**
|
|
272
|
+
* Get all postal codes with metadata.
|
|
273
|
+
* @returns Array of all postal codes
|
|
274
|
+
*/
|
|
275
|
+
export function getPostalCodes() {
|
|
276
|
+
return postalCodes;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Get postal code information by postal code.
|
|
280
|
+
* @param code - 4-digit postal code (e.g., "0001")
|
|
281
|
+
* @returns PostalCode object or undefined if not found
|
|
282
|
+
* @throws {TypeError} If code is not a string
|
|
283
|
+
*/
|
|
284
|
+
export function getPostalCodeByCode(code) {
|
|
285
|
+
if (typeof code !== 'string') {
|
|
286
|
+
throw new TypeError('Postal code must be a string');
|
|
287
|
+
}
|
|
288
|
+
return postalCodes.find(pc => pc.k_postal_code === code);
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Get postal codes by postal place (case-insensitive partial match by default).
|
|
292
|
+
* @param place - Postal place name or partial name to search for
|
|
293
|
+
* @param options - Search options
|
|
294
|
+
* @returns Array of matching postal codes
|
|
295
|
+
* @throws {TypeError} If place is not a string
|
|
296
|
+
*/
|
|
297
|
+
export function getPostalCodesByPlace(place, options = {}) {
|
|
298
|
+
if (typeof place !== 'string') {
|
|
299
|
+
throw new TypeError('Postal place must be a string');
|
|
300
|
+
}
|
|
301
|
+
const { caseSensitive = false, exactMatch = false } = options;
|
|
302
|
+
const searchTerm = caseSensitive ? place : place.toLowerCase();
|
|
303
|
+
return postalCodes.filter(pc => {
|
|
304
|
+
const placeName = caseSensitive
|
|
305
|
+
? pc.k_postal_place
|
|
306
|
+
: pc.k_postal_place.toLowerCase();
|
|
307
|
+
return exactMatch
|
|
308
|
+
? placeName === searchTerm
|
|
309
|
+
: placeName.includes(searchTerm);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Get postal codes by municipality ID.
|
|
314
|
+
* @param municipalityId - 4-digit municipality ID (e.g., "0301" for Oslo)
|
|
315
|
+
* @returns Array of postal codes for the municipality
|
|
316
|
+
* @throws {TypeError} If municipalityId is not a string
|
|
317
|
+
*/
|
|
318
|
+
export function getPostalCodesByMunicipalityId(municipalityId) {
|
|
319
|
+
if (typeof municipalityId !== 'string') {
|
|
320
|
+
throw new TypeError('Municipality ID must be a string');
|
|
321
|
+
}
|
|
322
|
+
return postalCodes.filter(pc => pc.k_id === municipalityId);
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Get unique postal places.
|
|
326
|
+
* @returns Array of unique postal place names sorted alphabetically
|
|
327
|
+
*/
|
|
328
|
+
export function getUniquePostalPlaces() {
|
|
329
|
+
const uniquePlaces = new Set();
|
|
330
|
+
postalCodes.forEach(pc => uniquePlaces.add(pc.k_postal_place));
|
|
331
|
+
return Array.from(uniquePlaces).sort();
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Get postal codes within a range.
|
|
335
|
+
* @param startCode - Starting postal code (inclusive)
|
|
336
|
+
* @param endCode - Ending postal code (inclusive)
|
|
337
|
+
* @returns Array of postal codes within the range
|
|
338
|
+
* @throws {TypeError} If start or end codes are not strings
|
|
339
|
+
*/
|
|
340
|
+
export function getPostalCodesInRange(startCode, endCode) {
|
|
341
|
+
if (typeof startCode !== 'string' || typeof endCode !== 'string') {
|
|
342
|
+
throw new TypeError('Start and end codes must be strings');
|
|
343
|
+
}
|
|
344
|
+
return postalCodes.filter(pc => pc.k_postal_code >= startCode && pc.k_postal_code <= endCode);
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Get postal codes statistics.
|
|
348
|
+
* @returns Object with total count and unique places count
|
|
349
|
+
*/
|
|
350
|
+
export function getPostalCodesStats() {
|
|
351
|
+
const uniquePlaces = new Set();
|
|
352
|
+
const uniqueMunicipalities = new Set();
|
|
353
|
+
postalCodes.forEach(pc => {
|
|
354
|
+
uniquePlaces.add(pc.k_postal_place);
|
|
355
|
+
uniqueMunicipalities.add(pc.k_id);
|
|
356
|
+
});
|
|
357
|
+
return {
|
|
358
|
+
totalPostalCodes: postalCodes.length,
|
|
359
|
+
uniquePlaces: uniquePlaces.size,
|
|
360
|
+
uniqueMunicipalities: uniqueMunicipalities.size
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Get postal codes sorted by code.
|
|
365
|
+
* @param ascending - Sort in ascending order if true (default: true)
|
|
366
|
+
* @returns Array of postal codes sorted by postal code
|
|
367
|
+
*/
|
|
368
|
+
export function getPostalCodesSorted(ascending = true) {
|
|
369
|
+
const sorted = [...postalCodes].sort((a, b) => ascending
|
|
370
|
+
? a.k_postal_code.localeCompare(b.k_postal_code)
|
|
371
|
+
: b.k_postal_code.localeCompare(a.k_postal_code));
|
|
372
|
+
return sorted;
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Check if a postal code exists.
|
|
376
|
+
* @param code - 4-digit postal code to check
|
|
377
|
+
* @returns True if postal code exists, false otherwise
|
|
378
|
+
*/
|
|
379
|
+
export function isValidPostalCode(code) {
|
|
380
|
+
if (typeof code !== 'string') {
|
|
381
|
+
return false;
|
|
382
|
+
}
|
|
383
|
+
return postalCodes.some(pc => pc.k_postal_code === code);
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Get postal codes by municipality name.
|
|
387
|
+
* Note: This function combines data from municipalities and postal codes.
|
|
388
|
+
* @param municipalityName - Municipality name to search for
|
|
389
|
+
* @param exactMatch - Whether to match exactly (default: false)
|
|
390
|
+
* @returns Array of postal codes for municipalities matching the name
|
|
391
|
+
*/
|
|
392
|
+
export function getPostalCodesByMunicipalityName(municipalityName, exactMatch = false) {
|
|
393
|
+
const matchingMunicipalities = getMunicipalitiesByName(municipalityName, { exactMatch });
|
|
394
|
+
const municipalityIds = matchingMunicipalities.map(m => m.k_id);
|
|
395
|
+
return postalCodes.filter(pc => municipalityIds.includes(pc.k_id));
|
|
396
|
+
}
|
|
397
|
+
//# sourceMappingURL=index.js.map
|