@fraczak/k 6.8.0 → 6.9.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.
@@ -1,89 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Test the binary codec with basic k types
4
- */
5
-
6
- import { deriveClosedPattern } from './codec.mjs';
7
- import { encodeToWire, decodeWire } from './prefix-codec.mjs';
8
- import { patternToPropertyList } from './pattern-json.mjs';
9
- import { Value, isProduct, isVariant } from '../../Value.mjs';
10
- import hash from '../../hash.mjs';
11
-
12
- // Type repository
13
- const types = {};
14
-
15
- // Define unit type: {}
16
- const unitTypeDef = { code: 'product', product: {} };
17
- const unitTypeStr = '$C0={};';
18
- const unitTypeName = hash(unitTypeStr);
19
- types[unitTypeName] = unitTypeDef;
20
-
21
- console.log('Unit type name:', unitTypeName);
22
-
23
- // Define bool type: <{} false, {} true>
24
- const boolTypeDef = {
25
- code: 'union',
26
- union: {
27
- false: unitTypeName,
28
- true: unitTypeName
29
- }
30
- };
31
- const boolTypeStr = `$C0=<${unitTypeName}"false",${unitTypeName}"true">;`;
32
- const boolTypeName = hash(boolTypeStr);
33
- types[boolTypeName] = boolTypeDef;
34
-
35
- console.log('Bool type name:', boolTypeName);
36
-
37
- // Resolver function
38
- function resolveType(typeName) {
39
- if (!types[typeName]) {
40
- throw new Error(`Unknown type: ${typeName}`);
41
- }
42
- return types[typeName];
43
- }
44
-
45
- // Test 1: Unit value
46
- console.log('\n=== Test 1: Unit ===');
47
- const unitValue = Value.product({});
48
- console.log('Encoding unit:', unitValue.toString());
49
-
50
- const unitPattern = patternToPropertyList(deriveClosedPattern(unitTypeName, unitTypeDef, resolveType));
51
- const unitEncoded = encodeToWire(unitValue, unitPattern);
52
- console.log('Encoded bytes:', unitEncoded.length, 'bytes');
53
- console.log('Hex:', unitEncoded.toString('hex'));
54
-
55
- const unitDecoded = decodeWire(unitEncoded);
56
- console.log('Decoded value:', unitDecoded.value.toString());
57
- console.log('Match:', isProduct(unitDecoded.value) &&
58
- Object.keys(unitDecoded.value.product).length === 0);
59
-
60
- // Test 2: Bool false
61
- console.log('\n=== Test 2: Bool (false) ===');
62
- const falseValue = Value.variant('false', Value.product({}));
63
- console.log('Encoding false:', falseValue.toString());
64
-
65
- const boolPattern = patternToPropertyList(deriveClosedPattern(boolTypeName, boolTypeDef, resolveType));
66
- const falseEncoded = encodeToWire(falseValue, boolPattern);
67
- console.log('Encoded bytes:', falseEncoded.length, 'bytes');
68
- console.log('Hex:', falseEncoded.toString('hex'));
69
-
70
- const falseDecoded = decodeWire(falseEncoded);
71
- console.log('Decoded value:', falseDecoded.value.toString());
72
- console.log('Match:', isVariant(falseDecoded.value) &&
73
- falseDecoded.value.tag === 'false');
74
-
75
- // Test 3: Bool true
76
- console.log('\n=== Test 3: Bool (true) ===');
77
- const trueValue = Value.variant('true', Value.product({}));
78
- console.log('Encoding true:', trueValue.toString());
79
-
80
- const trueEncoded = encodeToWire(trueValue, boolPattern);
81
- console.log('Encoded bytes:', trueEncoded.length, 'bytes');
82
- console.log('Hex:', trueEncoded.toString('hex'));
83
-
84
- const trueDecoded = decodeWire(trueEncoded);
85
- console.log('Decoded value:', trueDecoded.value.toString());
86
- console.log('Match:', isVariant(trueDecoded.value) &&
87
- trueDecoded.value.tag === 'true');
88
-
89
- console.log('\n=== All tests complete ===');
@@ -1,34 +0,0 @@
1
- import { isProduct, isVariant } from "../../Value.mjs";
2
-
3
- function typeDefsFromValue(value) {
4
- let nextId = 0;
5
- const defs = {};
6
-
7
- function visit(node) {
8
- const name = `:auto${nextId++}`;
9
- if (isProduct(node)) {
10
- const labels = Object.keys(node.product).sort();
11
- const product = {};
12
- for (const label of labels) {
13
- product[label] = visit(node.product[label]);
14
- }
15
- defs[name] = { code: "product", product };
16
- return name;
17
- }
18
-
19
- if (isVariant(node)) {
20
- const childName = visit(node.value);
21
- defs[name] = { code: "union", union: { [node.tag]: childName } };
22
- return name;
23
- }
24
-
25
- throw new Error(`Unsupported runtime value node: ${node?.constructor?.name || typeof node}`);
26
- }
27
-
28
- const root = visit(value);
29
- defs.__root__ = { code: "ref", ref: `@${root}` };
30
- return { defs, root };
31
- }
32
-
33
- export { typeDefsFromValue };
34
- export default { typeDefsFromValue };