Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 48 additions & 46 deletions pkg/commands/geo_pos.test.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
import { expect, test } from "bun:test";
import { describe, expect, test } from "bun:test";
import { newHttpClient } from "../test-utils";
import { GeoAddCommand } from "./geo_add";
import { GeoPosCommand } from "./geo_pos";

const client = newHttpClient();

test("should swallow non-existing member and return only the valid ones", async () => {
const key = "Sicily";
const members = ["Palermo", "Catania", "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);
expect(response.length).toEqual(3);
});
describe("GEOPOS tests", () => {
test("should swallow non-existing member and return only the valid ones", async () => {
const key = "Sicily";
const members = ["Palermo", "Catania", "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);
expect(response.length).toEqual(3);
});

test("should return three valid positions", async () => {
const key = "Sicily";
const members = ["Palermo", "Catania", "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);
test("should return three valid positions", async () => {
const key = "Sicily";
const members = ["Palermo", "Catania", "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]).exec(client);
const response = await new GeoPosCommand([key, members]).exec(client);

expect(response.every(Boolean)).toEqual(true);
});
expect(response.every(Boolean)).toEqual(true);
});

test("should return empty array due to null value FooBar", async () => {
const key = "Sicily";
const members = ["Palermo"];
await new GeoAddCommand([
key,
{ longitude: 13.361389, latitude: 38.115556, member: members[0] },
]).exec(client);
const response = await new GeoPosCommand([key, "FooBar"]).exec(client);
expect(response).toEqual([]);
});
test("should return empty array due to null value FooBar", async () => {
const key = "Sicily";
const members = ["Palermo"];
await new GeoAddCommand([
key,
{ longitude: 13.361389, latitude: 38.115556, member: members[0] },
]).exec(client);
const response = await new GeoPosCommand([key, "FooBar"]).exec(client);
expect(response).toEqual([]);
});

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);
expect(response.length).toEqual(3);
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);
expect(response.length).toEqual(3);
});
});
14 changes: 7 additions & 7 deletions pkg/commands/geo_pos.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Command, CommandOptions } from "./command";
import { Command, CommandOptions } from "./command.ts";

type Coordinates = {
lng: string;
lat: string;
lng: number;
lat: number;
};

/**
Expand All @@ -11,7 +11,7 @@ type Coordinates = {
export class GeoPosCommand<TMember = string> extends Command<(string | null)[][], Coordinates[]> {
constructor(
cmd: [string, ...(TMember[] | TMember[])],
opts?: CommandOptions<(string | null)[][], Coordinates[]>,
opts?: CommandOptions<(string | null)[][], Coordinates[]>
) {
const [key] = cmd;
// Check if the second argument is an array of strings (members).
Expand All @@ -26,13 +26,13 @@ export class GeoPosCommand<TMember = string> extends Command<(string | null)[][]
}
}

function transform<TData extends Coordinates[]>(result: (string | null)[][]): TData {
function transform(result: (string | null)[][]): Coordinates[] {
const final: Coordinates[] = [];
for (const pos of result) {
if (!pos?.[0] || !pos?.[1]) {
continue;
}
final.push({ lng: pos[0], lat: pos[1] });
final.push({ lng: parseFloat(pos[0]), lat: parseFloat(pos[1]) });
}
return final as TData;
return final;
}
1 change: 1 addition & 0 deletions pkg/commands/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export * from "./flushall";
export * from "./flushdb";
export * from "./geo_add";
export * from "./geo_dist";
export * from "./geo_pos";
export * from "./get";
export * from "./getbit";
export * from "./getdel";
Expand Down
7 changes: 7 additions & 0 deletions pkg/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
FlushDBCommand,
GeoAddCommand,
GeoDistCommand,
GeoPosCommand,
GetBitCommand,
GetCommand,
GetDelCommand,
Expand Down Expand Up @@ -1117,6 +1118,12 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
geodist: (...args: CommandArgs<typeof GeoDistCommand>) =>
new GeoDistCommand(args, this.commandOptions).exec(this.client),

/**
* @see https://redis.io/commands/geopos
*/
geopos: (...args: CommandArgs<typeof GeoPosCommand>) =>
new GeoPosCommand(args, this.commandOptions).exec(this.client),

/**
* @see https://redis.io/commands/json.get
*/
Expand Down
7 changes: 7 additions & 0 deletions pkg/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
FlushDBCommand,
GeoAddCommand,
GeoDistCommand,
GeoPosCommand,
GetBitCommand,
GetCommand,
GetDelCommand,
Expand Down Expand Up @@ -249,6 +250,12 @@ export class Redis {
geoadd: (...args: CommandArgs<typeof GeoAddCommand>) =>
new GeoAddCommand(args, this.opts).exec(this.client),

/**
* @see https://redis.io/commands/geopos
*/
geopos: (...args: CommandArgs<typeof GeoPosCommand>) =>
new GeoPosCommand(args, this.opts).exec(this.client),

/**
* @see https://redis.io/commands/geodist
*/
Expand Down