UNPKG

react-google-geocoding

Version:

A lightweight wrapper around Google Places Autocomplete and Directions APIs.

2 lines (1 loc) 6.44 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n,o,i=t(e),r="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function u(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var a=u(function(){if(o)return n;o=1;var e=/^\s+|\s+$/g,t=/^[-+]0x[0-9a-f]+$/i,i=/^0b[01]+$/i,u=/^0o[0-7]+$/i,a=parseInt,c="object"==typeof r&&r&&r.Object===Object&&r,f="object"==typeof self&&self&&self.Object===Object&&self,s=c||f||Function("return this")(),l=Object.prototype.toString,d=Math.max,p=Math.min,v=function(){return s.Date.now()};function y(e){var t=typeof e;return!!e&&("object"==t||"function"==t)}function g(n){if("number"==typeof n)return n;if(function(e){return"symbol"==typeof e||function(e){return!!e&&"object"==typeof e}(e)&&"[object Symbol]"==l.call(e)}(n))return NaN;if(y(n)){var o="function"==typeof n.valueOf?n.valueOf():n;n=y(o)?o+"":o}if("string"!=typeof n)return 0===n?n:+n;n=n.replace(e,"");var r=i.test(n);return r||u.test(n)?a(n.slice(2),r?2:8):t.test(n)?NaN:+n}return n=function(e,t,n){var o,i,r,u,a,c,f=0,s=!1,l=!1,b=!0;if("function"!=typeof e)throw new TypeError("Expected a function");function w(t){var n=o,r=i;return o=i=void 0,f=t,u=e.apply(r,n)}function h(e){var n=e-c;return void 0===c||n>=t||n<0||l&&e-f>=r}function m(){var e=v();if(h(e))return S(e);a=setTimeout(m,function(e){var n=t-(e-c);return l?p(n,r-(e-f)):n}(e))}function S(e){return a=void 0,b&&o?w(e):(o=i=void 0,u)}function O(){var e=v(),n=h(e);if(o=arguments,i=this,c=e,n){if(void 0===a)return function(e){return f=e,a=setTimeout(m,t),s?w(e):u}(c);if(l)return a=setTimeout(m,t),w(c)}return void 0===a&&(a=setTimeout(m,t)),u}return t=g(t)||0,y(n)&&(s=!!n.leading,r=(l="maxWait"in n)?d(g(n.maxWait)||0,t):r,b="trailing"in n?!!n.trailing:b),O.cancel=function(){void 0!==a&&clearTimeout(a),f=0,o=c=i=a=void 0},O.flush=function(){return void 0===a?u:S(v())},O}}());function c(t,n,o){void 0===n&&(n=500);var i=e.useRef();!function(t){var n=e.useRef(t);n.current=t,e.useEffect((function(){return function(){n.current()}}),[])}((function(){i.current&&i.current.cancel()}));var r=e.useMemo((function(){var e=a(t,n,o),r=function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];return e.apply(void 0,t)};return r.cancel=function(){e.cancel()},r.isPending=function(){return!!i.current},r.flush=function(){return e.flush()},r}),[t,n,o]);return e.useEffect((function(){i.current=a(t,n,o)}),[t,n,o]),r}function f(e,t,n,o){return new(n||(n=Promise))((function(i,r){function u(e){try{c(o.next(e))}catch(e){r(e)}}function a(e){try{c(o.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(u,a)}c((o=o.apply(e,t||[])).next())}))}function s(e,t){var n,o,i,r={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=a(0),u.throw=a(1),u.return=a(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function a(a){return function(c){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;u&&(u=0,a[0]&&(r=0)),r;)try{if(n=1,o&&(i=2&a[0]?o.return:a[0]?o.throw||((i=o.return)&&i.call(o),0):o.next)&&!(i=i.call(o,a[1])).done)return i;switch(o=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return r.label++,{value:a[1],done:!1};case 5:r.label++,o=a[1],a=[0];continue;case 7:a=r.ops.pop(),r.trys.pop();continue;default:if(!(i=r.trys,(i=i.length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){r=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){r.label=a[1];break}if(6===a[0]&&r.label<i[1]){r.label=i[1],i=a;break}if(i&&r.label<i[2]){r.label=i[2],r.ops.push(a);break}i[2]&&r.ops.pop(),r.trys.pop();continue}a=t.call(e,r)}catch(e){a=[6,e],o=0}finally{n=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}"function"==typeof SuppressedError&&SuppressedError;exports.useDirections=function(e){var t=e.origin,n=e.destination,o=e.language,r=e.region,u=e.avoidFerries,a=e.avoidHighways,c=e.avoidTolls,l=e.drivingOptions,d=e.optimizeWaypoints,p=e.provideRouteAlternatives,v=e.transitOptions,y=e.unitSystem,g=e.waypoints,b=e.travelMode,w=i.default.useRef(null),h=i.default.useState(null),m=h[0],S=h[1],O=i.default.useState(!1),j=O[0],x=O[1],E=i.default.useState(null),T=E[0],R=E[1];i.default.useEffect((function(){void 0!==window&&window.google&&(w.current=new window.google.maps.DirectionsService)}),[]);return i.default.useEffect((function(){if(t||n){if(!w.current)throw new Error("Directions Service not initialized");x(!0),f(void 0,void 0,void 0,(function(){var e;return s(this,(function(i){switch(i.label){case 0:return[4,null===(e=w.current)||void 0===e?void 0:e.route({origin:t,destination:n,language:o,region:r,avoidFerries:u,avoidHighways:a,avoidTolls:c,drivingOptions:l,optimizeWaypoints:d,provideRouteAlternatives:p,transitOptions:v,unitSystem:y,waypoints:g,travelMode:b},(function(e,t){S(e),R(t),x(!1)}))];case 1:return i.sent(),[2]}}))}))}}),[t,n]),{directions:m,isLoading:j,status:T}},exports.usePlacePredictions=function(t){var n=t.input,o=t.debounceMs,r=void 0===o?300:o,u=t.sessionToken,a=t.locationBias,f=t.locationRestriction,s=t.language,l=t.types,d=t.componentRestrictions,p=t.offset,v=t.origin,y=t.region,g=t.location,b=t.radius,w=t.bounds,h=i.default.useRef(null),m=i.default.useState([]),S=m[0],O=m[1],j=i.default.useState(!1),x=j[0],E=j[1],T=i.default.useState(null),R=T[0],M=T[1],P=function(t,n,o){var i,r=null!==(i=null==o?void 0:o.equalityFn)&&void 0!==i?i:function(e,t){return e===t},u=t instanceof Function?t():t,a=e.useState(u),f=a[0],s=a[1],l=e.useRef(u),d=c(s,n,o);return r(l.current,u)||(d(u),l.current=u),[f,d]}(n,r)[0];return i.default.useEffect((function(){void 0!==window&&window.google&&(h.current=new window.google.maps.places.AutocompleteService)}),[]),i.default.useEffect((function(){if(n){if(!h.current)throw new Error("Autocomplete Service not initialized");E(!0),h.current.getPlacePredictions({input:P,language:s,locationBias:a,region:y,offset:p,origin:v,locationRestriction:f,sessionToken:u,types:l,componentRestrictions:d,location:g,radius:b,bounds:w},(function(e,t){O(e||[]),M(t),E(!1)}))}}),[P]),{predictions:S,isLoading:x,status:R}};