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