UNPKG

react-country-hook

Version:

A React.js Hook that provides country information for visitors

1 lines 1.31 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.useCountry=void 0;var react_1=require("react"),data_1=require("./data.min"),DEFAULT_COUNTRY={name:"",nameAr:"",timezone:"",flagCode:"",prefix:"",callCode:"",phoneMax:10,phoneMin:9},useCountry=function(e){var n=(0,react_1.useState)(DEFAULT_COUNTRY),r=n[0],t=n[1];return(0,react_1.useEffect)((function(){try{var n=find(e);t(n||DEFAULT_COUNTRY)}catch(e){console.error("useCountry: Error in finding country,",e),t(DEFAULT_COUNTRY)}}),[e]),r};exports.useCountry=useCountry;var find=function(e){try{if(!e||(null==e?void 0:e.default)){var n=Intl.DateTimeFormat().resolvedOptions().timeZone;return data_1.phoneCodes.find((function(e){return e.timezone===n}))||handleDefault((null==e?void 0:e.default)||"")||null}var r=Object.keys(e)[0];if(!r)return null;var t=data_1.phoneCodes.find((function(n){return n[r]===e[r]}));return t||(null==e?void 0:e.default)?t||(e.default?handleDefault(e.default):null)||null:(console.warn("useCountry: Country not found"),null)}catch(e){return console.error("useCountry: Error in finding country:",e),null}},handleDefault=function(e){if(!e)return null;var n=["name","nameAr","callCode","timezone","flagCode"];return data_1.phoneCodes.find((function(r){return n.some((function(n){return r[n]===e}))}))||null};