mol_time_all 1.1.1371 → 1.1.1373

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
@@ -4068,6 +4068,32 @@ var $;
4068
4068
  $.$mol_tree2_to_string = $mol_tree2_to_string;
4069
4069
  })($ || ($ = {}));
4070
4070
 
4071
+ ;
4072
+ "use strict";
4073
+ var $;
4074
+ (function ($) {
4075
+ function $mol_maybe(value) {
4076
+ return (value == null) ? [] : [value];
4077
+ }
4078
+ $.$mol_maybe = $mol_maybe;
4079
+ })($ || ($ = {}));
4080
+
4081
+ ;
4082
+ "use strict";
4083
+ var $;
4084
+ (function ($) {
4085
+ $mol_test({
4086
+ 'all cases of using maybe'() {
4087
+ $mol_assert_equal($mol_maybe(0)[0], 0);
4088
+ $mol_assert_equal($mol_maybe(false)[0], false);
4089
+ $mol_assert_equal($mol_maybe(null)[0], void 0);
4090
+ $mol_assert_equal($mol_maybe(void 0)[0], void 0);
4091
+ $mol_assert_equal($mol_maybe(void 0).map(v => v.toString())[0], void 0);
4092
+ $mol_assert_equal($mol_maybe(0).map(v => v.toString())[0], '0');
4093
+ },
4094
+ });
4095
+ })($ || ($ = {}));
4096
+
4071
4097
  ;
4072
4098
  "use strict";
4073
4099
  var $;
@@ -4135,33 +4161,38 @@ var $;
4135
4161
  return $$.$mol_tree2_to_string(this);
4136
4162
  }
4137
4163
  insert(value, ...path) {
4164
+ return this.update($mol_maybe(value), ...path)[0];
4165
+ }
4166
+ update(value, ...path) {
4138
4167
  if (path.length === 0)
4139
4168
  return value;
4140
4169
  const type = path[0];
4141
4170
  if (typeof type === 'string') {
4142
4171
  let replaced = false;
4143
- const sub = this.kids.map((item, index) => {
4172
+ const sub = this.kids.flatMap((item, index) => {
4144
4173
  if (item.type !== type)
4145
4174
  return item;
4146
4175
  replaced = true;
4147
- return item.insert(value, ...path.slice(1));
4176
+ return item.update(value, ...path.slice(1));
4148
4177
  }).filter(Boolean);
4149
4178
  if (!replaced && value) {
4150
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
4179
+ sub.push(...this.struct(type, []).update(value, ...path.slice(1)));
4151
4180
  }
4152
- return this.clone(sub);
4181
+ return [this.clone(sub)];
4153
4182
  }
4154
4183
  else if (typeof type === 'number') {
4155
- const sub = this.kids.slice();
4156
- sub[type] = (sub[type] || this.list([]))
4157
- .insert(value, ...path.slice(1));
4158
- return this.clone(sub.filter(Boolean));
4184
+ const ins = (this.kids[type] || this.list([]))
4185
+ .update(value, ...path.slice(1));
4186
+ return [this.clone([
4187
+ ...this.kids.slice(0, type),
4188
+ ...ins,
4189
+ ...this.kids.slice(type + 1),
4190
+ ])];
4159
4191
  }
4160
4192
  else {
4161
4193
  const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
4162
- .map(item => item.insert(value, ...path.slice(1)))
4163
- .filter(Boolean);
4164
- return this.clone(kids);
4194
+ .flatMap(item => item.update(value, ...path.slice(1)));
4195
+ return [this.clone(kids)];
4165
4196
  }
4166
4197
  }
4167
4198
  select(...path) {
@@ -4237,37 +4268,85 @@ var $;
4237
4268
  "use strict";
4238
4269
  var $;
4239
4270
  (function ($_1) {
4271
+ function check(tree, ideal) {
4272
+ $mol_assert_equal(tree.toString(), $$.$mol_tree2_from_string(ideal).toString());
4273
+ }
4240
4274
  $mol_test({
4241
4275
  'inserting'($) {
4242
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
4243
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
4244
- .toString(), 'a b x\n');
4245
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
4246
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
4247
- .toString(), 'a b c x\n');
4248
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
4249
- .insert($mol_tree2.struct('x'), 0, 0, 0)
4250
- .toString(), 'a b x\n');
4251
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
4252
- .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
4253
- .toString(), 'a b \\\n\tx\n');
4254
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
4255
- .insert($mol_tree2.struct('x'), null, null, null)
4256
- .toString(), 'a b x\n');
4257
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
4258
- .insert($mol_tree2.struct('x'), null, null, null, null)
4259
- .toString(), 'a b \\\n\tx\n');
4276
+ check($.$mol_tree2_from_string(`
4277
+ a b c d
4278
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c'), `
4279
+ a b x
4280
+ `);
4281
+ check($.$mol_tree2_from_string(`
4282
+ a b
4283
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd'), `
4284
+ a b c x
4285
+ `);
4286
+ check($.$mol_tree2_from_string(`
4287
+ a b c d
4288
+ `)
4289
+ .insert($mol_tree2.struct('x'), 0, 0, 0), `
4290
+ a b x
4291
+ `);
4292
+ check($.$mol_tree2_from_string(`
4293
+ a b
4294
+ `)
4295
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0), `
4296
+ a b \\
4297
+ x
4298
+ `);
4299
+ check($.$mol_tree2_from_string(`
4300
+ a b c d
4301
+ `)
4302
+ .insert($mol_tree2.struct('x'), null, null, null), `
4303
+ a b x
4304
+ `);
4305
+ check($.$mol_tree2_from_string(`
4306
+ a b
4307
+ `)
4308
+ .insert($mol_tree2.struct('x'), null, null, null, null), `
4309
+ a b \\
4310
+ x
4311
+ `);
4312
+ },
4313
+ 'updating'($) {
4314
+ check($.$mol_tree2_from_string(`
4315
+ a b c d
4316
+ `).update([], 'a', 'b', 'c')[0], `
4317
+ a b
4318
+ `);
4319
+ check($.$mol_tree2_from_string(`
4320
+ a b c d
4321
+ `).update([$mol_tree2.struct('x')])[0], `
4322
+ x
4323
+ `);
4324
+ check($.$mol_tree2_from_string(`
4325
+ a b c d
4326
+ `).update([$mol_tree2.struct('x'), $mol_tree2.struct('y')], 'a', 'b', 'c')[0], `
4327
+ a b
4328
+ x
4329
+ y
4330
+ `);
4260
4331
  },
4261
4332
  'deleting'($) {
4262
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
4263
- .insert(null, 'a', 'b', 'c')
4264
- .toString(), 'a b\n');
4265
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
4266
- .insert(null, 0, 0, 0)
4267
- .toString(), 'a b\n');
4333
+ const base = $.$mol_tree2_from_string(`
4334
+ a b c d
4335
+ `);
4336
+ check(base.insert(null, 'a', 'b', 'c'), `
4337
+ a b
4338
+ `);
4339
+ check(base.update(base.select('a', 'b', 'c', null).kids, 'a', 'b', 'c')[0], `
4340
+ a b d
4341
+ `);
4342
+ check(base.insert(null, 0, 0, 0), `
4343
+ a b
4344
+ `);
4268
4345
  },
4269
4346
  'hack'($) {
4270
- const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
4347
+ const res = $.$mol_tree2_from_string(`
4348
+ foo bar xxx
4349
+ `)
4271
4350
  .hack({
4272
4351
  'bar': (input, belt) => [input.struct('777', input.hack(belt))],
4273
4352
  });