Skip to content

Commit

Permalink
Merge branch 'main' of github.com:upstash/upstash-redis into DX-414
Browse files Browse the repository at this point in the history
  • Loading branch information
ogzhanolguncu committed Oct 19, 2023
2 parents 3895c2d + 1298187 commit 28fc701
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 162 deletions.
83 changes: 83 additions & 0 deletions pkg/commands/geo_dist.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { newHttpClient } from "../test-utils.ts";

import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

import { GeoAddCommand } from "./geo_add.ts";
import { GeoDistCommand } from "./geo_dist.ts";

const client = newHttpClient();

Deno.test("should return distance successfully in meters", async () => {
await new GeoAddCommand([
"Sicily",
{ longitude: 13.361389, latitude: 38.115556, member: "Palermo" },
{ longitude: 15.087269, latitude: 37.502669, member: "Catania" },
]).exec(client);

const res = await new GeoDistCommand(["Sicily", "Palermo", "Catania"]).exec(
client,
);

assertEquals(res, 166274.1516);
});

Deno.test("should return distance for object members", async () => {
await new GeoAddCommand([
"Sicily",
{ longitude: 13.361389, latitude: 38.115556, member: { name: "Palermo" } },
{ longitude: 15.087269, latitude: 37.502669, member: { name: "Catania" } },
]).exec(client);

const res = await new GeoDistCommand(["Sicily", { name: "Palermo" }, {
name: "Catania",
}]).exec(
client,
);

assertEquals(res, 166274.1516);
});

Deno.test("should return distance successfully in kilometers", async () => {
await new GeoAddCommand([
"Sicily",
{ longitude: 13.361389, latitude: 38.115556, member: "Palermo" },
{ longitude: 15.087269, latitude: 37.502669, member: "Catania" },
]).exec(client);

const res = await new GeoDistCommand(["Sicily", "Palermo", "Catania", "KM"])
.exec(client);

assertEquals(res, 166.2742);
});

Deno.test("should return distance successfully in miles", async () => {
await new GeoAddCommand([
"Sicily",
{ longitude: 13.361389, latitude: 38.115556, member: "Palermo" },
{ longitude: 15.087269, latitude: 37.502669, member: "Catania" },
]).exec(client);

const res = await new GeoDistCommand(["Sicily", "Palermo", "Catania", "MI"])
.exec(client);

assertEquals(res, 103.3182);
});

Deno.test("should return distance successfully in feet", async () => {
await new GeoAddCommand([
"Sicily",
{ longitude: 13.361389, latitude: 38.115556, member: "Palermo" },
{ longitude: 15.087269, latitude: 37.502669, member: "Catania" },
]).exec(client);

const res = await new GeoDistCommand(["Sicily", "Palermo", "Catania", "FT"])
.exec(client);

assertEquals(res?.toString(), "545518.8700");
});

Deno.test("should return null if members doesn't exist", async () => {
const res = await new GeoDistCommand(["Sicily", "FOO", "BAR"]).exec(client);

assertEquals(res, null);
});
19 changes: 19 additions & 0 deletions pkg/commands/geo_dist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Command, CommandOptions } from "./command.ts";

/**
* @see https://redis.io/commands/geodist
*/
export class GeoDistCommand<TMemberType = string>
extends Command<number | null, number | null> {
constructor(
[key, member1, member2, unit = "M"]: [
key: string,
member1: TMemberType,
member2: TMemberType,
unit?: "M" | "KM" | "FT" | "MI",
],
opts?: CommandOptions<number | null, number | null>,
) {
super(["GEODIST", key, member1, member2, unit], opts);
}
}
13 changes: 7 additions & 6 deletions pkg/commands/mget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { Command, CommandOptions } from "./command.ts";
/**
* @see https://redis.io/commands/mget
*/
export class MGetCommand<TData extends unknown[]> extends Command<
(string | null)[],
TData
> {
export class MGetCommand<TData extends unknown[]>
extends Command<(string | null)[], TData> {
constructor(
cmd: [...keys: string[]],
cmd: [string[]] | [...string[] | string[]],
opts?: CommandOptions<(string | null)[], TData>,
) {
super(["mget", ...cmd], opts);
const keys = Array.isArray(cmd[0])
? (cmd[0] as string[])
: (cmd as string[]);
super(["mget", ...keys], opts);
}
}
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.ts";
export * from "./flushdb.ts";
export * from "./geo_add.ts";
export * from "./geo_pos.ts";
export * from "./geo_dist.ts";
export * from "./get.ts";
export * from "./getbit.ts";
export * from "./getdel.ts";
Expand Down
Loading

0 comments on commit 28fc701

Please sign in to comment.