ikea-availability-checker
Version:
ikea product in-store availability checker and product search
102 lines (101 loc) • 3.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stores = void 0;
exports.findByQuery = findByQuery;
exports.findById = findById;
exports.findOneById = findOneById;
exports.findByCountryCode = findByCountryCode;
exports.getCountryCodes = getCountryCodes;
exports.getLanguageCode = getLanguageCode;
const i18n_iso_countries_1 = require("i18n-iso-countries");
const helper_1 = require("../cli/lib/helper");
const stores_json_1 = __importDefault(require("./../data/stores.json"));
// create list of stores with typescript types and sorted by countryCode
// and buCode in ascending order
exports.stores = stores_json_1.default
.map((store) => {
store.country = (0, i18n_iso_countries_1.getName)(store.countryCode, "en") || '';
return store;
})
.sort((a, b) => {
// sort by countryCode and buCode ascending order
return (a.countryCode.localeCompare(b.countryCode) +
a.buCode.localeCompare(b.buCode));
});
/**
* Find stores by matching the given query against the buCode, countryCode or
* name of a store and return an array of all matching stores.
*/
function findByQuery(
/** query case insensitive search query */
query,
/** optional additional countryCode that must match */
countryCode = null) {
const regexp = query instanceof RegExp ? query : new RegExp(String(query), "i");
countryCode = countryCode ? (0, helper_1.normalizeCountryCode)(countryCode) : countryCode;
return exports.stores
.filter((d) => regexp.test(d.name) || d.buCode == query)
.filter((d) => (countryCode ? d.countryCode === countryCode : true));
}
function findById(buCodes) {
if (!Array.isArray(buCodes))
buCodes = [buCodes];
const codes = buCodes.map((c) => String(c));
return exports.stores.filter((store) => codes.indexOf(store.buCode) > -1);
}
function findOneById(buCode) {
return exports.stores.find((store) => store.buCode == buCode);
}
function findByCountryCode(countryCodes) {
if (!Array.isArray(countryCodes))
countryCodes = [countryCodes];
const cc = countryCodes.map(helper_1.normalizeCountryCode);
return exports.stores.filter((store) => cc.indexOf(store.countryCode) > -1);
}
/**
* Returns an array with all ISO 3166-1 alpha 2 country codes that have at
* least one store in alphabetical order.
*/
function getCountryCodes() {
const countryCodes = exports.stores.map((store) => store.countryCode);
return Array.from(new Set(countryCodes)).sort((a, b) => a.localeCompare(b));
}
/**
* Transforms a ISO 3166-2 country code like "gb" to the ISO 639-2
* language code ("en") that is supported by the IOWS endpoint.
*/
function getLanguageCode(countryCode) {
const cc = (0, helper_1.normalizeCountryCode)(countryCode);
// the best matching language code to use when sending requests to a
// specific country
const map = {
ae: "en",
at: "de",
au: "en",
be: "fr",
ca: "en",
ch: "de",
cn: "zh",
cz: "cs",
dk: "da",
gb: "en",
hk: "en",
ie: "en",
jo: "en",
jp: "ja",
kr: "ko",
kw: "en",
my: "en",
qa: "en",
sa: "en",
se: "sv",
sg: "en",
th: "en",
tw: "zh",
us: "en",
};
return map[String(cc)] || cc;
}