mam 1.11.607 → 1.11.608

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.mjs CHANGED
@@ -1123,6 +1123,16 @@ var $;
1123
1123
  $.$mol_tree2_to_string = $mol_tree2_to_string;
1124
1124
  })($ || ($ = {}));
1125
1125
 
1126
+ ;
1127
+ "use strict";
1128
+ var $;
1129
+ (function ($) {
1130
+ function $mol_maybe(value) {
1131
+ return (value == null) ? [] : [value];
1132
+ }
1133
+ $.$mol_maybe = $mol_maybe;
1134
+ })($ || ($ = {}));
1135
+
1126
1136
  ;
1127
1137
  "use strict";
1128
1138
  var $;
@@ -1190,33 +1200,38 @@ var $;
1190
1200
  return $$.$mol_tree2_to_string(this);
1191
1201
  }
1192
1202
  insert(value, ...path) {
1203
+ return this.update($mol_maybe(value), ...path)[0];
1204
+ }
1205
+ update(value, ...path) {
1193
1206
  if (path.length === 0)
1194
1207
  return value;
1195
1208
  const type = path[0];
1196
1209
  if (typeof type === 'string') {
1197
1210
  let replaced = false;
1198
- const sub = this.kids.map((item, index) => {
1211
+ const sub = this.kids.flatMap((item, index) => {
1199
1212
  if (item.type !== type)
1200
1213
  return item;
1201
1214
  replaced = true;
1202
- return item.insert(value, ...path.slice(1));
1215
+ return item.update(value, ...path.slice(1));
1203
1216
  }).filter(Boolean);
1204
1217
  if (!replaced && value) {
1205
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
1218
+ sub.push(...this.struct(type, []).update(value, ...path.slice(1)));
1206
1219
  }
1207
- return this.clone(sub);
1220
+ return [this.clone(sub)];
1208
1221
  }
1209
1222
  else if (typeof type === 'number') {
1210
- const sub = this.kids.slice();
1211
- sub[type] = (sub[type] || this.list([]))
1212
- .insert(value, ...path.slice(1));
1213
- return this.clone(sub.filter(Boolean));
1223
+ const ins = (this.kids[type] || this.list([]))
1224
+ .update(value, ...path.slice(1));
1225
+ return [this.clone([
1226
+ ...this.kids.slice(0, type),
1227
+ ...ins,
1228
+ ...this.kids.slice(type + 1),
1229
+ ])];
1214
1230
  }
1215
1231
  else {
1216
1232
  const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
1217
- .map(item => item.insert(value, ...path.slice(1)))
1218
- .filter(Boolean);
1219
- return this.clone(kids);
1233
+ .flatMap(item => item.update(value, ...path.slice(1)));
1234
+ return [this.clone(kids)];
1220
1235
  }
1221
1236
  }
1222
1237
  select(...path) {
package/node.test.js CHANGED
@@ -1114,6 +1114,16 @@ var $;
1114
1114
  $.$mol_tree2_to_string = $mol_tree2_to_string;
1115
1115
  })($ || ($ = {}));
1116
1116
 
1117
+ ;
1118
+ "use strict";
1119
+ var $;
1120
+ (function ($) {
1121
+ function $mol_maybe(value) {
1122
+ return (value == null) ? [] : [value];
1123
+ }
1124
+ $.$mol_maybe = $mol_maybe;
1125
+ })($ || ($ = {}));
1126
+
1117
1127
  ;
1118
1128
  "use strict";
1119
1129
  var $;
@@ -1181,33 +1191,38 @@ var $;
1181
1191
  return $$.$mol_tree2_to_string(this);
1182
1192
  }
1183
1193
  insert(value, ...path) {
1194
+ return this.update($mol_maybe(value), ...path)[0];
1195
+ }
1196
+ update(value, ...path) {
1184
1197
  if (path.length === 0)
1185
1198
  return value;
1186
1199
  const type = path[0];
1187
1200
  if (typeof type === 'string') {
1188
1201
  let replaced = false;
1189
- const sub = this.kids.map((item, index) => {
1202
+ const sub = this.kids.flatMap((item, index) => {
1190
1203
  if (item.type !== type)
1191
1204
  return item;
1192
1205
  replaced = true;
1193
- return item.insert(value, ...path.slice(1));
1206
+ return item.update(value, ...path.slice(1));
1194
1207
  }).filter(Boolean);
1195
1208
  if (!replaced && value) {
1196
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
1209
+ sub.push(...this.struct(type, []).update(value, ...path.slice(1)));
1197
1210
  }
1198
- return this.clone(sub);
1211
+ return [this.clone(sub)];
1199
1212
  }
1200
1213
  else if (typeof type === 'number') {
1201
- const sub = this.kids.slice();
1202
- sub[type] = (sub[type] || this.list([]))
1203
- .insert(value, ...path.slice(1));
1204
- return this.clone(sub.filter(Boolean));
1214
+ const ins = (this.kids[type] || this.list([]))
1215
+ .update(value, ...path.slice(1));
1216
+ return [this.clone([
1217
+ ...this.kids.slice(0, type),
1218
+ ...ins,
1219
+ ...this.kids.slice(type + 1),
1220
+ ])];
1205
1221
  }
1206
1222
  else {
1207
1223
  const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
1208
- .map(item => item.insert(value, ...path.slice(1)))
1209
- .filter(Boolean);
1210
- return this.clone(kids);
1224
+ .flatMap(item => item.update(value, ...path.slice(1)));
1225
+ return [this.clone(kids)];
1211
1226
  }
1212
1227
  }
1213
1228
  select(...path) {
@@ -8894,41 +8909,105 @@ var $;
8894
8909
  });
8895
8910
  })($ || ($ = {}));
8896
8911
 
8912
+ ;
8913
+ "use strict";
8914
+ var $;
8915
+ (function ($) {
8916
+ $mol_test({
8917
+ 'all cases of using maybe'() {
8918
+ $mol_assert_equal($mol_maybe(0)[0], 0);
8919
+ $mol_assert_equal($mol_maybe(false)[0], false);
8920
+ $mol_assert_equal($mol_maybe(null)[0], void 0);
8921
+ $mol_assert_equal($mol_maybe(void 0)[0], void 0);
8922
+ $mol_assert_equal($mol_maybe(void 0).map(v => v.toString())[0], void 0);
8923
+ $mol_assert_equal($mol_maybe(0).map(v => v.toString())[0], '0');
8924
+ },
8925
+ });
8926
+ })($ || ($ = {}));
8927
+
8897
8928
  ;
8898
8929
  "use strict";
8899
8930
  var $;
8900
8931
  (function ($_1) {
8932
+ function check(tree, ideal) {
8933
+ $mol_assert_equal(tree.toString(), $$.$mol_tree2_from_string(ideal).toString());
8934
+ }
8901
8935
  $mol_test({
8902
8936
  'inserting'($) {
8903
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
8904
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
8905
- .toString(), 'a b x\n');
8906
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
8907
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
8908
- .toString(), 'a b c x\n');
8909
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
8910
- .insert($mol_tree2.struct('x'), 0, 0, 0)
8911
- .toString(), 'a b x\n');
8912
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
8913
- .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
8914
- .toString(), 'a b \\\n\tx\n');
8915
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
8916
- .insert($mol_tree2.struct('x'), null, null, null)
8917
- .toString(), 'a b x\n');
8918
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
8919
- .insert($mol_tree2.struct('x'), null, null, null, null)
8920
- .toString(), 'a b \\\n\tx\n');
8937
+ check($.$mol_tree2_from_string(`
8938
+ a b c d
8939
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c'), `
8940
+ a b x
8941
+ `);
8942
+ check($.$mol_tree2_from_string(`
8943
+ a b
8944
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd'), `
8945
+ a b c x
8946
+ `);
8947
+ check($.$mol_tree2_from_string(`
8948
+ a b c d
8949
+ `)
8950
+ .insert($mol_tree2.struct('x'), 0, 0, 0), `
8951
+ a b x
8952
+ `);
8953
+ check($.$mol_tree2_from_string(`
8954
+ a b
8955
+ `)
8956
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0), `
8957
+ a b \\
8958
+ x
8959
+ `);
8960
+ check($.$mol_tree2_from_string(`
8961
+ a b c d
8962
+ `)
8963
+ .insert($mol_tree2.struct('x'), null, null, null), `
8964
+ a b x
8965
+ `);
8966
+ check($.$mol_tree2_from_string(`
8967
+ a b
8968
+ `)
8969
+ .insert($mol_tree2.struct('x'), null, null, null, null), `
8970
+ a b \\
8971
+ x
8972
+ `);
8973
+ },
8974
+ 'updating'($) {
8975
+ check($.$mol_tree2_from_string(`
8976
+ a b c d
8977
+ `).update([], 'a', 'b', 'c')[0], `
8978
+ a b
8979
+ `);
8980
+ check($.$mol_tree2_from_string(`
8981
+ a b c d
8982
+ `).update([$mol_tree2.struct('x')])[0], `
8983
+ x
8984
+ `);
8985
+ check($.$mol_tree2_from_string(`
8986
+ a b c d
8987
+ `).update([$mol_tree2.struct('x'), $mol_tree2.struct('y')], 'a', 'b', 'c')[0], `
8988
+ a b
8989
+ x
8990
+ y
8991
+ `);
8921
8992
  },
8922
8993
  'deleting'($) {
8923
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
8924
- .insert(null, 'a', 'b', 'c')
8925
- .toString(), 'a b\n');
8926
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
8927
- .insert(null, 0, 0, 0)
8928
- .toString(), 'a b\n');
8994
+ const base = $.$mol_tree2_from_string(`
8995
+ a b c d
8996
+ `);
8997
+ check(base.insert(null, 'a', 'b', 'c'), `
8998
+ a b
8999
+ `);
9000
+ check(base.update(base.select('a', 'b', 'c', null).kids, 'a', 'b', 'c')[0], `
9001
+ a b d
9002
+ `);
9003
+ check(base.insert(null, 0, 0, 0), `
9004
+ a b
9005
+ `);
8929
9006
  },
8930
9007
  'hack'($) {
8931
- const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
9008
+ const res = $.$mol_tree2_from_string(`
9009
+ foo bar xxx
9010
+ `)
8932
9011
  .hack({
8933
9012
  'bar': (input, belt) => [input.struct('777', input.hack(belt))],
8934
9013
  });