question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Plans to improve API response

See original GitHub issue

Hi,

In the past few weeks I had to implement this but for React Native, so I ended up doing mostly the same you are doing here…

All good, until they made me notice that results are not displayed the same as in the demo in https://community.algolia.com/places. After expending some time having a look to the code, I found this file, and I was really surprised about all the parsing you are doing to what comes in the response.

I was wondering if there is a plan to improve the API in a way that there would be no need to do so, and also improve its structure, since now it’s very hard to get what some fields means…

This is what I’m doing now

import _isEqual from 'lodash/isEqual';

const getName = ({locale_names}) =>
  locale_names[0].replace('<streetnum>', '');

const getFirst = array => array && array[0];

const getCity = ({city, county}) =>
  getFirst(city)
  || getFirst(county);

const getPlace = result => ({
  city: getCity(result),
  countryCode: result.country_code,
  name: getName(result),
  postalCode: result.postcode[0],
  state: result.administrative && result.administrative[0],
});

const getCoordinate = ({_geoloc}) => ({
  latitude: _geoloc.lat,
  longitude: _geoloc.lng,
});

const areLocationsEqual = (l1, l2) =>
  l1 && l2
  && _isEqual(l1.coordinate, l2.coordinate)
  && _isEqual(l1.place, l2.place);

const toLocation = (result, currentLocation) => {
  const location = {
    coordinate: getCoordinate(result),
    place: getPlace(result),
  };

  location.isCurrent = areLocationsEqual(location, currentLocation);

  return location;
};

const manyToLocation = (results, currentLocation) => results.map(
  curResult => toLocation(
    curResult,
    currentLocation
  )
);

export default manyToLocation;

In some parts we are doing the same, but in others now that I have seen what you are doing, well, I guess I misunderstood some names and I ended up with a couple of differences…

Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
JonathanMontanecommented, Sep 22, 2017

@vvo @Nagriar This is actually a simple typo bug. There’s a mismatch between what’s being pushed to bestAttributes (index and matchedWords) and what’s being used for each item in the array (index and words).

You have

bestAttributes.push({
  index: i,
  matchedWords: highlightedValues[i].words,
});

and

bestAttributes.sort((a, b) => {
  if (a.words > b.words) {
    return -1;
  } else if (a.words < b.words) {
    return 1;
  }
  return a.index - b.index;
});

lines for reference:

Either change matchedWords to words, or the opposite and that should solve the first issue @mgtitimoli is talking about. (The sort currently does nothing)

0reactions
JonathanMontanecommented, Jan 22, 2019

Sorry for the late reply, this was fixed in #435

Read more comments on GitHub >

github_iconTop Results From Across the Web

8 Tips for Optimizing an API - DreamFactory Software- Blog
Want to know how optimizing an API will enable better performance? These tips will have you on your way to getting the most...
Read more >
5 Ways on How to Improve Your API's Performance [Guest Post]
Think Ahead about the Things That Slow APIs Down · Build Your API for High Performance on a Comprehensive Design Tool · Use...
Read more >
3 Ways to Make Your API Responses Flexible - Zapier
Most APIs deliver their responses the exact same way to every client. That's by design; developers need to know what to expect.
Read more >
5 Tips to Optimize Your API's Performance - LoadNinja
#1. Cache Requests ... Caching is one of the best ways to improve API performance. If you have requests that frequently produce the...
Read more >
Tips and tricks on optimizing your API performance
One of the best ways to improve API performance is by caching to serve content faster. If there are concurrent requests frequently producing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found