clarity-pattern-parser 4.0.2 → 4.0.3
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/dist/index.browser.js +205 -205
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.esm.js +205 -205
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +205 -205
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +7 -2
package/dist/index.browser.js
CHANGED
|
@@ -1044,6 +1044,211 @@
|
|
|
1044
1044
|
}
|
|
1045
1045
|
}
|
|
1046
1046
|
|
|
1047
|
+
class Visitor {
|
|
1048
|
+
constructor(root = null, selectedNodes = []) {
|
|
1049
|
+
this.root = root;
|
|
1050
|
+
this.selectedNodes = selectedNodes;
|
|
1051
|
+
}
|
|
1052
|
+
flatten() {
|
|
1053
|
+
this.selectedNodes.forEach((node) => {
|
|
1054
|
+
if (node.children.length > 0) {
|
|
1055
|
+
const children = [];
|
|
1056
|
+
Visitor.walkUp(node, (descendant) => {
|
|
1057
|
+
if (descendant.children.length === 0) {
|
|
1058
|
+
children.push(descendant);
|
|
1059
|
+
}
|
|
1060
|
+
});
|
|
1061
|
+
node.children = children;
|
|
1062
|
+
}
|
|
1063
|
+
});
|
|
1064
|
+
return this;
|
|
1065
|
+
}
|
|
1066
|
+
remove() {
|
|
1067
|
+
if (this.root == null) {
|
|
1068
|
+
return this;
|
|
1069
|
+
}
|
|
1070
|
+
this.recursiveRemove(this.root);
|
|
1071
|
+
return this;
|
|
1072
|
+
}
|
|
1073
|
+
recursiveRemove(node) {
|
|
1074
|
+
const nodesToRemove = this.selectedNodes;
|
|
1075
|
+
for (let x = 0; x < node.children.length; x++) {
|
|
1076
|
+
if (nodesToRemove.indexOf(node.children[x]) > -1) {
|
|
1077
|
+
node.children.splice(x, 1);
|
|
1078
|
+
x--;
|
|
1079
|
+
}
|
|
1080
|
+
else {
|
|
1081
|
+
this.recursiveRemove(node.children[x]);
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
wrap(callback) {
|
|
1086
|
+
const visitor = new Visitor(this.root);
|
|
1087
|
+
visitor.selectRoot().transform((node) => {
|
|
1088
|
+
if (this.selectedNodes.includes(node)) {
|
|
1089
|
+
return callback(node);
|
|
1090
|
+
}
|
|
1091
|
+
return node;
|
|
1092
|
+
});
|
|
1093
|
+
return this;
|
|
1094
|
+
}
|
|
1095
|
+
unwrap() {
|
|
1096
|
+
if (this.root == null) {
|
|
1097
|
+
return this;
|
|
1098
|
+
}
|
|
1099
|
+
Visitor.walkDown(this.root, (node, stack) => {
|
|
1100
|
+
if (this.selectedNodes.includes(node)) {
|
|
1101
|
+
const parent = stack[stack.length - 1];
|
|
1102
|
+
const grandParent = stack[stack.length - 2];
|
|
1103
|
+
if (parent != null && grandParent != null) {
|
|
1104
|
+
const index = grandParent.children.indexOf(parent);
|
|
1105
|
+
if (index > -1) {
|
|
1106
|
+
grandParent.children.splice(index, 1, ...parent.children);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
});
|
|
1111
|
+
return this;
|
|
1112
|
+
}
|
|
1113
|
+
prepend(callback) {
|
|
1114
|
+
if (this.root == null) {
|
|
1115
|
+
return this;
|
|
1116
|
+
}
|
|
1117
|
+
Visitor.walkUp(this.root, (node, stack) => {
|
|
1118
|
+
if (this.selectedNodes.includes(node)) {
|
|
1119
|
+
const parent = stack[stack.length - 1];
|
|
1120
|
+
if (parent != null) {
|
|
1121
|
+
const index = parent.children.indexOf(node);
|
|
1122
|
+
if (index > -1) {
|
|
1123
|
+
parent.children.splice(index, 0, callback(node));
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
});
|
|
1128
|
+
return this;
|
|
1129
|
+
}
|
|
1130
|
+
append(callback) {
|
|
1131
|
+
if (this.root == null) {
|
|
1132
|
+
return this;
|
|
1133
|
+
}
|
|
1134
|
+
Visitor.walkDown(this.root, (node, stack) => {
|
|
1135
|
+
if (this.selectedNodes.includes(node)) {
|
|
1136
|
+
const parent = stack[stack.length - 1];
|
|
1137
|
+
if (parent != null) {
|
|
1138
|
+
const index = parent.children.indexOf(node);
|
|
1139
|
+
if (index > -1) {
|
|
1140
|
+
parent.children.splice(index + 1, 0, callback(node));
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
});
|
|
1145
|
+
return this;
|
|
1146
|
+
}
|
|
1147
|
+
transform(callback) {
|
|
1148
|
+
this.selectedNodes.forEach((node) => {
|
|
1149
|
+
return this.recursiveTransform(node, callback);
|
|
1150
|
+
});
|
|
1151
|
+
return this;
|
|
1152
|
+
}
|
|
1153
|
+
recursiveTransform(node, callback) {
|
|
1154
|
+
const length = node.children.length;
|
|
1155
|
+
for (let x = 0; x < length; x++) {
|
|
1156
|
+
node.children[x] = this.recursiveTransform(node.children[x], callback);
|
|
1157
|
+
}
|
|
1158
|
+
return callback(node);
|
|
1159
|
+
}
|
|
1160
|
+
selectAll() {
|
|
1161
|
+
return this.select((n) => true);
|
|
1162
|
+
}
|
|
1163
|
+
selectNode(node) {
|
|
1164
|
+
return new Visitor(this.root, [...this.selectedNodes, node]);
|
|
1165
|
+
}
|
|
1166
|
+
deselectNode(node) {
|
|
1167
|
+
const visitor = new Visitor(this.root, this.selectedNodes.slice());
|
|
1168
|
+
return visitor.filter((n) => n !== node);
|
|
1169
|
+
}
|
|
1170
|
+
select(callback) {
|
|
1171
|
+
if (this.root == null) {
|
|
1172
|
+
return this;
|
|
1173
|
+
}
|
|
1174
|
+
const node = this.root;
|
|
1175
|
+
const selectedNodes = [];
|
|
1176
|
+
if (node.children.length > 0) {
|
|
1177
|
+
Visitor.walkDown(node, (descendant) => {
|
|
1178
|
+
if (callback(descendant)) {
|
|
1179
|
+
selectedNodes.push(descendant);
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
1182
|
+
}
|
|
1183
|
+
return new Visitor(this.root, selectedNodes);
|
|
1184
|
+
}
|
|
1185
|
+
forEach(callback) {
|
|
1186
|
+
this.selectedNodes.forEach(callback);
|
|
1187
|
+
return this;
|
|
1188
|
+
}
|
|
1189
|
+
filter(callback) {
|
|
1190
|
+
return new Visitor(this.root, this.selectedNodes.filter(callback));
|
|
1191
|
+
}
|
|
1192
|
+
map(callback) {
|
|
1193
|
+
return new Visitor(this.root, this.selectedNodes.map(callback));
|
|
1194
|
+
}
|
|
1195
|
+
selectRoot() {
|
|
1196
|
+
if (this.root == null) {
|
|
1197
|
+
return this;
|
|
1198
|
+
}
|
|
1199
|
+
return new Visitor(this.root, [this.root]);
|
|
1200
|
+
}
|
|
1201
|
+
first() {
|
|
1202
|
+
return this.get(0);
|
|
1203
|
+
}
|
|
1204
|
+
last() {
|
|
1205
|
+
return this.get(this.selectedNodes.length - 1);
|
|
1206
|
+
}
|
|
1207
|
+
get(index) {
|
|
1208
|
+
const node = this.selectedNodes[index];
|
|
1209
|
+
if (node == null) {
|
|
1210
|
+
throw new Error(`Couldn't find node at index: ${index}, out of ${this.selectedNodes.length}.`);
|
|
1211
|
+
}
|
|
1212
|
+
return new Visitor(node, []);
|
|
1213
|
+
}
|
|
1214
|
+
clear() {
|
|
1215
|
+
this.selectedNodes = [];
|
|
1216
|
+
return this;
|
|
1217
|
+
}
|
|
1218
|
+
setRoot(root) {
|
|
1219
|
+
this.root = root;
|
|
1220
|
+
return this;
|
|
1221
|
+
}
|
|
1222
|
+
static select(root, callback) {
|
|
1223
|
+
if (callback != null) {
|
|
1224
|
+
return new Visitor(root).select(callback);
|
|
1225
|
+
}
|
|
1226
|
+
else {
|
|
1227
|
+
return new Visitor(root);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
static walkUp(node, callback, ancestors = []) {
|
|
1231
|
+
ancestors.push(node);
|
|
1232
|
+
if (node.children.length > 0) {
|
|
1233
|
+
const children = node.children.slice();
|
|
1234
|
+
children.forEach((c) => this.walkUp(c, callback, ancestors));
|
|
1235
|
+
}
|
|
1236
|
+
ancestors.pop();
|
|
1237
|
+
callback(node, ancestors);
|
|
1238
|
+
return this;
|
|
1239
|
+
}
|
|
1240
|
+
static walkDown(node, callback, ancestors = []) {
|
|
1241
|
+
callback(node, ancestors);
|
|
1242
|
+
ancestors.push(node);
|
|
1243
|
+
if (node.children.length > 0) {
|
|
1244
|
+
const children = node.children.slice();
|
|
1245
|
+
children.forEach((c) => this.walkDown(c, callback, ancestors));
|
|
1246
|
+
}
|
|
1247
|
+
ancestors.pop();
|
|
1248
|
+
return this;
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1047
1252
|
class TextSuggester {
|
|
1048
1253
|
constructor() {
|
|
1049
1254
|
this.cursor = null;
|
|
@@ -1288,211 +1493,6 @@
|
|
|
1288
1493
|
}
|
|
1289
1494
|
}
|
|
1290
1495
|
|
|
1291
|
-
class Visitor {
|
|
1292
|
-
constructor(root = null, selectedNodes = []) {
|
|
1293
|
-
this.root = root;
|
|
1294
|
-
this.selectedNodes = selectedNodes;
|
|
1295
|
-
}
|
|
1296
|
-
flatten() {
|
|
1297
|
-
this.selectedNodes.forEach((node) => {
|
|
1298
|
-
if (node.children.length > 0) {
|
|
1299
|
-
const children = [];
|
|
1300
|
-
Visitor.walkUp(node, (descendant) => {
|
|
1301
|
-
if (descendant.children.length === 0) {
|
|
1302
|
-
children.push(descendant);
|
|
1303
|
-
}
|
|
1304
|
-
});
|
|
1305
|
-
node.children = children;
|
|
1306
|
-
}
|
|
1307
|
-
});
|
|
1308
|
-
return this;
|
|
1309
|
-
}
|
|
1310
|
-
remove() {
|
|
1311
|
-
if (this.root == null) {
|
|
1312
|
-
return this;
|
|
1313
|
-
}
|
|
1314
|
-
this.recursiveRemove(this.root);
|
|
1315
|
-
return this;
|
|
1316
|
-
}
|
|
1317
|
-
recursiveRemove(node) {
|
|
1318
|
-
const nodesToRemove = this.selectedNodes;
|
|
1319
|
-
for (let x = 0; x < node.children.length; x++) {
|
|
1320
|
-
if (nodesToRemove.indexOf(node.children[x]) > -1) {
|
|
1321
|
-
node.children.splice(x, 1);
|
|
1322
|
-
x--;
|
|
1323
|
-
}
|
|
1324
|
-
else {
|
|
1325
|
-
this.recursiveRemove(node.children[x]);
|
|
1326
|
-
}
|
|
1327
|
-
}
|
|
1328
|
-
}
|
|
1329
|
-
wrap(callback) {
|
|
1330
|
-
const visitor = new Visitor(this.root);
|
|
1331
|
-
visitor.selectRoot().transform((node) => {
|
|
1332
|
-
if (this.selectedNodes.includes(node)) {
|
|
1333
|
-
return callback(node);
|
|
1334
|
-
}
|
|
1335
|
-
return node;
|
|
1336
|
-
});
|
|
1337
|
-
return this;
|
|
1338
|
-
}
|
|
1339
|
-
unwrap() {
|
|
1340
|
-
if (this.root == null) {
|
|
1341
|
-
return this;
|
|
1342
|
-
}
|
|
1343
|
-
Visitor.walkDown(this.root, (node, stack) => {
|
|
1344
|
-
if (this.selectedNodes.includes(node)) {
|
|
1345
|
-
const parent = stack[stack.length - 1];
|
|
1346
|
-
const grandParent = stack[stack.length - 2];
|
|
1347
|
-
if (parent != null && grandParent != null) {
|
|
1348
|
-
const index = grandParent.children.indexOf(parent);
|
|
1349
|
-
if (index > -1) {
|
|
1350
|
-
grandParent.children.splice(index, 1, ...parent.children);
|
|
1351
|
-
}
|
|
1352
|
-
}
|
|
1353
|
-
}
|
|
1354
|
-
});
|
|
1355
|
-
return this;
|
|
1356
|
-
}
|
|
1357
|
-
prepend(callback) {
|
|
1358
|
-
if (this.root == null) {
|
|
1359
|
-
return this;
|
|
1360
|
-
}
|
|
1361
|
-
Visitor.walkUp(this.root, (node, stack) => {
|
|
1362
|
-
if (this.selectedNodes.includes(node)) {
|
|
1363
|
-
const parent = stack[stack.length - 1];
|
|
1364
|
-
if (parent != null) {
|
|
1365
|
-
const index = parent.children.indexOf(node);
|
|
1366
|
-
if (index > -1) {
|
|
1367
|
-
parent.children.splice(index, 0, callback(node));
|
|
1368
|
-
}
|
|
1369
|
-
}
|
|
1370
|
-
}
|
|
1371
|
-
});
|
|
1372
|
-
return this;
|
|
1373
|
-
}
|
|
1374
|
-
append(callback) {
|
|
1375
|
-
if (this.root == null) {
|
|
1376
|
-
return this;
|
|
1377
|
-
}
|
|
1378
|
-
Visitor.walkDown(this.root, (node, stack) => {
|
|
1379
|
-
if (this.selectedNodes.includes(node)) {
|
|
1380
|
-
const parent = stack[stack.length - 1];
|
|
1381
|
-
if (parent != null) {
|
|
1382
|
-
const index = parent.children.indexOf(node);
|
|
1383
|
-
if (index > -1) {
|
|
1384
|
-
parent.children.splice(index + 1, 0, callback(node));
|
|
1385
|
-
}
|
|
1386
|
-
}
|
|
1387
|
-
}
|
|
1388
|
-
});
|
|
1389
|
-
return this;
|
|
1390
|
-
}
|
|
1391
|
-
transform(callback) {
|
|
1392
|
-
this.selectedNodes.forEach((node) => {
|
|
1393
|
-
return this.recursiveTransform(node, callback);
|
|
1394
|
-
});
|
|
1395
|
-
return this;
|
|
1396
|
-
}
|
|
1397
|
-
recursiveTransform(node, callback) {
|
|
1398
|
-
const length = node.children.length;
|
|
1399
|
-
for (let x = 0; x < length; x++) {
|
|
1400
|
-
node.children[x] = this.recursiveTransform(node.children[x], callback);
|
|
1401
|
-
}
|
|
1402
|
-
return callback(node);
|
|
1403
|
-
}
|
|
1404
|
-
selectAll() {
|
|
1405
|
-
return this.select((n) => true);
|
|
1406
|
-
}
|
|
1407
|
-
selectNode(node) {
|
|
1408
|
-
return new Visitor(this.root, [...this.selectedNodes, node]);
|
|
1409
|
-
}
|
|
1410
|
-
deselectNode(node) {
|
|
1411
|
-
const visitor = new Visitor(this.root, this.selectedNodes.slice());
|
|
1412
|
-
return visitor.filter((n) => n !== node);
|
|
1413
|
-
}
|
|
1414
|
-
select(callback) {
|
|
1415
|
-
if (this.root == null) {
|
|
1416
|
-
return this;
|
|
1417
|
-
}
|
|
1418
|
-
const node = this.root;
|
|
1419
|
-
const selectedNodes = [];
|
|
1420
|
-
if (node.children.length > 0) {
|
|
1421
|
-
Visitor.walkDown(node, (descendant) => {
|
|
1422
|
-
if (callback(descendant)) {
|
|
1423
|
-
selectedNodes.push(descendant);
|
|
1424
|
-
}
|
|
1425
|
-
});
|
|
1426
|
-
}
|
|
1427
|
-
return new Visitor(this.root, selectedNodes);
|
|
1428
|
-
}
|
|
1429
|
-
forEach(callback) {
|
|
1430
|
-
this.selectedNodes.forEach(callback);
|
|
1431
|
-
return this;
|
|
1432
|
-
}
|
|
1433
|
-
filter(callback) {
|
|
1434
|
-
return new Visitor(this.root, this.selectedNodes.filter(callback));
|
|
1435
|
-
}
|
|
1436
|
-
map(callback) {
|
|
1437
|
-
return new Visitor(this.root, this.selectedNodes.map(callback));
|
|
1438
|
-
}
|
|
1439
|
-
selectRoot() {
|
|
1440
|
-
if (this.root == null) {
|
|
1441
|
-
return this;
|
|
1442
|
-
}
|
|
1443
|
-
return new Visitor(this.root, [this.root]);
|
|
1444
|
-
}
|
|
1445
|
-
first() {
|
|
1446
|
-
return this.get(0);
|
|
1447
|
-
}
|
|
1448
|
-
last() {
|
|
1449
|
-
return this.get(this.selectedNodes.length - 1);
|
|
1450
|
-
}
|
|
1451
|
-
get(index) {
|
|
1452
|
-
const node = this.selectedNodes[index];
|
|
1453
|
-
if (node == null) {
|
|
1454
|
-
throw new Error(`Couldn't find node at index: ${index}, out of ${this.selectedNodes.length}.`);
|
|
1455
|
-
}
|
|
1456
|
-
return new Visitor(node, []);
|
|
1457
|
-
}
|
|
1458
|
-
clear() {
|
|
1459
|
-
this.selectedNodes = [];
|
|
1460
|
-
return this;
|
|
1461
|
-
}
|
|
1462
|
-
setRoot(root) {
|
|
1463
|
-
this.root = root;
|
|
1464
|
-
return this;
|
|
1465
|
-
}
|
|
1466
|
-
static select(root, callback) {
|
|
1467
|
-
if (callback != null) {
|
|
1468
|
-
return new Visitor(root).select(callback);
|
|
1469
|
-
}
|
|
1470
|
-
else {
|
|
1471
|
-
return new Visitor(root);
|
|
1472
|
-
}
|
|
1473
|
-
}
|
|
1474
|
-
static walkUp(node, callback, ancestors = []) {
|
|
1475
|
-
ancestors.push(node);
|
|
1476
|
-
if (node.children.length > 0) {
|
|
1477
|
-
const children = node.children.slice();
|
|
1478
|
-
children.forEach((c) => this.walkUp(c, callback, ancestors));
|
|
1479
|
-
}
|
|
1480
|
-
ancestors.pop();
|
|
1481
|
-
callback(node, ancestors);
|
|
1482
|
-
return this;
|
|
1483
|
-
}
|
|
1484
|
-
static walkDown(node, callback, ancestors = []) {
|
|
1485
|
-
callback(node, ancestors);
|
|
1486
|
-
ancestors.push(node);
|
|
1487
|
-
if (node.children.length > 0) {
|
|
1488
|
-
const children = node.children.slice();
|
|
1489
|
-
children.forEach((c) => this.walkDown(c, callback, ancestors));
|
|
1490
|
-
}
|
|
1491
|
-
ancestors.pop();
|
|
1492
|
-
return this;
|
|
1493
|
-
}
|
|
1494
|
-
}
|
|
1495
|
-
|
|
1496
1496
|
exports.And = And;
|
|
1497
1497
|
exports.Cursor = Cursor;
|
|
1498
1498
|
exports.Literal = Literal;
|