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
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);
}
}
1 change: 1 addition & 0 deletions pkg/commands/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from "./expireat.ts";
export * from "./flushall.ts";
export * from "./flushdb.ts";
export * from "./geo_add.ts";
export * from "./geo_dist.ts";
export * from "./get.ts";
export * from "./getbit.ts";
export * from "./getdel.ts";
Expand Down
14 changes: 14 additions & 0 deletions pkg/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
ExpireCommand,
FlushAllCommand,
FlushDBCommand,
GeoAddCommand,
GeoDistCommand,
GetBitCommand,
GetCommand,
GetDelCommand,
Expand Down Expand Up @@ -1152,6 +1154,18 @@ export class Pipeline<TCommands extends Command<any, any>[] = []> {
forget: (...args: CommandArgs<typeof JsonForgetCommand>) =>
this.chain(new JsonForgetCommand(args, this.commandOptions)),

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

/**
* @see https://redis.io/commands/geodist
*/
geodist: (...args: CommandArgs<typeof GeoDistCommand>) =>
new GeoDistCommand(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 @@ -17,6 +17,7 @@ import {
FlushAllCommand,
FlushDBCommand,
GeoAddCommand,
GeoDistCommand,
GetBitCommand,
GetCommand,
GetDelCommand,
Expand Down Expand Up @@ -248,6 +249,12 @@ export class Redis {
geoadd: (...args: CommandArgs<typeof GeoAddCommand>) =>
new GeoAddCommand(args, this.opts).exec(this.client),

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

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