Skip to content

Commit

Permalink
fix: members are generic
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Oct 19, 2023
1 parent e5fc2c0 commit 3895c2d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
19 changes: 19 additions & 0 deletions pkg/commands/geo_pos_test.ts → pkg/commands/geo_pos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,22 @@ Deno.test("should return empty array due to null value FooBar", async () => {
const response = await new GeoPosCommand([key, "FooBar"]).exec(client);
assertEquals(response, []);
});

Deno.test("should work with object members", async () => {
const key = "Sicily";
const members = [
{ name: "Palermo" },
{ name: "Catania" },
{ name: "Marsala" },
];
await new GeoAddCommand([
key,
{ longitude: 13.361389, latitude: 38.115556, member: members[0] },
{ longitude: 15.087269, latitude: 37.502669, member: members[1] },
{ longitude: 12.4372, latitude: 37.7981, member: members[2] },
]).exec(client);
const response = await new GeoPosCommand([key, [...members, "FooBar"]]).exec(
client,
);
assertEquals(response.length, 3);
});
12 changes: 7 additions & 5 deletions pkg/commands/geo_pos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ type Coordinates = {
/**
* @see https://redis.io/commands/geopos
*/
export class GeoPosCommand<TData extends Coordinates[]> extends Command<
export class GeoPosCommand<TMember = string> extends Command<
(string | null)[][],
TData
Coordinates[]
> {
constructor(
cmd: [string, ...string[]] | [string, string[]],
opts?: CommandOptions<(string | null)[][], TData>
cmd: [string, ...TMember[] | TMember[]],
opts?: CommandOptions<(string | null)[][], Coordinates[]>,
) {
const [key] = cmd;
// Check if the second argument is an array of strings (members).
Expand All @@ -29,7 +29,9 @@ export class GeoPosCommand<TData extends Coordinates[]> extends Command<
}
}

function transform<TData extends Coordinates[]>(result: (string | null)[][]): TData {
function transform<TData extends Coordinates[]>(
result: (string | null)[][],
): TData {
const final: Coordinates[] = [];
for (const pos of result) {
if (!pos?.[0] || !pos?.[1]) continue;
Expand Down

0 comments on commit 3895c2d

Please sign in to comment.