metano-runtime 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +195 -0
- package/dist/system/delegates.d.ts +40 -0
- package/dist/system/delegates.js +74 -0
- package/dist/system/index.d.ts +2 -0
- package/dist/system/index.js +2 -0
- package/dist/system/json/serializer-context.js +6 -1
- package/dist/system/json/types.d.ts +23 -4
- package/dist/system/uuid.d.ts +44 -0
- package/dist/system/uuid.js +49 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# metano-runtime
|
|
2
|
+
|
|
3
|
+
Runtime support library for code generated by the [Metano](https://github.com/danfma/metano)
|
|
4
|
+
C# → TypeScript transpiler. You generally don't install or use this directly —
|
|
5
|
+
Metano adds it to your generated `package.json#dependencies` automatically, and
|
|
6
|
+
the generated code imports from it as needed.
|
|
7
|
+
|
|
8
|
+
## What's in here
|
|
9
|
+
|
|
10
|
+
### `HashCode` — C#-compatible hash combining
|
|
11
|
+
|
|
12
|
+
Used by generated `hashCode()` methods on records/classes. Implements
|
|
13
|
+
[xxHash32](https://github.com/Cyan4973/xxHash) for fast, well-distributed hashes.
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { HashCode } from "metano-runtime";
|
|
17
|
+
|
|
18
|
+
const hc = new HashCode();
|
|
19
|
+
hc.add(someString);
|
|
20
|
+
hc.add(someNumber);
|
|
21
|
+
hc.add(someObject);
|
|
22
|
+
const hash = hc.toHashCode(); // number
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### `HashSet<T>` — value-equality set
|
|
26
|
+
|
|
27
|
+
A `Set`-like collection that uses `equals()`/`hashCode()` methods on objects when
|
|
28
|
+
available, instead of reference equality. Mirrors C# `HashSet<T>` semantics.
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { HashSet } from "metano-runtime";
|
|
32
|
+
|
|
33
|
+
class Point {
|
|
34
|
+
constructor(readonly x: number, readonly y: number) {}
|
|
35
|
+
equals(other: Point) { return this.x === other.x && this.y === other.y; }
|
|
36
|
+
hashCode() { const hc = new HashCode(); hc.add(this.x); hc.add(this.y); return hc.toHashCode(); }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const set = new HashSet<Point>();
|
|
40
|
+
set.add(new Point(1, 2));
|
|
41
|
+
set.has(new Point(1, 2)); // true — structural equality
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### `Enumerable` — LINQ for TypeScript
|
|
45
|
+
|
|
46
|
+
Lazy, composable sequences with the full LINQ API. Used by the transpiler to lower
|
|
47
|
+
C# `IEnumerable<T>` chains.
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { Enumerable } from "metano-runtime";
|
|
51
|
+
|
|
52
|
+
const result = Enumerable.from([1, 2, 3, 4, 5])
|
|
53
|
+
.where((x) => x > 1)
|
|
54
|
+
.select((x) => x * 2)
|
|
55
|
+
.toArray(); // [4, 6, 8, 10]
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**Operators supported:**
|
|
59
|
+
|
|
60
|
+
- **Composition**: `where`, `select`, `selectMany`, `orderBy`, `orderByDescending`,
|
|
61
|
+
`thenBy`, `thenByDescending`, `take`, `takeWhile`, `skip`, `skipWhile`, `distinct`,
|
|
62
|
+
`distinctBy`, `groupBy`, `concat`, `reverse`, `zip`, `append`, `prepend`, `union`,
|
|
63
|
+
`intersect`, `except`
|
|
64
|
+
- **Terminals**: `toArray`, `toMap`, `toSet`, `first`, `firstOrDefault`, `last`,
|
|
65
|
+
`lastOrDefault`, `single`, `singleOrDefault`, `any`, `all`, `count`, `sum`, `average`,
|
|
66
|
+
`min`, `max`, `minBy`, `maxBy`, `contains`, `aggregate`
|
|
67
|
+
|
|
68
|
+
### `UUID` — branded UUID type
|
|
69
|
+
|
|
70
|
+
A branded primitive that `System.Guid` maps to. At runtime it's literally a
|
|
71
|
+
`string`, so serialization and interop with ordinary string APIs work without
|
|
72
|
+
ceremony, but the type system distinguishes "a validated UUID" from "any old
|
|
73
|
+
string".
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { UUID } from "metano-runtime";
|
|
77
|
+
|
|
78
|
+
const id = UUID.newUuid(); // "550e8400-e29b-41d4-a716-446655440000"
|
|
79
|
+
const compact = UUID.newCompact(); // "550e8400e29b41d4a716446655440000"
|
|
80
|
+
const empty = UUID.empty; // "00000000-0000-0000-0000-000000000000"
|
|
81
|
+
const wrapped = UUID.create("abc-123"); // unchecked wrap, caller is trusted
|
|
82
|
+
|
|
83
|
+
if (UUID.isUuid(someValue)) {
|
|
84
|
+
// type narrowed to UUID here
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The Metano transpiler uses `UUID.create()` in JSON deserialization and
|
|
89
|
+
`UUID.newUuid()` as the lowering for `Guid.NewGuid()`.
|
|
90
|
+
|
|
91
|
+
### Primitive type guards
|
|
92
|
+
|
|
93
|
+
Runtime type predicates used by generated overload dispatchers:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import {
|
|
97
|
+
isInt32,
|
|
98
|
+
isString,
|
|
99
|
+
isBoolean,
|
|
100
|
+
isFloat64,
|
|
101
|
+
isBigInt,
|
|
102
|
+
// etc.
|
|
103
|
+
} from "metano-runtime";
|
|
104
|
+
|
|
105
|
+
if (isInt32(value)) {
|
|
106
|
+
// value is number && Number.isInteger(value) && within int32 range
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### JSON serialization (`metano-runtime/system/json`)
|
|
111
|
+
|
|
112
|
+
A declarative serialization framework that the transpiler uses to convert
|
|
113
|
+
`JsonSerializerContext` subclasses. You don't construct these by hand — Metano
|
|
114
|
+
generates them from your C# code.
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
import { JsonSerializer } from "metano-runtime/system/json";
|
|
118
|
+
import { JsonContext } from "#/json-context"; // generated
|
|
119
|
+
|
|
120
|
+
const todo = new TodoItem("Write docs", false, "high");
|
|
121
|
+
const json = JsonSerializer.serialize(todo, JsonContext.default.todoItem);
|
|
122
|
+
// → { title: "Write docs", completed: false, priority: "high" }
|
|
123
|
+
|
|
124
|
+
const parsed = JsonSerializer.deserialize(json, JsonContext.default.todoItem);
|
|
125
|
+
// → TodoItem { title: "Write docs", completed: false, priority: "high" }
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
**Built-in type descriptors:**
|
|
129
|
+
|
|
130
|
+
- `primitive` — string, number, boolean passthrough
|
|
131
|
+
- `temporal` — `Temporal.*` types via `toString()` / `parse()`
|
|
132
|
+
- `decimal` — `decimal.js` via `toNumber()` / `new Decimal()`
|
|
133
|
+
- `map` — `Map<K,V>` ↔ plain object
|
|
134
|
+
- `array` — `T[]` with recursive element handling
|
|
135
|
+
- `hashSet` — `HashSet<T>` ↔ array
|
|
136
|
+
- `branded` — `[InlineWrapper]` types (passthrough + `.create()`)
|
|
137
|
+
- `enum` — string enum validation
|
|
138
|
+
- `numericEnum` — numeric enum validation
|
|
139
|
+
- `nullable` — wraps inner descriptor with null handling
|
|
140
|
+
- `ref` — lazy reference to another `TypeSpec`
|
|
141
|
+
|
|
142
|
+
### `ImmutableCollection` helpers
|
|
143
|
+
|
|
144
|
+
Pure functions that emulate `System.Collections.Immutable.ImmutableList<T>` /
|
|
145
|
+
`ImmutableArray<T>` operations on plain arrays, so immutable collections serialize
|
|
146
|
+
naturally without a custom wrapper:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import { ImmutableCollection } from "metano-runtime";
|
|
150
|
+
|
|
151
|
+
const a = [1, 2, 3];
|
|
152
|
+
const b = ImmutableCollection.add(a, 4); // [1, 2, 3, 4] — new array
|
|
153
|
+
const c = ImmutableCollection.removeAt(b, 0); // [2, 3, 4]
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Installation
|
|
157
|
+
|
|
158
|
+
You normally **do not install this manually**. Metano adds it to your generated
|
|
159
|
+
`package.json#dependencies` automatically. If you need to install it for some
|
|
160
|
+
reason:
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
# Bun (recommended for Metano projects)
|
|
164
|
+
bun add metano-runtime
|
|
165
|
+
|
|
166
|
+
# npm
|
|
167
|
+
npm install metano-runtime
|
|
168
|
+
|
|
169
|
+
# pnpm
|
|
170
|
+
pnpm add metano-runtime
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Peer dependencies
|
|
174
|
+
|
|
175
|
+
- `@js-temporal/polyfill` — required if your transpiled code uses `DateTime`,
|
|
176
|
+
`DateOnly`, `TimeOnly`, etc. Metano adds this to your `dependencies` automatically
|
|
177
|
+
whenever it detects a Temporal mapping.
|
|
178
|
+
|
|
179
|
+
## Development
|
|
180
|
+
|
|
181
|
+
This package lives in the [Metano monorepo](https://github.com/danfma/metano/tree/main/js/metano-runtime).
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
cd js/metano-runtime
|
|
185
|
+
bun install
|
|
186
|
+
bun run build # tsgo -b .
|
|
187
|
+
bun test # 245+ tests
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Tests live under `test/` mirroring the `src/` directory structure. Imports use
|
|
191
|
+
`#/` subpath imports pointing at `src/` (configured in `package.json#imports`).
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
MIT
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multicast delegate support for C# event/delegate lowering.
|
|
3
|
+
*
|
|
4
|
+
* A delegate is a plain function tagged with a hidden listener list. When `+=`
|
|
5
|
+
* is used on a function, it's promoted to a multicast delegate via
|
|
6
|
+
* {@link delegateAdd}. The promoted delegate IS still a function with the
|
|
7
|
+
* same signature — callers can't tell the difference.
|
|
8
|
+
*
|
|
9
|
+
* Uses an array (not Set) because C# multicast delegates allow duplicate
|
|
10
|
+
* subscriptions — adding the same handler twice invokes it twice, and
|
|
11
|
+
* `-=` removes only the last matching occurrence.
|
|
12
|
+
*
|
|
13
|
+
* Zero overhead when `+=` is never used: the function stays a plain function.
|
|
14
|
+
*/
|
|
15
|
+
declare const DELEGATE_LISTENERS: unique symbol;
|
|
16
|
+
type AnyFunc = (...args: any[]) => any;
|
|
17
|
+
type DelegateFunc<T extends AnyFunc> = T & {
|
|
18
|
+
[DELEGATE_LISTENERS]: T[];
|
|
19
|
+
};
|
|
20
|
+
/** Returns true if the function has been promoted to a multicast delegate. */
|
|
21
|
+
export declare function isDelegate<T extends AnyFunc>(fn: T | null): fn is DelegateFunc<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a multicast delegate from one or more handlers. The returned
|
|
24
|
+
* function calls every handler in insertion order and returns the last
|
|
25
|
+
* handler's return value (matching C# multicast delegate semantics).
|
|
26
|
+
*/
|
|
27
|
+
export declare function createDelegate<T extends AnyFunc>(...handlers: T[]): T;
|
|
28
|
+
/**
|
|
29
|
+
* Adds a handler to a delegate. If the target is a plain function, it's
|
|
30
|
+
* promoted to a multicast delegate first. If the target is null, the handler
|
|
31
|
+
* itself is returned (first subscription on a nullable event).
|
|
32
|
+
*/
|
|
33
|
+
export declare function delegateAdd<T extends AnyFunc>(target: T | null, handler: T): T;
|
|
34
|
+
/**
|
|
35
|
+
* Removes a handler from a delegate. Removes the LAST matching occurrence
|
|
36
|
+
* (matching C# `-=` semantics). Returns null when the last handler is
|
|
37
|
+
* removed, or the target unchanged if the handler wasn't found.
|
|
38
|
+
*/
|
|
39
|
+
export declare function delegateRemove<T extends AnyFunc>(target: T | null, handler: T): T | null;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multicast delegate support for C# event/delegate lowering.
|
|
3
|
+
*
|
|
4
|
+
* A delegate is a plain function tagged with a hidden listener list. When `+=`
|
|
5
|
+
* is used on a function, it's promoted to a multicast delegate via
|
|
6
|
+
* {@link delegateAdd}. The promoted delegate IS still a function with the
|
|
7
|
+
* same signature — callers can't tell the difference.
|
|
8
|
+
*
|
|
9
|
+
* Uses an array (not Set) because C# multicast delegates allow duplicate
|
|
10
|
+
* subscriptions — adding the same handler twice invokes it twice, and
|
|
11
|
+
* `-=` removes only the last matching occurrence.
|
|
12
|
+
*
|
|
13
|
+
* Zero overhead when `+=` is never used: the function stays a plain function.
|
|
14
|
+
*/
|
|
15
|
+
const DELEGATE_LISTENERS = Symbol("delegate_listeners");
|
|
16
|
+
/** Returns true if the function has been promoted to a multicast delegate. */
|
|
17
|
+
export function isDelegate(fn) {
|
|
18
|
+
return fn !== null && DELEGATE_LISTENERS in fn;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a multicast delegate from one or more handlers. The returned
|
|
22
|
+
* function calls every handler in insertion order and returns the last
|
|
23
|
+
* handler's return value (matching C# multicast delegate semantics).
|
|
24
|
+
*/
|
|
25
|
+
export function createDelegate(...handlers) {
|
|
26
|
+
const listeners = [...handlers];
|
|
27
|
+
const delegate = ((...args) => {
|
|
28
|
+
let result;
|
|
29
|
+
for (const listener of listeners) {
|
|
30
|
+
result = listener(...args);
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(delegate, DELEGATE_LISTENERS, {
|
|
35
|
+
value: listeners,
|
|
36
|
+
enumerable: false,
|
|
37
|
+
});
|
|
38
|
+
return delegate;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Adds a handler to a delegate. If the target is a plain function, it's
|
|
42
|
+
* promoted to a multicast delegate first. If the target is null, the handler
|
|
43
|
+
* itself is returned (first subscription on a nullable event).
|
|
44
|
+
*/
|
|
45
|
+
export function delegateAdd(target, handler) {
|
|
46
|
+
if (target === null)
|
|
47
|
+
return handler;
|
|
48
|
+
if (isDelegate(target)) {
|
|
49
|
+
target[DELEGATE_LISTENERS].push(handler);
|
|
50
|
+
return target;
|
|
51
|
+
}
|
|
52
|
+
return createDelegate(target, handler);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Removes a handler from a delegate. Removes the LAST matching occurrence
|
|
56
|
+
* (matching C# `-=` semantics). Returns null when the last handler is
|
|
57
|
+
* removed, or the target unchanged if the handler wasn't found.
|
|
58
|
+
*/
|
|
59
|
+
export function delegateRemove(target, handler) {
|
|
60
|
+
if (target === null)
|
|
61
|
+
return null;
|
|
62
|
+
if (isDelegate(target)) {
|
|
63
|
+
const list = target[DELEGATE_LISTENERS];
|
|
64
|
+
const idx = list.lastIndexOf(handler);
|
|
65
|
+
if (idx >= 0)
|
|
66
|
+
list.splice(idx, 1);
|
|
67
|
+
if (list.length === 0)
|
|
68
|
+
return null;
|
|
69
|
+
if (list.length === 1)
|
|
70
|
+
return list[0];
|
|
71
|
+
return target;
|
|
72
|
+
}
|
|
73
|
+
return target === handler ? null : target;
|
|
74
|
+
}
|
package/dist/system/index.d.ts
CHANGED
package/dist/system/index.js
CHANGED
|
@@ -25,7 +25,12 @@ export class SerializerContext {
|
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
27
27
|
createSpec(spec) {
|
|
28
|
-
|
|
28
|
+
// Only register class-backed types (those with a constructor reference).
|
|
29
|
+
// [PlainObject] specs have no `type` field — they're accessed via their
|
|
30
|
+
// getter on the context, not via constructor-keyed resolve().
|
|
31
|
+
if (spec.type) {
|
|
32
|
+
this._registry.set(spec.type, spec);
|
|
33
|
+
}
|
|
29
34
|
return spec;
|
|
30
35
|
}
|
|
31
36
|
/**
|
|
@@ -32,10 +32,24 @@ export type HashSetDescriptor = {
|
|
|
32
32
|
readonly kind: "hashSet";
|
|
33
33
|
readonly element: TypeDescriptor;
|
|
34
34
|
};
|
|
35
|
-
/**
|
|
35
|
+
/**
|
|
36
|
+
* Branded type ([InlineWrapper]) — passthrough on serialize, create() on
|
|
37
|
+
* deserialize.
|
|
38
|
+
*
|
|
39
|
+
* The `create` parameter is typed `any` by design: the descriptor is a
|
|
40
|
+
* serializer-internal lookup table, and the runtime calls `create()` with
|
|
41
|
+
* whatever JSON-parsed primitive matched the wire format (a string for most
|
|
42
|
+
* branded IDs, a number for numeric wrappers, etc.). Narrowing to `unknown`
|
|
43
|
+
* would refuse to accept the generated `UserId.create: (value: string) => UserId`
|
|
44
|
+
* under TypeScript's contravariant parameter check, and the fix would be to
|
|
45
|
+
* widen every `[InlineWrapper]`'s generated `.create()` to accept `unknown`
|
|
46
|
+
* and re-validate — which pollutes the branded type's user-facing surface
|
|
47
|
+
* for no real benefit, since consumers always call `UserId.create("abc")`
|
|
48
|
+
* with a typed string, never with raw parsed JSON.
|
|
49
|
+
*/
|
|
36
50
|
export type BrandedDescriptor = {
|
|
37
51
|
readonly kind: "branded";
|
|
38
|
-
readonly create: (value:
|
|
52
|
+
readonly create: (value: any) => unknown;
|
|
39
53
|
};
|
|
40
54
|
/** String enum — passthrough on serialize, validate on deserialize */
|
|
41
55
|
export type EnumDescriptor = {
|
|
@@ -64,8 +78,13 @@ export interface PropertySpec {
|
|
|
64
78
|
readonly optional?: boolean;
|
|
65
79
|
}
|
|
66
80
|
export interface TypeSpec<T = unknown> {
|
|
67
|
-
/**
|
|
68
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Constructor reference (for instanceof checks and context lookups).
|
|
83
|
+
* Optional for [PlainObject] types that lower to TypeScript interfaces —
|
|
84
|
+
* they have no runtime constructor, so the spec is accessed via its getter
|
|
85
|
+
* on the context (e.g., `ctx.storedTodo`) rather than via `resolve(Class)`.
|
|
86
|
+
*/
|
|
87
|
+
readonly type?: abstract new (...args: any[]) => T;
|
|
69
88
|
/** Parent type's spec — properties are inherited */
|
|
70
89
|
readonly base?: TypeSpec;
|
|
71
90
|
/** Constructs T from a Record<ts-field-name, deserialized-value> */
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UUID — a branded primitive type for values that represent a RFC 4122 UUID.
|
|
3
|
+
*
|
|
4
|
+
* At runtime a `UUID` is literally a `string` (the brand is a compile-time
|
|
5
|
+
* only marker), so serialization, logging, and interop with ordinary string
|
|
6
|
+
* APIs "just work" with zero overhead. The brand exists purely so the type
|
|
7
|
+
* system distinguishes "an arbitrary string" from "a validated UUID", which
|
|
8
|
+
* matches the intent of C#'s `System.Guid`.
|
|
9
|
+
*
|
|
10
|
+
* This is the type Metano emits for `System.Guid` in transpiled C# code.
|
|
11
|
+
*/
|
|
12
|
+
export type UUID = string & {
|
|
13
|
+
readonly __brand: "UUID";
|
|
14
|
+
};
|
|
15
|
+
export declare namespace UUID {
|
|
16
|
+
/**
|
|
17
|
+
* Wraps an existing string as a `UUID` without validation. The caller is
|
|
18
|
+
* responsible for ensuring the string actually looks like a UUID — this is
|
|
19
|
+
* the escape hatch for values that come from trusted sources (the server,
|
|
20
|
+
* the database, `newUuid()` itself).
|
|
21
|
+
*/
|
|
22
|
+
function create(value: string): UUID;
|
|
23
|
+
/**
|
|
24
|
+
* Generates a fresh random UUID in the canonical RFC 4122 form
|
|
25
|
+
* (`"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"`). Mirrors `Guid.NewGuid()`.
|
|
26
|
+
*/
|
|
27
|
+
function newUuid(): UUID;
|
|
28
|
+
/**
|
|
29
|
+
* Generates a fresh random UUID in the compact form (no hyphens). Mirrors
|
|
30
|
+
* `Guid.NewGuid().ToString("N")`.
|
|
31
|
+
*/
|
|
32
|
+
function newCompact(): UUID;
|
|
33
|
+
/**
|
|
34
|
+
* The canonical all-zero UUID (`"00000000-0000-0000-0000-000000000000"`).
|
|
35
|
+
* Mirrors `Guid.Empty`.
|
|
36
|
+
*/
|
|
37
|
+
const empty: UUID;
|
|
38
|
+
/**
|
|
39
|
+
* Type guard — returns true if `value` is a string shaped like a UUID
|
|
40
|
+
* (either canonical with hyphens or compact without). The check is a
|
|
41
|
+
* regular expression match, not full RFC 4122 version/variant validation.
|
|
42
|
+
*/
|
|
43
|
+
function isUuid(value: unknown): value is UUID;
|
|
44
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// The namespace pattern mirrors generated [InlineWrapper] output so Metano
|
|
2
|
+
// can treat UUID exactly like a user-defined branded type — including in the
|
|
3
|
+
// JSON serializer's `branded` descriptor.
|
|
4
|
+
export var UUID;
|
|
5
|
+
(function (UUID) {
|
|
6
|
+
/**
|
|
7
|
+
* Wraps an existing string as a `UUID` without validation. The caller is
|
|
8
|
+
* responsible for ensuring the string actually looks like a UUID — this is
|
|
9
|
+
* the escape hatch for values that come from trusted sources (the server,
|
|
10
|
+
* the database, `newUuid()` itself).
|
|
11
|
+
*/
|
|
12
|
+
function create(value) {
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
UUID.create = create;
|
|
16
|
+
/**
|
|
17
|
+
* Generates a fresh random UUID in the canonical RFC 4122 form
|
|
18
|
+
* (`"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"`). Mirrors `Guid.NewGuid()`.
|
|
19
|
+
*/
|
|
20
|
+
function newUuid() {
|
|
21
|
+
return crypto.randomUUID();
|
|
22
|
+
}
|
|
23
|
+
UUID.newUuid = newUuid;
|
|
24
|
+
/**
|
|
25
|
+
* Generates a fresh random UUID in the compact form (no hyphens). Mirrors
|
|
26
|
+
* `Guid.NewGuid().ToString("N")`.
|
|
27
|
+
*/
|
|
28
|
+
function newCompact() {
|
|
29
|
+
return crypto.randomUUID().replace(/-/g, "");
|
|
30
|
+
}
|
|
31
|
+
UUID.newCompact = newCompact;
|
|
32
|
+
/**
|
|
33
|
+
* The canonical all-zero UUID (`"00000000-0000-0000-0000-000000000000"`).
|
|
34
|
+
* Mirrors `Guid.Empty`.
|
|
35
|
+
*/
|
|
36
|
+
UUID.empty = "00000000-0000-0000-0000-000000000000";
|
|
37
|
+
/**
|
|
38
|
+
* Type guard — returns true if `value` is a string shaped like a UUID
|
|
39
|
+
* (either canonical with hyphens or compact without). The check is a
|
|
40
|
+
* regular expression match, not full RFC 4122 version/variant validation.
|
|
41
|
+
*/
|
|
42
|
+
function isUuid(value) {
|
|
43
|
+
if (typeof value !== "string")
|
|
44
|
+
return false;
|
|
45
|
+
// 8-4-4-4-12 hex groups (canonical form) OR 32 contiguous hex chars (N form)
|
|
46
|
+
return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$|^[0-9a-fA-F]{32}$/.test(value);
|
|
47
|
+
}
|
|
48
|
+
UUID.isUuid = isUuid;
|
|
49
|
+
})(UUID || (UUID = {}));
|