mol_text_distance 0.0.1184 → 0.0.1185

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
@@ -3407,6 +3407,32 @@ var $;
3407
3407
  $.$mol_tree2_to_string = $mol_tree2_to_string;
3408
3408
  })($ || ($ = {}));
3409
3409
 
3410
+ ;
3411
+ "use strict";
3412
+ var $;
3413
+ (function ($) {
3414
+ function $mol_maybe(value) {
3415
+ return (value == null) ? [] : [value];
3416
+ }
3417
+ $.$mol_maybe = $mol_maybe;
3418
+ })($ || ($ = {}));
3419
+
3420
+ ;
3421
+ "use strict";
3422
+ var $;
3423
+ (function ($) {
3424
+ $mol_test({
3425
+ 'all cases of using maybe'() {
3426
+ $mol_assert_equal($mol_maybe(0)[0], 0);
3427
+ $mol_assert_equal($mol_maybe(false)[0], false);
3428
+ $mol_assert_equal($mol_maybe(null)[0], void 0);
3429
+ $mol_assert_equal($mol_maybe(void 0)[0], void 0);
3430
+ $mol_assert_equal($mol_maybe(void 0).map(v => v.toString())[0], void 0);
3431
+ $mol_assert_equal($mol_maybe(0).map(v => v.toString())[0], '0');
3432
+ },
3433
+ });
3434
+ })($ || ($ = {}));
3435
+
3410
3436
  ;
3411
3437
  "use strict";
3412
3438
  var $;
@@ -3474,33 +3500,38 @@ var $;
3474
3500
  return $$.$mol_tree2_to_string(this);
3475
3501
  }
3476
3502
  insert(value, ...path) {
3503
+ return this.update($mol_maybe(value), ...path)[0];
3504
+ }
3505
+ update(value, ...path) {
3477
3506
  if (path.length === 0)
3478
3507
  return value;
3479
3508
  const type = path[0];
3480
3509
  if (typeof type === 'string') {
3481
3510
  let replaced = false;
3482
- const sub = this.kids.map((item, index) => {
3511
+ const sub = this.kids.flatMap((item, index) => {
3483
3512
  if (item.type !== type)
3484
3513
  return item;
3485
3514
  replaced = true;
3486
- return item.insert(value, ...path.slice(1));
3515
+ return item.update(value, ...path.slice(1));
3487
3516
  }).filter(Boolean);
3488
3517
  if (!replaced && value) {
3489
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
3518
+ sub.push(...this.struct(type, []).update(value, ...path.slice(1)));
3490
3519
  }
3491
- return this.clone(sub);
3520
+ return [this.clone(sub)];
3492
3521
  }
3493
3522
  else if (typeof type === 'number') {
3494
- const sub = this.kids.slice();
3495
- sub[type] = (sub[type] || this.list([]))
3496
- .insert(value, ...path.slice(1));
3497
- return this.clone(sub.filter(Boolean));
3523
+ const ins = (this.kids[type] || this.list([]))
3524
+ .update(value, ...path.slice(1));
3525
+ return [this.clone([
3526
+ ...this.kids.slice(0, type),
3527
+ ...ins,
3528
+ ...this.kids.slice(type + 1),
3529
+ ])];
3498
3530
  }
3499
3531
  else {
3500
3532
  const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
3501
- .map(item => item.insert(value, ...path.slice(1)))
3502
- .filter(Boolean);
3503
- return this.clone(kids);
3533
+ .flatMap(item => item.update(value, ...path.slice(1)));
3534
+ return [this.clone(kids)];
3504
3535
  }
3505
3536
  }
3506
3537
  select(...path) {
@@ -3576,37 +3607,85 @@ var $;
3576
3607
  "use strict";
3577
3608
  var $;
3578
3609
  (function ($_1) {
3610
+ function check(tree, ideal) {
3611
+ $mol_assert_equal(tree.toString(), $$.$mol_tree2_from_string(ideal).toString());
3612
+ }
3579
3613
  $mol_test({
3580
3614
  'inserting'($) {
3581
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3582
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
3583
- .toString(), 'a b x\n');
3584
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
3585
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
3586
- .toString(), 'a b c x\n');
3587
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3588
- .insert($mol_tree2.struct('x'), 0, 0, 0)
3589
- .toString(), 'a b x\n');
3590
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
3591
- .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
3592
- .toString(), 'a b \\\n\tx\n');
3593
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3594
- .insert($mol_tree2.struct('x'), null, null, null)
3595
- .toString(), 'a b x\n');
3596
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
3597
- .insert($mol_tree2.struct('x'), null, null, null, null)
3598
- .toString(), 'a b \\\n\tx\n');
3615
+ check($.$mol_tree2_from_string(`
3616
+ a b c d
3617
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c'), `
3618
+ a b x
3619
+ `);
3620
+ check($.$mol_tree2_from_string(`
3621
+ a b
3622
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd'), `
3623
+ a b c x
3624
+ `);
3625
+ check($.$mol_tree2_from_string(`
3626
+ a b c d
3627
+ `)
3628
+ .insert($mol_tree2.struct('x'), 0, 0, 0), `
3629
+ a b x
3630
+ `);
3631
+ check($.$mol_tree2_from_string(`
3632
+ a b
3633
+ `)
3634
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0), `
3635
+ a b \\
3636
+ x
3637
+ `);
3638
+ check($.$mol_tree2_from_string(`
3639
+ a b c d
3640
+ `)
3641
+ .insert($mol_tree2.struct('x'), null, null, null), `
3642
+ a b x
3643
+ `);
3644
+ check($.$mol_tree2_from_string(`
3645
+ a b
3646
+ `)
3647
+ .insert($mol_tree2.struct('x'), null, null, null, null), `
3648
+ a b \\
3649
+ x
3650
+ `);
3651
+ },
3652
+ 'updating'($) {
3653
+ check($.$mol_tree2_from_string(`
3654
+ a b c d
3655
+ `).update([], 'a', 'b', 'c')[0], `
3656
+ a b
3657
+ `);
3658
+ check($.$mol_tree2_from_string(`
3659
+ a b c d
3660
+ `).update([$mol_tree2.struct('x')])[0], `
3661
+ x
3662
+ `);
3663
+ check($.$mol_tree2_from_string(`
3664
+ a b c d
3665
+ `).update([$mol_tree2.struct('x'), $mol_tree2.struct('y')], 'a', 'b', 'c')[0], `
3666
+ a b
3667
+ x
3668
+ y
3669
+ `);
3599
3670
  },
3600
3671
  'deleting'($) {
3601
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3602
- .insert(null, 'a', 'b', 'c')
3603
- .toString(), 'a b\n');
3604
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3605
- .insert(null, 0, 0, 0)
3606
- .toString(), 'a b\n');
3672
+ const base = $.$mol_tree2_from_string(`
3673
+ a b c d
3674
+ `);
3675
+ check(base.insert(null, 'a', 'b', 'c'), `
3676
+ a b
3677
+ `);
3678
+ check(base.update(base.select('a', 'b', 'c', null).kids, 'a', 'b', 'c')[0], `
3679
+ a b d
3680
+ `);
3681
+ check(base.insert(null, 0, 0, 0), `
3682
+ a b
3683
+ `);
3607
3684
  },
3608
3685
  'hack'($) {
3609
- const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
3686
+ const res = $.$mol_tree2_from_string(`
3687
+ foo bar xxx
3688
+ `)
3610
3689
  .hack({
3611
3690
  'bar': (input, belt) => [input.struct('777', input.hack(belt))],
3612
3691
  });