-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (154 loc) · 5.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict'
var data = require('./data.js') // data object
var mappedForRegions = null // data where regions will be lookup keys
var mappedForDistricts = null // data where districts will be lookup keys
var InvalidPostalcode = 'Invalid Postalcode' // Invalid PLZ Err
var WrongFormatError = 'Wrong param provided' // Invalid Format Err
var ParamMissing = 'Param is missing' // Param missing err
var GermanPLZRegex = /^([0]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{3}$/ // German PLZ Regex to test
/**
* Wrapper Function to Map Data to Regions
* @private
* @return {Object}
*/
function createMappedForRegions () {
// create empty obj
mappedForRegions = {}
// iterate over all postal codes
Object.keys(data).forEach(function (plz) {
// get all regions for one postal code
var regions = data[plz].regions
// iterate over all regions and fill them into result object
regions.forEach(function (region) {
region = region.toLowerCase()
// district exists, push into result
if (mappedForRegions[region]) {
mappedForRegions[region].push(plz)
} else {
// district does not exist, create array
mappedForRegions[region] = [plz]
}
})
})
return mappedForRegions
}
/**
* Wrapper Function to Map Data to Districts
* @private
* @return {Object}
*/
function createMappedForDistricts () {
// create empty obj
mappedForDistricts = {}
// iterate over all postal codes
Object.keys(data).forEach(function (plz) {
// get all districts for one postal code
var districts = data[plz].districts
// iterate over all districts and fill them into result object
districts.forEach(function (district) {
district = district.toLowerCase()
// district exists, push into result
if (mappedForDistricts[district]) {
mappedForDistricts[district].push(plz)
} else {
// district does not exist, create array
mappedForDistricts[district] = [plz]
}
})
})
return mappedForDistricts
}
/**
* Get Regions and Districts to Postalcode
* @param {String} postalcode German 5 digit postal code
* @param {String} property "districts" or "regions" if you want to get one specific property
* @return {Object|undefined} Object with "districts" and "regions" Keys storing their values
*/
function get (postalcode, property) {
if (!postalcode) {
// dirty quick undefined / null / false check
throw new Error(ParamMissing + ': postalcode')
}
if (typeof postalcode === 'number') {
postalcode = postalcode.toString()
}
if (typeof postalcode === 'string') {
if (postalcode.match(GermanPLZRegex)) {
var normalizedPostalCode = parseInt(postalcode, 10).toString()
if (property === 'districts' || property === 'regions') {
return data[normalizedPostalCode] ? data[normalizedPostalCode][property] : []
}
return data[normalizedPostalCode]
}
throw new Error(InvalidPostalcode)
}
return []
}
/**
* Get Districts by Postalcode
* Warning: This might *not* return a valid district but the name of a city due to limitations.
* Smaller cities aren't guaranteed to yield their districts,
* they will return the city name instead.
* Example: Searchfing for "68159" will not tell you which district in
* Mannheim the Postal code belongs to but tell you that it belongs to Mannheim
* @param {String} postalcode German 5 digit postal code
* @return {Array.<String>} Array of Districts
*/
function getDistricts (postalcode) {
return get(postalcode, 'districts')
}
/**
* Get Regions (Bundesländer) by Postalcode
* @param {String} postalcode German 5 digit postal code
* @return {Array.<String>} Array of Regions
*/
function getRegions (postalcode) {
return get(postalcode, 'regions')
}
/**
* Get All Postal codes for one Region (Bundesland)
* @param {String} region The region (Bundesland) e.g "Berlin"
* @return {Array.<String>} Array of Postalcodes
*/
function getPostalCodesByRegion (region) {
if (!region) {
throw new Error(ParamMissing + ': region')
}
if (typeof region !== 'string') {
throw new Error(WrongFormatError)
}
mappedForRegions = mappedForRegions || createMappedForRegions()
return mappedForRegions[region.toLowerCase()] || []
}
/**
* Get All Postal codes for one District
* Warning: Providing a *valid* german district might return empty
* results due to current limitations.
* You can find smaller cities by providing the city name instead of district name in the query.
* Suggested example:
* Search for district first, if your result has length 0, search for the city.
* If that's 0 again, check for region (Bundesland) - with getPostalCodesByRegion
* @param {String} region The District e.g "Gesundbrunnen" (Berlin)
* @return {Array.<String>} Array of Postalcodes
*/
function getPostalCodesByDistrict (district) {
if (!district) {
throw new Error(ParamMissing + ': district')
}
if (typeof district !== 'string') {
throw new Error(WrongFormatError)
}
mappedForDistricts = mappedForDistricts || createMappedForDistricts()
return mappedForDistricts[district.toLowerCase()] || []
}
/**
* Module export
* @type {Object}
*/
module.exports = {
get: get,
getPostalCodesByDistrict: getPostalCodesByDistrict,
getPostalCodesByRegion: getPostalCodesByRegion,
getDistricts: getDistricts,
getRegions: getRegions
}