UNPKG

universal-geocoder

Version:

Universal geocoding abstraction server-side and client-side with multiple built-in providers

138 lines 5.59 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var query_1 = require("../../query"); var Geocoded_1 = __importDefault(require("../../Geocoded")); var AdminLevel_1 = require("../../AdminLevel"); var MapQuestGeocodeQuery = /** @class */ (function (_super) { __extends(MapQuestGeocodeQuery, _super); function MapQuestGeocodeQuery(_a) { var location = _a.location, geocodeQueryObject = __rest(_a, ["location"]); var _this = _super.call(this, __assign(__assign({}, geocodeQueryObject), { text: location ? "from_location" : geocodeQueryObject.text })) || this; if (!location) { return _this; } if ("getLatLng" in location) { _this.location = location.toObject(); return _this; } if ("getLocality" in location) { _this.location = MapQuestGeocodeQuery.convertGeocodedToLocationObject(location); return _this; } var geocodedPropsNumber = 0; var mapQuestLocationPropsNumber = 0; var geocodedObject = Geocoded_1.default.create({}).toObject(); Object.keys(geocodedObject).forEach(function (prop) { if (prop in location) { geocodedPropsNumber += 1; } }); [ "street", "adminArea5", "city", "adminArea4", "county", "adminArea3", "state", "adminArea1", "country", "postalCode", "type", ].forEach(function (prop) { if (prop in location) { mapQuestLocationPropsNumber += 1; } }); if (geocodedPropsNumber > mapQuestLocationPropsNumber) { _this.location = MapQuestGeocodeQuery.convertGeocodedToLocationObject(Geocoded_1.default.create(location)); return _this; } _this.location = location; return _this; } MapQuestGeocodeQuery.create = function (object) { return new this(object); }; MapQuestGeocodeQuery.prototype.toObject = function () { return __assign(__assign({}, _super.prototype.toObject.call(this)), { location: this.location }); }; MapQuestGeocodeQuery.prototype.withLocation = function (location) { return new MapQuestGeocodeQuery(__assign(__assign({}, this.toObject()), { location: location })); }; MapQuestGeocodeQuery.prototype.getLocation = function () { return this.location; }; MapQuestGeocodeQuery.convertGeocodedToLocationObject = function (geocoded) { var location = {}; var streetParts = [ geocoded.getStreetNumber() || "", geocoded.getStreetName() || "", ]; var street = streetParts.join(" "); if (street) { location.street = street; } if (geocoded.getLocality()) { location.city = geocoded.getLocality(); } if (geocoded.getCountry()) { location.country = geocoded.getCountry(); } if (geocoded.getPostalCode()) { location.postalCode = geocoded.getPostalCode(); } geocoded.getAdminLevels().forEach(function (adminLevel) { if (adminLevel.getLevel() === AdminLevel_1.ADMIN_LEVEL_CODES.STATE_CODE) { location.state = adminLevel.getCode() || adminLevel.getName(); } if (adminLevel.getLevel() === AdminLevel_1.ADMIN_LEVEL_CODES.COUNTY_CODE) { location.county = adminLevel.getCode() || adminLevel.getName(); } }); return location; }; return MapQuestGeocodeQuery; }(query_1.GeocodeQuery)); exports.default = MapQuestGeocodeQuery; //# sourceMappingURL=MapQuestGeocodeQuery.js.map