Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 120x 25x 25x 1x 24x 24x 39x 26x 13x 2x 12x 8x 4x 26x 2x 24x 24x 8x 8x 8x 8x 16x 1x 15x 1x 14x 5x 9x 1x 8x 8x 1x 1x 1x 7x 2x 2x 2x 5x 5x 8x 8x 5x | const SLOT_REF_PATTERN = /^(\$|&)(\d+)(?:\.(.+))?$/u;
/**
* Convert a smallcaps-encoded CapData value into a plain JS value with
* human-readable representations. This is a display-only transformation,
* not a marshal decode.
*
* @param capData - The CapData to prettify.
* @param capData.body - The smallcaps-encoded body string (prefixed with `#`).
* @param capData.slots - The slot KRefs.
* @returns The prettified value.
*/
export function prettifySmallcaps(capData: {
body: string;
slots: string[];
}): unknown {
const { body, slots } = capData;
if (!body.startsWith('#')) {
throw new Error(
`Expected body to start with '#', got: ${body.slice(0, 20)}`,
);
}
const parsed: unknown = JSON.parse(body.slice(1));
return walkValue(parsed, slots);
}
/**
* Recursively walk a parsed smallcaps value, replacing encoded
* representations with human-readable equivalents.
*
* @param value - The value to walk.
* @param slots - The slot KRefs.
* @returns The transformed value.
*/
function walkValue(value: unknown, slots: string[]): unknown {
if (typeof value === 'string') {
return walkString(value, slots);
}
if (Array.isArray(value)) {
return value.map((item) => walkValue(item, slots));
}
if (typeof value === 'object' && value !== null) {
return walkObject(value as Record<string, unknown>, slots);
}
return value;
}
/**
* Decode a smallcaps-encoded string into a human-readable value.
*
* @param value - The encoded string.
* @param slots - The slot KRefs.
* @returns The decoded value.
*/
function walkString(value: string, slots: string[]): unknown {
// Escaped string: strip the `!` prefix.
if (value.startsWith('!')) {
return value.slice(1);
}
// Slot refs: remotable ($N, $N.iface) and promise (&N).
const match = SLOT_REF_PATTERN.exec(value);
if (match) {
const index = Number(match[2]);
const kref = slots[index] ?? `?${index}`;
const iface = match[3];
return iface ? `<${kref}> (${iface})` : `<${kref}>`;
}
// Non-negative bigint (+N).
if (value.startsWith('+')) {
return `${value.slice(1)}n`;
}
// Negative bigint (-N).
if (value.startsWith('-')) {
return `${value}n`;
}
// Manifest constant (#undefined, #NaN, #Infinity, #-Infinity, #-0).
if (value.startsWith('#')) {
return `[${value.slice(1)}]`;
}
// Symbol (%name).
if (value.startsWith('%')) {
return `[Symbol: ${value.slice(1)}]`;
}
return value;
}
/**
* Decode a smallcaps-encoded object, handling tagged values, errors, and
* key unescaping.
*
* @param obj - The encoded object.
* @param slots - The slot KRefs.
* @returns The decoded value.
*/
function walkObject(obj: Record<string, unknown>, slots: string[]): unknown {
// Tagged value: { "#tag": t, "payload": p }
if ('#tag' in obj) {
const tag = walkValue(obj['#tag'], slots);
const payload = walkValue(obj.payload, slots);
return { [`[Tagged: ${String(tag)}]`]: payload };
}
// Error: { "#error": msg, "name": "TypeError" }
if ('#error' in obj) {
const message = walkValue(obj['#error'], slots);
const name = obj.name === undefined ? 'Error' : walkValue(obj.name, slots);
return `[${String(name)}: ${String(message)}]`;
}
// Regular record — unescape keys with `!` prefix.
const result: Record<string, unknown> = {};
for (const [key, val] of Object.entries(obj)) {
const unescapedKey = key.startsWith('!') ? key.slice(1) : key;
result[unescapedKey] = walkValue(val, slots);
}
return result;
}
|