-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created google place autocomplete service.
- Loading branch information
Showing
11 changed files
with
962 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"presets": ["es2015"], | ||
"plugins": [ | ||
["transform-object-rest-spread", { "useBuiltIns": true }] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea/ | ||
node_modules/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = init; | ||
function init() { | ||
if (!global.google || !global.google.maps) { | ||
throw new Error('Please import google maps'); | ||
} | ||
|
||
if (!/3\.\d\d\.\d+/.test(global.google.maps.version)) { | ||
throw new Error('Please import a google maps version 3+'); | ||
} | ||
|
||
return { | ||
getGoogleAutocompleteService: function getGoogleAutocompleteService() { | ||
return new global.google.maps.places.AutocompleteService(); | ||
}, | ||
getGooglePlacesService: function getGooglePlacesService() { | ||
return new global.google.maps.places.PlacesService(document.createElement('div')); | ||
}, | ||
getGeocoder: function getGeocoder() { | ||
return new global.google.maps.Geocoder(); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var googleStatus = exports.googleStatus = {}; | ||
var _google$maps$places$P = google.maps.places.PlacesServiceStatus; | ||
googleStatus.ERROR = _google$maps$places$P.ERROR; | ||
googleStatus.INVALID_REQUEST = _google$maps$places$P.INVALID_REQUEST; | ||
googleStatus.OK = _google$maps$places$P.OK; | ||
googleStatus.OVER_QUERY_LIMIT = _google$maps$places$P.OVER_QUERY_LIMIT; | ||
googleStatus.REQUEST_DENIED = _google$maps$places$P.REQUEST_DENIED; | ||
googleStatus.UNKNOWN_ERROR = _google$maps$places$P.UNKNOWN_ERROR; | ||
googleStatus.ZERO_RESULTS = _google$maps$places$P.ZERO_RESULTS; | ||
var status = exports.status = { | ||
INVALID_INPUT: 'INVALID_INPUT', | ||
NO_RESULTS: 'NO_RESULTS', | ||
SUCCESS: 'SUCCESS', | ||
PARTIAL_SUCCESS: 'PARTIAL_SUCCESS' | ||
}; | ||
|
||
var defaultPlaceTypes = exports.defaultPlaceTypes = { | ||
street_address: 'streetAddress', | ||
route: 'route', | ||
street_number: 'streetNumber', | ||
neighborhood: 'neighborhood', | ||
postal_code: 'postalCode', | ||
sublocality: 'sublocality', | ||
locality: 'locality', | ||
administrative_area_level_1: 'administrativeAreaLevel1', | ||
administrative_area_level_2: 'administrativeAreaLevel2', | ||
administrative_area_level_3: 'administrativeAreaLevel3', | ||
administrative_area_level_4: 'administrativeAreaLevel4', | ||
administrative_area_level_5: 'administrativeAreaLevel5', | ||
country: 'country' | ||
}; | ||
|
||
var validSearchTypes = exports.validSearchTypes = { | ||
geocode: 'geocode', | ||
address: 'address', | ||
establishment: 'establishment', | ||
'(regions)': '(regions)', | ||
'(cities)': '(cities)' | ||
}; | ||
|
||
var validStrategies = exports.validStrategies = ['searchByPlaceId', 'searchByText', 'searchWithGeocoder']; | ||
|
||
var outputTypes = exports.outputTypes = { | ||
street_address: [defaultPlaceTypes.streetAddress, defaultPlaceTypes.route, defaultPlaceTypes.streetNumber, defaultPlaceTypes.neighborhood, defaultPlaceTypes.postalCode, defaultPlaceTypes.sublocality, defaultPlaceTypes.locality, defaultPlaceTypes.administrativeAreaLevel1, defaultPlaceTypes.administrativeAreaLevel2, defaultPlaceTypes.administrativeAreaLevel3, defaultPlaceTypes.administrativeAreaLevel4, defaultPlaceTypes.administrativeAreaLevel5, defaultPlaceTypes.country], | ||
postal_code: [defaultPlaceTypes.postalCode, defaultPlaceTypes.sublocality, defaultPlaceTypes.locality, defaultPlaceTypes.administrativeAreaLevel1, defaultPlaceTypes.administrativeAreaLevel2, defaultPlaceTypes.administrativeAreaLevel3, defaultPlaceTypes.administrativeAreaLevel4, defaultPlaceTypes.administrativeAreaLevel5, defaultPlaceTypes.country], | ||
locality: [defaultPlaceTypes.locality, defaultPlaceTypes.administrativeAreaLevel1, defaultPlaceTypes.administrativeAreaLevel2, defaultPlaceTypes.administrativeAreaLevel3, defaultPlaceTypes.administrativeAreaLevel4, defaultPlaceTypes.administrativeAreaLevel5, defaultPlaceTypes.country], | ||
administrative_area_level_1: [defaultPlaceTypes.administrativeAreaLevel1, defaultPlaceTypes.country] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.rejectByRegex = exports.pipeStrategies = exports.getSearchStrategies = exports.getRestrictions = exports.getPlaceTypes = exports.findPlaceByType = exports.emptyResults = exports.getSearchType = exports.getLatLong = undefined; | ||
|
||
var _constants = require('./constants'); | ||
|
||
function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); } | ||
|
||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
|
||
var getLatLong = exports.getLatLong = function getLatLong(lat, long) { | ||
return new global.google.maps.LatLng(lat, long); | ||
}; | ||
|
||
var getSearchType = exports.getSearchType = function getSearchType(_ref) { | ||
var _ref$type = _ref.type, | ||
type = _ref$type === undefined ? 'locality' : _ref$type; | ||
return _constants.validSearchTypes[type] || _constants.validSearchTypes.geocode; | ||
}; | ||
|
||
var emptyResults = exports.emptyResults = function emptyResults(s) { | ||
return { status: s }; | ||
}; | ||
|
||
var findPlaceByType = exports.findPlaceByType = function findPlaceByType(data, placeType) { | ||
var placeTypes = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return data.find(function (d) { | ||
return placeTypes[d.types[0]] === placeType; | ||
}) || {}; | ||
}; | ||
|
||
var getPlaceTypes = exports.getPlaceTypes = function getPlaceTypes(_ref2) { | ||
var _ref2$outputPlaceType = _ref2.outputPlaceTypes, | ||
outputPlaceTypes = _ref2$outputPlaceType === undefined ? [] : _ref2$outputPlaceType; | ||
|
||
return Object.keys(_constants.defaultPlaceTypes).reduce(function (types, type) { | ||
if (!outputPlaceTypes.includes(_constants.defaultPlaceTypes[type])) { | ||
return types; | ||
} | ||
|
||
return Object.assign({}, types, _defineProperty({}, type, _constants.defaultPlaceTypes[type])); | ||
}, {}); | ||
}; | ||
|
||
var getRestrictions = exports.getRestrictions = function getRestrictions(_ref3) { | ||
var _ref3$filterByCountry = _ref3.filterByCountry, | ||
country = _ref3$filterByCountry === undefined ? '' : _ref3$filterByCountry; | ||
return { country: country }; | ||
}; | ||
|
||
var getSearchStrategies = exports.getSearchStrategies = function getSearchStrategies(_ref4) { | ||
var searchStrategies = _ref4.searchStrategies; | ||
|
||
if (!searchStrategies) { | ||
return _constants.validStrategies; | ||
} | ||
|
||
if (!Array.isArray(searchStrategies)) { | ||
throw new Error('searchStrategies should be of type array'); | ||
} | ||
|
||
return searchStrategies.filter(function (s) { | ||
var isValid = _constants.validStrategies.includes(s); | ||
|
||
if (!isValid) { | ||
console.error(s + ' is not a valid strategy - skipped'); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
}; | ||
|
||
var pipeStrategies = exports.pipeStrategies = function pipeStrategies(selectedStrategies, allStrategies, resolve) { | ||
var _selectedStrategies = _toArray(selectedStrategies), | ||
firstFunc = _selectedStrategies[0], | ||
restFuncs = _selectedStrategies.slice(1); | ||
|
||
if (selectedStrategies.some(function (s) { | ||
return typeof allStrategies[s] !== 'function'; | ||
})) { | ||
return {}; | ||
} | ||
|
||
if (!restFuncs.length) { | ||
return resolve(allStrategies[firstFunc]()); | ||
} | ||
|
||
return restFuncs.reduce(function (pre, func) { | ||
return resolve(pre, allStrategies[func]); | ||
}, allStrategies[firstFunc]()); | ||
}; | ||
|
||
var rejectByRegex = exports.rejectByRegex = function rejectByRegex(predictions, regex) { | ||
if (!regex) { | ||
return predictions; | ||
} | ||
|
||
return predictions.filter(function (p) { | ||
return !regex.test(p.description); | ||
}); | ||
}; | ||
|
||
exports.default = { | ||
getLatLong: getLatLong, | ||
getSearchType: getSearchType, | ||
getRestrictions: getRestrictions, | ||
emptyResults: emptyResults, | ||
findPlaceByType: findPlaceByType, | ||
getPlaceTypes: getPlaceTypes, | ||
getSearchStrategies: getSearchStrategies, | ||
pipeStrategies: pipeStrategies, | ||
rejectByRegex: rejectByRegex | ||
}; |
Oops, something went wrong.