@fraczak/k 8.0.0 → 8.2.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/CHANGELOG.md +51 -0
- package/DOCS/CODECS.md +22 -24
- package/DOCS/CODEC_ARCHITECTURE.md +3 -5
- package/DOCS/JSON_APPROACHES_COMPARISON.md +9 -13
- package/DOCS/NAMED_PATTERNS.md +23 -0
- package/DOCS/PATTERNS.md +5 -0
- package/DOCS/REPL.md +45 -1
- package/DOCS/WEB_REPL_REQUIREMENTS.md +3 -4
- package/Examples/djson.k +631 -165
- package/README.md +1 -3
- package/browser/repl-browser.mjs +0 -4
- package/browser/vfs-data.mjs +3 -9
- package/codecs/README.md +1 -3
- package/codecs/djson.mjs +375 -84
- package/package.json +1 -4
- package/pretty.mjs +110 -2
- package/repl.mjs +49 -12
- package/scripts/build-repl-html.mjs +1 -11
- package/scripts/run-tests.mjs +5 -0
- package/tests/test-codecs.mjs +4 -34
- package/tests/test-djson-methods.mjs +285 -0
- package/tests/test-repl-html.mjs +0 -4
- package/tests/test-repl.mjs +58 -62
- package/Examples/djson-btrie.k +0 -414
- package/Examples/djson-ptrie.k +0 -663
- package/codecs/djson-btrie.mjs +0 -560
- package/codecs/djson-ptrie.mjs +0 -587
- package/scripts/perf-djson.mjs +0 -1142
- package/tests/bench-djson-tries.mjs +0 -340
package/pretty.mjs
CHANGED
|
@@ -178,5 +178,113 @@ function patterns2filters(typePatternGraph, ...patternIds) {
|
|
|
178
178
|
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
|
|
182
|
-
|
|
181
|
+
function patternGraphToCanonical(typePatternGraph, rootId) {
|
|
182
|
+
const newTypePatternGraph = new TypePatternGraph(
|
|
183
|
+
typePatternGraph.registerCodeDef,
|
|
184
|
+
typePatternGraph.findCode
|
|
185
|
+
);
|
|
186
|
+
const renamed = typePatternGraph.clone([rootId], newTypePatternGraph);
|
|
187
|
+
const newRootId = newTypePatternGraph.find(renamed[rootId]);
|
|
188
|
+
|
|
189
|
+
const inDegree = newTypePatternGraph.patterns.nodes.map(() => 0);
|
|
190
|
+
inDegree[newRootId]++;
|
|
191
|
+
newTypePatternGraph.edges.forEach((outEdges) =>
|
|
192
|
+
Object.values(outEdges).forEach((destMap) =>
|
|
193
|
+
Object.values(destMap).forEach((dst) => inDegree[dst]++)
|
|
194
|
+
)
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
const variables = inDegree.reduce((vars, degree, i) => {
|
|
198
|
+
const p = newTypePatternGraph.get_pattern(i);
|
|
199
|
+
if (degree > 1 || p.pattern === "(...)") {
|
|
200
|
+
vars[i] = true;
|
|
201
|
+
}
|
|
202
|
+
return vars;
|
|
203
|
+
}, {});
|
|
204
|
+
|
|
205
|
+
const filterVars = {};
|
|
206
|
+
const canonicalParams = [];
|
|
207
|
+
|
|
208
|
+
const buildFilter = (path, patternId) => {
|
|
209
|
+
let named_filter = {};
|
|
210
|
+
const pattern = newTypePatternGraph.get_pattern(patternId);
|
|
211
|
+
if (variables[patternId]) {
|
|
212
|
+
if (filterVars[patternId]) {
|
|
213
|
+
return { type: "name", name: filterVars[patternId] };
|
|
214
|
+
}
|
|
215
|
+
const varName = `X${Object.keys(filterVars).length}`;
|
|
216
|
+
filterVars[patternId] = varName;
|
|
217
|
+
named_filter = { name: varName };
|
|
218
|
+
if (pattern.pattern === "(...)") {
|
|
219
|
+
canonicalParams.push(varName);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const edges = newTypePatternGraph.edges[patternId] || {};
|
|
224
|
+
const fields = () =>
|
|
225
|
+
Object.keys(edges)
|
|
226
|
+
.sort()
|
|
227
|
+
.reduce((flds, key) => {
|
|
228
|
+
flds[key] = buildFilter(
|
|
229
|
+
[...path, key],
|
|
230
|
+
Object.values(edges[key])[0]
|
|
231
|
+
);
|
|
232
|
+
return flds;
|
|
233
|
+
}, {});
|
|
234
|
+
|
|
235
|
+
switch (pattern.pattern) {
|
|
236
|
+
case "type": {
|
|
237
|
+
const code = newTypePatternGraph.findCode(pattern.type);
|
|
238
|
+
if (code?.code === "product") {
|
|
239
|
+
return { type: "product", fields: fields(), ...named_filter };
|
|
240
|
+
} else if (code?.code === "union") {
|
|
241
|
+
return { type: "union", fields: fields(), ...named_filter };
|
|
242
|
+
}
|
|
243
|
+
return { type: "code", code: pattern.type, ...named_filter };
|
|
244
|
+
}
|
|
245
|
+
case "(...)":
|
|
246
|
+
return {
|
|
247
|
+
type: null,
|
|
248
|
+
open: true,
|
|
249
|
+
fields: fields(),
|
|
250
|
+
...named_filter
|
|
251
|
+
};
|
|
252
|
+
case "{...}":
|
|
253
|
+
return {
|
|
254
|
+
type: "product",
|
|
255
|
+
open: true,
|
|
256
|
+
fields: fields(),
|
|
257
|
+
...named_filter
|
|
258
|
+
};
|
|
259
|
+
case "<...>":
|
|
260
|
+
return {
|
|
261
|
+
type: "union",
|
|
262
|
+
open: true,
|
|
263
|
+
fields: fields(),
|
|
264
|
+
...named_filter
|
|
265
|
+
};
|
|
266
|
+
case "{}":
|
|
267
|
+
return { type: "product", fields: fields(), ...named_filter };
|
|
268
|
+
case "<>":
|
|
269
|
+
return { type: "union", fields: fields(), ...named_filter };
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const filter = buildFilter([], newRootId);
|
|
274
|
+
return { filter, canonicalParams };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export default {
|
|
278
|
+
prettyCode,
|
|
279
|
+
prettyRel,
|
|
280
|
+
prettyFilter,
|
|
281
|
+
patterns2filters,
|
|
282
|
+
patternGraphToCanonical
|
|
283
|
+
};
|
|
284
|
+
export {
|
|
285
|
+
prettyCode,
|
|
286
|
+
prettyRel,
|
|
287
|
+
prettyFilter,
|
|
288
|
+
patterns2filters,
|
|
289
|
+
patternGraphToCanonical
|
|
290
|
+
};
|
package/repl.mjs
CHANGED
|
@@ -21,7 +21,12 @@ if (typeof globalThis !== "undefined") {
|
|
|
21
21
|
globalThis.isVariant = isVariant;
|
|
22
22
|
globalThis.patternFromFilter = patternFromFilter;
|
|
23
23
|
}
|
|
24
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
patternGraphToCanonical,
|
|
26
|
+
patterns2filters,
|
|
27
|
+
prettyCode,
|
|
28
|
+
prettyFilter
|
|
29
|
+
} from "./pretty.mjs";
|
|
25
30
|
import {
|
|
26
31
|
compileObject,
|
|
27
32
|
compileLibrary,
|
|
@@ -366,9 +371,15 @@ function listAliases(aliases) {
|
|
|
366
371
|
return entries.map(([name, hash]) => `${name} = ${hash}`).join("\n");
|
|
367
372
|
}
|
|
368
373
|
|
|
369
|
-
function
|
|
370
|
-
if (!state || !pat) return null;
|
|
371
|
-
if (pat._canonical
|
|
374
|
+
function resolvePatternCanonicalInfo(state, name, pat) {
|
|
375
|
+
if (!state || !pat) return { hash: null, def: null, params: null };
|
|
376
|
+
if (pat._canonical && pat._canonicalDef) {
|
|
377
|
+
return {
|
|
378
|
+
hash: pat._canonical,
|
|
379
|
+
def: pat._canonicalDef,
|
|
380
|
+
params: pat._canonicalParams || null
|
|
381
|
+
};
|
|
382
|
+
}
|
|
372
383
|
const params = pat.params || [];
|
|
373
384
|
const source = `? ${name}[${params.join(", ")}]`;
|
|
374
385
|
try {
|
|
@@ -385,6 +396,12 @@ function resolvePatternCanonicalName(state, name, pat) {
|
|
|
385
396
|
const rootId = mainRel.typePatternGraph.find(
|
|
386
397
|
mainRel.def.patterns[0]
|
|
387
398
|
);
|
|
399
|
+
const { filter: canonicalFilter, canonicalParams } =
|
|
400
|
+
patternGraphToCanonical(mainRel.typePatternGraph, rootId);
|
|
401
|
+
if (canonicalFilter) {
|
|
402
|
+
pat._canonicalDef = prettyFilter(canonicalFilter);
|
|
403
|
+
pat._canonicalParams = canonicalParams;
|
|
404
|
+
}
|
|
388
405
|
const propList = patternToPropertyList(
|
|
389
406
|
exportPatternGraph(mainRel.typePatternGraph, rootId)
|
|
390
407
|
);
|
|
@@ -392,26 +409,46 @@ function resolvePatternCanonicalName(state, name, pat) {
|
|
|
392
409
|
if (codeHash) {
|
|
393
410
|
state.codes = codes.dump();
|
|
394
411
|
pat._canonical = codeHash;
|
|
395
|
-
return
|
|
412
|
+
return {
|
|
413
|
+
hash: codeHash,
|
|
414
|
+
def: pat._canonicalDef || null,
|
|
415
|
+
params: pat._canonicalParams || null
|
|
416
|
+
};
|
|
396
417
|
}
|
|
397
418
|
}
|
|
398
419
|
const relHash = annotated.relAlias?.__main__ || null;
|
|
399
420
|
if (relHash) {
|
|
400
421
|
pat._canonical = relHash;
|
|
401
|
-
return
|
|
422
|
+
return {
|
|
423
|
+
hash: relHash,
|
|
424
|
+
def: pat._canonicalDef || null,
|
|
425
|
+
params: pat._canonicalParams || null
|
|
426
|
+
};
|
|
402
427
|
}
|
|
403
428
|
} catch {
|
|
404
429
|
restoreCodes(state);
|
|
405
|
-
return null;
|
|
430
|
+
return { hash: null, def: null, params: null };
|
|
406
431
|
}
|
|
407
|
-
return
|
|
432
|
+
return {
|
|
433
|
+
hash: null,
|
|
434
|
+
def: pat._canonicalDef || null,
|
|
435
|
+
params: pat._canonicalParams || null
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function resolvePatternCanonicalName(state, name, pat) {
|
|
440
|
+
return resolvePatternCanonicalInfo(state, name, pat).hash;
|
|
408
441
|
}
|
|
409
442
|
|
|
410
443
|
function prettyPattern(name, pat, state = null) {
|
|
411
|
-
const
|
|
412
|
-
const
|
|
413
|
-
|
|
414
|
-
|
|
444
|
+
const info = resolvePatternCanonicalInfo(state, name, pat);
|
|
445
|
+
const paramList = info.params != null
|
|
446
|
+
? info.params
|
|
447
|
+
: (pat?.params || []);
|
|
448
|
+
const params = paramList.join(", ");
|
|
449
|
+
const filterStr = info.def ||
|
|
450
|
+
(pat?.filter ? prettyFilter(pat.filter) : "()");
|
|
451
|
+
const suffix = info.hash ? ` -- ${info.hash}` : "";
|
|
415
452
|
return `${name}[${params}] = ${filterStr};${suffix}`;
|
|
416
453
|
}
|
|
417
454
|
|
|
@@ -84,9 +84,7 @@ const verifiedExamples = [
|
|
|
84
84
|
"ieee.k",
|
|
85
85
|
"poly.k",
|
|
86
86
|
"karatsuba-mult.k",
|
|
87
|
-
"djson.k"
|
|
88
|
-
"djson-btrie.k",
|
|
89
|
-
"djson-ptrie.k"
|
|
87
|
+
"djson.k"
|
|
90
88
|
];
|
|
91
89
|
const examplesDir = path.join(projectRoot, "Examples");
|
|
92
90
|
for (const filename of verifiedExamples) {
|
|
@@ -102,8 +100,6 @@ for (const filename of verifiedExamples) {
|
|
|
102
100
|
const verifiedCodecs = [
|
|
103
101
|
"json.mjs",
|
|
104
102
|
"djson.mjs",
|
|
105
|
-
"djson-btrie.mjs",
|
|
106
|
-
"djson-ptrie.mjs",
|
|
107
103
|
"utf8.mjs",
|
|
108
104
|
"int.mjs",
|
|
109
105
|
"unit.mjs",
|
|
@@ -1698,8 +1694,6 @@ const htmlContent = `<!DOCTYPE html>
|
|
|
1698
1694
|
<option value="poly.k">poly.k</option>
|
|
1699
1695
|
<option value="karatsuba-mult.k">karatsuba-mult.k</option>
|
|
1700
1696
|
<option value="djson.k">djson.k</option>
|
|
1701
|
-
<option value="djson-btrie.k">djson-btrie.k</option>
|
|
1702
|
-
<option value="djson-ptrie.k">djson-ptrie.k</option>
|
|
1703
1697
|
</optgroup>
|
|
1704
1698
|
<optgroup label="Quick Expressions">
|
|
1705
1699
|
<option value="expr:{} | ok">{} | ok</option>
|
|
@@ -1720,14 +1714,10 @@ const htmlContent = `<!DOCTYPE html>
|
|
|
1720
1714
|
<option value="expr::codec load utf8.mjs">:codec load utf8.mjs</option>
|
|
1721
1715
|
<option value="expr::codec load json.mjs">:codec load json.mjs</option>
|
|
1722
1716
|
<option value="expr::codec load djson.mjs">:codec load djson.mjs</option>
|
|
1723
|
-
<option value="expr::codec load djson-btrie.mjs">:codec load djson-btrie.mjs</option>
|
|
1724
|
-
<option value="expr::codec load djson-ptrie.mjs">:codec load djson-ptrie.mjs</option>
|
|
1725
1717
|
<option value="expr::codec load ieee.mjs">:codec load ieee.mjs</option>
|
|
1726
1718
|
<option value="expr::codec load unit.mjs">:codec load unit.mjs</option>
|
|
1727
1719
|
<option value="expr::input json.mjs {"a":12,"b":"hello"}">:input json.mjs {"a":12,"b":"hello"}</option>
|
|
1728
1720
|
<option value="expr::input djson.mjs {"a":12,"b":"hello"}">:input djson.mjs {"a":12,"b":"hello"}</option>
|
|
1729
|
-
<option value="expr::input djson-btrie.mjs {"a":12,"b":"hello"}">:input djson-btrie.mjs {"a":12,"b":"hello"}</option>
|
|
1730
|
-
<option value="expr::input djson-ptrie.mjs {"a":12,"b":"hello"}">:input djson-ptrie.mjs {"a":12,"b":"hello"}</option>
|
|
1731
1721
|
<option value="expr::input int.mjs [0,1,2]">:input int.mjs [0,1,2]</option>
|
|
1732
1722
|
</optgroup>
|
|
1733
1723
|
</select>
|
package/scripts/run-tests.mjs
CHANGED
|
@@ -43,6 +43,11 @@ const tests = [
|
|
|
43
43
|
["tests/test-hash-normalization.mjs", node, ["tests/test-hash-normalization.mjs"]],
|
|
44
44
|
["tests/test-structural-values.mjs", node, ["tests/test-structural-values.mjs"]],
|
|
45
45
|
["tests/test-codecs.mjs", node, ["tests/test-codecs.mjs"]],
|
|
46
|
+
[
|
|
47
|
+
"tests/test-djson-methods.mjs",
|
|
48
|
+
node,
|
|
49
|
+
["tests/test-djson-methods.mjs"]
|
|
50
|
+
],
|
|
46
51
|
["tests/test-deep-wire-value.mjs", node, ["tests/test-deep-wire-value.mjs"]],
|
|
47
52
|
["tests/test-k-object.mjs", node, ["tests/test-k-object.mjs"]],
|
|
48
53
|
["tests/test-kir.mjs", node, ["tests/test-kir.mjs"]],
|
package/tests/test-codecs.mjs
CHANGED
|
@@ -9,8 +9,6 @@ import * as SDK from "../codecs/runtime/codec-sdk.mjs";
|
|
|
9
9
|
import * as IntCodec from "../codecs/int.mjs";
|
|
10
10
|
import * as JsonCodec from "../codecs/json.mjs";
|
|
11
11
|
import * as DjsonCodec from "../codecs/djson.mjs";
|
|
12
|
-
import * as DjsonBtrieCodec from "../codecs/djson-btrie.mjs";
|
|
13
|
-
import * as DjsonPtrieCodec from "../codecs/djson-ptrie.mjs";
|
|
14
12
|
import * as Utf8Codec from "../codecs/utf8.mjs";
|
|
15
13
|
import * as Utf16Codec from "../codecs/utf16.mjs";
|
|
16
14
|
import * as IeeeCodec from "../codecs/ieee.mjs";
|
|
@@ -57,16 +55,6 @@ for (const [name, codec, sampleInput] of [
|
|
|
57
55
|
["int.mjs", IntCodec, "42"],
|
|
58
56
|
["json.mjs", JsonCodec, '{"x": 1}'],
|
|
59
57
|
["djson.mjs", DjsonCodec, '{"x": 1, "arr": [true, null, "hi"]}'],
|
|
60
|
-
[
|
|
61
|
-
"djson-btrie.mjs",
|
|
62
|
-
DjsonBtrieCodec,
|
|
63
|
-
'{"x": 1, "arr": [true, null, "hi"]}'
|
|
64
|
-
],
|
|
65
|
-
[
|
|
66
|
-
"djson-ptrie.mjs",
|
|
67
|
-
DjsonPtrieCodec,
|
|
68
|
-
'{"x": 1, "arr": [true, null, "hi"]}'
|
|
69
|
-
],
|
|
70
58
|
["utf8.mjs", Utf8Codec, "hello"],
|
|
71
59
|
["utf16.mjs", Utf16Codec, "hello"],
|
|
72
60
|
["ieee.mjs", IeeeCodec, "3.14"],
|
|
@@ -87,8 +75,6 @@ for (const relPath of [
|
|
|
87
75
|
"codecs/int.mjs",
|
|
88
76
|
"codecs/json.mjs",
|
|
89
77
|
"codecs/djson.mjs",
|
|
90
|
-
"codecs/djson-btrie.mjs",
|
|
91
|
-
"codecs/djson-ptrie.mjs",
|
|
92
78
|
"codecs/utf8.mjs",
|
|
93
79
|
"codecs/utf16.mjs",
|
|
94
80
|
"codecs/ieee.mjs",
|
|
@@ -328,15 +314,11 @@ console.log("8. Verifying fast string decoding & bounded rendering...");
|
|
|
328
314
|
);
|
|
329
315
|
}
|
|
330
316
|
|
|
331
|
-
// 9. DJSON
|
|
332
|
-
console.log("9. Verifying DJSON
|
|
317
|
+
// 9. DJSON Singleton Pattern Invariants & Round-Trip
|
|
318
|
+
console.log("9. Verifying DJSON singleton pattern invariants...");
|
|
333
319
|
{
|
|
334
320
|
assert(Array.isArray(DjsonCodec.DJSON_PATTERN));
|
|
335
|
-
assert.equal(DjsonCodec.DJSON_PATTERN.length,
|
|
336
|
-
assert(Array.isArray(DjsonBtrieCodec.DJSON_BTRIE_PATTERN));
|
|
337
|
-
assert.equal(DjsonBtrieCodec.DJSON_BTRIE_PATTERN.length, 27);
|
|
338
|
-
assert(Array.isArray(DjsonPtrieCodec.DJSON_PTRIE_PATTERN));
|
|
339
|
-
assert.equal(DjsonPtrieCodec.DJSON_PTRIE_PATTERN.length, 33);
|
|
321
|
+
assert.equal(DjsonCodec.DJSON_PATTERN.length, 27);
|
|
340
322
|
|
|
341
323
|
const samples = [
|
|
342
324
|
null,
|
|
@@ -351,19 +333,7 @@ console.log("9. Verifying DJSON family singleton pattern invariants...");
|
|
|
351
333
|
];
|
|
352
334
|
|
|
353
335
|
for (const [label, codec, pat, relPath] of [
|
|
354
|
-
["djson", DjsonCodec, DjsonCodec.DJSON_PATTERN, "codecs/djson.mjs"]
|
|
355
|
-
[
|
|
356
|
-
"djson-btrie",
|
|
357
|
-
DjsonBtrieCodec,
|
|
358
|
-
DjsonBtrieCodec.DJSON_BTRIE_PATTERN,
|
|
359
|
-
"codecs/djson-btrie.mjs"
|
|
360
|
-
],
|
|
361
|
-
[
|
|
362
|
-
"djson-ptrie",
|
|
363
|
-
DjsonPtrieCodec,
|
|
364
|
-
DjsonPtrieCodec.DJSON_PTRIE_PATTERN,
|
|
365
|
-
"codecs/djson-ptrie.mjs"
|
|
366
|
-
]
|
|
336
|
+
["djson", DjsonCodec, DjsonCodec.DJSON_PATTERN, "codecs/djson.mjs"]
|
|
367
337
|
]) {
|
|
368
338
|
for (const sample of samples) {
|
|
369
339
|
const jsonStr = JSON.stringify(sample);
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// tests/test-djson-methods.mjs
|
|
2
|
+
// Test suite for standard dynamic JSON methods working on json? type.
|
|
3
|
+
|
|
4
|
+
import assert from "node:assert/strict";
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
|
|
9
|
+
import k from "../index.mjs";
|
|
10
|
+
import { Value } from "../Value.mjs";
|
|
11
|
+
import {
|
|
12
|
+
fromJsonValue,
|
|
13
|
+
toJsonValue
|
|
14
|
+
} from "../codecs/djson.mjs";
|
|
15
|
+
import {
|
|
16
|
+
textToStringValue,
|
|
17
|
+
stringValueToText
|
|
18
|
+
} from "../codecs/runtime/unicode-string.mjs";
|
|
19
|
+
|
|
20
|
+
const root = path.resolve(
|
|
21
|
+
path.dirname(fileURLToPath(import.meta.url)),
|
|
22
|
+
".."
|
|
23
|
+
);
|
|
24
|
+
const libPath = path.join(root, "Examples", "djson.k");
|
|
25
|
+
const djsonK = readFileSync(libPath, "utf8");
|
|
26
|
+
|
|
27
|
+
function compileOp(op) {
|
|
28
|
+
return k.compile(`${djsonK}\n${op}\n`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function makeIndex(n) {
|
|
32
|
+
if (n === 0) {
|
|
33
|
+
return Value.variant(
|
|
34
|
+
"0",
|
|
35
|
+
Value.variant("_", Value.product({}))
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
const s = n.toString(2);
|
|
39
|
+
let v = Value.variant("_", Value.product({}));
|
|
40
|
+
for (let i = s.length - 1; i >= 0; i--) {
|
|
41
|
+
v = Value.variant(s[i], v);
|
|
42
|
+
}
|
|
43
|
+
return v;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
console.log("==> Running Dynamic JSON (json?) Methods Test Suite");
|
|
47
|
+
|
|
48
|
+
// Test 1: Object Accessors (get, has?, set, del)
|
|
49
|
+
console.log("--> Test 1: Object Accessors (get, has?, set, del)");
|
|
50
|
+
{
|
|
51
|
+
const fnGet = compileOp("get");
|
|
52
|
+
const fnHas = compileOp("has?");
|
|
53
|
+
const fnSet = compileOp("set");
|
|
54
|
+
const fnDel = compileOp("del");
|
|
55
|
+
|
|
56
|
+
const doc = {
|
|
57
|
+
name: "Alice",
|
|
58
|
+
age: 30,
|
|
59
|
+
tags: ["developer", "k"]
|
|
60
|
+
};
|
|
61
|
+
const kDoc = fromJsonValue(doc);
|
|
62
|
+
|
|
63
|
+
// get "name"
|
|
64
|
+
const resName = fnGet(Value.product({
|
|
65
|
+
key: textToStringValue("name"),
|
|
66
|
+
json: kDoc
|
|
67
|
+
}));
|
|
68
|
+
assert.equal(toJsonValue(resName), "Alice");
|
|
69
|
+
|
|
70
|
+
// get "age"
|
|
71
|
+
const resAge = fnGet(Value.product({
|
|
72
|
+
key: textToStringValue("age"),
|
|
73
|
+
json: kDoc
|
|
74
|
+
}));
|
|
75
|
+
assert.equal(toJsonValue(resAge), 30);
|
|
76
|
+
|
|
77
|
+
// has? hit
|
|
78
|
+
const hit = fnHas(Value.product({
|
|
79
|
+
key: textToStringValue("name"),
|
|
80
|
+
json: kDoc
|
|
81
|
+
}));
|
|
82
|
+
assert.ok(hit);
|
|
83
|
+
|
|
84
|
+
// has? miss
|
|
85
|
+
const miss = fnHas(Value.product({
|
|
86
|
+
key: textToStringValue("missing"),
|
|
87
|
+
json: kDoc
|
|
88
|
+
}));
|
|
89
|
+
assert.equal(miss, undefined);
|
|
90
|
+
|
|
91
|
+
// set "city" = "Paris"
|
|
92
|
+
const withCity = fnSet(Value.product({
|
|
93
|
+
key: textToStringValue("city"),
|
|
94
|
+
val: fromJsonValue("Paris"),
|
|
95
|
+
json: kDoc
|
|
96
|
+
}));
|
|
97
|
+
assert.equal(toJsonValue(withCity).city, "Paris");
|
|
98
|
+
assert.equal(toJsonValue(withCity).name, "Alice");
|
|
99
|
+
|
|
100
|
+
// del "age"
|
|
101
|
+
const withoutAge = fnDel(Value.product({
|
|
102
|
+
key: textToStringValue("age"),
|
|
103
|
+
json: withCity
|
|
104
|
+
}));
|
|
105
|
+
assert.equal(toJsonValue(withoutAge).age, undefined);
|
|
106
|
+
assert.equal(toJsonValue(withoutAge).city, "Paris");
|
|
107
|
+
|
|
108
|
+
console.log(" Passed: Object get, has?, set, del on json?.");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Test 2: List Accessors (nth, has_index?, set_nth, del_nth, push)
|
|
112
|
+
console.log("--> Test 2: List Accessors (nth, set_nth, del_nth, push)");
|
|
113
|
+
{
|
|
114
|
+
const fnNth = compileOp("nth");
|
|
115
|
+
const fnHasIdx = compileOp("has_index?");
|
|
116
|
+
const fnSetNth = compileOp("set_nth");
|
|
117
|
+
const fnDelNth = compileOp("del_nth");
|
|
118
|
+
const fnPush = compileOp("push");
|
|
119
|
+
|
|
120
|
+
const list = [10, 20, 30];
|
|
121
|
+
const kList = fromJsonValue(list);
|
|
122
|
+
|
|
123
|
+
// nth 0, 1, 2
|
|
124
|
+
assert.equal(
|
|
125
|
+
toJsonValue(fnNth(Value.product({
|
|
126
|
+
index: makeIndex(0),
|
|
127
|
+
json: kList
|
|
128
|
+
}))),
|
|
129
|
+
10
|
|
130
|
+
);
|
|
131
|
+
assert.equal(
|
|
132
|
+
toJsonValue(fnNth(Value.product({
|
|
133
|
+
index: makeIndex(1),
|
|
134
|
+
json: kList
|
|
135
|
+
}))),
|
|
136
|
+
20
|
|
137
|
+
);
|
|
138
|
+
assert.equal(
|
|
139
|
+
toJsonValue(fnNth(Value.product({
|
|
140
|
+
index: makeIndex(2),
|
|
141
|
+
json: kList
|
|
142
|
+
}))),
|
|
143
|
+
30
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
// has_index? hit and miss
|
|
147
|
+
assert.ok(fnHasIdx(Value.product({
|
|
148
|
+
index: makeIndex(1),
|
|
149
|
+
json: kList
|
|
150
|
+
})));
|
|
151
|
+
assert.equal(
|
|
152
|
+
fnHasIdx(Value.product({
|
|
153
|
+
index: makeIndex(99),
|
|
154
|
+
json: kList
|
|
155
|
+
})),
|
|
156
|
+
undefined
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
// set_nth 1 -> 99
|
|
160
|
+
const updatedList = fnSetNth(Value.product({
|
|
161
|
+
index: makeIndex(1),
|
|
162
|
+
val: fromJsonValue(99),
|
|
163
|
+
json: kList
|
|
164
|
+
}));
|
|
165
|
+
assert.deepEqual(toJsonValue(updatedList), [10, 99, 30]);
|
|
166
|
+
|
|
167
|
+
// del_nth 1
|
|
168
|
+
const delList = fnDelNth(Value.product({
|
|
169
|
+
index: makeIndex(1),
|
|
170
|
+
json: updatedList
|
|
171
|
+
}));
|
|
172
|
+
assert.deepEqual(toJsonValue(delList), [10, 30]);
|
|
173
|
+
assert.equal(
|
|
174
|
+
fnHasIdx(Value.product({
|
|
175
|
+
index: makeIndex(1),
|
|
176
|
+
json: delList
|
|
177
|
+
})),
|
|
178
|
+
undefined
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
// push 40
|
|
182
|
+
const pushed = fnPush(Value.product({
|
|
183
|
+
val: fromJsonValue(40),
|
|
184
|
+
json: kList
|
|
185
|
+
}));
|
|
186
|
+
assert.deepEqual(toJsonValue(pushed), [10, 20, 30, 40]);
|
|
187
|
+
|
|
188
|
+
console.log(" Passed: List nth, set_nth, del_nth, push on json?.");
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Test 3: Keys and Values (keys, keys_raw, values, values_raw)
|
|
192
|
+
console.log("--> Test 3: Keys and Values (keys, values)");
|
|
193
|
+
{
|
|
194
|
+
const fnKeys = compileOp("keys");
|
|
195
|
+
const fnKeysRaw = compileOp("keys_raw");
|
|
196
|
+
const fnValues = compileOp("values");
|
|
197
|
+
const fnValuesRaw = compileOp("values_raw");
|
|
198
|
+
|
|
199
|
+
const doc = { b: 2, a: 1, c: 3 };
|
|
200
|
+
const kDoc = fromJsonValue(doc);
|
|
201
|
+
|
|
202
|
+
// keys (json? list of string)
|
|
203
|
+
const resKeys = fnKeys(kDoc);
|
|
204
|
+
assert.deepEqual(toJsonValue(resKeys), ["a", "b", "c"]);
|
|
205
|
+
|
|
206
|
+
// keys_raw (raw K cons-list of strings)
|
|
207
|
+
const resKeysRaw = fnKeysRaw(kDoc);
|
|
208
|
+
const rawKeyStrings = [];
|
|
209
|
+
let curr = resKeysRaw;
|
|
210
|
+
while (curr && curr.type === "<>" && curr.tag === "cons") {
|
|
211
|
+
rawKeyStrings.push(stringValueToText(curr.value.product.car));
|
|
212
|
+
curr = curr.value.product.cdr;
|
|
213
|
+
}
|
|
214
|
+
assert.deepEqual(rawKeyStrings, ["a", "b", "c"]);
|
|
215
|
+
|
|
216
|
+
// values on object (json? list of values)
|
|
217
|
+
const resValues = fnValues(kDoc);
|
|
218
|
+
assert.deepEqual(toJsonValue(resValues), [1, 2, 3]);
|
|
219
|
+
|
|
220
|
+
// values on list (json? identity)
|
|
221
|
+
const listJson = fromJsonValue([100, 200]);
|
|
222
|
+
assert.deepEqual(toJsonValue(fnValues(listJson)), [100, 200]);
|
|
223
|
+
|
|
224
|
+
console.log(" Passed: Keys and values extraction on json?.");
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Test 4: Cardinality and Length (length, length_nat)
|
|
228
|
+
console.log("--> Test 4: Cardinality and Length (length, length_nat)");
|
|
229
|
+
{
|
|
230
|
+
const fnLength = compileOp("length");
|
|
231
|
+
|
|
232
|
+
const kObj = fromJsonValue({ x: 1, y: 2, z: 3, w: 4 });
|
|
233
|
+
const kArr = fromJsonValue(["a", "b", "c"]);
|
|
234
|
+
const kStr = fromJsonValue("hello");
|
|
235
|
+
|
|
236
|
+
// length returns json? number (float64)
|
|
237
|
+
assert.equal(toJsonValue(fnLength(kObj)), 4);
|
|
238
|
+
assert.equal(toJsonValue(fnLength(kArr)), 3);
|
|
239
|
+
assert.equal(toJsonValue(fnLength(kStr)), 5);
|
|
240
|
+
|
|
241
|
+
console.log(
|
|
242
|
+
" Passed: Length queries on json? object, list, string."
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Test 5: Type Predicates and type_of
|
|
247
|
+
console.log("--> Test 5: Type Predicates and type_of");
|
|
248
|
+
{
|
|
249
|
+
const fnTypeOf = compileOp("type_of");
|
|
250
|
+
const fnIsObj = compileOp("is_object?");
|
|
251
|
+
const fnIsList = compileOp("is_list?");
|
|
252
|
+
const fnIsStr = compileOp("is_string?");
|
|
253
|
+
const fnIsNum = compileOp("is_number?");
|
|
254
|
+
const fnIsBool = compileOp("is_bool?");
|
|
255
|
+
const fnIsNull = compileOp("is_null?");
|
|
256
|
+
|
|
257
|
+
const kObj = fromJsonValue({ a: 1 });
|
|
258
|
+
const kArr = fromJsonValue([1, 2]);
|
|
259
|
+
const kStr = fromJsonValue("hi");
|
|
260
|
+
const kNum = fromJsonValue(3.14);
|
|
261
|
+
const kBool = fromJsonValue(true);
|
|
262
|
+
const kNull = fromJsonValue(null);
|
|
263
|
+
|
|
264
|
+
assert.equal(fnTypeOf(kObj).tag, "object");
|
|
265
|
+
assert.equal(fnTypeOf(kArr).tag, "list");
|
|
266
|
+
assert.equal(fnTypeOf(kStr).tag, "string");
|
|
267
|
+
assert.equal(fnTypeOf(kNum).tag, "number");
|
|
268
|
+
assert.equal(fnTypeOf(kBool).tag, "bool");
|
|
269
|
+
assert.equal(fnTypeOf(kNull).tag, "null");
|
|
270
|
+
|
|
271
|
+
assert.ok(fnIsObj(kObj));
|
|
272
|
+
assert.equal(fnIsObj(kArr), undefined);
|
|
273
|
+
|
|
274
|
+
assert.ok(fnIsList(kArr));
|
|
275
|
+
assert.equal(fnIsList(kObj), undefined);
|
|
276
|
+
|
|
277
|
+
assert.ok(fnIsStr(kStr));
|
|
278
|
+
assert.ok(fnIsNum(kNum));
|
|
279
|
+
assert.ok(fnIsBool(kBool));
|
|
280
|
+
assert.ok(fnIsNull(kNull));
|
|
281
|
+
|
|
282
|
+
console.log(" Passed: Type predicates and type_of inspection.");
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
console.log("==> All Dynamic JSON Methods Tests Passed Successfully!");
|
package/tests/test-repl-html.mjs
CHANGED