Last active
May 23, 2016 04:28
-
-
Save cumanzor/5320433e9e042b162d6d833968f6e7d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Attempts to parse the string into a SK address. | |
* https://en.wikipedia.org/wiki/Addresses_in_South_Korea#Postal_Address | |
* https://en.wikipedia.org/wiki/ISO_3166-2:KR | |
* https://en.wikipedia.org/wiki/Administrative_divisions_of_South_Korea#Gun_.28County.29 | |
* @returns An object of the form {province, city, street, zip} | |
*/ | |
function parseAddress(string) { | |
var address = {}; | |
address.extra = []; | |
//for crawling purposes, these are all provinces. | |
var province = ["-do", "-teukbyeol-jachido", "-teukbyeolsi", "busan", "-teukbyeol-jachisi", "-gwangyeoksi"]; | |
//Cities and districts | |
var city = ["-si", "-gun", "-gu", "seoul", "mokpo"]; | |
//town, township and neighborhood | |
var town = ["-eup", "-myeon", "-dong"]; | |
//rural areas | |
var rural = ["-tong", "-ri"]; | |
var hamlet = ["-ban"]; | |
//zipcode | |
var zipRegex = /(\d+-\d+)$/; | |
var parts = string.split(" "); //space note | |
parts.forEach(function(item, idx) { | |
var item = item.toLowerCase().replace(",", ""); | |
if (province.find(compare, item)) { | |
address.province = item.replace(/-.+/, "").capitalize(); | |
} else if (city.find(compare, item)) { | |
address.city = item.replace(/-.+/, "").capitalize(); | |
} else if (town.find(compare, item)) { | |
address.town = item.replace().capitalize(); //keep the -dong in the town name. | |
} else if (rural.find(compare, item)) { | |
address.ruralArea = item.capitalize(); | |
} else if (hamlet.find(compare, item)) { | |
address.hamlet = item.capitalize(); | |
} | |
else if (item.match(zipRegex)) { | |
address.zip = item; | |
} else if (item.match(/south|korea/)) { | |
//discard | |
} | |
else { | |
address.extra.push(item.capitalize()); | |
} | |
}); | |
//find callback | |
function compare(element, idx, arr) { | |
var reg = new RegExp(element + "$", "i"); | |
return reg.test(this); | |
} | |
address.streetLevelAddress = address.extra; | |
if (address.town) address.extra.push(address.town); | |
if (address.rural) address.extra.push(address.rural); | |
if (address.hamlet) address.extra.push(address.hamlet); | |
address.streetLevelAddress = address.extra.join(" "); | |
return address; | |
} | |
/* | |
* Polyfill required for <ES6 | |
*/ | |
//polyfill arr.find method until the current version of node supports ES6 features | |
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find | |
if (!Array.prototype.find) { | |
Array.prototype.find = function(predicate) { | |
if (this === null) { | |
throw new TypeError('Array.prototype.find called on null or undefined'); | |
} | |
if (typeof predicate !== 'function') { | |
throw new TypeError('predicate must be a function'); | |
} | |
var list = Object(this); | |
var length = list.length >>> 0; | |
var thisArg = arguments[1]; | |
var value; | |
for (var i = 0; i < length; i++) { | |
value = list[i]; | |
if (predicate.call(thisArg, value, i, list)) { | |
return value; | |
} | |
} | |
return undefined; | |
}; | |
} | |
/** | |
* Capitalize the first character of a string. | |
* Usage: myString.capitalize() -> MyString. | |
*/ | |
String.prototype.capitalize = function() { | |
return this.charAt(0).toUpperCase() + this.slice(1); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment