From e0dfb55c5457aec489fc084c4567e791b4c65eb1 Mon Sep 17 00:00:00 2001 From: dujinkim Date: Wed, 26 Mar 2025 00:37:41 +0000 Subject: 3/25 까지의 대표님 작업사항 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/id.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lib/id.ts (limited to 'lib/id.ts') diff --git a/lib/id.ts b/lib/id.ts new file mode 100644 index 00000000..e6e44cbd --- /dev/null +++ b/lib/id.ts @@ -0,0 +1,43 @@ +import { customAlphabet } from "nanoid" + +const prefixes = { + task: "tsk", +} + +interface GenerateIdOptions { + /** + * The length of the generated ID. + * @default 12 + * @example 12 => "abc123def456" + * */ + length?: number + /** + * The separator to use between the prefix and the generated ID. + * @default "_" + * @example "_" => "str_abc123" + * */ + separator?: string +} + +/** + * Generates a unique ID with optional prefix and configuration. + * @param prefixOrOptions The prefix string or options object + * @param options The options for generating the ID + */ +export function generateId( + prefixOrOptions?: keyof typeof prefixes | GenerateIdOptions, + options: GenerateIdOptions = {} +) { + if (typeof prefixOrOptions === "object") { + options = prefixOrOptions + prefixOrOptions = undefined + } + + const { length = 12, separator = "_" } = options + const id = customAlphabet( + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", + length + )() + + return prefixOrOptions ? `${prefixes[prefixOrOptions]}${separator}${id}` : id +} -- cgit v1.2.3