mol_jsx_lib 0.0.1286 → 0.0.1288

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
@@ -1305,6 +1305,16 @@ var $;
1305
1305
  $.$mol_tree2_to_string = $mol_tree2_to_string;
1306
1306
  })($ || ($ = {}));
1307
1307
 
1308
+ ;
1309
+ "use strict";
1310
+ var $;
1311
+ (function ($) {
1312
+ function $mol_maybe(value) {
1313
+ return (value == null) ? [] : [value];
1314
+ }
1315
+ $.$mol_maybe = $mol_maybe;
1316
+ })($ || ($ = {}));
1317
+
1308
1318
  ;
1309
1319
  "use strict";
1310
1320
  var $;
@@ -1372,33 +1382,38 @@ var $;
1372
1382
  return $$.$mol_tree2_to_string(this);
1373
1383
  }
1374
1384
  insert(value, ...path) {
1385
+ return this.update($mol_maybe(value), ...path)[0];
1386
+ }
1387
+ update(value, ...path) {
1375
1388
  if (path.length === 0)
1376
1389
  return value;
1377
1390
  const type = path[0];
1378
1391
  if (typeof type === 'string') {
1379
1392
  let replaced = false;
1380
- const sub = this.kids.map((item, index) => {
1393
+ const sub = this.kids.flatMap((item, index) => {
1381
1394
  if (item.type !== type)
1382
1395
  return item;
1383
1396
  replaced = true;
1384
- return item.insert(value, ...path.slice(1));
1397
+ return item.update(value, ...path.slice(1));
1385
1398
  }).filter(Boolean);
1386
1399
  if (!replaced && value) {
1387
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
1400
+ sub.push(...this.struct(type, []).update(value, ...path.slice(1)));
1388
1401
  }
1389
- return this.clone(sub);
1402
+ return [this.clone(sub)];
1390
1403
  }
1391
1404
  else if (typeof type === 'number') {
1392
- const sub = this.kids.slice();
1393
- sub[type] = (sub[type] || this.list([]))
1394
- .insert(value, ...path.slice(1));
1395
- return this.clone(sub.filter(Boolean));
1405
+ const ins = (this.kids[type] || this.list([]))
1406
+ .update(value, ...path.slice(1));
1407
+ return [this.clone([
1408
+ ...this.kids.slice(0, type),
1409
+ ...ins,
1410
+ ...this.kids.slice(type + 1),
1411
+ ])];
1396
1412
  }
1397
1413
  else {
1398
1414
  const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
1399
- .map(item => item.insert(value, ...path.slice(1)))
1400
- .filter(Boolean);
1401
- return this.clone(kids);
1415
+ .flatMap(item => item.update(value, ...path.slice(1)));
1416
+ return [this.clone(kids)];
1402
1417
  }
1403
1418
  }
1404
1419
  select(...path) {
package/node.test.js CHANGED
@@ -1296,6 +1296,16 @@ var $;
1296
1296
  $.$mol_tree2_to_string = $mol_tree2_to_string;
1297
1297
  })($ || ($ = {}));
1298
1298
 
1299
+ ;
1300
+ "use strict";
1301
+ var $;
1302
+ (function ($) {
1303
+ function $mol_maybe(value) {
1304
+ return (value == null) ? [] : [value];
1305
+ }
1306
+ $.$mol_maybe = $mol_maybe;
1307
+ })($ || ($ = {}));
1308
+
1299
1309
  ;
1300
1310
  "use strict";
1301
1311
  var $;
@@ -1363,33 +1373,38 @@ var $;
1363
1373
  return $$.$mol_tree2_to_string(this);
1364
1374
  }
1365
1375
  insert(value, ...path) {
1376
+ return this.update($mol_maybe(value), ...path)[0];
1377
+ }
1378
+ update(value, ...path) {
1366
1379
  if (path.length === 0)
1367
1380
  return value;
1368
1381
  const type = path[0];
1369
1382
  if (typeof type === 'string') {
1370
1383
  let replaced = false;
1371
- const sub = this.kids.map((item, index) => {
1384
+ const sub = this.kids.flatMap((item, index) => {
1372
1385
  if (item.type !== type)
1373
1386
  return item;
1374
1387
  replaced = true;
1375
- return item.insert(value, ...path.slice(1));
1388
+ return item.update(value, ...path.slice(1));
1376
1389
  }).filter(Boolean);
1377
1390
  if (!replaced && value) {
1378
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
1391
+ sub.push(...this.struct(type, []).update(value, ...path.slice(1)));
1379
1392
  }
1380
- return this.clone(sub);
1393
+ return [this.clone(sub)];
1381
1394
  }
1382
1395
  else if (typeof type === 'number') {
1383
- const sub = this.kids.slice();
1384
- sub[type] = (sub[type] || this.list([]))
1385
- .insert(value, ...path.slice(1));
1386
- return this.clone(sub.filter(Boolean));
1396
+ const ins = (this.kids[type] || this.list([]))
1397
+ .update(value, ...path.slice(1));
1398
+ return [this.clone([
1399
+ ...this.kids.slice(0, type),
1400
+ ...ins,
1401
+ ...this.kids.slice(type + 1),
1402
+ ])];
1387
1403
  }
1388
1404
  else {
1389
1405
  const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
1390
- .map(item => item.insert(value, ...path.slice(1)))
1391
- .filter(Boolean);
1392
- return this.clone(kids);
1406
+ .flatMap(item => item.update(value, ...path.slice(1)));
1407
+ return [this.clone(kids)];
1393
1408
  }
1394
1409
  }
1395
1410
  select(...path) {
@@ -3412,41 +3427,105 @@ var $;
3412
3427
  });
3413
3428
  })($ || ($ = {}));
3414
3429
 
3430
+ ;
3431
+ "use strict";
3432
+ var $;
3433
+ (function ($) {
3434
+ $mol_test({
3435
+ 'all cases of using maybe'() {
3436
+ $mol_assert_equal($mol_maybe(0)[0], 0);
3437
+ $mol_assert_equal($mol_maybe(false)[0], false);
3438
+ $mol_assert_equal($mol_maybe(null)[0], void 0);
3439
+ $mol_assert_equal($mol_maybe(void 0)[0], void 0);
3440
+ $mol_assert_equal($mol_maybe(void 0).map(v => v.toString())[0], void 0);
3441
+ $mol_assert_equal($mol_maybe(0).map(v => v.toString())[0], '0');
3442
+ },
3443
+ });
3444
+ })($ || ($ = {}));
3445
+
3415
3446
  ;
3416
3447
  "use strict";
3417
3448
  var $;
3418
3449
  (function ($_1) {
3450
+ function check(tree, ideal) {
3451
+ $mol_assert_equal(tree.toString(), $$.$mol_tree2_from_string(ideal).toString());
3452
+ }
3419
3453
  $mol_test({
3420
3454
  'inserting'($) {
3421
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3422
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
3423
- .toString(), 'a b x\n');
3424
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
3425
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
3426
- .toString(), 'a b c x\n');
3427
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3428
- .insert($mol_tree2.struct('x'), 0, 0, 0)
3429
- .toString(), 'a b x\n');
3430
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
3431
- .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
3432
- .toString(), 'a b \\\n\tx\n');
3433
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3434
- .insert($mol_tree2.struct('x'), null, null, null)
3435
- .toString(), 'a b x\n');
3436
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
3437
- .insert($mol_tree2.struct('x'), null, null, null, null)
3438
- .toString(), 'a b \\\n\tx\n');
3455
+ check($.$mol_tree2_from_string(`
3456
+ a b c d
3457
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c'), `
3458
+ a b x
3459
+ `);
3460
+ check($.$mol_tree2_from_string(`
3461
+ a b
3462
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd'), `
3463
+ a b c x
3464
+ `);
3465
+ check($.$mol_tree2_from_string(`
3466
+ a b c d
3467
+ `)
3468
+ .insert($mol_tree2.struct('x'), 0, 0, 0), `
3469
+ a b x
3470
+ `);
3471
+ check($.$mol_tree2_from_string(`
3472
+ a b
3473
+ `)
3474
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0), `
3475
+ a b \\
3476
+ x
3477
+ `);
3478
+ check($.$mol_tree2_from_string(`
3479
+ a b c d
3480
+ `)
3481
+ .insert($mol_tree2.struct('x'), null, null, null), `
3482
+ a b x
3483
+ `);
3484
+ check($.$mol_tree2_from_string(`
3485
+ a b
3486
+ `)
3487
+ .insert($mol_tree2.struct('x'), null, null, null, null), `
3488
+ a b \\
3489
+ x
3490
+ `);
3491
+ },
3492
+ 'updating'($) {
3493
+ check($.$mol_tree2_from_string(`
3494
+ a b c d
3495
+ `).update([], 'a', 'b', 'c')[0], `
3496
+ a b
3497
+ `);
3498
+ check($.$mol_tree2_from_string(`
3499
+ a b c d
3500
+ `).update([$mol_tree2.struct('x')])[0], `
3501
+ x
3502
+ `);
3503
+ check($.$mol_tree2_from_string(`
3504
+ a b c d
3505
+ `).update([$mol_tree2.struct('x'), $mol_tree2.struct('y')], 'a', 'b', 'c')[0], `
3506
+ a b
3507
+ x
3508
+ y
3509
+ `);
3439
3510
  },
3440
3511
  'deleting'($) {
3441
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3442
- .insert(null, 'a', 'b', 'c')
3443
- .toString(), 'a b\n');
3444
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
3445
- .insert(null, 0, 0, 0)
3446
- .toString(), 'a b\n');
3512
+ const base = $.$mol_tree2_from_string(`
3513
+ a b c d
3514
+ `);
3515
+ check(base.insert(null, 'a', 'b', 'c'), `
3516
+ a b
3517
+ `);
3518
+ check(base.update(base.select('a', 'b', 'c', null).kids, 'a', 'b', 'c')[0], `
3519
+ a b d
3520
+ `);
3521
+ check(base.insert(null, 0, 0, 0), `
3522
+ a b
3523
+ `);
3447
3524
  },
3448
3525
  'hack'($) {
3449
- const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
3526
+ const res = $.$mol_tree2_from_string(`
3527
+ foo bar xxx
3528
+ `)
3450
3529
  .hack({
3451
3530
  'bar': (input, belt) => [input.struct('777', input.hack(belt))],
3452
3531
  });