diff --git a/dist/telefonos_argentinos.min.js b/dist/telefonos_argentinos.min.js index 703b821..ac8416c 100644 --- a/dist/telefonos_argentinos.min.js +++ b/dist/telefonos_argentinos.min.js @@ -1,10 +1 @@ -/* -title: Argentinian phonenumber validator \ - Validador de números telefónicos argentinos. -autor: Agustín Bouillet -year: 2016 -email: agustin.bouillet@gmail.com -website: www.bouillet.com.ar -gitHub: https://github.com/agustinbouillet/validador-de-numeros-de-telefono-argentinos -*/ -const json_url="https://spreadsheets.google.com/feeds/list/14H7VE3zfllDDTC73L0bL7nyjkdodPMXvqs1CH__xgFY/1/public/values?alt=json";var geo_politics=[];function TelefonoArgentino(e){this.input=e,this.getData=telefono,this.getType=getTelephoneType,this.isValid=isValid,this.filterInt=filterInt(e),this.invalidChars=invalidChars,this.getGeoPolitc=getGeoPolitc}function region_by_code(e){var t=!1;return(geo_politics=JSON.parse(localStorage.getItem("geo_data")))&&geo_politics.forEach(function(n){n.code==e&&(t=n)}),t}function telefono(){var e=!1,t=this.input.replace(/([\-\.\(\)\[\]\s\+]+)/g,"").match(/^(?:(?:(00)?(?:(\+?54)|(0)?)?(?:(9)?(((3894|3892|3891|3888|3887|3886|3885|3878|3877|3876|3873|3869|3868|3867|3865|3863|3862|3861|3858|3857|3856|3855|3854|3846|3845|3844|3843|3841|3838|3837|3835|3832|3827|3826|3825|3821|3786|3782|3781|3777|3775|3774|3773|3772|3758|3757|3756|3755|3754|3751|3743|3741|3735|3734|3731|3725|3721|3718|3716|3715|3711|3585|3584|3583|3582|3576|3575|3574|3573|3572|3571|3564|3563|3562|3549|3548|3547|3546|3544|3543|3542|3541|3537|3533|3532|3525|3524|3522|3521|3498|3497|3496|3493|3492|3491|3489|3487|3483|3482|3476|3472|3471|3469|3468|3467|3466|3465|3464|3463|3462|3460|3458|3456|3455|3454|3447|3446|3445|3444|3442|3438|3437|3436|3435|3409|3408|3407|3406|3405|3404|3402|3401|3400|3388|3387|3385|3382|3329|3327|2983|2982|2972|2966|2964|2963|2962|2954|2953|2952|2948|2946|2945|2942|2940|2936|2935|2934|2933|2932|2931|2929|2928|2927|2926|2925|2924|2923|2922|2921|2920|2903|2902|2901|2658|2657|2656|2655|2652|2651|2648|2647|2646|2626|2625|2624|2622|2478|2477|2475|2474|2473|2396|2395|2394|2393|2392|2358|2357|2356|2355|2354|2353|2352|2346|2345|2344|2343|2342|2338|2337|2336|2335|2334|2333|2331|2326|2325|2324|2323|2320|2317|2316|2314|2302|2297|2296|2292|2291|2286|2285|2284|2283|2281|2274|2273|2272|2271|2268|2267|2266|2265|2264|2262|2261|2257|2255|2254|2252|2246|2245|2244|2243|2242|2241|2229|2227|2226|2225|2224|2223|2221|2202)(15)?([\d]{6})|(388|387|385|383|381|380|379|376|370|364|362|358|353|351|348|345|343|342|341|336|299|298|297|294|291|280|266|264|263|261|260|249|237|236|230|223|221|220)(15)?([\d]{7})|(11)(15)?([\d]{8})|(15)([\d]{6})|(15)([\d]{7})|(15)([\d]{8})|([\d]{6,8}))|(0800|0810|0822|0823|0610|0611|0612|0609|0600|0747|0939|0605|0603)([\d]{7,8}))?)|(000|19|100|101|102|103|105|106|107|108|110|112|113|114|115|121|125|126|130|131|132|133|134|135|136|137|138|139|144|145|147|911|000)))$/);if(null!==t){var n=getType(e=setData(t));if(!n)return!1;e.input=this.input,e.type=n,e.format=numberFormat(e),e.htmlify=htmlify(e)}return e}function filterInt(e){return re=/([^\d])/g,subst="",result=e.replace(re,subst),result}function invalidChars(){for(var e,t=/([^\d\-\(\)\[\]\s\+\.])/g,n=new Array;null!==(e=t.exec(this.input));)e.index===t.lastIndex&&t.lastIndex++,n.indexOf(e[0])<0&&n.push(e[0]);return n.length>0&&n}function setData(e){function t(t){var n=!1;return e.forEach(function(i,o){t.includes(o)&&void 0!==e[o]&&(n=e[o])}),n}var n={filter_input:void 0!==e[0]&&e[0],international:void 0!==e[1]&&e[1],country:void 0!==e[2]&&e[2],national_call:void 0!==e[3]&&e[3],mobile:void 0!==e[4]&&e[4],special:void 0!==e[25]&&e[25],specific:void 0!==e[23]&&e[23]};n.number=t([9,15,12,21,17,19,22,24]);n.mobile_prefix=t([8,11,14,18,20]);return n.area_code=t([13,10,7]),n}function isValid(){return!!this.getData()}function getTelephoneType(){return this.getData().type}function getType(e){return(!e.mobile_prefix||!e.mobile)&&(e.special?type="special":e.specific&&7===e.number.length?type="specific":e.area_code&&e.area_code.length+e.number.length===10?type=e.mobile_prefix||e.mobile?"mobile":"landline":!e.area_code&&e.number.length>=6&&e.number.length<=8?type=e.mobile_prefix||e.mobile?"mobile":"landline":type=!1,type)}function cleanupNumberFormat(e){return e.replace(/(^\s*|\s(?=\s+)|\s*$)/g,"")}function numberFormat(e){if(!e.number)return e.filter_input;var t=`${e.number.slice(0,e.number.length-4)}-${e.number.slice(-4)}`,n=e.country?"+"+e.country:"",i=[];for(key in e)i[key]=e[key]?e[key]:"";return cleanupNumberFormat([i.international,n,i.mobile,i.national_call+i.area_code,i.mobile_prefix,i.specific,t].join(" "))}function htmlify(e){if(!e.number)return e.filter_input;var t=`${e.number.slice(0,e.number.length-4)}-${e.number.slice(-4)}`,n=document.createElement("span");n.className="number",n.textContent=t;var i=document.createElement("span");i.className="country",i.dataset.country="1",i.textContent=e.country?"+"+e.country:"";var o=[];for(key in e){var r=document.createElement("span");e[key]?(r.className=key.replace("_","-"),r.dataset[key.replace(/[^a-zA-Z]/g,"")]=1,r.textContent=e[key],o[key]=r.outerHTML):o[key]=""}return cleanupNumberFormat([o.international,i.outerHTML,o.mobile,o.national_call+o.area_code,o.mobile_prefix,o.specific,n.outerHTML].join(" "))}function getGeoPolitc(e){var t=[];if(!this.getData().area_code)return!1;var n=region_by_code(this.getData().area_code);if(n){var i=n.localities.replaceAll(/,\s*/g,",").split(",");t={provincia:n.jurisdiction,ciudad:i[0],localidades:i}}return t}localStorage.getItem("geo_data")?geo_politics=localStorage.getItem("geo_data"):fetch(json_url,{method:"get",headers:{Accept:"application/json","Content-Type":"application/json"}}).then(function(e){return e.json()}).then(function(e){if(geo_politics=[],e.hasOwnProperty("feed"))for(var t=0;t<=e.feed.entry.length-1;t++)geo_politics.push({code:e.feed.entry[t].gsx$code.$t,jurisdiction:e.feed.entry[t].gsx$jurisdiction.$t,localities:e.feed.entry[t].gsx$localities.$t});else e&&e[0].hasOwnProperty("code")&&(geo_politics=e);localStorage.setItem("geo_data",JSON.stringify(geo_politics))}).catch(function(e){console.log("parsing failed",e)}); \ No newline at end of file +var geo_politics=[];const gapi_id="14H7VE3zfllDDTC73L0bL7nyjkdodPMXvqs1CH__xgFY",gapi_sheet_name="db",gapi_credential="AIzaSyAll9EH1aTmZDewNSyM_CU_AIsGOiEDyZs",gapi_uri=`https://sheets.googleapis.com/v4/spreadsheets/${gapi_id}/values/db?key=${gapi_credential}&alt=json`;var gapi_legacy=function(e){if(!e.hasOwnProperty("values"))return!1;const t=e.values[0];let i=[];const n=/\s|\/|_/gi;return e.values.forEach((e,a)=>{if(a>0){let a={};for(var r in t){var o=e.hasOwnProperty(r)?e[r].trim():"";a[t[r].toLowerCase().replace(n,"")]=o}i.push(a)}}),i};function TelefonoArgentino(e){this.input=e,this.getData=telefono,this.getType=getTelephoneType,this.isValid=isValid,this.filterInt=filterInt(e),this.invalidChars=invalidChars,this.getGeoPolitc=getGeoPolitc}function region_by_code(e){var t=!1;return(geo_politics=JSON.parse(localStorage.getItem("geo_data")))&&geo_politics.forEach(function(i){i.code==e&&(t=i)}),t}function telefono(){var e=!1,t=this.input.replace(/([\-\.\(\)\[\]\s\+]+)/g,"").match(/^(?:(?:(00)?(?:(\+?54)|(0)?)?(?:(9)?(((3894|3892|3891|3888|3887|3886|3885|3878|3877|3876|3873|3869|3868|3867|3865|3863|3862|3861|3858|3857|3856|3855|3854|3846|3845|3844|3843|3841|3838|3837|3835|3832|3827|3826|3825|3821|3786|3782|3781|3777|3775|3774|3773|3772|3758|3757|3756|3755|3754|3751|3743|3741|3735|3734|3731|3725|3721|3718|3716|3715|3711|3585|3584|3583|3582|3576|3575|3574|3573|3572|3571|3564|3563|3562|3549|3548|3547|3546|3544|3543|3542|3541|3537|3533|3532|3525|3524|3522|3521|3498|3497|3496|3493|3492|3491|3489|3487|3483|3482|3476|3472|3471|3469|3468|3467|3466|3465|3464|3463|3462|3460|3458|3456|3455|3454|3447|3446|3445|3444|3442|3438|3437|3436|3435|3409|3408|3407|3406|3405|3404|3402|3401|3400|3388|3387|3385|3382|3329|3327|2983|2982|2972|2966|2964|2963|2962|2954|2953|2952|2948|2946|2945|2942|2940|2936|2935|2934|2933|2932|2931|2929|2928|2927|2926|2925|2924|2923|2922|2921|2920|2903|2902|2901|2658|2657|2656|2655|2652|2651|2648|2647|2646|2626|2625|2624|2622|2478|2477|2475|2474|2473|2396|2395|2394|2393|2392|2358|2357|2356|2355|2354|2353|2352|2346|2345|2344|2343|2342|2338|2337|2336|2335|2334|2333|2331|2326|2325|2324|2323|2320|2317|2316|2314|2302|2297|2296|2292|2291|2286|2285|2284|2283|2281|2274|2273|2272|2271|2268|2267|2266|2265|2264|2262|2261|2257|2255|2254|2252|2246|2245|2244|2243|2242|2241|2229|2227|2226|2225|2224|2223|2221|2202)(15)?([\d]{6})|(388|387|385|383|381|380|379|376|370|364|362|358|353|351|348|345|343|342|341|336|299|298|297|294|291|280|266|264|263|261|260|249|237|236|230|223|221|220)(15)?([\d]{7})|(11)(15)?([\d]{8})|(15)([\d]{6})|(15)([\d]{7})|(15)([\d]{8})|([\d]{6,8}))|(0800|0810|0822|0823|0610|0611|0612|0609|0600|0747|0939|0605|0603)([\d]{7,8}))?)|(000|19|100|101|102|103|105|106|107|108|110|112|113|114|115|121|125|126|130|131|132|133|134|135|136|137|138|139|144|145|147|911|000)))$/);if(null!==t){var i=getType(e=setData(t));if(!i)return!1;e.input=this.input,e.type=i,e.format=numberFormat(e),e.htmlify=htmlify(e)}return e}function filterInt(e){return re=/([^\d])/g,subst="",result=e.replace(re,subst),result}function invalidChars(){for(var e,t=/([^\d\-\(\)\[\]\s\+\.])/g,i=new Array;null!==(e=t.exec(this.input));)e.index===t.lastIndex&&t.lastIndex++,i.indexOf(e[0])<0&&i.push(e[0]);return i.length>0&&i}function setData(e){function t(t){var i=!1;return e.forEach(function(n,a){t.includes(a)&&void 0!==e[a]&&(i=e[a])}),i}var i={filter_input:void 0!==e[0]&&e[0],international:void 0!==e[1]&&e[1],country:void 0!==e[2]&&e[2],national_call:void 0!==e[3]&&e[3],mobile:void 0!==e[4]&&e[4],special:void 0!==e[25]&&e[25],specific:void 0!==e[23]&&e[23]};i.number=t([9,15,12,21,17,19,22,24]);i.mobile_prefix=t([8,11,14,18,20]);return i.area_code=t([13,10,7]),i}function isValid(){return!!this.getData()}function getTelephoneType(){return this.getData().type}function getType(e){return(!e.mobile_prefix||!e.mobile)&&(e.special?type="special":e.specific&&7===e.number.length?type="specific":e.area_code&&e.area_code.length+e.number.length===10?type=e.mobile_prefix||e.mobile?"mobile":"landline":!e.area_code&&e.number.length>=6&&e.number.length<=8?type=e.mobile_prefix||e.mobile?"mobile":"landline":type=!1,type)}function cleanupNumberFormat(e){return e.replace(/(^\s*|\s(?=\s+)|\s*$)/g,"")}function numberFormat(e){if(!e.number)return e.filter_input;var t=`${e.number.slice(0,e.number.length-4)}-${e.number.slice(-4)}`,i=e.country?"+"+e.country:"",n=[];for(key in e)n[key]=e[key]?e[key]:"";return cleanupNumberFormat([n.international,i,n.mobile,n.national_call+n.area_code,n.mobile_prefix,n.specific,t].join(" "))}function htmlify(e){if(!e.number)return e.filter_input;var t=`${e.number.slice(0,e.number.length-4)}-${e.number.slice(-4)}`,i=document.createElement("span");i.className="number",i.textContent=t;var n=document.createElement("span");n.className="country",n.dataset.country="1",n.textContent=e.country?"+"+e.country:"";var a=[];for(key in e){var r=document.createElement("span");e[key]?(r.className=key.replace("_","-"),r.dataset[key.replace(/[^a-zA-Z]/g,"")]=1,r.textContent=e[key],a[key]=r.outerHTML):a[key]=""}return cleanupNumberFormat([a.international,n.outerHTML,a.mobile,a.national_call+a.area_code,a.mobile_prefix,a.specific,i.outerHTML].join(" "))}function getGeoPolitc(e){var t=[];if(!this.getData().area_code)return!1;var i=region_by_code(this.getData().area_code);if(i){var n=i.localities.replaceAll(/,\s*/g,",").split(",");t={provincia:i.jurisdiction,ciudad:n[0],localidades:n}}return t}localStorage.getItem("geo_data")?geo_politics=localStorage.getItem("geo_data"):fetch(gapi_uri,{method:"get",headers:{Accept:"application/json","Content-Type":"application/json"}}).then(function(e){return e.json()}).then(function(e){geo_politics=gapi_legacy(e),localStorage.setItem("geo_data",JSON.stringify(geo_politics))}).catch(function(e){console.log("parsing failed",e)}); \ No newline at end of file diff --git a/src/telefonos_argentinos.js b/src/telefonos_argentinos.js index 04307e7..529d7c3 100644 --- a/src/telefonos_argentinos.js +++ b/src/telefonos_argentinos.js @@ -7,15 +7,46 @@ email: agustin.bouillet@gmail.com website: www.bouillet.com.ar gitHub: https://github.com/agustinbouillet/validador-de-numeros-de-telefono-argentinos */ -const json_url = 'https://spreadsheets.google.com/feeds/list/14H7VE3zfllDDTC73L0bL7nyjkdodPMXvqs1CH__xgFY/1/public/values?alt=json'; var geo_politics = []; +const gapi_id = '14H7VE3zfllDDTC73L0bL7nyjkdodPMXvqs1CH__xgFY'; +const gapi_sheet_name = "db"; +const gapi_credential = "AIzaSyAll9EH1aTmZDewNSyM_CU_AIsGOiEDyZs"; +const gapi_uri = `https://sheets.googleapis.com/v4/spreadsheets/${gapi_id}/values/${gapi_sheet_name}?key=${gapi_credential}&alt=json`; + + + +var gapi_legacy = function(response){ + if(!response.hasOwnProperty('values')){ + return false; + } + + const keys = response.values[0]; + let entry = []; + const regex = /\s|\/|_/gi; + + response.values.forEach((v, k) => { + if(k > 0){ + + let zip = {}; + for(var i in keys){ + var value = (v.hasOwnProperty(i))? v[i].trim() : ""; + var key = keys[i].toLowerCase().replace(regex, ''); + zip[key] = value; + } + entry.push(zip); + } + }); + + return entry; +} + /** * Retorna el JSON con las regiones. */ if(!localStorage.getItem('geo_data')){ - fetch(json_url, { + fetch(gapi_uri, { method: "get", headers: { "Accept" : "application/json", @@ -24,24 +55,7 @@ if(!localStorage.getItem('geo_data')){ }).then(function(response) { return response.json(); }).then(function(data) { - geo_politics = []; - - // Chequea si la data es de spreadshhets - if(data.hasOwnProperty('feed')){ - - for(var i = 0; i <= data.feed.entry.length - 1; i++){ - - geo_politics.push({ - code : data.feed.entry[i].gsx$code.$t, - jurisdiction : data.feed.entry[i].gsx$jurisdiction.$t, - localities : data.feed.entry[i].gsx$localities.$t - }); - - } - } else if(data && data[0].hasOwnProperty('code')) { - geo_politics = data; - } - + geo_politics = gapi_legacy(data); localStorage.setItem('geo_data', JSON.stringify(geo_politics)); }).catch(function(ex) {