Skip to content

Commit

Permalink
Move util to utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
chmac committed Nov 25, 2024
1 parent 1189367 commit 0ac3324
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
11 changes: 1 addition & 10 deletions nr-common/mod.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
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;
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),
Expand Down
11 changes: 10 additions & 1 deletion nr-common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0ac3324

Please sign in to comment.