@omnicar/sam-zip-city
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -0,1 +1,2 @@ | ||
import { IsoLocale } from '@omnicar/sam-types'; | ||
export declare type Country = 'DK' | 'SE'; | ||
@@ -10,8 +11,9 @@ export interface IZipCodes { | ||
zipcode: number | string; | ||
country: Country; | ||
locale: IsoLocale; | ||
fileLocation?: string; | ||
global?: any; | ||
} | ||
export declare type CityConf = Pick<ICityLookup, 'country' | 'fileLocation' | 'global'>; | ||
export declare type CityConf = Pick<ICityLookup, 'locale' | 'fileLocation' | 'global'>; | ||
export declare const getCountryFromLocale: (locale: IsoLocale) => Country; | ||
export declare const initZipCityCountry: (cityLookup: Pick<ICityLookup, "locale" | "fileLocation" | "global">) => Promise<IZipCodes | undefined>; | ||
export declare const getCityFromZip: (cityLookup: ICityLookup) => Promise<string | false>; | ||
export declare const initZipCityCountry: (cityLookup: Pick<ICityLookup, "country" | "fileLocation" | "global">) => Promise<IZipCodes | undefined>; |
@@ -6,27 +6,11 @@ "use strict"; | ||
var zipcodeCache = {}; | ||
exports.getCityFromZip = function (cityLookup) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var zipcode, countryMap, zipKey, cityName; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
zipcode = tslib_1.__assign({}, cityLookup).zipcode; | ||
return [4 /*yield*/, exports.initZipCityCountry(cityLookup)]; | ||
case 1: | ||
countryMap = _a.sent(); | ||
if (!countryMap) { | ||
return [2 /*return*/, false]; | ||
} | ||
zipKey = ("" + zipcode).replace(' ', ''); | ||
cityName = countryMap.hasOwnProperty(zipKey) ? countryMap[zipKey] : false; | ||
return [2 /*return*/, cityName]; | ||
} | ||
}); | ||
}); }; | ||
var defaultFileLocation = 'https://cdn.jsdelivr.net/gh/omnicar/sam-zip-city/dist/countries/'; | ||
exports.getCountryFromLocale = function (locale) { return locale.split('-')[1]; }; | ||
exports.initZipCityCountry = function (cityLookup) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var country, _a, fileLocation, global, myGlobal, zipcodeCountryMap, scriptPath; | ||
var locale, _a, fileLocation, global, country, myGlobal, zipcodeCountryMap, scriptPath; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
country = cityLookup.country, _a = cityLookup.fileLocation, fileLocation = _a === void 0 ? defaultFileLocation : _a, global = cityLookup.global; | ||
locale = cityLookup.locale, _a = cityLookup.fileLocation, fileLocation = _a === void 0 ? defaultFileLocation : _a, global = cityLookup.global; | ||
country = exports.getCountryFromLocale(locale); | ||
if (zipcodeCache[country]) { | ||
@@ -55,2 +39,20 @@ return [2 /*return*/, zipcodeCache[country]]; | ||
}); }; | ||
exports.getCityFromZip = function (cityLookup) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var zipcode, countryMap, zipKey, cityName; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
zipcode = cityLookup.zipcode; | ||
return [4 /*yield*/, exports.initZipCityCountry(cityLookup)]; | ||
case 1: | ||
countryMap = _a.sent(); | ||
if (!countryMap) { | ||
return [2 /*return*/, false]; | ||
} | ||
zipKey = ("" + zipcode).replace(' ', ''); | ||
cityName = countryMap.hasOwnProperty(zipKey) ? countryMap[zipKey] : false; | ||
return [2 /*return*/, cityName]; | ||
} | ||
}); | ||
}); }; | ||
var getZipcodeMapFromGlobal = function (global, country) { | ||
@@ -67,10 +69,15 @@ var zipcodeCountryMap = global["zipcodeMap" + country]; | ||
if (!existingScript) { | ||
var s = void 0; | ||
s = document.createElement('script'); | ||
s.src = src; | ||
s.id = id; | ||
s.onload = resolve; | ||
s.onerror = reject; | ||
s.async = true; | ||
document.head.appendChild(s); | ||
if (document.head) { | ||
var s = void 0; | ||
s = document.createElement('script'); | ||
s.src = src; | ||
s.id = id; | ||
s.onload = resolve; | ||
s.onerror = reject; | ||
s.async = true; | ||
document.head.appendChild(s); | ||
} | ||
else { | ||
reject(); | ||
} | ||
} | ||
@@ -77,0 +84,0 @@ else { |
@@ -12,8 +12,12 @@ { | ||
"license": "MIT", | ||
"dependencies": { | ||
"tslib": "^1.9.3" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^23.3.1", | ||
"@types/node": "^10.7.1", | ||
"jest": "^23.5.0", | ||
"ts-jest": "^23.1.3", | ||
"typescript": "^2.8.3" | ||
"@omnicar/sam-types": "0.0.128", | ||
"@types/jest": "23.3.10", | ||
"@types/node": "10.12.17", | ||
"jest": "23.6.0", | ||
"ts-jest": "23.10.5", | ||
"typescript": "3.2.2" | ||
}, | ||
@@ -48,6 +52,3 @@ "jest": { | ||
}, | ||
"dependencies": { | ||
"tslib": "^1.9.3" | ||
}, | ||
"version": "0.0.4" | ||
"version": "0.0.5" | ||
} |
7946
8
119
6