diff --git a/nr-common/mod.ts b/nr-common/mod.ts index f9bc066..8118f09 100644 --- a/nr-common/mod.ts +++ b/nr-common/mod.ts @@ -1,5 +1,6 @@ import { OPEN_LOCATION_CODE_TAG_NAME } from "./constants.ts"; import { z } from "./deps.ts"; +import { isPlusCode } from "./utils.ts"; // import { version as PACKAGE_VERSION } from "./deno.json" with { type: "json" }; export const CONTENT_MINIMUM_LENGTH = 3; @@ -7,16 +8,6 @@ export const CONTENT_MAXIMUM_LENGTH = 300; const PACKAGE_VERSION = "0.0.1"; -function isHex(s: string) { - return s.split("").every((c) => "0123456789abcdef".split("").includes(c)); -} - -function isPlusCode(code: string) { - const re = - /(^|\s)([23456789C][23456789CFGHJMPQRV][23456789CFGHJMPQRVWX]{6}\+[23456789CFGHJMPQRVWX]{2,7})(\s|$)/i; - return re.test(code); -} - export const eventSchema = z .object({ id: z.string().length(64), diff --git a/nr-common/utils.ts b/nr-common/utils.ts index 0f1e12e..ff635ac 100644 --- a/nr-common/utils.ts +++ b/nr-common/utils.ts @@ -24,6 +24,12 @@ export function isHexKey(key: string): boolean { return true; } +export function isPlusCode(code: string) { + const re = + /(^|\s)([23456789C][23456789CFGHJMPQRV][23456789CFGHJMPQRVWX]{6}\+[23456789CFGHJMPQRVWX]{2,7})(\s|$)/i; + return re.test(code); +} + export function getCurrentTimestamp() { return Math.round(Date.now() / 1e3); } @@ -84,8 +90,11 @@ export function getAllPlusCodePrefixes( plusCode: string, minimumLength: number ): string[] { + if (minimumLength % 2 !== 0) { + throw new Error("#HqXbxX-invalid-minimum-length"); + } const numberOfCodes = (8 - minimumLength) / 2 + 1; - const plusCodes = Array.from({ length: numberOfCodes }).map((_value, index) => + const plusCodes = Array.from({ length: numberOfCodes }, (_value, index) => getPlusCodePrefix(plusCode, minimumLength + index * 2) ); return plusCodes;