snapshot of https://github.com/mountain-loop/yaak.git (main, 2026-09-23; blobs > 131072B omitted)
This commit is contained in:
14
plugins/template-function-uuid/package.json
Normal file
14
plugins/template-function-uuid/package.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "@yaak/template-function-uuid",
|
||||
"displayName": "UUID Template Functions",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"description": "Template functions for generating UUIDs",
|
||||
"scripts": {
|
||||
"build": "yaakcli build",
|
||||
"dev": "yaakcli dev"
|
||||
},
|
||||
"dependencies": {
|
||||
"uuid": "^14.0.0"
|
||||
}
|
||||
}
|
||||
81
plugins/template-function-uuid/src/index.ts
Normal file
81
plugins/template-function-uuid/src/index.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import type { CallTemplateFunctionArgs, Context, PluginDefinition } from "@yaakapp/api";
|
||||
import { v1, v3, v4, v5, v6, v7 } from "uuid";
|
||||
|
||||
export const plugin: PluginDefinition = {
|
||||
templateFunctions: [
|
||||
{
|
||||
name: "uuid.v1",
|
||||
description: "Generate a UUID V1",
|
||||
args: [],
|
||||
async onRender(): Promise<string | null> {
|
||||
return v1();
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "uuid.v3",
|
||||
description: "Generate a UUID V3",
|
||||
args: [
|
||||
{ type: "text", name: "name", label: "Name" },
|
||||
{
|
||||
type: "text",
|
||||
name: "namespace",
|
||||
label: "Namespace UUID",
|
||||
description: "A valid UUID to use as the namespace",
|
||||
placeholder: "24ced880-3bf4-11f0-8329-cd053d577f0e",
|
||||
},
|
||||
],
|
||||
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
return v3(String(args.values.name), String(args.values.namespace));
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "uuid.v4",
|
||||
description: "Generate a UUID V4",
|
||||
args: [],
|
||||
async onRender(): Promise<string | null> {
|
||||
return v4();
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "uuid.v5",
|
||||
description: "Generate a UUID V5",
|
||||
args: [
|
||||
{ type: "text", name: "name", label: "Name" },
|
||||
{ type: "text", name: "namespace", label: "Namespace" },
|
||||
],
|
||||
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
return v5(String(args.values.name), String(args.values.namespace));
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "uuid.v6",
|
||||
description: "Generate a UUID V6",
|
||||
args: [
|
||||
{
|
||||
type: "text",
|
||||
name: "timestamp",
|
||||
label: "Timestamp",
|
||||
optional: true,
|
||||
description: "Can be any format that can be parsed by JavaScript new Date(...)",
|
||||
placeholder: "2025-05-28T11:15:00Z",
|
||||
},
|
||||
],
|
||||
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
// `timestamp` is optional, so an absent or unparseable value has to fall
|
||||
// back to v6()'s own clock. Handing it NaN msecs is not a fallback: the
|
||||
// uuid package masks NaN down to a zero timestamp (1582-10-15).
|
||||
const timestamp = String(args.values.timestamp ?? "").trim();
|
||||
const msecs = timestamp ? new Date(timestamp).getTime() : Number.NaN;
|
||||
return Number.isNaN(msecs) ? v6() : v6({ msecs });
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "uuid.v7",
|
||||
description: "Generate a UUID V7",
|
||||
args: [],
|
||||
async onRender(): Promise<string | null> {
|
||||
return v7();
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
37
plugins/template-function-uuid/tests/uuid.test.ts
Normal file
37
plugins/template-function-uuid/tests/uuid.test.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import type { Context } from "@yaakapp/api";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
import { plugin } from "../src";
|
||||
|
||||
// A v6 UUID stores 60 bits of 100-nanosecond intervals since 1582-10-15 in its
|
||||
// first three groups, with the version nibble removed.
|
||||
function v6Millis(uuid: string): number {
|
||||
const hex = uuid.replace(/-/g, "");
|
||||
const intervals = BigInt(`0x${hex.slice(0, 12)}${hex.slice(13, 16)}`);
|
||||
return Number(intervals / 10000n) - 12219292800000;
|
||||
}
|
||||
|
||||
describe("uuid.v6", () => {
|
||||
const v6 = plugin.templateFunctions?.find((f) => f.name === "uuid.v6");
|
||||
|
||||
it("uses the current time when no usable timestamp is given", async () => {
|
||||
const inputs = [{}, { timestamp: "" }, { timestamp: null }, { timestamp: "not-a-date" }];
|
||||
for (const values of inputs) {
|
||||
const before = Date.now();
|
||||
const result = await v6?.onRender({} as Context, { values, purpose: "send" });
|
||||
const after = Date.now();
|
||||
|
||||
expect(result).not.toMatch(/^00000000-0000-6/);
|
||||
const millis = v6Millis(result as string);
|
||||
expect(millis).toBeGreaterThanOrEqual(before);
|
||||
expect(millis).toBeLessThanOrEqual(after);
|
||||
}
|
||||
});
|
||||
|
||||
it("embeds a timestamp that is given", async () => {
|
||||
const result = await v6?.onRender({} as Context, {
|
||||
values: { timestamp: "2025-05-28T11:15:00Z" },
|
||||
purpose: "send",
|
||||
});
|
||||
expect(v6Millis(result as string)).toBe(Date.parse("2025-05-28T11:15:00Z"));
|
||||
});
|
||||
});
|
||||
3
plugins/template-function-uuid/tsconfig.json
Normal file
3
plugins/template-function-uuid/tsconfig.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json"
|
||||
}
|
||||
Reference in New Issue
Block a user