mol_vary 0.0.41 → 0.0.43

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/node.test.js CHANGED
@@ -2327,14 +2327,23 @@ var $;
2327
2327
  $mol_vary_spec[$mol_vary_spec["true"] = 'T'.charCodeAt(0)] = "true";
2328
2328
  $mol_vary_spec[$mol_vary_spec["fake"] = 'F'.charCodeAt(0)] = "fake";
2329
2329
  $mol_vary_spec[$mol_vary_spec["both"] = 'B'.charCodeAt(0)] = "both";
2330
- $mol_vary_spec[$mol_vary_spec["fp16"] = 93] = "fp16";
2331
- $mol_vary_spec[$mol_vary_spec["fp32"] = 94] = "fp32";
2332
- $mol_vary_spec[$mol_vary_spec["fp64"] = 95] = "fp64";
2330
+ $mol_vary_spec[$mol_vary_spec["fp16"] = 'H'.charCodeAt(0)] = "fp16";
2331
+ $mol_vary_spec[$mol_vary_spec["fp32"] = 'S'.charCodeAt(0)] = "fp32";
2332
+ $mol_vary_spec[$mol_vary_spec["fp64"] = 'D'.charCodeAt(0)] = "fp64";
2333
+ $mol_vary_spec[$mol_vary_spec["f128"] = 'Q'.charCodeAt(0)] = "f128";
2334
+ $mol_vary_spec[$mol_vary_spec["f256"] = 'O'.charCodeAt(0)] = "f256";
2333
2335
  })($mol_vary_spec = $.$mol_vary_spec || ($.$mol_vary_spec = {}));
2334
- let buffer = new Uint8Array(256);
2335
- let pack = new DataView(buffer.buffer);
2336
- class $mol_vary extends Object {
2337
- static pack(data) {
2336
+ const pojo_maker = (keys) => (vals) => {
2337
+ const obj = {};
2338
+ for (let i = 0; i < keys.length; ++i)
2339
+ obj[keys[i]] = vals[i];
2340
+ return obj;
2341
+ };
2342
+ class $mol_vary_class extends Object {
2343
+ lean_symbol = Symbol('$mol_vary_lean');
2344
+ array = new Uint8Array(256);
2345
+ buffer = new DataView(this.array.buffer);
2346
+ pack(data) {
2338
2347
  let pos = 0;
2339
2348
  let capacity = 9;
2340
2349
  const offsets = new Map();
@@ -2343,19 +2352,19 @@ var $;
2343
2352
  if (size < 0)
2344
2353
  return;
2345
2354
  capacity += size;
2346
- if (buffer.byteLength >= capacity)
2355
+ if (this.array.byteLength >= capacity)
2347
2356
  return;
2348
2357
  const buffer2 = new Uint8Array(Math.ceil(capacity / 4096) * 4096);
2349
- buffer2.set(buffer);
2350
- buffer = buffer2;
2351
- pack = new DataView(buffer.buffer);
2358
+ buffer2.set(this.array);
2359
+ this.array = buffer2;
2360
+ this.buffer = new DataView(this.array.buffer);
2352
2361
  };
2353
2362
  const release = (size) => {
2354
2363
  capacity -= size;
2355
2364
  };
2356
2365
  const dump_unum = (tip, val) => {
2357
2366
  if (val < $mol_vary_len.L1) {
2358
- buffer[pos++] = tip | Number(val);
2367
+ this.array[pos++] = tip | Number(val);
2359
2368
  release(8);
2360
2369
  return;
2361
2370
  }
@@ -2365,25 +2374,25 @@ var $;
2365
2374
  return dump_unum($mol_vary_tip.link, offset);
2366
2375
  }
2367
2376
  if (val < 2 ** 8) {
2368
- buffer[pos++] = tip | $mol_vary_len.L1;
2369
- buffer[pos++] = Number(val);
2377
+ this.array[pos++] = tip | $mol_vary_len.L1;
2378
+ this.array[pos++] = Number(val);
2370
2379
  release(7);
2371
2380
  }
2372
2381
  else if (val < 2 ** 16) {
2373
- buffer[pos++] = tip | $mol_vary_len.L2;
2374
- pack.setUint16(pos, Number(val), true);
2382
+ this.array[pos++] = tip | $mol_vary_len.L2;
2383
+ this.buffer.setUint16(pos, Number(val), true);
2375
2384
  pos += 2;
2376
2385
  release(6);
2377
2386
  }
2378
2387
  else if (val < 2 ** 32) {
2379
- buffer[pos++] = tip | $mol_vary_len.L4;
2380
- pack.setUint32(pos, Number(val), true);
2388
+ this.array[pos++] = tip | $mol_vary_len.L4;
2389
+ this.buffer.setUint32(pos, Number(val), true);
2381
2390
  pos += 4;
2382
2391
  release(4);
2383
2392
  }
2384
2393
  else if (val < 2n ** 64n) {
2385
- buffer[pos++] = tip | $mol_vary_len.L8;
2386
- pack.setBigUint64(pos, BigInt(val), true);
2394
+ this.array[pos++] = tip | $mol_vary_len.L8;
2395
+ this.buffer.setBigUint64(pos, BigInt(val), true);
2387
2396
  pos += 8;
2388
2397
  }
2389
2398
  else {
@@ -2394,7 +2403,7 @@ var $;
2394
2403
  };
2395
2404
  const dump_snum = (val) => {
2396
2405
  if (val > -$mol_vary_len.L1) {
2397
- buffer[pos++] = Number(val);
2406
+ this.array[pos++] = Number(val);
2398
2407
  release(8);
2399
2408
  return;
2400
2409
  }
@@ -2402,25 +2411,25 @@ var $;
2402
2411
  if (offset !== undefined)
2403
2412
  return dump_unum($mol_vary_tip.link, offset);
2404
2413
  if (val >= -(2 ** 7)) {
2405
- buffer[pos++] = -$mol_vary_len.L1;
2406
- buffer[pos++] = Number(val);
2414
+ this.array[pos++] = -$mol_vary_len.L1;
2415
+ this.array[pos++] = Number(val);
2407
2416
  release(7);
2408
2417
  }
2409
2418
  else if (val >= -(2 ** 15)) {
2410
- buffer[pos++] = -$mol_vary_len.L2;
2411
- pack.setInt16(pos, Number(val), true);
2419
+ this.array[pos++] = -$mol_vary_len.L2;
2420
+ this.buffer.setInt16(pos, Number(val), true);
2412
2421
  pos += 2;
2413
2422
  release(6);
2414
2423
  }
2415
2424
  else if (val >= -(2 ** 31)) {
2416
- buffer[pos++] = -$mol_vary_len.L4;
2417
- pack.setInt32(pos, Number(val), true);
2425
+ this.array[pos++] = -$mol_vary_len.L4;
2426
+ this.buffer.setInt32(pos, Number(val), true);
2418
2427
  pos += 4;
2419
2428
  release(4);
2420
2429
  }
2421
2430
  else if (val >= -(2n ** 63n)) {
2422
- buffer[pos++] = -$mol_vary_len.L8;
2423
- pack.setBigInt64(pos, BigInt(val), true);
2431
+ this.array[pos++] = -$mol_vary_len.L8;
2432
+ this.buffer.setBigInt64(pos, BigInt(val), true);
2424
2433
  pos += 8;
2425
2434
  }
2426
2435
  else {
@@ -2433,17 +2442,17 @@ var $;
2433
2442
  if (buf.byteLength > 264)
2434
2443
  $mol_fail(new Error('Number too high', { cause: { val } }));
2435
2444
  acquire(buf.byteLength - 7);
2436
- buffer[pos++] = -$mol_vary_len.LA;
2437
- buffer[pos++] = buf.byteLength - 9;
2438
- buffer.set(buf, pos);
2445
+ this.array[pos++] = -$mol_vary_len.LA;
2446
+ this.array[pos++] = buf.byteLength - 9;
2447
+ this.array.set(buf, pos);
2439
2448
  pos += buf.byteLength;
2440
2449
  };
2441
2450
  const dump_float = (val) => {
2442
2451
  const offset = offsets.get(val);
2443
2452
  if (offset !== undefined)
2444
2453
  return dump_unum($mol_vary_tip.link, offset);
2445
- buffer[pos++] = $mol_vary_spec.fp64;
2446
- pack.setFloat64(pos, val, true);
2454
+ this.array[pos++] = $mol_vary_spec.fp64;
2455
+ this.buffer.setFloat64(pos, val, true);
2447
2456
  pos += 8;
2448
2457
  offsets.set(val, offsets.size);
2449
2458
  };
@@ -2453,7 +2462,7 @@ var $;
2453
2462
  return dump_unum($mol_vary_tip.link, offset);
2454
2463
  dump_unum($mol_vary_tip.text, val.length);
2455
2464
  acquire(val.length * 3);
2456
- const len = $mol_charset_encode_to(val, buffer, pos);
2465
+ const len = $mol_charset_encode_to(val, this.array, pos);
2457
2466
  pos += len;
2458
2467
  release(val.length * 3 - len);
2459
2468
  offsets.set(val, offsets.size);
@@ -2466,31 +2475,31 @@ var $;
2466
2475
  dump_unum($mol_vary_tip.blob, val.byteLength);
2467
2476
  acquire(1 + val.byteLength);
2468
2477
  if (val instanceof Uint8Array)
2469
- buffer[pos++] = $mol_vary_tip.uint | $mol_vary_len.L1;
2478
+ this.array[pos++] = $mol_vary_tip.uint | $mol_vary_len.L1;
2470
2479
  else if (val instanceof Uint16Array)
2471
- buffer[pos++] = $mol_vary_tip.uint | $mol_vary_len.L2;
2480
+ this.array[pos++] = $mol_vary_tip.uint | $mol_vary_len.L2;
2472
2481
  else if (val instanceof Uint32Array)
2473
- buffer[pos++] = $mol_vary_tip.uint | $mol_vary_len.L4;
2482
+ this.array[pos++] = $mol_vary_tip.uint | $mol_vary_len.L4;
2474
2483
  else if (val instanceof BigUint64Array)
2475
- buffer[pos++] = $mol_vary_tip.uint | $mol_vary_len.L8;
2484
+ this.array[pos++] = $mol_vary_tip.uint | $mol_vary_len.L8;
2476
2485
  else if (val instanceof Int8Array)
2477
- buffer[pos++] = $mol_vary_tip.sint | ~$mol_vary_len.L1;
2486
+ this.array[pos++] = $mol_vary_tip.sint | ~$mol_vary_len.L1;
2478
2487
  else if (val instanceof Int16Array)
2479
- buffer[pos++] = $mol_vary_tip.sint | ~$mol_vary_len.L2;
2488
+ this.array[pos++] = $mol_vary_tip.sint | ~$mol_vary_len.L2;
2480
2489
  else if (val instanceof Int32Array)
2481
- buffer[pos++] = $mol_vary_tip.sint | ~$mol_vary_len.L4;
2490
+ this.array[pos++] = $mol_vary_tip.sint | ~$mol_vary_len.L4;
2482
2491
  else if (val instanceof BigInt64Array)
2483
- buffer[pos++] = $mol_vary_tip.sint | ~$mol_vary_len.L8;
2492
+ this.array[pos++] = $mol_vary_tip.sint | ~$mol_vary_len.L8;
2484
2493
  else if (typeof Float16Array === 'function' && val instanceof Float16Array)
2485
- buffer[pos++] = $mol_vary_spec.fp16;
2494
+ this.array[pos++] = $mol_vary_spec.fp16;
2486
2495
  else if (val instanceof Float32Array)
2487
- buffer[pos++] = $mol_vary_spec.fp32;
2496
+ this.array[pos++] = $mol_vary_spec.fp32;
2488
2497
  else if (val instanceof Float64Array)
2489
- buffer[pos++] = $mol_vary_spec.fp64;
2498
+ this.array[pos++] = $mol_vary_spec.fp64;
2490
2499
  else
2491
2500
  $mol_fail(new Error(`Unsupported type`));
2492
2501
  const src = (val instanceof Uint8Array) ? val : new Uint8Array(val.buffer, val.byteOffset, val.byteLength);
2493
- buffer.set(src, pos);
2502
+ this.array.set(src, pos);
2494
2503
  pos += val.byteLength;
2495
2504
  offsets.set(val, offsets.size);
2496
2505
  };
@@ -2524,7 +2533,7 @@ var $;
2524
2533
  const offset = offsets.get(val);
2525
2534
  if (offset !== undefined)
2526
2535
  return dump_unum($mol_vary_tip.link, offset);
2527
- const [keys, vals] = val[$.$mol_vary_lean]?.(val) ?? [shape(val), Object.values(val)];
2536
+ const [keys, vals] = this.lean_find(val)?.(val) ?? [shape(val), Object.values(val)];
2528
2537
  dump_unum($mol_vary_tip.tupl, vals.length);
2529
2538
  acquire((vals.length + 1) * 9);
2530
2539
  dump_list(keys);
@@ -2541,12 +2550,12 @@ var $;
2541
2550
  const dump = (val) => {
2542
2551
  switch (typeof val) {
2543
2552
  case 'undefined': {
2544
- buffer[pos++] = $mol_vary_spec.both;
2553
+ this.array[pos++] = $mol_vary_spec.both;
2545
2554
  release(8);
2546
2555
  return;
2547
2556
  }
2548
2557
  case 'boolean': {
2549
- buffer[pos++] = val ? $mol_vary_spec.true : $mol_vary_spec.fake;
2558
+ this.array[pos++] = val ? $mol_vary_spec.true : $mol_vary_spec.fake;
2550
2559
  release(8);
2551
2560
  return;
2552
2561
  }
@@ -2567,7 +2576,7 @@ var $;
2567
2576
  case 'object': {
2568
2577
  if (!val) {
2569
2578
  release(8);
2570
- return buffer[pos++] = $mol_vary_spec.none;
2579
+ return this.array[pos++] = $mol_vary_spec.none;
2571
2580
  }
2572
2581
  if (ArrayBuffer.isView(val))
2573
2582
  return dump_buffer(val);
@@ -2581,10 +2590,10 @@ var $;
2581
2590
  dump(data);
2582
2591
  if (pos !== capacity)
2583
2592
  $mol_fail(new Error('Wrong reserved capacity', { cause: { capacity, size: pos, data } }));
2584
- return buffer.slice(0, pos);
2593
+ return this.array.slice(0, pos);
2585
2594
  }
2586
- static take(buffer) {
2587
- const pack = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
2595
+ take(array) {
2596
+ const buffer = new DataView(array.buffer, array.byteOffset, array.byteLength);
2588
2597
  const stream = [];
2589
2598
  let pos = 0;
2590
2599
  const read_unum = (kind) => {
@@ -2594,18 +2603,18 @@ var $;
2594
2603
  return num;
2595
2604
  let res = 0;
2596
2605
  if (num === $mol_vary_len.L1) {
2597
- res = pack.getUint8(pos++);
2606
+ res = buffer.getUint8(pos++);
2598
2607
  }
2599
2608
  else if (num === $mol_vary_len.L2) {
2600
- res = pack.getUint16(pos, true);
2609
+ res = buffer.getUint16(pos, true);
2601
2610
  pos += 2;
2602
2611
  }
2603
2612
  else if (num === $mol_vary_len.L4) {
2604
- res = pack.getUint32(pos, true);
2613
+ res = buffer.getUint32(pos, true);
2605
2614
  pos += 4;
2606
2615
  }
2607
2616
  else if (num === $mol_vary_len.L8) {
2608
- res = pack.getBigUint64(pos, true);
2617
+ res = buffer.getBigUint64(pos, true);
2609
2618
  if (res <= Number.MAX_SAFE_INTEGER)
2610
2619
  res = Number(res);
2611
2620
  pos += 8;
@@ -2618,30 +2627,30 @@ var $;
2618
2627
  return res;
2619
2628
  };
2620
2629
  const read_snum = (kind) => {
2621
- const num = pack.getInt8(pos++);
2630
+ const num = buffer.getInt8(pos++);
2622
2631
  if (num > -$mol_vary_len.L1)
2623
2632
  return num;
2624
2633
  let res = 0;
2625
2634
  if (num === -$mol_vary_len.L1) {
2626
- res = pack.getInt8(pos++);
2635
+ res = buffer.getInt8(pos++);
2627
2636
  }
2628
2637
  else if (num === -$mol_vary_len.L2) {
2629
- res = pack.getInt16(pos, true);
2638
+ res = buffer.getInt16(pos, true);
2630
2639
  pos += 2;
2631
2640
  }
2632
2641
  else if (num === -$mol_vary_len.L4) {
2633
- res = pack.getInt32(pos, true);
2642
+ res = buffer.getInt32(pos, true);
2634
2643
  pos += 4;
2635
2644
  }
2636
2645
  else if (num === -$mol_vary_len.L8) {
2637
- res = pack.getBigInt64(pos, true);
2646
+ res = buffer.getBigInt64(pos, true);
2638
2647
  if (res >= Number.MIN_SAFE_INTEGER && res <= Number.MAX_SAFE_INTEGER)
2639
2648
  res = Number(res);
2640
2649
  pos += 8;
2641
2650
  }
2642
2651
  else if (num === -$mol_vary_len.LA) {
2643
- const len = pack.getUint8(pos++) + 9;
2644
- res = $mol_bigint_decode(new Uint8Array(pack.buffer, pack.byteOffset + pos, len));
2652
+ const len = buffer.getUint8(pos++) + 9;
2653
+ res = $mol_bigint_decode(new Uint8Array(buffer.buffer, buffer.byteOffset + pos, len));
2645
2654
  pos += len;
2646
2655
  }
2647
2656
  else {
@@ -2652,20 +2661,20 @@ var $;
2652
2661
  };
2653
2662
  const read_text = (kind) => {
2654
2663
  const len = read_unum(kind);
2655
- const [text, bytes] = $mol_charset_decode_from(buffer, pack.byteOffset + pos, len);
2664
+ const [text, bytes] = $mol_charset_decode_from(array, buffer.byteOffset + pos, len);
2656
2665
  pos += bytes;
2657
2666
  stream.push(text);
2658
2667
  return text;
2659
2668
  };
2660
2669
  const read_buffer = (len, TypedArray) => {
2661
- const bin = new TypedArray(buffer.slice(pos, pos + len).buffer);
2670
+ const bin = new TypedArray(array.slice(pos, pos + len).buffer);
2662
2671
  pos += len;
2663
2672
  stream.push(bin);
2664
2673
  return bin;
2665
2674
  };
2666
2675
  const read_blob = (kind) => {
2667
2676
  const len = read_unum(kind);
2668
- const kind_item = pack.getUint8(pos++);
2677
+ const kind_item = buffer.getUint8(pos++);
2669
2678
  switch (kind_item) {
2670
2679
  case $mol_vary_len.L1: return read_buffer(len, Uint8Array);
2671
2680
  case $mol_vary_len.L2: return read_buffer(len, Uint16Array);
@@ -2705,13 +2714,8 @@ var $;
2705
2714
  const node = this.rich_node(keys);
2706
2715
  let rich = node.get(null);
2707
2716
  if (!rich)
2708
- node.set(null, rich = (...vals) => {
2709
- const obj = {};
2710
- for (let i = 0; i < len; ++i)
2711
- obj[keys[i]] = vals[i];
2712
- return obj;
2713
- });
2714
- const obj = rich(...vals);
2717
+ node.set(null, rich = pojo_maker(keys));
2718
+ const obj = rich(vals);
2715
2719
  stream.push(obj);
2716
2720
  return obj;
2717
2721
  };
@@ -2730,19 +2734,19 @@ var $;
2730
2734
  ++pos;
2731
2735
  return undefined;
2732
2736
  case $mol_vary_spec.fp64: {
2733
- const val = pack.getFloat64(++pos, true);
2737
+ const val = buffer.getFloat64(++pos, true);
2734
2738
  stream.push(val);
2735
2739
  pos += 8;
2736
2740
  return val;
2737
2741
  }
2738
2742
  case $mol_vary_spec.fp32: {
2739
- const val = pack.getFloat32(++pos, true);
2743
+ const val = buffer.getFloat32(++pos, true);
2740
2744
  stream.push(val);
2741
2745
  pos += 4;
2742
2746
  return val;
2743
2747
  }
2744
2748
  case $mol_vary_spec.fp16: {
2745
- const val = pack.getFloat16(++pos, true);
2749
+ const val = buffer.getFloat16(++pos, true);
2746
2750
  stream.push(val);
2747
2751
  pos += 2;
2748
2752
  return val;
@@ -2752,7 +2756,7 @@ var $;
2752
2756
  }
2753
2757
  };
2754
2758
  const read_vary = () => {
2755
- const kind = pack.getUint8(pos);
2759
+ const kind = buffer.getUint8(pos);
2756
2760
  const tip = kind & 0b111_00000;
2757
2761
  switch (tip) {
2758
2762
  case $mol_vary_tip.uint: return read_unum(kind);
@@ -2767,14 +2771,21 @@ var $;
2767
2771
  }
2768
2772
  };
2769
2773
  const result = read_vary();
2770
- if (pos !== buffer.byteLength)
2771
- $mol_fail(new Error('Buffer too large', { cause: { size: buffer.byteLength, taken: pos, result } }));
2774
+ if (pos !== array.byteLength)
2775
+ $mol_fail(new Error('Buffer too large', { cause: { size: array.byteLength, taken: pos, result } }));
2772
2776
  return result;
2773
2777
  }
2774
- static rich_index = new Map([
2778
+ rich_index = new Map([
2775
2779
  [null, () => ({})]
2776
2780
  ]);
2777
- static rich_node(keys) {
2781
+ room() {
2782
+ const room = new $mol_vary_class;
2783
+ Object.setPrototypeOf(room, this);
2784
+ const index_clone = (map) => new Map([...map].map(([k, v]) => [k, k === null ? v : index_clone(v)]));
2785
+ room.rich_index = index_clone(this.rich_index);
2786
+ return room;
2787
+ }
2788
+ rich_node(keys) {
2778
2789
  let node = this.rich_index;
2779
2790
  for (const key of keys) {
2780
2791
  let sub = node.get(key);
@@ -2785,33 +2796,72 @@ var $;
2785
2796
  }
2786
2797
  return node;
2787
2798
  }
2788
- static type(Class, keys, lean, rich) {
2799
+ lean_find(val) {
2800
+ const lean = val[this.lean_symbol];
2801
+ if (lean)
2802
+ return lean;
2803
+ const sup = Object.getPrototypeOf(this);
2804
+ if (sup === Object.prototype)
2805
+ return;
2806
+ return sup.lean_find(val);
2807
+ }
2808
+ type({ type, keys, rich, lean }) {
2789
2809
  this.rich_node(keys).set(null, rich);
2790
- Class.prototype[$.$mol_vary_lean] = (val) => [keys, lean(val)];
2810
+ type.prototype[this.lean_symbol] = (val) => [keys, lean(val)];
2791
2811
  }
2792
2812
  }
2793
- $.$mol_vary = $mol_vary;
2794
- $.$mol_vary_lean = Symbol.for('$mol_vary_lean');
2795
- $mol_vary.type(Map, ['keys', 'vals'], obj => [[...obj.keys()], [...obj.values()]], (keys, vals) => new Map(keys.map((k, i) => [k, vals[i]])));
2796
- $mol_vary.type(Set, ['set'], obj => [[...obj.values()]], vals => new Set(vals));
2797
- $mol_vary.type(Date, ['unix_time'], obj => [obj.valueOf() / 1000], ts => new Date(ts * 1000));
2798
- $mol_vary.type($mol_dom.Element, ['elem', 'keys', 'vals', 'kids'], node => {
2799
- const attrs = [...node.attributes];
2800
- const kids = [...node.childNodes].map(kid => kid instanceof $mol_dom.Text ? kid.nodeValue : kid);
2801
- return [node.nodeName, attrs.map(attr => attr.nodeName), attrs.map(attr => attr.nodeValue), kids];
2802
- }, (name, keys, vals, kids) => {
2803
- const el = $mol_dom.document.createElement(name);
2804
- for (let i = 0; i < keys.length; ++i)
2805
- el.setAttribute(keys[i], vals[i]);
2806
- for (let kid of kids) {
2807
- if (typeof kid === 'string')
2808
- kid = $mol_dom.document.createTextNode(kid);
2809
- el.appendChild(kid);
2810
- }
2811
- return el;
2813
+ $.$mol_vary_class = $mol_vary_class;
2814
+ $.$mol_vary = new $mol_vary_class;
2815
+ $.$mol_vary.type({
2816
+ type: Map,
2817
+ keys: ['keys', 'vals'],
2818
+ lean: obj => [[...obj.keys()], [...obj.values()]],
2819
+ rich: ([keys, vals]) => new Map(keys.map((k, i) => [k, vals[i]])),
2820
+ });
2821
+ $.$mol_vary.type({
2822
+ type: Set,
2823
+ keys: ['set'],
2824
+ lean: obj => [[...obj.values()]],
2825
+ rich: ([vals]) => new Set(vals),
2826
+ });
2827
+ $.$mol_vary.type({
2828
+ type: Date,
2829
+ keys: ['unix_time'],
2830
+ lean: obj => [obj.valueOf() / 1000],
2831
+ rich: ([ts]) => new Date(ts * 1000),
2832
+ });
2833
+ $.$mol_vary.type({
2834
+ type: $mol_dom.Element,
2835
+ keys: ['elem', 'keys', 'vals', 'kids'],
2836
+ lean: node => {
2837
+ const attrs = [...node.attributes];
2838
+ const kids = [...node.childNodes].map(kid => kid instanceof $mol_dom.Text ? kid.nodeValue : kid);
2839
+ return [node.nodeName, attrs.map(attr => attr.nodeName), attrs.map(attr => attr.nodeValue), kids];
2840
+ },
2841
+ rich: ([name, keys, vals, kids]) => {
2842
+ const el = $mol_dom.document.createElement(name);
2843
+ for (let i = 0; i < keys.length; ++i)
2844
+ el.setAttribute(keys[i], vals[i]);
2845
+ for (let kid of kids) {
2846
+ if (typeof kid === 'string')
2847
+ kid = $mol_dom.document.createTextNode(kid);
2848
+ el.appendChild(kid);
2849
+ }
2850
+ return el;
2851
+ },
2852
+ });
2853
+ $.$mol_vary.type({
2854
+ type: $mol_dom.Comment,
2855
+ keys: ['#comment'],
2856
+ lean: node => [node.nodeValue],
2857
+ rich: ([text]) => $mol_dom.document.createComment(text),
2858
+ });
2859
+ $.$mol_vary.type({
2860
+ type: $mol_dom.ProcessingInstruction,
2861
+ keys: ['target', 'text'],
2862
+ lean: node => [node.nodeName, node.nodeValue],
2863
+ rich: ([target, text]) => $mol_dom.document.createProcessingInstruction(target, text),
2812
2864
  });
2813
- $mol_vary.type($mol_dom.Comment, ['#comment'], node => [node.nodeValue], text => $mol_dom.document.createComment(text));
2814
- $mol_vary.type($mol_dom.ProcessingInstruction, ['target', 'text'], node => [node.nodeName, node.nodeValue], (target, text) => $mol_dom.document.createProcessingInstruction(target, text));
2815
2865
  })($ || ($ = {}));
2816
2866
 
2817
2867
  ;
@@ -4886,10 +4936,10 @@ var $;
4886
4936
  const { none, both, fp16, fp32, fp64 } = $mol_vary_spec;
4887
4937
  const { L1, L2, L4, L8, LA } = $mol_vary_len;
4888
4938
  const str = $mol_charset_encode;
4889
- function check(vary, ideal) {
4890
- const pack = $mol_vary.pack(vary);
4939
+ function check(vary, ideal, Vary = $mol_vary) {
4940
+ const pack = Vary.pack(vary);
4891
4941
  $mol_assert_equal(pack, new Uint8Array(ideal));
4892
- $mol_assert_equal($mol_vary.take(pack), vary);
4942
+ $mol_assert_equal(Vary.take(pack), vary);
4893
4943
  }
4894
4944
  $mol_test({
4895
4945
  "vary pack logical"($) {
@@ -5066,7 +5116,7 @@ var $;
5066
5116
  tupl | 2, list | 2, text | 6, ...str('target'), text | 4, ...str('text'), text | 3, ...str('foo'), text | 3, ...str('bar'),
5067
5117
  ]);
5068
5118
  },
5069
- "vary pack custom class"($) {
5119
+ "vary pack custom types in rooms"($) {
5070
5120
  class Foo {
5071
5121
  a;
5072
5122
  b;
@@ -5079,8 +5129,16 @@ var $;
5079
5129
  return [this.a, this.b].values();
5080
5130
  }
5081
5131
  }
5082
- $mol_vary.type(Foo, ['custom_a', 'custom_b'], foo => [foo.a, foo.b], (a = 0, b = 0) => new Foo(a, b));
5083
- check(new Foo(1, 2), [tupl | 2, list | 2, text | 8, ...str('custom_a'), text | 8, ...str('custom_b'), 1, 2]);
5132
+ const Vary = $mol_vary.room();
5133
+ Vary.type({
5134
+ type: Foo,
5135
+ keys: ['summ', 'diff'],
5136
+ lean: foo => [foo.a + foo.b, foo.a - foo.b],
5137
+ rich: ([summ, diff]) => new Foo((summ + diff) / 2, (summ - diff) / 2),
5138
+ });
5139
+ check(new Foo(4, 2), [tupl | 2, list | 2, text | 4, ...str('summ'), text | 4, ...str('diff'), 6, 2], Vary);
5140
+ $mol_assert_equal($mol_vary.take($mol_vary.pack(new Foo(4, 2))), { a: 4, b: 2 });
5141
+ $mol_assert_equal(Vary.take(Vary.pack(new Map([[1, 2]]))), new Map([[1, 2]]));
5084
5142
  },
5085
5143
  });
5086
5144
  })($$ = $_1.$$ || ($_1.$$ = {}));