Skip to content

Commit

Permalink
Fix Cannot read property 'alpha2' of undefined - See moimikey#74
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadorequest committed Sep 5, 2019
1 parent 3de58a1 commit e90bef6
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ module.exports = (function iso3166 () {
const to2 = function to2 (alpha3) {
if (alpha3 && alpha3.length > 1) state = alpha3
if (state.length !== 3) return state
return ISOCodes.filter(function (row) {
const matchingCodes = ISOCodes.filter(function (row) {
return row.alpha3 === state
})[0].alpha2
})

if (matchingCodes.length > 0) {
return matchingCodes[0].alpha2
} else {
return undefined
}
}

/**
Expand All @@ -28,9 +34,15 @@ module.exports = (function iso3166 () {
const to3 = function to3 (alpha2) {
if (alpha2 && alpha2.length > 1) state = alpha2
if (state.length !== 2) return state
return ISOCodes.filter(function (row) {
const matchingCodes = ISOCodes.filter(function (row) {
return row.alpha2 === state
})[0].alpha3
})

if (matchingCodes.length > 0) {
return matchingCodes[0].alpha3
} else {
return undefined
}
}

/**
Expand Down

0 comments on commit e90bef6

Please sign in to comment.