snapshot of https://github.com/mountain-loop/yaak.git (main, 2026-09-23; blobs > 131072B omitted)
Some checks failed
Lint and Test / Lint/Test (push) Has been cancelled
Generate Sponsors README / deploy (push) Has been skipped

This commit is contained in:
upstream-mirror
2026-09-23 17:39:41 +05:00
commit 906fb231cc
1544 changed files with 164131 additions and 0 deletions

View 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"
}
}

View 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();
},
},
],
};

View 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"));
});
});

View File

@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.json"
}