File: /srv/www/csmcri-res-in-d10/web/libraries/algolia.places/src/formatHit.js
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = formatHit;
var _findCountryCode = _interopRequireDefault(require("./findCountryCode"));
var _findType = _interopRequireDefault(require("./findType"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
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; }
function getBestHighlightedForm(highlightedValues) {
var defaultValue = highlightedValues[0].value; // collect all other matches
var bestAttributes = [];
for (var i = 1; i < highlightedValues.length; ++i) {
if (highlightedValues[i].matchLevel !== 'none') {
bestAttributes.push({
index: i,
words: highlightedValues[i].matchedWords
});
}
} // no matches in this attribute, retrieve first value
if (bestAttributes.length === 0) {
return defaultValue;
} // sort the matches by `desc(words), asc(index)`
bestAttributes.sort(function (a, b) {
if (a.words > b.words) {
return -1;
} else if (a.words < b.words) {
return 1;
}
return a.index - b.index;
}); // and append the best match to the first value
return bestAttributes[0].index === 0 ? "".concat(defaultValue, " (").concat(highlightedValues[bestAttributes[1].index].value, ")") : "".concat(highlightedValues[bestAttributes[0].index].value, " (").concat(defaultValue, ")");
}
function getBestPostcode(postcodes, highlightedPostcodes) {
var defaultValue = highlightedPostcodes[0].value; // collect all other matches
var bestAttributes = [];
for (var i = 1; i < highlightedPostcodes.length; ++i) {
if (highlightedPostcodes[i].matchLevel !== 'none') {
bestAttributes.push({
index: i,
words: highlightedPostcodes[i].matchedWords
});
}
} // no matches in this attribute, retrieve first value
if (bestAttributes.length === 0) {
return {
postcode: postcodes[0],
highlightedPostcode: defaultValue
};
} // sort the matches by `desc(words)`
bestAttributes.sort(function (a, b) {
if (a.words > b.words) {
return -1;
} else if (a.words < b.words) {
return 1;
}
return a.index - b.index;
});
var postcode = postcodes[bestAttributes[0].index];
return {
postcode: postcode,
highlightedPostcode: highlightedPostcodes[bestAttributes[0].index].value
};
}
function formatHit(_ref) {
var formatInputValue = _ref.formatInputValue,
hit = _ref.hit,
hitIndex = _ref.hitIndex,
query = _ref.query,
rawAnswer = _ref.rawAnswer;
try {
var name = hit.locale_names[0];
var country = hit.country;
var administrative = hit.administrative && hit.administrative[0] !== name ? hit.administrative[0] : undefined;
var city = hit.city && hit.city[0] !== name ? hit.city[0] : undefined;
var suburb = hit.suburb && hit.suburb[0] !== name ? hit.suburb[0] : undefined;
var county = hit.county && hit.county[0] !== name ? hit.county[0] : undefined;
var _ref2 = hit.postcode && hit.postcode.length ? getBestPostcode(hit.postcode, hit._highlightResult.postcode) : {
postcode: undefined,
highlightedPostcode: undefined
},
postcode = _ref2.postcode,
highlightedPostcode = _ref2.highlightedPostcode;
var highlight = {
name: getBestHighlightedForm(hit._highlightResult.locale_names),
city: city ? getBestHighlightedForm(hit._highlightResult.city) : undefined,
administrative: administrative ? getBestHighlightedForm(hit._highlightResult.administrative) : undefined,
country: country ? hit._highlightResult.country.value : undefined,
suburb: suburb ? getBestHighlightedForm(hit._highlightResult.suburb) : undefined,
county: county ? getBestHighlightedForm(hit._highlightResult.county) : undefined,
postcode: highlightedPostcode
};
var suggestion = {
name: name,
administrative: administrative,
county: county,
city: city,
suburb: suburb,
country: country,
countryCode: (0, _findCountryCode["default"])(hit._tags),
type: (0, _findType["default"])(hit._tags),
latlng: {
lat: hit._geoloc.lat,
lng: hit._geoloc.lng
},
postcode: postcode,
postcodes: hit.postcode && hit.postcode.length ? hit.postcode : undefined
}; // this is the value to put inside the <input value=
var value = formatInputValue(suggestion);
return _objectSpread(_objectSpread({}, suggestion), {}, {
highlight: highlight,
hit: hit,
hitIndex: hitIndex,
query: query,
rawAnswer: rawAnswer,
value: value
});
} catch (e) {
/* eslint-disable no-console */
console.error('Could not parse object', hit);
console.error(e);
/* eslint-enable no-console */
return {
value: 'Could not parse object'
};
}
}