From 3c0e7e7bf5e4e491519079b1f547e47d03393c60 Mon Sep 17 00:00:00 2001 From: rolux Date: Sun, 18 Aug 2013 12:01:20 +0200 Subject: [PATCH] Geo module: add Ox.splitGeoname method --- source/Ox.Geo/Ox.Geo.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/source/Ox.Geo/Ox.Geo.js b/source/Ox.Geo/Ox.Geo.js index 510c50ff..1a3e0429 100644 --- a/source/Ox.Geo/Ox.Geo.js +++ b/source/Ox.Geo/Ox.Geo.js @@ -172,12 +172,8 @@ Ox.load.Geo = function(options, callback) { Ox.getCountryByGeoname = function(geoname) { // fixme: UAE correction doesn't belong here, fix in map geoname = (geoname || '').replace(' - United Arab Emirates', ', United Arab Emirates') - return Ox.getCountryByName( - geoname.split(', ').pop() - .replace('Sint Eustatius and Saba', 'Bonaire, Sint Eustatius and Saba') - .replace('Ascension and Tristan da Cunha', 'Saint Helena, Ascension and Tristan da Cunha') - ); - } + return Ox.getCountryByName(Ox.splitGeoname(geoname).pop()); + }; /*@ Ox.getCountryByName Returns a country object for a given country name @@ -257,6 +253,31 @@ Ox.load.Geo = function(options, callback) { return Ox.GEO_COLORS[str] || [128, 128, 128]; }; + /*@ + Ox.splitGeoname Splits a geoname into its component parts + (geoname) -> <[s]> Components + geoname Geoname + @*/ + Ox.splitGeoname = function(geoname) { + var countries = [ + 'Bonaire, Sint Eustatius and Saba', + 'Saint Helena, Ascension and Tristan da Cunha' + ], + split; + countries.forEach(function(country) { + if (Ox.endsWith(geoname, country)) { + geoname = geoname.replace(country, country.replace(', ', '; ')); + } + }); + split = geoname.split(', '); + countries.forEach(function(country) { + if (Ox.endsWith(Ox.last(split), country.replace(', ', '; '))) { + Ox.last(split, country); + } + }); + return split; + }; + callback(true); });