From 1dafbc86df6abee82ccce36cbbe45dd730ee6705 Mon Sep 17 00:00:00 2001 From: Justin Bennett Date: Thu, 12 Sep 2024 13:50:25 -0400 Subject: [PATCH] Pull isULID into the utils file --- scripts/ob.ts | 15 +-------------- src/pages/[...slug].astro | 8 ++------ src/utils.ts | 2 ++ 3 files changed, 5 insertions(+), 20 deletions(-) diff --git a/scripts/ob.ts b/scripts/ob.ts index 1cf7ee8..b9ef1d8 100644 --- a/scripts/ob.ts +++ b/scripts/ob.ts @@ -7,20 +7,7 @@ import { basename, dirname, join, extname } from "jsr:@std/path"; import { move } from "jsr:@std/fs"; import { extractYaml } from "jsr:@std/front-matter"; import { contentType } from "jsr:@std/media-types"; - -// async function write(text: string) { -// const encoder = new TextEncoder(); -// const data = encoder.encode(text); - -// // Write the data to stdout without a newline -// await Deno.stdout.write(data); -// } - -function isULID(str: string) { - // ULID regex to validate the format - const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i; - return ulidRegex.test(str); -} +import { isULID, slugify } from "../src/utils.ts"; const rename = new Command() .description("Renames a note with a ULID") diff --git a/src/pages/[...slug].astro b/src/pages/[...slug].astro index ee61902..db84e14 100644 --- a/src/pages/[...slug].astro +++ b/src/pages/[...slug].astro @@ -1,4 +1,6 @@ --- +import { isULID } from "../utils.ts"; + export const prerender = false; /** 1 month in seconds */ @@ -10,12 +12,6 @@ const CACHE_TTL = 60 * 60 * 24 * 30; // simpler for now. const HOME_SLUG = "01J4CPMJ91NF1DNQPE3TZ50HH0"; -const ulidRegex = /^[0-9A-HJ-NP-TV-Z]{26}$/i; - -function isULID(ulid: string) { - return ulidRegex.test(ulid); -} - /** * If a `get` KV request returns null and it was requested with a cacheTTL, * the empty result will be cached. That's usually not what we want, so this diff --git a/src/utils.ts b/src/utils.ts index 8fcf2dd..2beafea 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -6,3 +6,5 @@ export const slugify = (s: string) => export const isValidSha256 = (hashString: string) => /^[a-fA-F0-9]{64}$/.test(hashString); + +export const isULID = (str: string) => /^[0-9A-HJKMNP-TV-Z]{26}$/i.test(str);