mol_wire_lib 1.0.1422 → 1.0.1424

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) {
@@ -4055,41 +4070,105 @@ var $;
4055
4070
  });
4056
4071
  })($ || ($ = {}));
4057
4072
 
4073
+ ;
4074
+ "use strict";
4075
+ var $;
4076
+ (function ($) {
4077
+ $mol_test({
4078
+ 'all cases of using maybe'() {
4079
+ $mol_assert_equal($mol_maybe(0)[0], 0);
4080
+ $mol_assert_equal($mol_maybe(false)[0], false);
4081
+ $mol_assert_equal($mol_maybe(null)[0], void 0);
4082
+ $mol_assert_equal($mol_maybe(void 0)[0], void 0);
4083
+ $mol_assert_equal($mol_maybe(void 0).map(v => v.toString())[0], void 0);
4084
+ $mol_assert_equal($mol_maybe(0).map(v => v.toString())[0], '0');
4085
+ },
4086
+ });
4087
+ })($ || ($ = {}));
4088
+
4058
4089
  ;
4059
4090
  "use strict";
4060
4091
  var $;
4061
4092
  (function ($_1) {
4093
+ function check(tree, ideal) {
4094
+ $mol_assert_equal(tree.toString(), $$.$mol_tree2_from_string(ideal).toString());
4095
+ }
4062
4096
  $mol_test({
4063
4097
  'inserting'($) {
4064
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
4065
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
4066
- .toString(), 'a b x\n');
4067
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
4068
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
4069
- .toString(), 'a b c x\n');
4070
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
4071
- .insert($mol_tree2.struct('x'), 0, 0, 0)
4072
- .toString(), 'a b x\n');
4073
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
4074
- .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
4075
- .toString(), 'a b \\\n\tx\n');
4076
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
4077
- .insert($mol_tree2.struct('x'), null, null, null)
4078
- .toString(), 'a b x\n');
4079
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
4080
- .insert($mol_tree2.struct('x'), null, null, null, null)
4081
- .toString(), 'a b \\\n\tx\n');
4098
+ check($.$mol_tree2_from_string(`
4099
+ a b c d
4100
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c'), `
4101
+ a b x
4102
+ `);
4103
+ check($.$mol_tree2_from_string(`
4104
+ a b
4105
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd'), `
4106
+ a b c x
4107
+ `);
4108
+ check($.$mol_tree2_from_string(`
4109
+ a b c d
4110
+ `)
4111
+ .insert($mol_tree2.struct('x'), 0, 0, 0), `
4112
+ a b x
4113
+ `);
4114
+ check($.$mol_tree2_from_string(`
4115
+ a b
4116
+ `)
4117
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0), `
4118
+ a b \\
4119
+ x
4120
+ `);
4121
+ check($.$mol_tree2_from_string(`
4122
+ a b c d
4123
+ `)
4124
+ .insert($mol_tree2.struct('x'), null, null, null), `
4125
+ a b x
4126
+ `);
4127
+ check($.$mol_tree2_from_string(`
4128
+ a b
4129
+ `)
4130
+ .insert($mol_tree2.struct('x'), null, null, null, null), `
4131
+ a b \\
4132
+ x
4133
+ `);
4134
+ },
4135
+ 'updating'($) {
4136
+ check($.$mol_tree2_from_string(`
4137
+ a b c d
4138
+ `).update([], 'a', 'b', 'c')[0], `
4139
+ a b
4140
+ `);
4141
+ check($.$mol_tree2_from_string(`
4142
+ a b c d
4143
+ `).update([$mol_tree2.struct('x')])[0], `
4144
+ x
4145
+ `);
4146
+ check($.$mol_tree2_from_string(`
4147
+ a b c d
4148
+ `).update([$mol_tree2.struct('x'), $mol_tree2.struct('y')], 'a', 'b', 'c')[0], `
4149
+ a b
4150
+ x
4151
+ y
4152
+ `);
4082
4153
  },
4083
4154
  'deleting'($) {
4084
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
4085
- .insert(null, 'a', 'b', 'c')
4086
- .toString(), 'a b\n');
4087
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
4088
- .insert(null, 0, 0, 0)
4089
- .toString(), 'a b\n');
4155
+ const base = $.$mol_tree2_from_string(`
4156
+ a b c d
4157
+ `);
4158
+ check(base.insert(null, 'a', 'b', 'c'), `
4159
+ a b
4160
+ `);
4161
+ check(base.update(base.select('a', 'b', 'c', null).kids, 'a', 'b', 'c')[0], `
4162
+ a b d
4163
+ `);
4164
+ check(base.insert(null, 0, 0, 0), `
4165
+ a b
4166
+ `);
4090
4167
  },
4091
4168
  'hack'($) {
4092
- const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
4169
+ const res = $.$mol_tree2_from_string(`
4170
+ foo bar xxx
4171
+ `)
4093
4172
  .hack({
4094
4173
  'bar': (input, belt) => [input.struct('777', input.hack(belt))],
4095
4174
  });