mol_dump_lib 0.0.718 → 0.0.719

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) {
@@ -6543,16 +6558,6 @@ var $;
6543
6558
  ($mol_mem(($.$mol_check.prototype), "Title"));
6544
6559
 
6545
6560
 
6546
- ;
6547
- "use strict";
6548
- var $;
6549
- (function ($) {
6550
- function $mol_maybe(value) {
6551
- return (value == null) ? [] : [value];
6552
- }
6553
- $.$mol_maybe = $mol_maybe;
6554
- })($ || ($ = {}));
6555
-
6556
6561
  ;
6557
6562
  "use strict";
6558
6563
  var $;
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) {
@@ -6534,16 +6549,6 @@ var $;
6534
6549
  ($mol_mem(($.$mol_check.prototype), "Title"));
6535
6550
 
6536
6551
 
6537
- ;
6538
- "use strict";
6539
- var $;
6540
- (function ($) {
6541
- function $mol_maybe(value) {
6542
- return (value == null) ? [] : [value];
6543
- }
6544
- $.$mol_maybe = $mol_maybe;
6545
- })($ || ($ = {}));
6546
-
6547
6552
  ;
6548
6553
  "use strict";
6549
6554
  var $;
@@ -8131,41 +8136,105 @@ var $;
8131
8136
  });
8132
8137
  })($ || ($ = {}));
8133
8138
 
8139
+ ;
8140
+ "use strict";
8141
+ var $;
8142
+ (function ($) {
8143
+ $mol_test({
8144
+ 'all cases of using maybe'() {
8145
+ $mol_assert_equal($mol_maybe(0)[0], 0);
8146
+ $mol_assert_equal($mol_maybe(false)[0], false);
8147
+ $mol_assert_equal($mol_maybe(null)[0], void 0);
8148
+ $mol_assert_equal($mol_maybe(void 0)[0], void 0);
8149
+ $mol_assert_equal($mol_maybe(void 0).map(v => v.toString())[0], void 0);
8150
+ $mol_assert_equal($mol_maybe(0).map(v => v.toString())[0], '0');
8151
+ },
8152
+ });
8153
+ })($ || ($ = {}));
8154
+
8134
8155
  ;
8135
8156
  "use strict";
8136
8157
  var $;
8137
8158
  (function ($_1) {
8159
+ function check(tree, ideal) {
8160
+ $mol_assert_equal(tree.toString(), $$.$mol_tree2_from_string(ideal).toString());
8161
+ }
8138
8162
  $mol_test({
8139
8163
  'inserting'($) {
8140
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
8141
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
8142
- .toString(), 'a b x\n');
8143
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
8144
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
8145
- .toString(), 'a b c x\n');
8146
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
8147
- .insert($mol_tree2.struct('x'), 0, 0, 0)
8148
- .toString(), 'a b x\n');
8149
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
8150
- .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
8151
- .toString(), 'a b \\\n\tx\n');
8152
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
8153
- .insert($mol_tree2.struct('x'), null, null, null)
8154
- .toString(), 'a b x\n');
8155
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
8156
- .insert($mol_tree2.struct('x'), null, null, null, null)
8157
- .toString(), 'a b \\\n\tx\n');
8164
+ check($.$mol_tree2_from_string(`
8165
+ a b c d
8166
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c'), `
8167
+ a b x
8168
+ `);
8169
+ check($.$mol_tree2_from_string(`
8170
+ a b
8171
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd'), `
8172
+ a b c x
8173
+ `);
8174
+ check($.$mol_tree2_from_string(`
8175
+ a b c d
8176
+ `)
8177
+ .insert($mol_tree2.struct('x'), 0, 0, 0), `
8178
+ a b x
8179
+ `);
8180
+ check($.$mol_tree2_from_string(`
8181
+ a b
8182
+ `)
8183
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0), `
8184
+ a b \\
8185
+ x
8186
+ `);
8187
+ check($.$mol_tree2_from_string(`
8188
+ a b c d
8189
+ `)
8190
+ .insert($mol_tree2.struct('x'), null, null, null), `
8191
+ a b x
8192
+ `);
8193
+ check($.$mol_tree2_from_string(`
8194
+ a b
8195
+ `)
8196
+ .insert($mol_tree2.struct('x'), null, null, null, null), `
8197
+ a b \\
8198
+ x
8199
+ `);
8200
+ },
8201
+ 'updating'($) {
8202
+ check($.$mol_tree2_from_string(`
8203
+ a b c d
8204
+ `).update([], 'a', 'b', 'c')[0], `
8205
+ a b
8206
+ `);
8207
+ check($.$mol_tree2_from_string(`
8208
+ a b c d
8209
+ `).update([$mol_tree2.struct('x')])[0], `
8210
+ x
8211
+ `);
8212
+ check($.$mol_tree2_from_string(`
8213
+ a b c d
8214
+ `).update([$mol_tree2.struct('x'), $mol_tree2.struct('y')], 'a', 'b', 'c')[0], `
8215
+ a b
8216
+ x
8217
+ y
8218
+ `);
8158
8219
  },
8159
8220
  'deleting'($) {
8160
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
8161
- .insert(null, 'a', 'b', 'c')
8162
- .toString(), 'a b\n');
8163
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
8164
- .insert(null, 0, 0, 0)
8165
- .toString(), 'a b\n');
8221
+ const base = $.$mol_tree2_from_string(`
8222
+ a b c d
8223
+ `);
8224
+ check(base.insert(null, 'a', 'b', 'c'), `
8225
+ a b
8226
+ `);
8227
+ check(base.update(base.select('a', 'b', 'c', null).kids, 'a', 'b', 'c')[0], `
8228
+ a b d
8229
+ `);
8230
+ check(base.insert(null, 0, 0, 0), `
8231
+ a b
8232
+ `);
8166
8233
  },
8167
8234
  'hack'($) {
8168
- const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
8235
+ const res = $.$mol_tree2_from_string(`
8236
+ foo bar xxx
8237
+ `)
8169
8238
  .hack({
8170
8239
  'bar': (input, belt) => [input.struct('777', input.hack(belt))],
8171
8240
  });
@@ -10561,22 +10630,6 @@ var $;
10561
10630
  });
10562
10631
  })($ || ($ = {}));
10563
10632
 
10564
- ;
10565
- "use strict";
10566
- var $;
10567
- (function ($) {
10568
- $mol_test({
10569
- 'all cases of using maybe'() {
10570
- $mol_assert_equal($mol_maybe(0)[0], 0);
10571
- $mol_assert_equal($mol_maybe(false)[0], false);
10572
- $mol_assert_equal($mol_maybe(null)[0], void 0);
10573
- $mol_assert_equal($mol_maybe(void 0)[0], void 0);
10574
- $mol_assert_equal($mol_maybe(void 0).map(v => v.toString())[0], void 0);
10575
- $mol_assert_equal($mol_maybe(0).map(v => v.toString())[0], '0');
10576
- },
10577
- });
10578
- })($ || ($ = {}));
10579
-
10580
10633
  ;
10581
10634
  "use strict";
10582
10635
  var $;