mol_compare_deep 0.0.1271 → 0.0.1273

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
@@ -1007,6 +1007,32 @@ var $;
1007
1007
  $.$mol_tree2_to_string = $mol_tree2_to_string;
1008
1008
  })($ || ($ = {}));
1009
1009
 
1010
+ ;
1011
+ "use strict";
1012
+ var $;
1013
+ (function ($) {
1014
+ function $mol_maybe(value) {
1015
+ return (value == null) ? [] : [value];
1016
+ }
1017
+ $.$mol_maybe = $mol_maybe;
1018
+ })($ || ($ = {}));
1019
+
1020
+ ;
1021
+ "use strict";
1022
+ var $;
1023
+ (function ($) {
1024
+ $mol_test({
1025
+ 'all cases of using maybe'() {
1026
+ $mol_assert_equal($mol_maybe(0)[0], 0);
1027
+ $mol_assert_equal($mol_maybe(false)[0], false);
1028
+ $mol_assert_equal($mol_maybe(null)[0], void 0);
1029
+ $mol_assert_equal($mol_maybe(void 0)[0], void 0);
1030
+ $mol_assert_equal($mol_maybe(void 0).map(v => v.toString())[0], void 0);
1031
+ $mol_assert_equal($mol_maybe(0).map(v => v.toString())[0], '0');
1032
+ },
1033
+ });
1034
+ })($ || ($ = {}));
1035
+
1010
1036
  ;
1011
1037
  "use strict";
1012
1038
  var $;
@@ -1074,33 +1100,38 @@ var $;
1074
1100
  return $$.$mol_tree2_to_string(this);
1075
1101
  }
1076
1102
  insert(value, ...path) {
1103
+ return this.update($mol_maybe(value), ...path)[0];
1104
+ }
1105
+ update(value, ...path) {
1077
1106
  if (path.length === 0)
1078
1107
  return value;
1079
1108
  const type = path[0];
1080
1109
  if (typeof type === 'string') {
1081
1110
  let replaced = false;
1082
- const sub = this.kids.map((item, index) => {
1111
+ const sub = this.kids.flatMap((item, index) => {
1083
1112
  if (item.type !== type)
1084
1113
  return item;
1085
1114
  replaced = true;
1086
- return item.insert(value, ...path.slice(1));
1115
+ return item.update(value, ...path.slice(1));
1087
1116
  }).filter(Boolean);
1088
1117
  if (!replaced && value) {
1089
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
1118
+ sub.push(...this.struct(type, []).update(value, ...path.slice(1)));
1090
1119
  }
1091
- return this.clone(sub);
1120
+ return [this.clone(sub)];
1092
1121
  }
1093
1122
  else if (typeof type === 'number') {
1094
- const sub = this.kids.slice();
1095
- sub[type] = (sub[type] || this.list([]))
1096
- .insert(value, ...path.slice(1));
1097
- return this.clone(sub.filter(Boolean));
1123
+ const ins = (this.kids[type] || this.list([]))
1124
+ .update(value, ...path.slice(1));
1125
+ return [this.clone([
1126
+ ...this.kids.slice(0, type),
1127
+ ...ins,
1128
+ ...this.kids.slice(type + 1),
1129
+ ])];
1098
1130
  }
1099
1131
  else {
1100
1132
  const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
1101
- .map(item => item.insert(value, ...path.slice(1)))
1102
- .filter(Boolean);
1103
- return this.clone(kids);
1133
+ .flatMap(item => item.update(value, ...path.slice(1)));
1134
+ return [this.clone(kids)];
1104
1135
  }
1105
1136
  }
1106
1137
  select(...path) {
@@ -1176,37 +1207,85 @@ var $;
1176
1207
  "use strict";
1177
1208
  var $;
1178
1209
  (function ($_1) {
1210
+ function check(tree, ideal) {
1211
+ $mol_assert_equal(tree.toString(), $$.$mol_tree2_from_string(ideal).toString());
1212
+ }
1179
1213
  $mol_test({
1180
1214
  'inserting'($) {
1181
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1182
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
1183
- .toString(), 'a b x\n');
1184
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1185
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
1186
- .toString(), 'a b c x\n');
1187
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1188
- .insert($mol_tree2.struct('x'), 0, 0, 0)
1189
- .toString(), 'a b x\n');
1190
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1191
- .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
1192
- .toString(), 'a b \\\n\tx\n');
1193
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1194
- .insert($mol_tree2.struct('x'), null, null, null)
1195
- .toString(), 'a b x\n');
1196
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1197
- .insert($mol_tree2.struct('x'), null, null, null, null)
1198
- .toString(), 'a b \\\n\tx\n');
1215
+ check($.$mol_tree2_from_string(`
1216
+ a b c d
1217
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c'), `
1218
+ a b x
1219
+ `);
1220
+ check($.$mol_tree2_from_string(`
1221
+ a b
1222
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd'), `
1223
+ a b c x
1224
+ `);
1225
+ check($.$mol_tree2_from_string(`
1226
+ a b c d
1227
+ `)
1228
+ .insert($mol_tree2.struct('x'), 0, 0, 0), `
1229
+ a b x
1230
+ `);
1231
+ check($.$mol_tree2_from_string(`
1232
+ a b
1233
+ `)
1234
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0), `
1235
+ a b \\
1236
+ x
1237
+ `);
1238
+ check($.$mol_tree2_from_string(`
1239
+ a b c d
1240
+ `)
1241
+ .insert($mol_tree2.struct('x'), null, null, null), `
1242
+ a b x
1243
+ `);
1244
+ check($.$mol_tree2_from_string(`
1245
+ a b
1246
+ `)
1247
+ .insert($mol_tree2.struct('x'), null, null, null, null), `
1248
+ a b \\
1249
+ x
1250
+ `);
1251
+ },
1252
+ 'updating'($) {
1253
+ check($.$mol_tree2_from_string(`
1254
+ a b c d
1255
+ `).update([], 'a', 'b', 'c')[0], `
1256
+ a b
1257
+ `);
1258
+ check($.$mol_tree2_from_string(`
1259
+ a b c d
1260
+ `).update([$mol_tree2.struct('x')])[0], `
1261
+ x
1262
+ `);
1263
+ check($.$mol_tree2_from_string(`
1264
+ a b c d
1265
+ `).update([$mol_tree2.struct('x'), $mol_tree2.struct('y')], 'a', 'b', 'c')[0], `
1266
+ a b
1267
+ x
1268
+ y
1269
+ `);
1199
1270
  },
1200
1271
  'deleting'($) {
1201
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1202
- .insert(null, 'a', 'b', 'c')
1203
- .toString(), 'a b\n');
1204
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1205
- .insert(null, 0, 0, 0)
1206
- .toString(), 'a b\n');
1272
+ const base = $.$mol_tree2_from_string(`
1273
+ a b c d
1274
+ `);
1275
+ check(base.insert(null, 'a', 'b', 'c'), `
1276
+ a b
1277
+ `);
1278
+ check(base.update(base.select('a', 'b', 'c', null).kids, 'a', 'b', 'c')[0], `
1279
+ a b d
1280
+ `);
1281
+ check(base.insert(null, 0, 0, 0), `
1282
+ a b
1283
+ `);
1207
1284
  },
1208
1285
  'hack'($) {
1209
- const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
1286
+ const res = $.$mol_tree2_from_string(`
1287
+ foo bar xxx
1288
+ `)
1210
1289
  .hack({
1211
1290
  'bar': (input, belt) => [input.struct('777', input.hack(belt))],
1212
1291
  });