mol_plot_all 1.2.1400 → 1.2.1402

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
@@ -1256,6 +1256,16 @@ var $;
1256
1256
  $.$mol_tree2_to_string = $mol_tree2_to_string;
1257
1257
  })($ || ($ = {}));
1258
1258
 
1259
+ ;
1260
+ "use strict";
1261
+ var $;
1262
+ (function ($) {
1263
+ function $mol_maybe(value) {
1264
+ return (value == null) ? [] : [value];
1265
+ }
1266
+ $.$mol_maybe = $mol_maybe;
1267
+ })($ || ($ = {}));
1268
+
1259
1269
  ;
1260
1270
  "use strict";
1261
1271
  var $;
@@ -1323,33 +1333,38 @@ var $;
1323
1333
  return $$.$mol_tree2_to_string(this);
1324
1334
  }
1325
1335
  insert(value, ...path) {
1336
+ return this.update($mol_maybe(value), ...path)[0];
1337
+ }
1338
+ update(value, ...path) {
1326
1339
  if (path.length === 0)
1327
1340
  return value;
1328
1341
  const type = path[0];
1329
1342
  if (typeof type === 'string') {
1330
1343
  let replaced = false;
1331
- const sub = this.kids.map((item, index) => {
1344
+ const sub = this.kids.flatMap((item, index) => {
1332
1345
  if (item.type !== type)
1333
1346
  return item;
1334
1347
  replaced = true;
1335
- return item.insert(value, ...path.slice(1));
1348
+ return item.update(value, ...path.slice(1));
1336
1349
  }).filter(Boolean);
1337
1350
  if (!replaced && value) {
1338
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
1351
+ sub.push(...this.struct(type, []).update(value, ...path.slice(1)));
1339
1352
  }
1340
- return this.clone(sub);
1353
+ return [this.clone(sub)];
1341
1354
  }
1342
1355
  else if (typeof type === 'number') {
1343
- const sub = this.kids.slice();
1344
- sub[type] = (sub[type] || this.list([]))
1345
- .insert(value, ...path.slice(1));
1346
- return this.clone(sub.filter(Boolean));
1356
+ const ins = (this.kids[type] || this.list([]))
1357
+ .update(value, ...path.slice(1));
1358
+ return [this.clone([
1359
+ ...this.kids.slice(0, type),
1360
+ ...ins,
1361
+ ...this.kids.slice(type + 1),
1362
+ ])];
1347
1363
  }
1348
1364
  else {
1349
1365
  const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
1350
- .map(item => item.insert(value, ...path.slice(1)))
1351
- .filter(Boolean);
1352
- return this.clone(kids);
1366
+ .flatMap(item => item.update(value, ...path.slice(1)));
1367
+ return [this.clone(kids)];
1353
1368
  }
1354
1369
  }
1355
1370
  select(...path) {
package/node.test.js CHANGED
@@ -1247,6 +1247,16 @@ var $;
1247
1247
  $.$mol_tree2_to_string = $mol_tree2_to_string;
1248
1248
  })($ || ($ = {}));
1249
1249
 
1250
+ ;
1251
+ "use strict";
1252
+ var $;
1253
+ (function ($) {
1254
+ function $mol_maybe(value) {
1255
+ return (value == null) ? [] : [value];
1256
+ }
1257
+ $.$mol_maybe = $mol_maybe;
1258
+ })($ || ($ = {}));
1259
+
1250
1260
  ;
1251
1261
  "use strict";
1252
1262
  var $;
@@ -1314,33 +1324,38 @@ var $;
1314
1324
  return $$.$mol_tree2_to_string(this);
1315
1325
  }
1316
1326
  insert(value, ...path) {
1327
+ return this.update($mol_maybe(value), ...path)[0];
1328
+ }
1329
+ update(value, ...path) {
1317
1330
  if (path.length === 0)
1318
1331
  return value;
1319
1332
  const type = path[0];
1320
1333
  if (typeof type === 'string') {
1321
1334
  let replaced = false;
1322
- const sub = this.kids.map((item, index) => {
1335
+ const sub = this.kids.flatMap((item, index) => {
1323
1336
  if (item.type !== type)
1324
1337
  return item;
1325
1338
  replaced = true;
1326
- return item.insert(value, ...path.slice(1));
1339
+ return item.update(value, ...path.slice(1));
1327
1340
  }).filter(Boolean);
1328
1341
  if (!replaced && value) {
1329
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
1342
+ sub.push(...this.struct(type, []).update(value, ...path.slice(1)));
1330
1343
  }
1331
- return this.clone(sub);
1344
+ return [this.clone(sub)];
1332
1345
  }
1333
1346
  else if (typeof type === 'number') {
1334
- const sub = this.kids.slice();
1335
- sub[type] = (sub[type] || this.list([]))
1336
- .insert(value, ...path.slice(1));
1337
- return this.clone(sub.filter(Boolean));
1347
+ const ins = (this.kids[type] || this.list([]))
1348
+ .update(value, ...path.slice(1));
1349
+ return [this.clone([
1350
+ ...this.kids.slice(0, type),
1351
+ ...ins,
1352
+ ...this.kids.slice(type + 1),
1353
+ ])];
1338
1354
  }
1339
1355
  else {
1340
1356
  const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
1341
- .map(item => item.insert(value, ...path.slice(1)))
1342
- .filter(Boolean);
1343
- return this.clone(kids);
1357
+ .flatMap(item => item.update(value, ...path.slice(1)));
1358
+ return [this.clone(kids)];
1344
1359
  }
1345
1360
  }
1346
1361
  select(...path) {
@@ -7395,41 +7410,105 @@ var $;
7395
7410
  });
7396
7411
  })($ || ($ = {}));
7397
7412
 
7413
+ ;
7414
+ "use strict";
7415
+ var $;
7416
+ (function ($) {
7417
+ $mol_test({
7418
+ 'all cases of using maybe'() {
7419
+ $mol_assert_equal($mol_maybe(0)[0], 0);
7420
+ $mol_assert_equal($mol_maybe(false)[0], false);
7421
+ $mol_assert_equal($mol_maybe(null)[0], void 0);
7422
+ $mol_assert_equal($mol_maybe(void 0)[0], void 0);
7423
+ $mol_assert_equal($mol_maybe(void 0).map(v => v.toString())[0], void 0);
7424
+ $mol_assert_equal($mol_maybe(0).map(v => v.toString())[0], '0');
7425
+ },
7426
+ });
7427
+ })($ || ($ = {}));
7428
+
7398
7429
  ;
7399
7430
  "use strict";
7400
7431
  var $;
7401
7432
  (function ($_1) {
7433
+ function check(tree, ideal) {
7434
+ $mol_assert_equal(tree.toString(), $$.$mol_tree2_from_string(ideal).toString());
7435
+ }
7402
7436
  $mol_test({
7403
7437
  'inserting'($) {
7404
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
7405
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
7406
- .toString(), 'a b x\n');
7407
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
7408
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
7409
- .toString(), 'a b c x\n');
7410
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
7411
- .insert($mol_tree2.struct('x'), 0, 0, 0)
7412
- .toString(), 'a b x\n');
7413
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
7414
- .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
7415
- .toString(), 'a b \\\n\tx\n');
7416
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
7417
- .insert($mol_tree2.struct('x'), null, null, null)
7418
- .toString(), 'a b x\n');
7419
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
7420
- .insert($mol_tree2.struct('x'), null, null, null, null)
7421
- .toString(), 'a b \\\n\tx\n');
7438
+ check($.$mol_tree2_from_string(`
7439
+ a b c d
7440
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c'), `
7441
+ a b x
7442
+ `);
7443
+ check($.$mol_tree2_from_string(`
7444
+ a b
7445
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd'), `
7446
+ a b c x
7447
+ `);
7448
+ check($.$mol_tree2_from_string(`
7449
+ a b c d
7450
+ `)
7451
+ .insert($mol_tree2.struct('x'), 0, 0, 0), `
7452
+ a b x
7453
+ `);
7454
+ check($.$mol_tree2_from_string(`
7455
+ a b
7456
+ `)
7457
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0), `
7458
+ a b \\
7459
+ x
7460
+ `);
7461
+ check($.$mol_tree2_from_string(`
7462
+ a b c d
7463
+ `)
7464
+ .insert($mol_tree2.struct('x'), null, null, null), `
7465
+ a b x
7466
+ `);
7467
+ check($.$mol_tree2_from_string(`
7468
+ a b
7469
+ `)
7470
+ .insert($mol_tree2.struct('x'), null, null, null, null), `
7471
+ a b \\
7472
+ x
7473
+ `);
7474
+ },
7475
+ 'updating'($) {
7476
+ check($.$mol_tree2_from_string(`
7477
+ a b c d
7478
+ `).update([], 'a', 'b', 'c')[0], `
7479
+ a b
7480
+ `);
7481
+ check($.$mol_tree2_from_string(`
7482
+ a b c d
7483
+ `).update([$mol_tree2.struct('x')])[0], `
7484
+ x
7485
+ `);
7486
+ check($.$mol_tree2_from_string(`
7487
+ a b c d
7488
+ `).update([$mol_tree2.struct('x'), $mol_tree2.struct('y')], 'a', 'b', 'c')[0], `
7489
+ a b
7490
+ x
7491
+ y
7492
+ `);
7422
7493
  },
7423
7494
  'deleting'($) {
7424
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
7425
- .insert(null, 'a', 'b', 'c')
7426
- .toString(), 'a b\n');
7427
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
7428
- .insert(null, 0, 0, 0)
7429
- .toString(), 'a b\n');
7495
+ const base = $.$mol_tree2_from_string(`
7496
+ a b c d
7497
+ `);
7498
+ check(base.insert(null, 'a', 'b', 'c'), `
7499
+ a b
7500
+ `);
7501
+ check(base.update(base.select('a', 'b', 'c', null).kids, 'a', 'b', 'c')[0], `
7502
+ a b d
7503
+ `);
7504
+ check(base.insert(null, 0, 0, 0), `
7505
+ a b
7506
+ `);
7430
7507
  },
7431
7508
  'hack'($) {
7432
- const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
7509
+ const res = $.$mol_tree2_from_string(`
7510
+ foo bar xxx
7511
+ `)
7433
7512
  .hack({
7434
7513
  'bar': (input, belt) => [input.struct('777', input.hack(belt))],
7435
7514
  });