Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function convert(data) {
- const carriers = ["warp", "gsm", "dark_star"];
- const planKey = "annual_unlimited_premium";
- const offeringsByCarrier = {}; // carrier -> offering -> country[]
- for (const country of data) {
- const countryName = country.country;
- for (const carrier of carriers) {
- const offering = country[carrier][planKey];
- let offeringMap = (offeringsByCarrier[carrier] = offeringsByCarrier[carrier] || {});
- let countryList = (offeringMap[offering] = offeringMap[offering] || []);
- countryList.push(countryName);
- }
- }
- const countriesByCarrier = {}; // carrier -> Set<country>
- for (const [carrier, offerings] of Object.entries(offeringsByCarrier)) {
- let set = new Set();
- for (const [offering, countries] of Object.entries(offerings)) {
- if (offering == "Unavailable") continue;
- // Exclude Warp's eSIM-only (non-native) countries
- if (carrier == "warp" && offering.indexOf("mins") == -1) continue;
- set = set.union(new Set(countries));
- }
- countriesByCarrier[carrier] = set;
- }
- const exclusives = {}; // carrier -> Set<country>
- for (const [carrier, countries] of Object.entries(countriesByCarrier)) {
- const others = carriers.filter(c => c != carrier);
- const otherSet = others.reduce((accum, carrier) => accum.union(countriesByCarrier[carrier]), new Set());
- const exclusive = countries.difference(otherSet);
- exclusives[carrier] = exclusive;
- }
- const countryListString = coll => {
- const arr = Array.from(coll).sort();
- return `${arr.length} countries: ${arr.join(", ")}`;
- };
- const mapValues = (mapping, fn) => {
- let result = {};
- for (const [key, val] of Object.entries(mapping)) {
- result[key] = fn(val);
- }
- return result;
- };
- return {
- offeringsByCarrier,
- countriesByCarrier,
- exclusives,
- strings: {
- countriesByCarrier: mapValues(countriesByCarrier, countryListString),
- exclusives: mapValues(exclusives, countryListString),
- offeringsByCarrier: mapValues(offeringsByCarrier, (offerings) => mapValues(offerings, countryListString)),
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement