mol_mutable 0.0.913 → 0.0.915

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
@@ -3384,6 +3384,32 @@ var $;
3384
3384
  $.$mol_tree2_to_string = $mol_tree2_to_string;
3385
3385
  })($ || ($ = {}));
3386
3386
 
3387
+ ;
3388
+ "use strict";
3389
+ var $;
3390
+ (function ($) {
3391
+ function $mol_maybe(value) {
3392
+ return (value == null) ? [] : [value];
3393
+ }
3394
+ $.$mol_maybe = $mol_maybe;
3395
+ })($ || ($ = {}));
3396
+
3397
+ ;
3398
+ "use strict";
3399
+ var $;
3400
+ (function ($) {
3401
+ $mol_test({
3402
+ 'all cases of using maybe'() {
3403
+ $mol_assert_equal($mol_maybe(0)[0], 0);
3404
+ $mol_assert_equal($mol_maybe(false)[0], false);
3405
+ $mol_assert_equal($mol_maybe(null)[0], void 0);
3406
+ $mol_assert_equal($mol_maybe(void 0)[0], void 0);
3407
+ $mol_assert_equal($mol_maybe(void 0).map(v => v.toString())[0], void 0);
3408
+ $mol_assert_equal($mol_maybe(0).map(v => v.toString())[0], '0');
3409
+ },
3410
+ });
3411
+ })($ || ($ = {}));
3412
+
3387
3413
  ;
3388
3414
  "use strict";
3389
3415
  var $;
@@ -3451,33 +3477,38 @@ var $;
3451
3477
  return $$.$mol_tree2_to_string(this);
3452
3478
  }
3453
3479
  insert(value, ...path) {
3480
+ return this.update($mol_maybe(value), ...path)[0];
3481
+ }
3482
+ update(value, ...path) {
3454
3483
  if (path.length === 0)
3455
3484
  return value;
3456
3485
  const type = path[0];
3457
3486
  if (typeof type === 'string') {
3458
3487
  let replaced = false;
3459
- const sub = this.kids.map((item, index) => {
3488
+ const sub = this.kids.flatMap((item, index) => {
3460
3489
  if (item.type !== type)
3461
3490
  return item;
3462
3491
  replaced = true;
3463
- return item.insert(value, ...path.slice(1));
3492
+ return item.update(value, ...path.slice(1));
3464
3493
  }).filter(Boolean);
3465
3494
  if (!replaced && value) {
3466
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
3495
+ sub.push(...this.struct(type, []).update(value, ...path.slice(1)));
3467
3496
  }
3468
- return this.clone(sub);
3497
+ return [this.clone(sub)];
3469
3498
  }
3470
3499
  else if (typeof type === 'number') {
3471
- const sub = this.kids.slice();
3472
- sub[type] = (sub[type] || this.list([]))
3473
- .insert(value, ...path.slice(1));
3474
- return this.clone(sub.filter(Boolean));
3500
+ const ins = (this.kids[type] || this.list([]))
3501
+ .update(value, ...path.slice(1));
3502
+ return [this.clone([
3503
+ ...this.kids.slice(0, type),
3504
+ ...ins,
3505
+ ...this.kids.slice(type + 1),
3506
+ ])];
3475
3507
  }
3476
3508
  else {
3477
3509
  const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
3478
- .map(item => item.insert(value, ...path.slice(1)))
3479
- .filter(Boolean);
3480
- return this.clone(kids);
3510
+ .flatMap(item => item.update(value, ...path.slice(1)));
3511
+ return [this.clone(kids)];
3481
3512
  }
3482
3513
  }
3483
3514
  select(...path) {
@@ -3553,37 +3584,85 @@ var $;
3553
3584
  "use strict";
3554
3585
  var $;
3555
3586
  (function ($_1) {
3587
+ function check(tree, ideal) {
3588
+ $mol_assert_equal(tree.toString(), $$.$mol_tree2_from_string(ideal).toString());
3589
+ }
3556
3590
  $mol_test({
3557
3591
  'inserting'($) {
3558
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3559
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
3560
- .toString(), 'a b x\n');
3561
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
3562
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
3563
- .toString(), 'a b c x\n');
3564
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3565
- .insert($mol_tree2.struct('x'), 0, 0, 0)
3566
- .toString(), 'a b x\n');
3567
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
3568
- .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
3569
- .toString(), 'a b \\\n\tx\n');
3570
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3571
- .insert($mol_tree2.struct('x'), null, null, null)
3572
- .toString(), 'a b x\n');
3573
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
3574
- .insert($mol_tree2.struct('x'), null, null, null, null)
3575
- .toString(), 'a b \\\n\tx\n');
3592
+ check($.$mol_tree2_from_string(`
3593
+ a b c d
3594
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c'), `
3595
+ a b x
3596
+ `);
3597
+ check($.$mol_tree2_from_string(`
3598
+ a b
3599
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd'), `
3600
+ a b c x
3601
+ `);
3602
+ check($.$mol_tree2_from_string(`
3603
+ a b c d
3604
+ `)
3605
+ .insert($mol_tree2.struct('x'), 0, 0, 0), `
3606
+ a b x
3607
+ `);
3608
+ check($.$mol_tree2_from_string(`
3609
+ a b
3610
+ `)
3611
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0), `
3612
+ a b \\
3613
+ x
3614
+ `);
3615
+ check($.$mol_tree2_from_string(`
3616
+ a b c d
3617
+ `)
3618
+ .insert($mol_tree2.struct('x'), null, null, null), `
3619
+ a b x
3620
+ `);
3621
+ check($.$mol_tree2_from_string(`
3622
+ a b
3623
+ `)
3624
+ .insert($mol_tree2.struct('x'), null, null, null, null), `
3625
+ a b \\
3626
+ x
3627
+ `);
3628
+ },
3629
+ 'updating'($) {
3630
+ check($.$mol_tree2_from_string(`
3631
+ a b c d
3632
+ `).update([], 'a', 'b', 'c')[0], `
3633
+ a b
3634
+ `);
3635
+ check($.$mol_tree2_from_string(`
3636
+ a b c d
3637
+ `).update([$mol_tree2.struct('x')])[0], `
3638
+ x
3639
+ `);
3640
+ check($.$mol_tree2_from_string(`
3641
+ a b c d
3642
+ `).update([$mol_tree2.struct('x'), $mol_tree2.struct('y')], 'a', 'b', 'c')[0], `
3643
+ a b
3644
+ x
3645
+ y
3646
+ `);
3576
3647
  },
3577
3648
  'deleting'($) {
3578
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3579
- .insert(null, 'a', 'b', 'c')
3580
- .toString(), 'a b\n');
3581
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3582
- .insert(null, 0, 0, 0)
3583
- .toString(), 'a b\n');
3649
+ const base = $.$mol_tree2_from_string(`
3650
+ a b c d
3651
+ `);
3652
+ check(base.insert(null, 'a', 'b', 'c'), `
3653
+ a b
3654
+ `);
3655
+ check(base.update(base.select('a', 'b', 'c', null).kids, 'a', 'b', 'c')[0], `
3656
+ a b d
3657
+ `);
3658
+ check(base.insert(null, 0, 0, 0), `
3659
+ a b
3660
+ `);
3584
3661
  },
3585
3662
  'hack'($) {
3586
- const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
3663
+ const res = $.$mol_tree2_from_string(`
3664
+ foo bar xxx
3665
+ `)
3587
3666
  .hack({
3588
3667
  'bar': (input, belt) => [input.struct('777', input.hack(belt))],
3589
3668
  });