Skip to content

Commit

Permalink
fix: 🐛 nearby asIds response with distance
Browse files Browse the repository at this point in the history
  • Loading branch information
iwpnd committed Dec 13, 2022
1 parent d446d00 commit a6d9fef
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ This is a Typescript client for Tile38 that allows for a type-safe interaction w
"node": ">=14.x"
"redis" ">=4.3.0"
```

### Installation

```sh
Expand Down Expand Up @@ -446,16 +447,21 @@ await tile38.intersects('fleet').get('warehouses', 'Berlin').asIds();
#### Nearby

```typescript
await tile38.set('fleet', 'truck1')
.point(33.5123, -112.2693)
.exec();
await tile38.set('fleet', 'truck1').point(33.5123, -112.2693).exec();

await tile38.nearby('fleet').point(33.5124, -112.2694);
await tile38.nearby('fleet').point(33.5124, -112.2694).asCount();
> {"ok":true,"count":1,"cursor":0,"elapsed":"42.8µs"}

await tile38.nearby('fleet').point(33.5124, -112.2694, 10);
await tile38.nearby('fleet').point(33.5124, -112.2694, 10).asCount();
// because truck1 is further away than 10m
> {"ok":true,"count":0,"cursor":0,"elapsed":"36µs"}

// asIds request
await tile38.nearby('fleet').point(33.5124, -112.2694).asIds();
> {"ok":true,"ids":['truck1'],"cursor":0,"elapsed":"36µs"}
// asIds with distance
await tile38.nearby('fleet').distance().point(33.5124, -112.2694).asIds();
> {"ok":true,"ids":[{ "id":"truck1", "distance": 1 }],"cursor":0,"elapsed":"36µs"}
```

**Options**
Expand Down Expand Up @@ -801,6 +807,7 @@ Contributions are what make the open source community such an amazing place to b
MIT

## Maintainer

Vincent Priem - [@vpriem](https://github.com/vpriem)
Benjamin Ramser - [@iwpnd](https://github.com/iwpnd)
Juliana Schwarz - [@julschwarz](https://github.com/julschwarz)
Expand Down
2 changes: 1 addition & 1 deletion src/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export type StringObjectsResponse = ExtendResponse<{

// IDS
export type IdsResponse = ExtendResponse<{
ids: string[];
ids: (string | { id: string; distance: number })[];
count: number;
cursor: number;
}>;
Expand Down
28 changes: 28 additions & 0 deletions src/tests/nearby.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,34 @@ describe('nearby', () => {
]);
});

it('should return ids with distance', async () => {
const expected: IdsResponse = {
elapsed: expect.any(String) as string,
ok: true,
ids: [{ id: 'truck1', distance: expect.any(Number) as number }],
count: 1,
cursor: 0,
};

await expect(
tile38
.nearby('fleet')
.distance()
.point(33.5123, -112.2693, 100)
.asIds()
).resolves.toEqual(expected);

expect(command).toHaveBeenCalledWith('NEARBY', [
'fleet',
'DISTANCE',
'IDS',
'POINT',
33.5123,
-112.2693,
100,
]);
});

it('should return count', async () => {
const expected: CountResponse = {
elapsed: expect.any(String) as string,
Expand Down

0 comments on commit a6d9fef

Please sign in to comment.