mol_key 0.0.1254 → 0.0.1255

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
@@ -930,6 +930,32 @@ var $;
930
930
  $.$mol_tree2_to_string = $mol_tree2_to_string;
931
931
  })($ || ($ = {}));
932
932
 
933
+ ;
934
+ "use strict";
935
+ var $;
936
+ (function ($) {
937
+ function $mol_maybe(value) {
938
+ return (value == null) ? [] : [value];
939
+ }
940
+ $.$mol_maybe = $mol_maybe;
941
+ })($ || ($ = {}));
942
+
943
+ ;
944
+ "use strict";
945
+ var $;
946
+ (function ($) {
947
+ $mol_test({
948
+ 'all cases of using maybe'() {
949
+ $mol_assert_equal($mol_maybe(0)[0], 0);
950
+ $mol_assert_equal($mol_maybe(false)[0], false);
951
+ $mol_assert_equal($mol_maybe(null)[0], void 0);
952
+ $mol_assert_equal($mol_maybe(void 0)[0], void 0);
953
+ $mol_assert_equal($mol_maybe(void 0).map(v => v.toString())[0], void 0);
954
+ $mol_assert_equal($mol_maybe(0).map(v => v.toString())[0], '0');
955
+ },
956
+ });
957
+ })($ || ($ = {}));
958
+
933
959
  ;
934
960
  "use strict";
935
961
  var $;
@@ -997,33 +1023,38 @@ var $;
997
1023
  return $$.$mol_tree2_to_string(this);
998
1024
  }
999
1025
  insert(value, ...path) {
1026
+ return this.update($mol_maybe(value), ...path)[0];
1027
+ }
1028
+ update(value, ...path) {
1000
1029
  if (path.length === 0)
1001
1030
  return value;
1002
1031
  const type = path[0];
1003
1032
  if (typeof type === 'string') {
1004
1033
  let replaced = false;
1005
- const sub = this.kids.map((item, index) => {
1034
+ const sub = this.kids.flatMap((item, index) => {
1006
1035
  if (item.type !== type)
1007
1036
  return item;
1008
1037
  replaced = true;
1009
- return item.insert(value, ...path.slice(1));
1038
+ return item.update(value, ...path.slice(1));
1010
1039
  }).filter(Boolean);
1011
1040
  if (!replaced && value) {
1012
- sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
1041
+ sub.push(...this.struct(type, []).update(value, ...path.slice(1)));
1013
1042
  }
1014
- return this.clone(sub);
1043
+ return [this.clone(sub)];
1015
1044
  }
1016
1045
  else if (typeof type === 'number') {
1017
- const sub = this.kids.slice();
1018
- sub[type] = (sub[type] || this.list([]))
1019
- .insert(value, ...path.slice(1));
1020
- return this.clone(sub.filter(Boolean));
1046
+ const ins = (this.kids[type] || this.list([]))
1047
+ .update(value, ...path.slice(1));
1048
+ return [this.clone([
1049
+ ...this.kids.slice(0, type),
1050
+ ...ins,
1051
+ ...this.kids.slice(type + 1),
1052
+ ])];
1021
1053
  }
1022
1054
  else {
1023
1055
  const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
1024
- .map(item => item.insert(value, ...path.slice(1)))
1025
- .filter(Boolean);
1026
- return this.clone(kids);
1056
+ .flatMap(item => item.update(value, ...path.slice(1)));
1057
+ return [this.clone(kids)];
1027
1058
  }
1028
1059
  }
1029
1060
  select(...path) {
@@ -1099,37 +1130,85 @@ var $;
1099
1130
  "use strict";
1100
1131
  var $;
1101
1132
  (function ($_1) {
1133
+ function check(tree, ideal) {
1134
+ $mol_assert_equal(tree.toString(), $$.$mol_tree2_from_string(ideal).toString());
1135
+ }
1102
1136
  $mol_test({
1103
1137
  'inserting'($) {
1104
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1105
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
1106
- .toString(), 'a b x\n');
1107
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1108
- .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
1109
- .toString(), 'a b c x\n');
1110
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1111
- .insert($mol_tree2.struct('x'), 0, 0, 0)
1112
- .toString(), 'a b x\n');
1113
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1114
- .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
1115
- .toString(), 'a b \\\n\tx\n');
1116
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1117
- .insert($mol_tree2.struct('x'), null, null, null)
1118
- .toString(), 'a b x\n');
1119
- $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1120
- .insert($mol_tree2.struct('x'), null, null, null, null)
1121
- .toString(), 'a b \\\n\tx\n');
1138
+ check($.$mol_tree2_from_string(`
1139
+ a b c d
1140
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c'), `
1141
+ a b x
1142
+ `);
1143
+ check($.$mol_tree2_from_string(`
1144
+ a b
1145
+ `).insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd'), `
1146
+ a b c x
1147
+ `);
1148
+ check($.$mol_tree2_from_string(`
1149
+ a b c d
1150
+ `)
1151
+ .insert($mol_tree2.struct('x'), 0, 0, 0), `
1152
+ a b x
1153
+ `);
1154
+ check($.$mol_tree2_from_string(`
1155
+ a b
1156
+ `)
1157
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0), `
1158
+ a b \\
1159
+ x
1160
+ `);
1161
+ check($.$mol_tree2_from_string(`
1162
+ a b c d
1163
+ `)
1164
+ .insert($mol_tree2.struct('x'), null, null, null), `
1165
+ a b x
1166
+ `);
1167
+ check($.$mol_tree2_from_string(`
1168
+ a b
1169
+ `)
1170
+ .insert($mol_tree2.struct('x'), null, null, null, null), `
1171
+ a b \\
1172
+ x
1173
+ `);
1174
+ },
1175
+ 'updating'($) {
1176
+ check($.$mol_tree2_from_string(`
1177
+ a b c d
1178
+ `).update([], 'a', 'b', 'c')[0], `
1179
+ a b
1180
+ `);
1181
+ check($.$mol_tree2_from_string(`
1182
+ a b c d
1183
+ `).update([$mol_tree2.struct('x')])[0], `
1184
+ x
1185
+ `);
1186
+ check($.$mol_tree2_from_string(`
1187
+ a b c d
1188
+ `).update([$mol_tree2.struct('x'), $mol_tree2.struct('y')], 'a', 'b', 'c')[0], `
1189
+ a b
1190
+ x
1191
+ y
1192
+ `);
1122
1193
  },
1123
1194
  'deleting'($) {
1124
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1125
- .insert(null, 'a', 'b', 'c')
1126
- .toString(), 'a b\n');
1127
- $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1128
- .insert(null, 0, 0, 0)
1129
- .toString(), 'a b\n');
1195
+ const base = $.$mol_tree2_from_string(`
1196
+ a b c d
1197
+ `);
1198
+ check(base.insert(null, 'a', 'b', 'c'), `
1199
+ a b
1200
+ `);
1201
+ check(base.update(base.select('a', 'b', 'c', null).kids, 'a', 'b', 'c')[0], `
1202
+ a b d
1203
+ `);
1204
+ check(base.insert(null, 0, 0, 0), `
1205
+ a b
1206
+ `);
1130
1207
  },
1131
1208
  'hack'($) {
1132
- const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
1209
+ const res = $.$mol_tree2_from_string(`
1210
+ foo bar xxx
1211
+ `)
1133
1212
  .hack({
1134
1213
  'bar': (input, belt) => [input.struct('777', input.hack(belt))],
1135
1214
  });