UNPKG

kickass-utilities

Version:
82 lines (71 loc) 3.61 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } import fetchJsonp from 'fetch-jsonp'; import { countries } from 'country-data'; var GELOCOATION_DB_URL = 'https://geolocation-db.com/jsonp'; var countriesWithoutFlag = ['aq', 'bl', 'bq', 'cw', 'gg', 'im', 'je', 'mf', 'ss', 'sx']; var countryCodeOptions = countries.all.filter(function (_ref) { var status = _ref.status, alpha2 = _ref.alpha2, countryCallingCodes = _ref.countryCallingCodes; return status === 'assigned' && alpha2 && !countriesWithoutFlag.includes(alpha2.toLowerCase()) && (countryCallingCodes === null || countryCallingCodes === void 0 ? void 0 : countryCallingCodes[0]); }).map(function (_ref2) { var name = _ref2.name, alpha2 = _ref2.alpha2, countryCallingCodes = _ref2.countryCallingCodes; return { key: alpha2, value: "".concat(name, " (").concat(countryCallingCodes === null || countryCallingCodes === void 0 ? void 0 : countryCallingCodes[0], ")"), text: "(".concat(alpha2, ") ").concat(countryCallingCodes === null || countryCallingCodes === void 0 ? void 0 : countryCallingCodes[0]), flag: alpha2.toLowerCase() }; }); var fetchGeolocation = /*#__PURE__*/ function () { var _ref3 = _asyncToGenerator( /*#__PURE__*/ _regeneratorRuntime.mark(function _callee() { return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.t0 = formatGeolocation; _context.next = 3; return fetchJsonp(GELOCOATION_DB_URL, { jsonpCallbackFunction: 'callback' }); case 3: _context.next = 5; return _context.sent.json(); case 5: _context.t1 = _context.sent; return _context.abrupt("return", (0, _context.t0)(_context.t1)); case 7: case "end": return _context.stop(); } } }, _callee); })); return function fetchGeolocation() { return _ref3.apply(this, arguments); }; }(); var formatGeolocation = function formatGeolocation(_ref4) { var country_name = _ref4.country_name, country_code = _ref4.country_code, rest = _objectWithoutProperties(_ref4, ["country_name", "country_code"]); return _objectSpread({ countryName: country_name, countryCode: country_code }, rest); }; export default { countryCodeOptions: countryCodeOptions, fetchGeolocation: fetchGeolocation };