From ebdad8eed91fc3d441dd540efe2ed9d010f1f061 Mon Sep 17 00:00:00 2001 From: Leibale Date: Mon, 8 May 2023 14:18:53 +0300 Subject: [PATCH] geo --- packages/client/lib/commands/GEODIST.ts | 2 +- .../lib/commands/generic-transformers.ts | 189 ------------------ 2 files changed, 1 insertion(+), 190 deletions(-) diff --git a/packages/client/lib/commands/GEODIST.ts b/packages/client/lib/commands/GEODIST.ts index dc5bcf1f12..3e684d6757 100644 --- a/packages/client/lib/commands/GEODIST.ts +++ b/packages/client/lib/commands/GEODIST.ts @@ -1,5 +1,5 @@ import { RedisArgument, BlobStringReply, NullReply, Command } from '../RESP/types'; -import { GeoUnits } from './generic-transformers'; +import { GeoUnits } from './GEOSEARCH'; export default { FIRST_KEY_INDEX: 1, diff --git a/packages/client/lib/commands/generic-transformers.ts b/packages/client/lib/commands/generic-transformers.ts index 9a8eef476e..78bc983842 100644 --- a/packages/client/lib/commands/generic-transformers.ts +++ b/packages/client/lib/commands/generic-transformers.ts @@ -178,195 +178,6 @@ export function transformLMPopArguments( return args; } -type GeoCountArgument = number | { - value: number; - ANY?: true -}; - -export function pushGeoCountArgument( - args: CommandArguments, - count: GeoCountArgument | undefined -): CommandArguments { - if (typeof count === 'number') { - args.push('COUNT', count.toString()); - } else if (count) { - args.push('COUNT', count.value.toString()); - - if (count.ANY) { - args.push('ANY'); - } - } - - return args; -} - -export type GeoUnits = 'm' | 'km' | 'mi' | 'ft'; - -export interface GeoCoordinates { - longitude: RedisArgument | number; - latitude: RedisArgument | number; -} - -type GeoSearchFromMember = string; - -export type GeoSearchFrom = GeoSearchFromMember | GeoCoordinates; - -interface GeoSearchByRadius { - radius: number; - unit: GeoUnits; -} - -interface GeoSearchByBox { - width: number; - height: number; - unit: GeoUnits; -} - -export type GeoSearchBy = GeoSearchByRadius | GeoSearchByBox; - -export interface GeoSearchOptions { - SORT?: 'ASC' | 'DESC'; - COUNT?: GeoCountArgument; -} - -export function pushGeoSearchArguments( - args: CommandArguments, - key: RedisArgument, - from: GeoSearchFrom, - by: GeoSearchBy, - options?: GeoSearchOptions -): CommandArguments { - args.push(key); - - if (typeof from === 'string') { - args.push('FROMMEMBER', from); - } else { - args.push('FROMLONLAT', from.longitude.toString(), from.latitude.toString()); - } - - if ('radius' in by) { - args.push('BYRADIUS', by.radius.toString()); - } else { - args.push('BYBOX', by.width.toString(), by.height.toString()); - } - - args.push(by.unit); - - if (options?.SORT) { - args.push(options.SORT); - } - - pushGeoCountArgument(args, options?.COUNT); - - return args; -} - -export function pushGeoRadiusArguments( - args: CommandArguments, - key: RedisArgument, - from: GeoSearchFrom, - radius: number, - unit: GeoUnits, - options?: GeoSearchOptions -): CommandArguments { - args.push(key); - - if (typeof from === 'string') { - args.push(from); - } else { - args.push( - from.longitude.toString(), - from.latitude.toString() - ); - } - - args.push( - radius.toString(), - unit - ); - - if (options?.SORT) { - args.push(options.SORT); - } - - pushGeoCountArgument(args, options?.COUNT); - - return args; -} - -export interface GeoRadiusStoreOptions extends GeoSearchOptions { - STOREDIST?: boolean; -} - -export function pushGeoRadiusStoreArguments( - args: CommandArguments, - key: RedisArgument, - from: GeoSearchFrom, - radius: number, - unit: GeoUnits, - destination: RedisArgument, - options?: GeoRadiusStoreOptions -): CommandArguments { - pushGeoRadiusArguments(args, key, from, radius, unit, options); - - if (options?.STOREDIST) { - args.push('STOREDIST', destination); - } else { - args.push('STORE', destination); - } - - return args; -} - -export enum GeoReplyWith { - DISTANCE = 'WITHDIST', - HASH = 'WITHHASH', - COORDINATES = 'WITHCOORD' -} - -export interface GeoReplyWithMember { - member: string; - distance?: number; - hash?: string; - coordinates?: { - longitude: string; - latitude: string; - }; -} - -export function transformGeoMembersWithReply(reply: Array>, replyWith: Array): Array { - const replyWithSet = new Set(replyWith); - - let index = 0; - const distanceIndex = replyWithSet.has(GeoReplyWith.DISTANCE) && ++index, - hashIndex = replyWithSet.has(GeoReplyWith.HASH) && ++index, - coordinatesIndex = replyWithSet.has(GeoReplyWith.COORDINATES) && ++index; - - return reply.map(member => { - const transformedMember: GeoReplyWithMember = { - member: member[0] - }; - - if (distanceIndex) { - transformedMember.distance = member[distanceIndex]; - } - - if (hashIndex) { - transformedMember.hash = member[hashIndex]; - } - - if (coordinatesIndex) { - const [longitude, latitude] = member[coordinatesIndex]; - transformedMember.coordinates = { - longitude, - latitude - }; - } - - return transformedMember; - }); -} - export function transformEXAT(EXAT: number | Date): string { return (typeof EXAT === 'number' ? EXAT : Math.floor(EXAT.getTime() / 1000)).toString(); }