forked from tobias7an/postal-code-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (37 loc) · 1.24 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
const data = require('./data')
const sanitize = (input) => {
return String(input).toUpperCase().replace(/[^A-Z0-9 \-]/g,'').substr(0,15)
}
const defaultData = {
countryCode: undefined,
postalCodeType: 1,
pattern: /^([0-9A-Z\-\s]?){15}$/,
placeholder: 'Postal code'
}
const get = (countryCode) => {
let matchCountry = data.filter(c=>c.countryCode===countryCode)
return matchCountry.length ? matchCountry[0] : defaultData
}
const validate = (input, countryCode, sanitize) => {
let testedValue = sanitize ? sanitize(input) : String(input)
let countryData = get(countryCode)
// If country has no pattern, return true.
if(countryData.postalCodeType===0)
return true
// If country has patten, test against that pattern.
else if(countryData.postalCodeType===1)
return countryData.pattern.test(testedValue)
// If country has fixed value, it should already be correct as it was either set using module or checked and overwritten in db. Return true.
else if(countryData.postalCodeType===3)
return true
// Other values are invalid
else
return false
}
module.exports = {
postalCodeData: data,
getPostalCodeData: get,
defaultPostalCodeData: defaultData,
sanitizePostalCode: sanitize,
validatePostalCode: validate
}