mol_data_all 1.1.1479 → 1.1.1481

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
@@ -3705,6 +3705,32 @@ var $;
3705
3705
  $.$mol_tree2_to_string = $mol_tree2_to_string;
3706
3706
  })($ || ($ = {}));
3707
3707
 
3708
+ ;
3709
+ "use strict";
3710
+ var $;
3711
+ (function ($) {
3712
+ function $mol_maybe(value) {
3713
+ return (value == null) ? [] : [value];
3714
+ }
3715
+ $.$mol_maybe = $mol_maybe;
3716
+ })($ || ($ = {}));
3717
+
3718
+ ;
3719
+ "use strict";
3720
+ var $;
3721
+ (function ($) {
3722
+ $mol_test({
3723
+ 'all cases of using maybe'() {
3724
+ $mol_assert_equal($mol_maybe(0)[0], 0);
3725
+ $mol_assert_equal($mol_maybe(false)[0], false);
3726
+ $mol_assert_equal($mol_maybe(null)[0], void 0);
3727
+ $mol_assert_equal($mol_maybe(void 0)[0], void 0);
3728
+ $mol_assert_equal($mol_maybe(void 0).map(v => v.toString())[0], void 0);
3729
+ $mol_assert_equal($mol_maybe(0).map(v => v.toString())[0], '0');
3730
+ },
3731
+ });
3732
+ })($ || ($ = {}));
3733
+
3708
3734
  ;
3709
3735
  "use strict";
3710
3736
  var $;
@@ -3772,33 +3798,38 @@ var $;
3772
3798
  return $$.$mol_tree2_to_string(this);
3773
3799
  }
3774
3800
  insert(value, ...path) {
3801
+ return this.update($mol_maybe(value), ...path)[0];
3802
+ }
3803
+ update(value, ...path) {
3775
3804
  if (path.length === 0)
3776
3805
  return value;
3777
3806
  const type = path[0];
3778
3807
  if (typeof type === 'string') {
3779
3808
  let replaced = false;
3780
- const sub = this.kids.map((item, index) => {
3809
+ const sub = this.kids.flatMap((item, index) => {
3781
3810
  if (item.type !== type)
3782
3811
  return item;
3783
3812
  replaced = true;
3784
- return item.insert(value, ...path.slice(1));
3813
+ return item.update(value, ...path.slice(1));
3785
3814
  }).filter(Boolean);
3786
3815
  if (!replaced && value) {
3787
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
3816
+ sub.push(...this.struct(type, []).update(value, ...path.slice(1)));
3788
3817
  }
3789
- return this.clone(sub);
3818
+ return [this.clone(sub)];
3790
3819
  }
3791
3820
  else if (typeof type === 'number') {
3792
- const sub = this.kids.slice();
3793
- sub[type] = (sub[type] || this.list([]))
3794
- .insert(value, ...path.slice(1));
3795
- return this.clone(sub.filter(Boolean));
3821
+ const ins = (this.kids[type] || this.list([]))
3822
+ .update(value, ...path.slice(1));
3823
+ return [this.clone([
3824
+ ...this.kids.slice(0, type),
3825
+ ...ins,
3826
+ ...this.kids.slice(type + 1),
3827
+ ])];
3796
3828
  }
3797
3829
  else {
3798
3830
  const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
3799
- .map(item => item.insert(value, ...path.slice(1)))
3800
- .filter(Boolean);
3801
- return this.clone(kids);
3831
+ .flatMap(item => item.update(value, ...path.slice(1)));
3832
+ return [this.clone(kids)];
3802
3833
  }
3803
3834
  }
3804
3835
  select(...path) {
@@ -3874,37 +3905,85 @@ var $;
3874
3905
  "use strict";
3875
3906
  var $;
3876
3907
  (function ($_1) {
3908
+ function check(tree, ideal) {
3909
+ $mol_assert_equal(tree.toString(), $$.$mol_tree2_from_string(ideal).toString());
3910
+ }
3877
3911
  $mol_test({
3878
3912
  'inserting'($) {
3879
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3880
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
3881
- .toString(), 'a b x\n');
3882
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
3883
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
3884
- .toString(), 'a b c x\n');
3885
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3886
- .insert($mol_tree2.struct('x'), 0, 0, 0)
3887
- .toString(), 'a b x\n');
3888
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
3889
- .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
3890
- .toString(), 'a b \\\n\tx\n');
3891
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3892
- .insert($mol_tree2.struct('x'), null, null, null)
3893
- .toString(), 'a b x\n');
3894
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
3895
- .insert($mol_tree2.struct('x'), null, null, null, null)
3896
- .toString(), 'a b \\\n\tx\n');
3913
+ check($.$mol_tree2_from_string(`
3914
+ a b c d
3915
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c'), `
3916
+ a b x
3917
+ `);
3918
+ check($.$mol_tree2_from_string(`
3919
+ a b
3920
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd'), `
3921
+ a b c x
3922
+ `);
3923
+ check($.$mol_tree2_from_string(`
3924
+ a b c d
3925
+ `)
3926
+ .insert($mol_tree2.struct('x'), 0, 0, 0), `
3927
+ a b x
3928
+ `);
3929
+ check($.$mol_tree2_from_string(`
3930
+ a b
3931
+ `)
3932
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0), `
3933
+ a b \\
3934
+ x
3935
+ `);
3936
+ check($.$mol_tree2_from_string(`
3937
+ a b c d
3938
+ `)
3939
+ .insert($mol_tree2.struct('x'), null, null, null), `
3940
+ a b x
3941
+ `);
3942
+ check($.$mol_tree2_from_string(`
3943
+ a b
3944
+ `)
3945
+ .insert($mol_tree2.struct('x'), null, null, null, null), `
3946
+ a b \\
3947
+ x
3948
+ `);
3949
+ },
3950
+ 'updating'($) {
3951
+ check($.$mol_tree2_from_string(`
3952
+ a b c d
3953
+ `).update([], 'a', 'b', 'c')[0], `
3954
+ a b
3955
+ `);
3956
+ check($.$mol_tree2_from_string(`
3957
+ a b c d
3958
+ `).update([$mol_tree2.struct('x')])[0], `
3959
+ x
3960
+ `);
3961
+ check($.$mol_tree2_from_string(`
3962
+ a b c d
3963
+ `).update([$mol_tree2.struct('x'), $mol_tree2.struct('y')], 'a', 'b', 'c')[0], `
3964
+ a b
3965
+ x
3966
+ y
3967
+ `);
3897
3968
  },
3898
3969
  'deleting'($) {
3899
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3900
- .insert(null, 'a', 'b', 'c')
3901
- .toString(), 'a b\n');
3902
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3903
- .insert(null, 0, 0, 0)
3904
- .toString(), 'a b\n');
3970
+ const base = $.$mol_tree2_from_string(`
3971
+ a b c d
3972
+ `);
3973
+ check(base.insert(null, 'a', 'b', 'c'), `
3974
+ a b
3975
+ `);
3976
+ check(base.update(base.select('a', 'b', 'c', null).kids, 'a', 'b', 'c')[0], `
3977
+ a b d
3978
+ `);
3979
+ check(base.insert(null, 0, 0, 0), `
3980
+ a b
3981
+ `);
3905
3982
  },
3906
3983
  'hack'($) {
3907
- const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
3984
+ const res = $.$mol_tree2_from_string(`
3985
+ foo bar xxx
3986
+ `)
3908
3987
  .hack({
3909
3988
  'bar': (input, belt) => [input.struct('777', input.hack(belt))],
3910
3989
  });