Best JavaScript code snippet using playwright-internal
patch-traverser.js
Source:patch-traverser.js
...11 nodeReplacement: null,12 parents: [],13 keys: []14 };15 this._traverse(node);16 }17 removeNode() {18 const parent = this.state.parents.last();19 const key = this.state.keys.last();20 if (key.length === 2) {21 parent[key[0]].splice(key[1], 1);22 } else {23 parent[key[0]] = undefined;24 }25 this.state.nodeReplacement = null;26 this.state.skip = true;27 }28 replaceNode(newNode) {29 const parent = this.state.parents.last();30 const key = this.state.keys.last();31 if (key.length === 2) {32 parent[key[0]][key[1]] = newNode;33 } else {34 parent[key[0]] = newNode;35 }36 this.state.nodeReplacement = newNode;37 }38 insertBefore(nodes) {39 const parent = this.state.parents.last();40 const key = this.state.keys.last();41 if (key.length === 2) {42 parent[key[0]].splice(key[1], 0, ...nodes);43 }44 }45 insertAfter(nodes) {46 const parent = this.state.parents.last();47 const key = this.state.keys.last();48 if (key.length === 2) {49 parent[key[0]].splice(key[1] + 1, 0, ...nodes);50 }51 }52 _traverse(node) {53 if (this.state.stop) return;54 if (!this[node.type]) {55 throw node.type + ' does not exist.';56 }57 const { enter, exit } = this.options;58 if (typeof enter === 'function') {59 enter(node, this.state);60 }61 if (this.state.nodeReplacement) {62 const newNode = this.state.nodeReplacement;63 this.state.nodeReplacement = null;64 this._traverse(newNode);65 return;66 }67 if (this.state.stop) return;68 if (!this.state.skip) {69 this.state.depth++;70 this.state.parents.push(node);71 this[node.type](node);72 this.state.parents.pop();73 this.state.depth--;74 }75 if (typeof exit === 'function') {76 exit(node, this.state);77 }78 this.state.skip = false;79 }80 // Parents: Expression, Pattern81 Identifier() { }82 // Parent: Expression83 Literal() { }84 // Parent: Node85 Program(node) {86 const lastIndex = this.state.keys.push([]) - 187 for (let i = 0; i < node.body.length; i++) {88 this.state.keys[lastIndex] = ['body', i]89 this._traverse(node.body[i]);90 if (this.state.stop) {91 i = node.body.length;92 }93 }94 this.state.keys.pop()95 }96 // Parent: Statement97 ExpressionStatement(node) {98 const lastIndex = this.state.keys.push([]) - 199 this.state.keys[lastIndex] = ['expression']100 this._traverse(node.expression);101 this.state.keys.pop()102 }103 // Parent: Statement104 BlockStatement(node) {105 const lastIndex = this.state.keys.push([]) - 1106 for (let i = 0; i < node.body.length; i++) {107 this.state.keys[lastIndex] = ['body', i]108 this._traverse(node.body[i]);109 if (this.state.stop) {110 i = node.body.length;111 }112 }113 this.state.keys.pop()114 }115 // Parent: Statement116 EmptyStatement() { }117 // Parent: Statement118 DebuggerStatement() { }119 // Parent: Statement120 WithStatement(node) {121 const lastIndex = this.state.keys.push([]) - 1122 this.state.keys[lastIndex] = ['object']123 this._traverse(node.object);124 this.state.keys[lastIndex] = ['body']125 this._traverse(node.body);126 this.state.keys.pop()127 }128 // Parent: Statement129 ReturnStatement(node) {130 const lastIndex = this.state.keys.push([]) - 1131 if (node.argument != null) {132 this.state.keys[lastIndex] = ['argument'];133 this._traverse(node.argument);134 }135 this.state.keys.pop()136 }137 // Parent: Statement138 LabeledStatement(node) {139 const lastIndex = this.state.keys.push([]) - 1140 this.state.keys[lastIndex] = ['label']141 this._traverse(node.label);142 this.state.keys[lastIndex] = ['body']143 this._traverse(node.body);144 this.state.keys.pop()145 }146 // Parent: Statement147 BreakStatement(node) {148 const lastIndex = this.state.keys.push([]) - 1149 if (node.label != null) {150 this.state.keys[lastIndex] = ['label'];151 this._traverse(node.label);152 }153 this.state.keys.pop()154 }155 // Parent: Statement156 ContinueStatement(node) {157 const lastIndex = this.state.keys.push([]) - 1158 if (node.label != null) {159 this.state.keys[lastIndex] = ['label'];160 this._traverse(node.label);161 }162 this.state.keys.pop()163 }164 // Parent: Statement165 IfStatement(node) {166 const lastIndex = this.state.keys.push([]) - 1167 this.state.keys[lastIndex] = ['test']168 this._traverse(node.test);169 this.state.keys[lastIndex] = ['consequent']170 this._traverse(node.consequent);171 if (node.alternate != null) {172 this.state.keys[lastIndex] = ['alternate'];173 this._traverse(node.alternate);174 }175 this.state.keys.pop()176 }177 // Parent: Statement178 SwitchStatement(node) {179 const lastIndex = this.state.keys.push([]) - 1180 this.state.keys[lastIndex] = ['discriminant']181 this._traverse(node.discriminant);182 for (let i = 0; i < node.cases.length; i++) {183 this.state.keys[lastIndex] = ['cases', i]184 this._traverse(node.cases[i]);185 if (this.state.stop) {186 i = node.cases.length;187 }188 }189 this.state.keys.pop()190 }191 // Parent: Node192 SwitchCase(node) {193 const lastIndex = this.state.keys.push([]) - 1194 if (node.test != null) {195 this.state.keys[lastIndex] = ['test'];196 this._traverse(node.test);197 }198 for (let i = 0; i < node.consequent.length; i++) {199 this.state.keys[lastIndex] = ['consequent', i]200 this._traverse(node.consequent[i]);201 if (this.state.stop) {202 i = node.consequent.length;203 }204 }205 this.state.keys.pop()206 }207 // Parent: Statement208 ThrowStatement(node) {209 const lastIndex = this.state.keys.push([]) - 1210 this.state.keys[lastIndex] = ['argument']211 this._traverse(node.argument);212 this.state.keys.pop()213 }214 // Parent: Statement215 TryStatement(node) {216 const lastIndex = this.state.keys.push([]) - 1217 this.state.keys[lastIndex] = ['block']218 this._traverse(node.block);219 if (node.handler != null) {220 this.state.keys[lastIndex] = ['handler'];221 this._traverse(node.handler);222 }223 if (node.finalizer != null) {224 this.state.keys[lastIndex] = ['finalizer'];225 this._traverse(node.finalizer);226 }227 this.state.keys.pop()228 }229 // Parent: Node230 CatchClause(node) {231 const lastIndex = this.state.keys.push([]) - 1232 this.state.keys[lastIndex] = ['param']233 this._traverse(node.param);234 this.state.keys[lastIndex] = ['body']235 this._traverse(node.body);236 this.state.keys.pop()237 }238 // Parent: Statement239 WhileStatement(node) {240 const lastIndex = this.state.keys.push([]) - 1241 this.state.keys[lastIndex] = ['test']242 this._traverse(node.test);243 this.state.keys[lastIndex] = ['body']244 this._traverse(node.body);245 this.state.keys.pop()246 }247 // Parent: Statement248 DoWhileStatement(node) {249 const lastIndex = this.state.keys.push([]) - 1250 this.state.keys[lastIndex] = ['body']251 this._traverse(node.body);252 this.state.keys[lastIndex] = ['test']253 this._traverse(node.test);254 this.state.keys.pop()255 }256 // Parent: Statement257 ForStatement(node) {258 const lastIndex = this.state.keys.push([]) - 1259 if (node.init != null) {260 this.state.keys[lastIndex] = ['init'];261 this._traverse(node.init);262 }263 if (node.test != null) {264 this.state.keys[lastIndex] = ['test'];265 this._traverse(node.test);266 }267 if (node.update != null) {268 this.state.keys[lastIndex] = ['update'];269 this._traverse(node.update);270 }271 this.state.keys[lastIndex] = ['body']272 this._traverse(node.body);273 this.state.keys.pop()274 }275 // Parent: Statement276 ForInStatement(node) {277 const lastIndex = this.state.keys.push([]) - 1278 this.state.keys[lastIndex] = ['left']279 this._traverse(node.left);280 this.state.keys[lastIndex] = ['right']281 this._traverse(node.right);282 this.state.keys[lastIndex] = ['body']283 this._traverse(node.body);284 this.state.keys.pop()285 }286 // Parents: Function, Declaration287 FunctionDeclaration(node) {288 const lastIndex = this.state.keys.push([]) - 1289 if (node.id != null) {290 this.state.keys[lastIndex] = ['id'];291 this._traverse(node.id);292 }293 for (let i = 0; i < node.params.length; i++) {294 this.state.keys[lastIndex] = ['params', i]295 this._traverse(node.params[i]);296 if (this.state.stop) {297 i = node.params.length;298 }299 }300 this.state.keys[lastIndex] = ['body']301 this._traverse(node.body);302 this.state.keys.pop()303 }304 // Parent: Declaration305 VariableDeclaration(node) {306 const lastIndex = this.state.keys.push([]) - 1307 for (let i = 0; i < node.declarations.length; i++) {308 this.state.keys[lastIndex] = ['declarations', i]309 this._traverse(node.declarations[i]);310 if (this.state.stop) {311 i = node.declarations.length;312 }313 }314 this.state.keys.pop()315 }316 // Parent: Node317 VariableDeclarator(node) {318 const lastIndex = this.state.keys.push([]) - 1319 this.state.keys[lastIndex] = ['id']320 this._traverse(node.id);321 if (node.init != null) {322 this.state.keys[lastIndex] = ['init'];323 this._traverse(node.init);324 }325 this.state.keys.pop()326 }327 // Parent: Expression328 ThisExpression() { }329 // Parent: Expression330 ArrayExpression(node) {331 const lastIndex = this.state.keys.push([]) - 1332 for (let i = 0; i < node.elements.length; i++) {333 if (node.elements[i] != null) {334 this.state.keys[lastIndex] = ['elements', i]335 }336 this._traverse(node.elements[i]);337 if (this.state.stop) {338 i = node.elements.length;339 }340 }341 this.state.keys.pop()342 }343 // Parent: Expression344 ObjectExpression(node) {345 const lastIndex = this.state.keys.push([]) - 1346 for (let i = 0; i < node.properties.length; i++) {347 this.state.keys[lastIndex] = ['properties', i]348 this._traverse(node.properties[i]);349 if (this.state.stop) {350 i = node.properties.length;351 }352 }353 this.state.keys.pop()354 }355 // Parent: Node356 Property(node) {357 const lastIndex = this.state.keys.push([]) - 1358 this.state.keys[lastIndex] = ['key']359 this._traverse(node.key);360 this.state.keys[lastIndex] = ['value']361 this._traverse(node.value);362 this.state.keys.pop()363 }364 // Parents: Function, Expression365 FunctionExpression(node) {366 const lastIndex = this.state.keys.push([]) - 1367 if (node.id != null) {368 this.state.keys[lastIndex] = ['id'];369 this._traverse(node.id);370 }371 for (let i = 0; i < node.params.length; i++) {372 this.state.keys[lastIndex] = ['params', i]373 this._traverse(node.params[i]);374 if (this.state.stop) {375 i = node.params.length;376 }377 }378 this.state.keys[lastIndex] = ['body']379 this._traverse(node.body);380 this.state.keys.pop()381 }382 // Parent: Expression383 UnaryExpression(node) {384 const lastIndex = this.state.keys.push([]) - 1385 this.state.keys[lastIndex] = ['argument']386 this._traverse(node.argument);387 this.state.keys.pop()388 }389 // Parent: Expression390 UpdateExpression(node) {391 const lastIndex = this.state.keys.push([]) - 1392 this.state.keys[lastIndex] = ['argument']393 this._traverse(node.argument);394 this.state.keys.pop()395 }396 // Parent: Expression397 BinaryExpression(node) {398 const lastIndex = this.state.keys.push([]) - 1399 this.state.keys[lastIndex] = ['left']400 this._traverse(node.left);401 this.state.keys[lastIndex] = ['right']402 this._traverse(node.right);403 this.state.keys.pop()404 }405 // Parent: Expression406 AssignmentExpression(node) {407 const lastIndex = this.state.keys.push([]) - 1408 this.state.keys[lastIndex] = ['left']409 this._traverse(node.left);410 this.state.keys[lastIndex] = ['right']411 this._traverse(node.right);412 this.state.keys.pop()413 }414 // Parent: Expression415 LogicalExpression(node) {416 const lastIndex = this.state.keys.push([]) - 1417 this.state.keys[lastIndex] = ['left']418 this._traverse(node.left);419 this.state.keys[lastIndex] = ['right']420 this._traverse(node.right);421 this.state.keys.pop()422 }423 // Parents: Expression, Pattern424 MemberExpression(node) {425 const lastIndex = this.state.keys.push([]) - 1426 this.state.keys[lastIndex] = ['object']427 this._traverse(node.object);428 this.state.keys[lastIndex] = ['property']429 this._traverse(node.property);430 this.state.keys.pop()431 }432 // Parent: Expression433 ConditionalExpression(node) {434 const lastIndex = this.state.keys.push([]) - 1435 this.state.keys[lastIndex] = ['test']436 this._traverse(node.test);437 this.state.keys[lastIndex] = ['alternate']438 this._traverse(node.alternate);439 this.state.keys[lastIndex] = ['consequent']440 this._traverse(node.consequent);441 this.state.keys.pop()442 }443 // Parent: Expression444 CallExpression(node) {445 const lastIndex = this.state.keys.push([]) - 1446 this.state.keys[lastIndex] = ['callee']447 this._traverse(node.callee);448 for (let i = 0; i < node.arguments.length; i++) {449 this.state.keys[lastIndex] = ['arguments', i]450 this._traverse(node.arguments[i]);451 if (this.state.stop) {452 i = node.arguments.length;453 }454 }455 this.state.keys.pop()456 }457 // Parent: Expression458 NewExpression(node) {459 const lastIndex = this.state.keys.push([]) - 1460 this.state.keys[lastIndex] = ['callee']461 this._traverse(node.callee);462 for (let i = 0; i < node.arguments.length; i++) {463 this.state.keys[lastIndex] = ['arguments', i]464 this._traverse(node.arguments[i]);465 if (this.state.stop) {466 i = node.arguments.length;467 }468 }469 this.state.keys.pop()470 }471 // Parent: Expression472 SequenceExpression(node) {473 const lastIndex = this.state.keys.push([]) - 1474 for (let i = 0; i < node.expressions.length; i++) {475 this.state.keys[lastIndex] = ['expressions', i]476 this._traverse(node.expressions[i]);477 if (this.state.stop) {478 i = node.expressions.length;479 }480 }481 this.state.keys.pop()482 }483 // Parent: ForInStatement484 ForOfStatement(node) {485 const lastIndex = this.state.keys.push([]) - 1486 this.state.keys[lastIndex] = ['left']487 this._traverse(node.left);488 this.state.keys[lastIndex] = ['right']489 this._traverse(node.right);490 this.state.keys[lastIndex] = ['body']491 this._traverse(node.body);492 this.state.keys.pop()493 }494 // Parent: Node495 Super() { }496 // Parent: Node497 SpreadElement(node) {498 const lastIndex = this.state.keys.push([]) - 1499 this.state.keys[lastIndex] = ['argument']500 this._traverse(node.argument);501 this.state.keys.pop()502 }503 // Parents: Function, Expression504 ArrowFunctionExpression(node) {505 const lastIndex = this.state.keys.push([]) - 1506 if (node.id != null) {507 this.state.keys[lastIndex] = ['id'];508 this._traverse(node.id);509 }510 for (let i = 0; i < node.params.length; i++) {511 this.state.keys[lastIndex] = ['params', i]512 this._traverse(node.params[i]);513 if (this.state.stop) {514 i = node.params.length;515 }516 }517 this.state.keys[lastIndex] = ['body']518 this._traverse(node.body);519 this.state.keys.pop()520 }521 // Parent: Expression522 YieldExpression(node) {523 const lastIndex = this.state.keys.push([]) - 1524 if (node.argument != null) {525 this.state.keys[lastIndex] = ['argument'];526 this._traverse(node.argument);527 }528 this.state.keys.pop()529 }530 // Parent: Expression531 TemplateLiteral(node) {532 const lastIndex = this.state.keys.push([]) - 1533 for (let i = 0; i < node.quasis.length; i++) {534 this.state.keys[lastIndex] = ['quasis', i]535 this._traverse(node.quasis[i]);536 if (this.state.stop) {537 i = node.quasis.length;538 }539 }540 for (let i = 0; i < node.expressions.length; i++) {541 this.state.keys[lastIndex] = ['expressions', i]542 this._traverse(node.expressions[i]);543 if (this.state.stop) {544 i = node.expressions.length;545 }546 }547 this.state.keys.pop()548 }549 // Parent: Expression550 TaggedTemplateExpression(node) {551 const lastIndex = this.state.keys.push([]) - 1552 this.state.keys[lastIndex] = ['tag']553 this._traverse(node.tag);554 this.state.keys[lastIndex] = ['quasi']555 this._traverse(node.quasi);556 this.state.keys.pop()557 }558 // Parent: Node559 TemplateElement() { }560 // Parent: Pattern561 ObjectPattern(node) {562 const lastIndex = this.state.keys.push([]) - 1563 for (let i = 0; i < node.properties.length; i++) {564 this.state.keys[lastIndex] = ['properties', i]565 this._traverse(node.properties[i]);566 if (this.state.stop) {567 i = node.properties.length;568 }569 }570 this.state.keys.pop()571 }572 // Parent: Pattern573 ArrayPattern(node) {574 const lastIndex = this.state.keys.push([]) - 1575 for (let i = 0; i < node.elements.length; i++) {576 if (node.elements[i] != null) {577 this.state.keys[lastIndex] = ['elements', i]578 }579 this._traverse(node.elements[i]);580 if (this.state.stop) {581 i = node.elements.length;582 }583 }584 this.state.keys.pop()585 }586 // Parent: Pattern587 RestElement(node) {588 const lastIndex = this.state.keys.push([]) - 1589 this.state.keys[lastIndex] = ['argument']590 this._traverse(node.argument);591 this.state.keys.pop()592 }593 // Parent: Pattern594 AssignmentPattern(node) {595 const lastIndex = this.state.keys.push([]) - 1596 this.state.keys[lastIndex] = ['left']597 this._traverse(node.left);598 this.state.keys[lastIndex] = ['right']599 this._traverse(node.right);600 this.state.keys.pop()601 }602 // Parent: Node603 ClassBody(node) {604 const lastIndex = this.state.keys.push([]) - 1605 for (let i = 0; i < node.body.length; i++) {606 this.state.keys[lastIndex] = ['body', i]607 this._traverse(node.body[i]);608 if (this.state.stop) {609 i = node.body.length;610 }611 }612 this.state.keys.pop()613 }614 // Parent: Node615 MethodDefinition(node) {616 const lastIndex = this.state.keys.push([]) - 1617 this.state.keys[lastIndex] = ['key']618 this._traverse(node.key);619 this.state.keys[lastIndex] = ['value']620 this._traverse(node.value);621 this.state.keys.pop()622 }623 // Parents: Class, Declaration624 ClassDeclaration(node) {625 const lastIndex = this.state.keys.push([]) - 1626 if (node.id != null) {627 this.state.keys[lastIndex] = ['id'];628 this._traverse(node.id);629 }630 if (node.superClass != null) {631 this.state.keys[lastIndex] = ['superClass'];632 this._traverse(node.superClass);633 }634 this.state.keys[lastIndex] = ['body']635 this._traverse(node.body);636 this.state.keys.pop()637 }638 // Parents: Class, Expression639 ClassExpression(node) {640 const lastIndex = this.state.keys.push([]) - 1641 if (node.id != null) {642 this.state.keys[lastIndex] = ['id'];643 this._traverse(node.id);644 }645 if (node.superClass != null) {646 this.state.keys[lastIndex] = ['superClass'];647 this._traverse(node.superClass);648 }649 this.state.keys[lastIndex] = ['body']650 this._traverse(node.body);651 this.state.keys.pop()652 }653 // Parent: Expression654 MetaProperty(node) {655 const lastIndex = this.state.keys.push([]) - 1656 this.state.keys[lastIndex] = ['meta']657 this._traverse(node.meta);658 this.state.keys[lastIndex] = ['property']659 this._traverse(node.property);660 this.state.keys.pop()661 }662 // Parent: ModuleDeclaration663 ImportDeclaration(node) {664 const lastIndex = this.state.keys.push([]) - 1665 for (let i = 0; i < node.specifiers.length; i++) {666 this.state.keys[lastIndex] = ['specifiers', i]667 this._traverse(node.specifiers[i]);668 if (this.state.stop) {669 i = node.specifiers.length;670 }671 }672 this.state.keys[lastIndex] = ['source']673 this._traverse(node.source);674 this.state.keys.pop()675 }676 // Parent: ModuleSpecifier677 ImportSpecifier(node) {678 const lastIndex = this.state.keys.push([]) - 1679 this.state.keys[lastIndex] = ['local']680 this._traverse(node.local);681 this.state.keys[lastIndex] = ['imported']682 this._traverse(node.imported);683 this.state.keys.pop()684 }685 // Parent: ModuleSpecifier686 ImportDefaultSpecifier(node) {687 const lastIndex = this.state.keys.push([]) - 1688 this.state.keys[lastIndex] = ['local']689 this._traverse(node.local);690 this.state.keys.pop()691 }692 // Parent: ModuleSpecifier693 ImportNamespaceSpecifier(node) {694 const lastIndex = this.state.keys.push([]) - 1695 this.state.keys[lastIndex] = ['local']696 this._traverse(node.local);697 this.state.keys.pop()698 }699 // Parent: ModuleDeclaration700 ExportNamedDeclaration(node) {701 const lastIndex = this.state.keys.push([]) - 1702 if (node.declaration != null) {703 this.state.keys[lastIndex] = ['declaration'];704 this._traverse(node.declaration);705 }706 for (let i = 0; i < node.specifiers.length; i++) {707 this.state.keys[lastIndex] = ['specifiers', i]708 this._traverse(node.specifiers[i]);709 if (this.state.stop) {710 i = node.specifiers.length;711 }712 }713 if (node.source != null) {714 this.state.keys[lastIndex] = ['source'];715 this._traverse(node.source);716 }717 this.state.keys.pop()718 }719 // Parent: ModuleSpecifier720 ExportSpecifier(node) {721 const lastIndex = this.state.keys.push([]) - 1722 this.state.keys[lastIndex] = ['local']723 this._traverse(node.local);724 this.state.keys[lastIndex] = ['exported']725 this._traverse(node.exported);726 this.state.keys.pop()727 }728 // Parent: ModuleDeclaration729 ExportDefaultDeclaration(node) {730 const lastIndex = this.state.keys.push([]) - 1731 this.state.keys[lastIndex] = ['declaration']732 this._traverse(node.declaration);733 this.state.keys.pop()734 }735 // Parent: ModuleDeclaration736 ExportAllDeclaration(node) {737 const lastIndex = this.state.keys.push([]) - 1738 this.state.keys[lastIndex] = ['source']739 this._traverse(node.source);740 if (node.exported != null) {741 this.state.keys[lastIndex] = ['exported'];742 this._traverse(node.exported);743 }744 this.state.keys.pop()745 }746 // Parent: Expression747 AwaitExpression(node) {748 const lastIndex = this.state.keys.push([]) - 1749 this.state.keys[lastIndex] = ['argument']750 this._traverse(node.argument);751 this.state.keys.pop()752 }753 // Parent: Expression754 ImportExpression(node) {755 const lastIndex = this.state.keys.push([]) - 1756 this.state.keys[lastIndex] = ['source']757 this._traverse(node.source);758 this.state.keys.pop()759 }760}761// https://stackoverflow.com/a/5197219762if (typeof module !== 'undefined' && module.exports) {763 module.exports = EstreePatchTraverser;...
generic-traverser.js
Source:generic-traverser.js
...10 stop: false,11 parents: [],12 keys: []13 };14 this._traverse(node);15 }16 _traverse(node) {17 if (this.state.stop) return;18 if (!this[node.type]) {19 throw node.type + ' does not exist.';20 }21 const { enter, exit } = this.options;22 if (typeof enter === 'function') {23 enter(node, this.state);24 }25 if (this.state.stop) return;26 if (!this.state.skip) {27 this.state.depth++;28 this.state.parents.push(node);29 this[node.type](node);30 this.state.parents.pop();31 this.state.depth--;32 }33 if (typeof exit === 'function') {34 exit(node, this.state);35 }36 this.state.skip = false;37 }38 // Parents: Expression, Pattern39 Identifier() { }40 // Parent: Expression41 Literal() { }42 // Parent: Node43 Program(node) {44 const lastIndex = this.state.keys.push([]) - 145 for (let i = 0; i < node.body.length; i++) {46 this.state.keys[lastIndex] = ['body', i]47 this._traverse(node.body[i]);48 if (this.state.stop) {49 i = node.body.length;50 }51 }52 this.state.keys.pop()53 }54 // Parent: Statement55 ExpressionStatement(node) {56 const lastIndex = this.state.keys.push([]) - 157 this.state.keys[lastIndex] = ['expression']58 this._traverse(node.expression);59 this.state.keys.pop()60 }61 // Parent: Statement62 BlockStatement(node) {63 const lastIndex = this.state.keys.push([]) - 164 for (let i = 0; i < node.body.length; i++) {65 this.state.keys[lastIndex] = ['body', i]66 this._traverse(node.body[i]);67 if (this.state.stop) {68 i = node.body.length;69 }70 }71 this.state.keys.pop()72 }73 // Parent: Statement74 EmptyStatement() { }75 // Parent: Statement76 DebuggerStatement() { }77 // Parent: Statement78 WithStatement(node) {79 const lastIndex = this.state.keys.push([]) - 180 this.state.keys[lastIndex] = ['object']81 this._traverse(node.object);82 this.state.keys[lastIndex] = ['body']83 this._traverse(node.body);84 this.state.keys.pop()85 }86 // Parent: Statement87 ReturnStatement(node) {88 const lastIndex = this.state.keys.push([]) - 189 if (node.argument != null) {90 this.state.keys[lastIndex] = ['argument'];91 this._traverse(node.argument);92 }93 this.state.keys.pop()94 }95 // Parent: Statement96 LabeledStatement(node) {97 const lastIndex = this.state.keys.push([]) - 198 this.state.keys[lastIndex] = ['label']99 this._traverse(node.label);100 this.state.keys[lastIndex] = ['body']101 this._traverse(node.body);102 this.state.keys.pop()103 }104 // Parent: Statement105 BreakStatement(node) {106 const lastIndex = this.state.keys.push([]) - 1107 if (node.label != null) {108 this.state.keys[lastIndex] = ['label'];109 this._traverse(node.label);110 }111 this.state.keys.pop()112 }113 // Parent: Statement114 ContinueStatement(node) {115 const lastIndex = this.state.keys.push([]) - 1116 if (node.label != null) {117 this.state.keys[lastIndex] = ['label'];118 this._traverse(node.label);119 }120 this.state.keys.pop()121 }122 // Parent: Statement123 IfStatement(node) {124 const lastIndex = this.state.keys.push([]) - 1125 this.state.keys[lastIndex] = ['test']126 this._traverse(node.test);127 this.state.keys[lastIndex] = ['consequent']128 this._traverse(node.consequent);129 if (node.alternate != null) {130 this.state.keys[lastIndex] = ['alternate'];131 this._traverse(node.alternate);132 }133 this.state.keys.pop()134 }135 // Parent: Statement136 SwitchStatement(node) {137 const lastIndex = this.state.keys.push([]) - 1138 this.state.keys[lastIndex] = ['discriminant']139 this._traverse(node.discriminant);140 for (let i = 0; i < node.cases.length; i++) {141 this.state.keys[lastIndex] = ['cases', i]142 this._traverse(node.cases[i]);143 if (this.state.stop) {144 i = node.cases.length;145 }146 }147 this.state.keys.pop()148 }149 // Parent: Node150 SwitchCase(node) {151 const lastIndex = this.state.keys.push([]) - 1152 if (node.test != null) {153 this.state.keys[lastIndex] = ['test'];154 this._traverse(node.test);155 }156 for (let i = 0; i < node.consequent.length; i++) {157 this.state.keys[lastIndex] = ['consequent', i]158 this._traverse(node.consequent[i]);159 if (this.state.stop) {160 i = node.consequent.length;161 }162 }163 this.state.keys.pop()164 }165 // Parent: Statement166 ThrowStatement(node) {167 const lastIndex = this.state.keys.push([]) - 1168 this.state.keys[lastIndex] = ['argument']169 this._traverse(node.argument);170 this.state.keys.pop()171 }172 // Parent: Statement173 TryStatement(node) {174 const lastIndex = this.state.keys.push([]) - 1175 this.state.keys[lastIndex] = ['block']176 this._traverse(node.block);177 if (node.handler != null) {178 this.state.keys[lastIndex] = ['handler'];179 this._traverse(node.handler);180 }181 if (node.finalizer != null) {182 this.state.keys[lastIndex] = ['finalizer'];183 this._traverse(node.finalizer);184 }185 this.state.keys.pop()186 }187 // Parent: Node188 CatchClause(node) {189 const lastIndex = this.state.keys.push([]) - 1190 this.state.keys[lastIndex] = ['param']191 this._traverse(node.param);192 this.state.keys[lastIndex] = ['body']193 this._traverse(node.body);194 this.state.keys.pop()195 }196 // Parent: Statement197 WhileStatement(node) {198 const lastIndex = this.state.keys.push([]) - 1199 this.state.keys[lastIndex] = ['test']200 this._traverse(node.test);201 this.state.keys[lastIndex] = ['body']202 this._traverse(node.body);203 this.state.keys.pop()204 }205 // Parent: Statement206 DoWhileStatement(node) {207 const lastIndex = this.state.keys.push([]) - 1208 this.state.keys[lastIndex] = ['body']209 this._traverse(node.body);210 this.state.keys[lastIndex] = ['test']211 this._traverse(node.test);212 this.state.keys.pop()213 }214 // Parent: Statement215 ForStatement(node) {216 const lastIndex = this.state.keys.push([]) - 1217 if (node.init != null) {218 this.state.keys[lastIndex] = ['init'];219 this._traverse(node.init);220 }221 if (node.test != null) {222 this.state.keys[lastIndex] = ['test'];223 this._traverse(node.test);224 }225 if (node.update != null) {226 this.state.keys[lastIndex] = ['update'];227 this._traverse(node.update);228 }229 this.state.keys[lastIndex] = ['body']230 this._traverse(node.body);231 this.state.keys.pop()232 }233 // Parent: Statement234 ForInStatement(node) {235 const lastIndex = this.state.keys.push([]) - 1236 this.state.keys[lastIndex] = ['left']237 this._traverse(node.left);238 this.state.keys[lastIndex] = ['right']239 this._traverse(node.right);240 this.state.keys[lastIndex] = ['body']241 this._traverse(node.body);242 this.state.keys.pop()243 }244 // Parents: Function, Declaration245 FunctionDeclaration(node) {246 const lastIndex = this.state.keys.push([]) - 1247 if (node.id != null) {248 this.state.keys[lastIndex] = ['id'];249 this._traverse(node.id);250 }251 for (let i = 0; i < node.params.length; i++) {252 this.state.keys[lastIndex] = ['params', i]253 this._traverse(node.params[i]);254 if (this.state.stop) {255 i = node.params.length;256 }257 }258 this.state.keys[lastIndex] = ['body']259 this._traverse(node.body);260 this.state.keys.pop()261 }262 // Parent: Declaration263 VariableDeclaration(node) {264 const lastIndex = this.state.keys.push([]) - 1265 for (let i = 0; i < node.declarations.length; i++) {266 this.state.keys[lastIndex] = ['declarations', i]267 this._traverse(node.declarations[i]);268 if (this.state.stop) {269 i = node.declarations.length;270 }271 }272 this.state.keys.pop()273 }274 // Parent: Node275 VariableDeclarator(node) {276 const lastIndex = this.state.keys.push([]) - 1277 this.state.keys[lastIndex] = ['id']278 this._traverse(node.id);279 if (node.init != null) {280 this.state.keys[lastIndex] = ['init'];281 this._traverse(node.init);282 }283 this.state.keys.pop()284 }285 // Parent: Expression286 ThisExpression() { }287 // Parent: Expression288 ArrayExpression(node) {289 const lastIndex = this.state.keys.push([]) - 1290 for (let i = 0; i < node.elements.length; i++) {291 if (node.elements[i] != null) {292 this.state.keys[lastIndex] = ['elements', i]293 }294 this._traverse(node.elements[i]);295 if (this.state.stop) {296 i = node.elements.length;297 }298 }299 this.state.keys.pop()300 }301 // Parent: Expression302 ObjectExpression(node) {303 const lastIndex = this.state.keys.push([]) - 1304 for (let i = 0; i < node.properties.length; i++) {305 this.state.keys[lastIndex] = ['properties', i]306 this._traverse(node.properties[i]);307 if (this.state.stop) {308 i = node.properties.length;309 }310 }311 this.state.keys.pop()312 }313 // Parent: Node314 Property(node) {315 const lastIndex = this.state.keys.push([]) - 1316 this.state.keys[lastIndex] = ['key']317 this._traverse(node.key);318 this.state.keys[lastIndex] = ['value']319 this._traverse(node.value);320 this.state.keys.pop()321 }322 // Parents: Function, Expression323 FunctionExpression(node) {324 const lastIndex = this.state.keys.push([]) - 1325 if (node.id != null) {326 this.state.keys[lastIndex] = ['id'];327 this._traverse(node.id);328 }329 for (let i = 0; i < node.params.length; i++) {330 this.state.keys[lastIndex] = ['params', i]331 this._traverse(node.params[i]);332 if (this.state.stop) {333 i = node.params.length;334 }335 }336 this.state.keys[lastIndex] = ['body']337 this._traverse(node.body);338 this.state.keys.pop()339 }340 // Parent: Expression341 UnaryExpression(node) {342 const lastIndex = this.state.keys.push([]) - 1343 this.state.keys[lastIndex] = ['argument']344 this._traverse(node.argument);345 this.state.keys.pop()346 }347 // Parent: Expression348 UpdateExpression(node) {349 const lastIndex = this.state.keys.push([]) - 1350 this.state.keys[lastIndex] = ['argument']351 this._traverse(node.argument);352 this.state.keys.pop()353 }354 // Parent: Expression355 BinaryExpression(node) {356 const lastIndex = this.state.keys.push([]) - 1357 this.state.keys[lastIndex] = ['left']358 this._traverse(node.left);359 this.state.keys[lastIndex] = ['right']360 this._traverse(node.right);361 this.state.keys.pop()362 }363 // Parent: Expression364 AssignmentExpression(node) {365 const lastIndex = this.state.keys.push([]) - 1366 this.state.keys[lastIndex] = ['left']367 this._traverse(node.left);368 this.state.keys[lastIndex] = ['right']369 this._traverse(node.right);370 this.state.keys.pop()371 }372 // Parent: Expression373 LogicalExpression(node) {374 const lastIndex = this.state.keys.push([]) - 1375 this.state.keys[lastIndex] = ['left']376 this._traverse(node.left);377 this.state.keys[lastIndex] = ['right']378 this._traverse(node.right);379 this.state.keys.pop()380 }381 // Parents: Expression, Pattern382 MemberExpression(node) {383 const lastIndex = this.state.keys.push([]) - 1384 this.state.keys[lastIndex] = ['object']385 this._traverse(node.object);386 this.state.keys[lastIndex] = ['property']387 this._traverse(node.property);388 this.state.keys.pop()389 }390 // Parent: Expression391 ConditionalExpression(node) {392 const lastIndex = this.state.keys.push([]) - 1393 this.state.keys[lastIndex] = ['test']394 this._traverse(node.test);395 this.state.keys[lastIndex] = ['alternate']396 this._traverse(node.alternate);397 this.state.keys[lastIndex] = ['consequent']398 this._traverse(node.consequent);399 this.state.keys.pop()400 }401 // Parent: Expression402 CallExpression(node) {403 const lastIndex = this.state.keys.push([]) - 1404 this.state.keys[lastIndex] = ['callee']405 this._traverse(node.callee);406 for (let i = 0; i < node.arguments.length; i++) {407 this.state.keys[lastIndex] = ['arguments', i]408 this._traverse(node.arguments[i]);409 if (this.state.stop) {410 i = node.arguments.length;411 }412 }413 this.state.keys.pop()414 }415 // Parent: Expression416 NewExpression(node) {417 const lastIndex = this.state.keys.push([]) - 1418 this.state.keys[lastIndex] = ['callee']419 this._traverse(node.callee);420 for (let i = 0; i < node.arguments.length; i++) {421 this.state.keys[lastIndex] = ['arguments', i]422 this._traverse(node.arguments[i]);423 if (this.state.stop) {424 i = node.arguments.length;425 }426 }427 this.state.keys.pop()428 }429 // Parent: Expression430 SequenceExpression(node) {431 const lastIndex = this.state.keys.push([]) - 1432 for (let i = 0; i < node.expressions.length; i++) {433 this.state.keys[lastIndex] = ['expressions', i]434 this._traverse(node.expressions[i]);435 if (this.state.stop) {436 i = node.expressions.length;437 }438 }439 this.state.keys.pop()440 }441 // Parent: ForInStatement442 ForOfStatement(node) {443 const lastIndex = this.state.keys.push([]) - 1444 this.state.keys[lastIndex] = ['left']445 this._traverse(node.left);446 this.state.keys[lastIndex] = ['right']447 this._traverse(node.right);448 this.state.keys[lastIndex] = ['body']449 this._traverse(node.body);450 this.state.keys.pop()451 }452 // Parent: Node453 Super() { }454 // Parent: Node455 SpreadElement(node) {456 const lastIndex = this.state.keys.push([]) - 1457 this.state.keys[lastIndex] = ['argument']458 this._traverse(node.argument);459 this.state.keys.pop()460 }461 // Parents: Function, Expression462 ArrowFunctionExpression(node) {463 const lastIndex = this.state.keys.push([]) - 1464 if (node.id != null) {465 this.state.keys[lastIndex] = ['id'];466 this._traverse(node.id);467 }468 for (let i = 0; i < node.params.length; i++) {469 this.state.keys[lastIndex] = ['params', i]470 this._traverse(node.params[i]);471 if (this.state.stop) {472 i = node.params.length;473 }474 }475 this.state.keys[lastIndex] = ['body']476 this._traverse(node.body);477 this.state.keys.pop()478 }479 // Parent: Expression480 YieldExpression(node) {481 const lastIndex = this.state.keys.push([]) - 1482 if (node.argument != null) {483 this.state.keys[lastIndex] = ['argument'];484 this._traverse(node.argument);485 }486 this.state.keys.pop()487 }488 // Parent: Expression489 TemplateLiteral(node) {490 const lastIndex = this.state.keys.push([]) - 1491 for (let i = 0; i < node.quasis.length; i++) {492 this.state.keys[lastIndex] = ['quasis', i]493 this._traverse(node.quasis[i]);494 if (this.state.stop) {495 i = node.quasis.length;496 }497 }498 for (let i = 0; i < node.expressions.length; i++) {499 this.state.keys[lastIndex] = ['expressions', i]500 this._traverse(node.expressions[i]);501 if (this.state.stop) {502 i = node.expressions.length;503 }504 }505 this.state.keys.pop()506 }507 // Parent: Expression508 TaggedTemplateExpression(node) {509 const lastIndex = this.state.keys.push([]) - 1510 this.state.keys[lastIndex] = ['tag']511 this._traverse(node.tag);512 this.state.keys[lastIndex] = ['quasi']513 this._traverse(node.quasi);514 this.state.keys.pop()515 }516 // Parent: Node517 TemplateElement() { }518 // Parent: Pattern519 ObjectPattern(node) {520 const lastIndex = this.state.keys.push([]) - 1521 for (let i = 0; i < node.properties.length; i++) {522 this.state.keys[lastIndex] = ['properties', i]523 this._traverse(node.properties[i]);524 if (this.state.stop) {525 i = node.properties.length;526 }527 }528 this.state.keys.pop()529 }530 // Parent: Pattern531 ArrayPattern(node) {532 const lastIndex = this.state.keys.push([]) - 1533 for (let i = 0; i < node.elements.length; i++) {534 if (node.elements[i] != null) {535 this.state.keys[lastIndex] = ['elements', i]536 }537 this._traverse(node.elements[i]);538 if (this.state.stop) {539 i = node.elements.length;540 }541 }542 this.state.keys.pop()543 }544 // Parent: Pattern545 RestElement(node) {546 const lastIndex = this.state.keys.push([]) - 1547 this.state.keys[lastIndex] = ['argument']548 this._traverse(node.argument);549 this.state.keys.pop()550 }551 // Parent: Pattern552 AssignmentPattern(node) {553 const lastIndex = this.state.keys.push([]) - 1554 this.state.keys[lastIndex] = ['left']555 this._traverse(node.left);556 this.state.keys[lastIndex] = ['right']557 this._traverse(node.right);558 this.state.keys.pop()559 }560 // Parent: Node561 ClassBody(node) {562 const lastIndex = this.state.keys.push([]) - 1563 for (let i = 0; i < node.body.length; i++) {564 this.state.keys[lastIndex] = ['body', i]565 this._traverse(node.body[i]);566 if (this.state.stop) {567 i = node.body.length;568 }569 }570 this.state.keys.pop()571 }572 // Parent: Node573 MethodDefinition(node) {574 const lastIndex = this.state.keys.push([]) - 1575 this.state.keys[lastIndex] = ['key']576 this._traverse(node.key);577 this.state.keys[lastIndex] = ['value']578 this._traverse(node.value);579 this.state.keys.pop()580 }581 // Parents: Class, Declaration582 ClassDeclaration(node) {583 const lastIndex = this.state.keys.push([]) - 1584 if (node.id != null) {585 this.state.keys[lastIndex] = ['id'];586 this._traverse(node.id);587 }588 if (node.superClass != null) {589 this.state.keys[lastIndex] = ['superClass'];590 this._traverse(node.superClass);591 }592 this.state.keys[lastIndex] = ['body']593 this._traverse(node.body);594 this.state.keys.pop()595 }596 // Parents: Class, Expression597 ClassExpression(node) {598 const lastIndex = this.state.keys.push([]) - 1599 if (node.id != null) {600 this.state.keys[lastIndex] = ['id'];601 this._traverse(node.id);602 }603 if (node.superClass != null) {604 this.state.keys[lastIndex] = ['superClass'];605 this._traverse(node.superClass);606 }607 this.state.keys[lastIndex] = ['body']608 this._traverse(node.body);609 this.state.keys.pop()610 }611 // Parent: Expression612 MetaProperty(node) {613 const lastIndex = this.state.keys.push([]) - 1614 this.state.keys[lastIndex] = ['meta']615 this._traverse(node.meta);616 this.state.keys[lastIndex] = ['property']617 this._traverse(node.property);618 this.state.keys.pop()619 }620 // Parent: ModuleDeclaration621 ImportDeclaration(node) {622 const lastIndex = this.state.keys.push([]) - 1623 for (let i = 0; i < node.specifiers.length; i++) {624 this.state.keys[lastIndex] = ['specifiers', i]625 this._traverse(node.specifiers[i]);626 if (this.state.stop) {627 i = node.specifiers.length;628 }629 }630 this.state.keys[lastIndex] = ['source']631 this._traverse(node.source);632 this.state.keys.pop()633 }634 // Parent: ModuleSpecifier635 ImportSpecifier(node) {636 const lastIndex = this.state.keys.push([]) - 1637 this.state.keys[lastIndex] = ['local']638 this._traverse(node.local);639 this.state.keys[lastIndex] = ['imported']640 this._traverse(node.imported);641 this.state.keys.pop()642 }643 // Parent: ModuleSpecifier644 ImportDefaultSpecifier(node) {645 const lastIndex = this.state.keys.push([]) - 1646 this.state.keys[lastIndex] = ['local']647 this._traverse(node.local);648 this.state.keys.pop()649 }650 // Parent: ModuleSpecifier651 ImportNamespaceSpecifier(node) {652 const lastIndex = this.state.keys.push([]) - 1653 this.state.keys[lastIndex] = ['local']654 this._traverse(node.local);655 this.state.keys.pop()656 }657 // Parent: ModuleDeclaration658 ExportNamedDeclaration(node) {659 const lastIndex = this.state.keys.push([]) - 1660 if (node.declaration != null) {661 this.state.keys[lastIndex] = ['declaration'];662 this._traverse(node.declaration);663 }664 for (let i = 0; i < node.specifiers.length; i++) {665 this.state.keys[lastIndex] = ['specifiers', i]666 this._traverse(node.specifiers[i]);667 if (this.state.stop) {668 i = node.specifiers.length;669 }670 }671 if (node.source != null) {672 this.state.keys[lastIndex] = ['source'];673 this._traverse(node.source);674 }675 this.state.keys.pop()676 }677 // Parent: ModuleSpecifier678 ExportSpecifier(node) {679 const lastIndex = this.state.keys.push([]) - 1680 this.state.keys[lastIndex] = ['local']681 this._traverse(node.local);682 this.state.keys[lastIndex] = ['exported']683 this._traverse(node.exported);684 this.state.keys.pop()685 }686 // Parent: ModuleDeclaration687 ExportDefaultDeclaration(node) {688 const lastIndex = this.state.keys.push([]) - 1689 this.state.keys[lastIndex] = ['declaration']690 this._traverse(node.declaration);691 this.state.keys.pop()692 }693 // Parent: ModuleDeclaration694 ExportAllDeclaration(node) {695 const lastIndex = this.state.keys.push([]) - 1696 this.state.keys[lastIndex] = ['source']697 this._traverse(node.source);698 if (node.exported != null) {699 this.state.keys[lastIndex] = ['exported'];700 this._traverse(node.exported);701 }702 this.state.keys.pop()703 }704 // Parent: Expression705 AwaitExpression(node) {706 const lastIndex = this.state.keys.push([]) - 1707 this.state.keys[lastIndex] = ['argument']708 this._traverse(node.argument);709 this.state.keys.pop()710 }711 // Parent: Expression712 ImportExpression(node) {713 const lastIndex = this.state.keys.push([]) - 1714 this.state.keys[lastIndex] = ['source']715 this._traverse(node.source);716 this.state.keys.pop()717 }...
BinaryTree.js
Source:BinaryTree.js
...16 if (!node) throw new Error('there is no root!');17 let queue = [];18 const _traverse = (node) => {19 queue.push(node.value);20 if (node.left) _traverse(node.left);21 if (node.right) _traverse(node.right);22 }23 _traverse(node);24 return queue;25 }26 inOrder() {27 let node = this.root;28 let queue = [];29 if (!node) throw new Error('there is no root');30 const _traverse = (node) => {31 if (node.left) _traverse(node.left);32 queue.push(node.value);33 if (node.right) _traverse(node.right);34 }35 _traverse(node);36 return queue;37 }38 postOrder() {39 let node = (this.root);40 let queue = [];41 if (!node) throw new Error('ther is no root');42 const _traverse = (node) => {43 if (node.left) _traverse(node.left);44 if (node.right) _traverse(node.right);45 queue.push(node.value);46 }47 _traverse(node);48 return queue;49 }50 getMax() {51 let max = this.root.value;52 const _traverse = (node) => {53 if (node.value > max) {54 max = node.value;55 }56 if (node.left) _traverse(node.left);57 if (node.right) _traverse(node.right);58 }59 _traverse(this.root);60 return max;61 }62 getMin() {63 let min = this.root.value;64 const _traverse = (node) => {65 if (node.value < min) {66 min = node.value67 }68 if (node.left) _traverse(node.left);69 if (node.right) _traverse(node.right);70 }71 _traverse(this.root);72 return min;73 }74 breadthFirst() {75 let results = [];76 let queue = [];77 queue.push(this.root);78 const _traverse = (node) => {79 while (queue.length > 0) {80 node = queue.shift();81 results.push(node.value);82 if (node.left) queue.push(node.left);83 if (node.right) queue.push(node.right);84 }85 }86 _traverse(this.root);87 return results;88 }89 fizzbuzzTree() {90 if (!this.root) throw new Error('there is no root!~');91 let results = [];92 const _traverse = (node) => {93 if (node.value % 15 === 0) {94 node.value = 'FizzBuzz';95 results.push(node.value);96 }97 else if (node.value % 5 === 0) {98 node.value = 'Fizz';99 results.push(node.value);100 }101 else if (node.value % 3 === 0) {102 node.value = 'Buzz';103 results.push(node.value);104 }105 else {106 node.value = `${node.value}`;107 results.push(node.value);108 }109 if (node.left) _traverse(node.left);110 if (node.right) _traverse(node.right);111 }112 _traverse(this.root);113 return results;114 }115 sumOfOddNums() {116 let sum = 0;117 if (!this.root) throw new Error('there is no root');118 const _traverse = (node) => {119 if (node.value % 2 !== 0) {120 sum = sum + node.value;121 }122 if (node.left) _traverse(node.left);123 if (node.right) _traverse(node.right);124 }125 _traverse(this.root);126 return sum;127 }128 sumOfEvenNums() {129 let sum = 0;130 const _traverse = (node) => {131 if (node.value % 2 === 0) {132 sum = sum + node.value;133 }134 if (node.left) _traverse(node.left);135 if (node.right) _traverse(node.right);136 }137 _traverse(this.root);138 return sum;139 }140 lonelyNodes() {141 let array = [];142 if (!this.root) throw new Error('there is no root');143 const _traverse = (node) => {144 if (node.left) {145 if (!node.right) {146 array.push(node.left.value);147 }148 _traverse(node.left)149 };150 if (node.right) {151 if (!node.left) {152 array.push(node.right.value);153 }154 _traverse(node.right)155 }156 }157 _traverse(this.root);158 return array;159 }160 heightOfTree(node) {161 if (!node) return 0;162 else {163 let leftTraversal = this.heightOfTree(node.left);164 let rightTraversal = this.heightOfTree(node.right);165 if (leftTraversal > rightTraversal) {166 return leftTraversal + 1167 }168 else {169 return rightTraversal + 1170 };171 }172 }173 leavesNum() {174 if (!this.root) throw new Error('there is no root');175 let array = [];176 const _tarverse = (node) => {177 if (!node.left && !node.right) {178 array.push(node.value);179 }180 if (node.left) _tarverse(node.left);181 if (node.right) _tarverse(node.right);182 }183 _tarverse(this.root);184 return array;185 }186 invert() {187 if (!this.root) throw new Error('there is no root!~');188 const _traverse = (node) => {189 let temp = node.left;190 node.left = node.right;191 node.right = temp;192 if (node.left) _traverse(node.left);193 if (node.right) _traverse(node.right);194 }195 _traverse(this.root);196 }197 merge(t1, t2) {198 if (!t1 && !t2) return null;199 let root = new Node((t1 ? t1.value : null) + (t2 ? t2.value : null));200 root.left = this.merge((t1 ? t1.left : null), (t2 ? t2.left : null));201 root.right = this.merge((t1 ? t1.right : null), (t2 ? t2.right : null));202 return root;203 }204 identical(p, q) {205 if (!p && !q) return true;206 if (!p && q || p && !q || p && q && p.value !== q.value) {207 return false;208 }209 if (p && q) {210 this.identical(p.left, q.left);211 this.identical(p.right, q.right);212 return true;213 }214 }215 balanced() {216 if (!this.root) throw new Error('there is no root love!~');217 let isBalanced = true;218 const _traverse = (node) => {219 if (node === null) {220 return 0;221 }222 let leftTraversal = _traverse(node.left);223 let rightTraversal = _traverse(node.right);224 if (Math.abs(leftTraversal - rightTraversal) > 0) {225 isBalanced = false;226 }227 return Math.max(leftTraversal, rightTraversal) + 1;228 }229 _traverse(this.root);230 return isBalanced;231 }232 mirror(node) {233 if (node === null) {234 return node;235 }236 let leftTraversal = this.mirror(node.left);237 let rightTraversal = this.mirror(node.right);238 if (node.left) {239 node.left = rightTraversal;240 }241 if (node.right) {242 node.right = leftTraversal;243 }244 return node;245 }246 minDepth(node) {247 if (!node) return 0;248 if (node.left === null && node.right === null) return 1;249 if (node.left === null) return this.minDepth(node.right) + 1;250 if (node.right === null) return this.minDepth(node.left) + 1;251 return Math.min(this.minDepth(node.left), this.minDepth(node.right)) + 1;252 }253 // define a tarversal254 // define an empty array queue255 // push the root to the array256 // define length of gthe queue257 deepestRootSum() {258 if (!this.root) throw new Error('no root!~');259 let sum = 0;260 const _traverse = (node) => {261 if (!node.left && !node.right) {262 sum = sum + node.value;263 }264 if (node.left) _traverse(node.left);265 if (node.right) _traverse(node.right);266 }267 _traverse(this.root);268 return sum;269 }270 trimBt(root, L, R) {271 if (root === null) return root;272 if (root.value < L) return this.trimBt(root.right, L, R);273 if (root.value > R) return this.trimBt(root.left, L, R);274 root.right = this.trimBt(root.right, L, R);275 root.left = this.trimBt(root.left, L, R);276 return root;277 }278 rangeSum(L, R) {279 if (!this.root) return 0;280 let sum = 0281 const _traverse = (node) => {282 if (node.value > L && node.value < R) {283 sum = sum + node.value;284 }285 if (node.left) _traverse(node.left);286 if (node.right) _traverse(node.right);287 }288 _traverse(this.root);289 return sum;290 }291}292class BinarySearchTree extends BinaryTree {293 add(value) {294 let node = new Node(value);295 if (this.root === null) {296 this.root = node;297 }298 const _traverse = (temp) => {299 if (temp.value > value) {300 if (!temp.left) {301 temp.left = node;302 return;303 }304 _traverse(temp.left);305 }306 if (temp.value < value) {307 if (!temp.right) {308 temp.right = node;309 return;310 }311 _traverse(temp.right);312 }313 }314 _traverse(this.root);315 return;316 }317 contains(num) {318 if (this.root === null) throw new Error('there is no root');319 const _traverse = (node) => {320 if (node.value > num && node.left) {321 console.log(node.value);322 return _traverse(node.left);323 }324 if (node.value < num && node.right) {325 return _traverse(node.right);326 }327 if (node.value === num) {328 return true;329 }330 else { return false; }331 }332 return _traverse(this.root);333 }334 lowestCommonAncestor(root, p, q) {335 if (!root) return null;336 if (root.value === p || root.value === q) { return root.value; }337 let leftTraversal = this.lowestCommonAncestor(root.left, p, q);338 let rightTraversal = this.lowestCommonAncestor(root.right, p, q);339 if (!leftTraversal) {340 return rightTraversal;341 }342 else if (!rightTraversal) {343 return leftTraversal;344 }345 else return root.value;346 }...
binaryTrees.js
Source:binaryTrees.js
...11 const queue = [];12 const _traverse = (node) => {13 queue.push(node.value);14 if (node.left) {15 _traverse(node.left);16 }17 if (node.right) {18 _traverse(node.right);19 }20 };21 _traverse(this.root);22 return queue;23 } catch (error) {24 console.error({ message: error.message });25 }26 }27 postOrder() {28 try {29 if (!this.root) {30 throw new Error('empty tree');31 }32 const queue = [];33 const _traverse = (node) => {34 if (node.left) {35 _traverse(node.left);36 }37 if (node.right) {38 _traverse(node.right);39 }40 queue.push(node.value);41 };42 _traverse(this.root);43 return queue;44 } catch (error) {45 console.error({ message: error.message });46 }47 }48 inOrder() {49 try {50 if (!this.root) {51 throw new Error('empty tree');52 }53 const queue = [];54 const _traverse = (node) => {55 if (node.left) {56 _traverse(node.left);57 }58 queue.push(node.value);59 if (node.right) {60 _traverse(node.right);61 }62 };63 _traverse(this.root);64 return queue;65 } catch (error) {66 console.error({ message: error.message });67 }68 }69 getMaxValue() {70 try {71 if (!this.root) {72 return 'empty tree';73 }74 let maxValue = this.root.value;75 const _traverse = (node) => {76 if (node.value >= maxValue) {77 maxValue = node.value;78 }79 if (node.right) {80 _traverse(node.right);81 }82 if (node.left) {83 _traverse(node.left);84 }85 };86 _traverse(this.root);87 return maxValue;88 } catch (error) {89 console.error({ message: error.message });90 }91 }92 breadthFirst(tree) {93 try {94 if (!this.root) {95 return 'empty tree';96 }97 let queue = [];98 let results = [];99 tree = this.root;100 queue.push(tree);101 const _traverse = (node) => {102 while (queue.length > 0) {103 node = queue[0];104 results.push(node.value);105 if (node.left !== null) {106 queue.push(node.left);107 }108 if (node.right !== null) {109 queue.push(node.right);110 }111 queue.shift();112 }113 };114 _traverse(tree);115 return results;116 } catch (error) {117 console.error({ message: error.message });118 }119 }120 treeFizzBuzz(tree) {121 if (!this.root) {122 return 'Empty Tree';123 }124 tree = this.root;125 const results = [];126 const _traverse = (node) => {127 if (node.value % 15 === 0) {128 node.value = 'FizzBuzz';129 results.push(node.value);130 } else if (node.value % 5 === 0) {131 node.value = 'Buzz';132 results.push(node.value);133 } else if (node.value % 3 === 0) {134 node.value = 'Fizz';135 results.push(node.value);136 } else {137 node.value = `${node.value}`;138 results.push(node.value);139 }140 if (node.left !== null) {141 _traverse(node.left);142 }143 if (node.right !== null) {144 _traverse(node.right);145 }146 };147 _traverse(tree);148 return results;149 }150}...
tree.js
Source:tree.js
...14 preOrder() {15 const output = [];16 const _traverse = (node) => {17 output.push(node.value);18 if (node.left) _traverse(node.left);19 if (node.right) _traverse(node.right);20 };21 _traverse(this.root);22 return output;23 }24 // Left - Root - Right25 inOrder() {26 const output = [];27 const _traverse = (node) => {28 if (node.left) _traverse(node.left);29 output.push(node.value);30 if (node.right) _traverse(node.right);31 };32 _traverse(this.root);33 return output;34 }35 // Left - Right - Root36 postOrder() {37 const output = [];38 const _traverse = (node) => {39 if (node.left) _traverse(node.left);40 if (node.right) _traverse(node.right);41 output.push(node.value);42 };43 _traverse(this.root);44 return output;45 }46 findMax() {47 let max = 0;48 if (!this.root) return null;49 const _traverse = (node) => {50 if (node.left) {51 max = _max(node.left, max);52 _traverse(node.left);53 }54 max = _max(node, max);55 if (node.right) {56 max = _max(node.right, max);57 _traverse(node.right);58 }59 };60 _traverse(this.root);61 return max;62 }63 breadthFirst() {64 let output = [];65 if (!this.root) {66 return null;67 }68 const _breadthFirst = (root) => {69 let queue = [root];70 while (queue.length > 0) {71 let node = queue.shift();72 let value = node.value;73 output.push(value); // global array74 if (node.left == null && node.right == null) {...
binary-tree.js
Source:binary-tree.js
...6 const result = [];7 if (!this.root) throw new Error('Tree is Empty');8 const _traverse = (node) => {9 result.push(node.value);10 if (node.left) _traverse(node.left);11 if (node.right) _traverse(node.right);12 }13 _traverse(this.root);14 return result;15 }16 postOrder() {17 const result = [];18 if (!this.root) throw new Error('Tree is Empty');19 const _traverse = (node) => {20 if (node.left) _traverse(node.left);21 if (node.right) _traverse(node.right);22 result.push(node.value);23 };24 _traverse(this.root);25 return result;26 }27 inOrder() {28 const result = [];29 if (!this.root) throw new Error('Tree is Empty');30 const _traverse = (node) => {31 if (node.left) _traverse(node.left);32 result.push(node.value);33 if (node.right) _traverse(node.right);34 };35 _traverse(this.root);36 return result;37 }38 getMax() {39 if (!this.root) {40 throw new Error("Tree is Empty");41 }42 let max = this.root.value;43 const _traverse = (node) => {44 if (node.value >= max) {45 max = node.value;46 }47 if (node.left) _traverse(node.left);48 if (node.right) _traverse(node.right);49 }50 _traverse(this.root);51 return max;52 }53}...
bt.js
Source:bt.js
...6 const result = [];7 if (!this.root) throw new Error('Tree is Empty');8 const _traverse = (node) => {9 result.push(node.value);10 if (node.left) _traverse(node.left);11 if (node.right) _traverse(node.right);12 }13 _traverse(this.root);14 return result;15 }16 postOrder() {17 const result = [];18 if (!this.root) throw new Error('Tree is Empty');19 const _traverse = (node) => {20 if (node.left) _traverse(node.left);21 if (node.right) _traverse(node.right);22 result.push(node.value);23 };24 _traverse(this.root);25 return result;26 }27 inOrder() {28 const result = [];29 if (!this.root) throw new Error('Tree is Empty');30 const _traverse = (node) => {31 if (node.left) _traverse(node.left);32 result.push(node.value);33 if (node.right) _traverse(node.right);34 };35 _traverse(this.root);36 return result;37 }38 getMax() {39 if (!this.root) {40 throw new Error("Tree is Empty");41 }42 let max = this.root.value;43 const _traverse = (node) => {44 if (node.value >= max) {45 max = node.value;46 }47 if (node.left) _traverse(node.left);48 if (node.right) _traverse(node.right);49 }50 _traverse(this.root);51 return max;52 }53}...
index.js
Source:index.js
...10 const results = [];11 const _traverse = (node) => {12 //push the value to the array13 results.push(node.value);14 // check if it have left _traverse(node.left)15 if (node.left) _traverse(node.left);16 // check if it have right _traverse(node.right)17 if (node.right) _traverse(node.right);18 };19 _traverse(this.root);20 return results;21 }22 // Left - Root - Right23 inOrder() {24 const results = [];25 const _traverse = (node) => {26 // check if it have left _traverse(node.left)27 if (node.left) _traverse(node.left);28 //push the value to the array29 results.push(node.value);30 // check if it have right _traverse(node.right)31 if (node.right) _traverse(node.right);32 };33 _traverse(this.root);34 return results;35 }36 // left - Right - Root37 postOrder() {38 const results = [];39 const _traverse = (node) => {40 // check if it have left _traverse(node.left)41 if (node.left) _traverse(node.left);42 // check if it have right _traverse(node.right)43 if (node.right) _traverse(node.right);44 //push the value to the array45 results.push(node.value);46 };47 _traverse(this.root);48 return results;49 }50}...
Using AI Code Generation
1const { _traverse } = require('playwright/lib/utils/utils');2const { Page } = require('playwright/lib/server/page');3const { Frame } = require('playwright/lib/server/frame');4const { ElementHandle } = require('playwright/lib/server/dom');5const { JSHandle } = require('playwright/lib/server/jsHandle');6const { JSHandleDispatcher } = require('playwright/lib/server/dispatchers/jsHandleDispatcher');7const page = await browser.newPage();8const searchElementHandle = await page.$('[name="q"]');9const frame = page.mainFrame();10const elementHandle = await frame.$('.gLFyf');11const jsHandle = await elementHandle.getProperty('value');12const jsHandleDispatcher = new JSHandleDispatcher(page._connection, jsHandle);13const value = await jsHandleDispatcher.evaluateHandle((e) => e);14console.log(await value.jsonValue());15console.log(await _traverse(value, ['value', 'length']));16console.log(await _traverse(value, ['value', '0', 'length']));17console.log(await _traverse(value, ['value', '0', '0', 'length']));18console.log(await _traverse(value, ['value', '0', '0', '0', 'length']));19console.log(await _traverse(value, ['value', '0', '0', '0', '0', 'length']));20console.log(await _traverse(value, ['value', '0', '0', '0', '0', '0', 'length']));21console.log(await _traverse(value, ['value', '0', '0', '0', '0', '0', '0', 'length']));22console.log(await _traverse(value, ['value', '0', '0', '0', '0', '0', '0', '0', 'length']));23console.log(await _traverse(value, ['value', '0', '0', '0', '0', '0', '0', '0', '0', 'length']));24console.log(await _traverse(value, ['value', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'length']));25console.log(await _traverse(value, ['value', '0', '0', '0', '0', '0', '0', '0',
Using AI Code Generation
1const { _traverse } = require('playwright/lib/server/frames');2const { Page } = require('playwright/lib/server/page');3const { Frame } = require('playwright/lib/server/frame');4const page = await browser.newPage();5const frame = page.mainFrame();6const newFrame = await page.addInitScript(() => {7 const newFrame = document.createElement('iframe');8 document.body.appendChild(newFrame);9 return newFrame;10});11const frameElement = _traverse(frame, newFrame._id);12const frameObject = Frame.from(frameElement);13const newPage = Page.from(frameObject);14await newPage.waitForLoadState('load');
Using AI Code Generation
1const { _traverse } = require('playwright/lib/utils/utils');2const { Page } = require('playwright/lib/server/page');3const { ElementHandle } = require('playwright/lib/server/dom');4const page = new Page(null, null, null);5const elementHandle = new ElementHandle(null, null, null);6const result = _traverse(page, elementHandle, 'selector');7console.log(result);8const { _traverse } = require('playwright/lib/utils/utils');9const { Page } = require('playwright/lib/server/page');10const { ElementHandle } = require('playwright/lib/server/dom');11const page = new Page(null, null, null);12const elementHandle = new ElementHandle(null, null, null);13const result = _traverse(page, elementHandle, 'selector');14console.log(result);15const { _traverse } = require('playwright/lib/utils/utils');16const { Page } = require('playwright/lib/server/page');17const { ElementHandle } = require('playwright/lib/server/dom');18const page = new Page(null, null, null);19const elementHandle = new ElementHandle(null, null, null);20const result = _traverse(page, elementHandle, 'selector');21console.log(result);22const { _traverse } = require('playwright/lib/utils/utils');23const { Page } = require('playwright/lib/server/page');24const { ElementHandle } = require('playwright/lib/server/dom');25const page = new Page(null, null, null);26const elementHandle = new ElementHandle(null, null, null);27const result = _traverse(page, elementHandle, '
Using AI Code Generation
1const { _traverse } = require("playwright/lib/utils/utils");2const { ElementHandle } = require("playwright/lib/JSHandle");3const { _traverse } = require("playwright/lib/utils/utils");4const { ElementHandle } = require("playwright/lib/JSHandle");5const { chromium } = require("playwright");6(async () => {7 const browser = await chromium.launch();8 const context = await browser.newContext();9 const page = await context.newPage();10 const searchInput = await page.$("input[name='q']");11 const searchInputText = await _traverse(searchInput, "input.value");12 const searchInputText2 = await _traverse(searchInput, "input.value", {13 });14 await browser.close();15})();
Using AI Code Generation
1const { _traverse } = require('@playwright/test/lib/server/frames');2const frame = page.mainFrame();3const jsHandle = await frame.evaluateHandle(() => document.querySelector('body'));4const result = await _traverse(jsHandle);5console.log(result);6const { _traverse } = require('@playwright/test/lib/server/frames');7const frame = page.mainFrame();8const jsHandle = await frame.evaluateHandle(() => document.querySelector('body'));9const result = await _traverse(jsHandle);10console.log(result);11const { _traverse } = require('@playwright/test/lib/server/frames');12const frame = page.mainFrame();13const jsHandle = await frame.evaluateHandle(() => document.querySelector('body'));14const result = await _traverse(jsHandle);15console.log(result);16const { _traverse } = require('@playwright/test/lib/server/frames');17const frame = page.mainFrame();18const jsHandle = await frame.evaluateHandle(() => document.querySelector('body'));19const result = await _traverse(jsHandle);20console.log(result);21const { _traverse } = require('@playwright/test/lib/server/frames');22const frame = page.mainFrame();23const jsHandle = await frame.evaluateHandle(() => document.querySelector('body'));24const result = await _traverse(jsHandle);25console.log(result);26const { _traverse } = require('@playwright/test/lib/server/frames');27const frame = page.mainFrame();28const jsHandle = await frame.evaluateHandle(() => document.querySelector('body'));29const result = await _traverse(jsHandle);30console.log(result);31const { _traverse } = require('@playwright/test/lib/server/frames');32const frame = page.mainFrame();33const jsHandle = await frame.evaluateHandle(() => document.querySelector('body'));34const result = await _traverse(jsHandle);35console.log(result);36const { _traverse } = require('@playwright/test/lib/server/frames');37const frame = page.mainFrame();
Using AI Code Generation
1const { InternalAPI } = require('playwright');2const internalAPI = new InternalAPI();3const { _traverse } = internalAPI;4const selector = 'some-selector';5const page = new Page();6const handle = await page.$(selector);7const element = await handle.asElement();8const traverseResult = await _traverse(element, selector);9console.log(traverseResult);10const { InternalAPI } = require('playwright');11const internalAPI = new InternalAPI();12const { _traverse } = internalAPI;13const selector = 'some-selector';14const page = new Page();15const handle = await page.$(selector);16const element = await handle.asElement();17const traverseResult = await _traverse(element, selector);18console.log(traverseResult);19const { InternalAPI } = require('playwright');20const internalAPI = new InternalAPI();21const { _traverse } = internalAPI;22const selector = 'some-selector';23const page = new Page();24const handle = await page.$(selector);25const element = await handle.asElement();26const traverseResult = await _traverse(element, selector);27console.log(traverseResult);28const { InternalAPI } = require('playwright');29const internalAPI = new InternalAPI();30const { _traverse } = internalAPI;31const selector = 'some-selector';32const page = new Page();33const handle = await page.$(selector);34const element = await handle.asElement();35const traverseResult = await _traverse(element, selector);36console.log(traverseResult);37const { InternalAPI } = require('playwright');38const internalAPI = new InternalAPI();39const { _traverse } = internalAPI;40const selector = 'some-selector';41const page = new Page();42const handle = await page.$(selector);43const element = await handle.asElement();44const traverseResult = await _traverse(element, selector);45console.log(traverseResult);46const { InternalAPI } = require('playwright');
Using AI Code Generation
1const { _traverse } = require('playwright/lib/server/dom.js');2const { parseSelector } = require('playwright/lib/server/selectorParser.js');3const selector = 'css=div#target';4const parsedSelector = parseSelector(selector);5const element = await _traverse(parsedSelector, document);6console.log(element);7const { _traverse } = require('playwright/lib/server/dom.js');8const { parseSelector } = require('playwright/lib/server/selectorParser.js');9const selector = 'css=div#target';10const parsedSelector = parseSelector(selector);11const element = await _traverse(parsedSelector, document);12console.log(element);13const { _traverse } = require('playwright/lib/server/dom.js');14const { parseSelector } = require('playwright/lib/server/selectorParser.js');15const selector = 'css=div#target';16const parsedSelector = parseSelector(selector);17const element = await _traverse(parsedSelector, document);18console.log(element);19const { _traverse } = require('playwright/lib/server/dom.js');20const { parseSelector } = require('playwright/lib/server/selectorParser.js');21const selector = 'css=div#target';22const parsedSelector = parseSelector(selector);23const element = await _traverse(parsedSelector, document);24console.log(element);25const { _traverse } = require('playwright/lib/server/dom.js');26const { parseSelector } = require('play
Using AI Code Generation
1const { _traverse } = require('playwright/lib/server/dom.js');2const { chromium } = require('playwright');3const path = require('path');4(async () => {5 const browser = await chromium.launch();6 const page = await browser.newPage();7 const element = await _traverse(page.mainFrame(), (node) => {8 if (node.nodeType === 3 && node.nodeValue === 'I\'m Feeling Lucky') {9 return node;10 }11 });12 await browser.close();13})();
Using AI Code Generation
1const { _traverse } = require('playwright/lib/client/selectorEngine');2const selector = 'text=Click me';3const text = await _traverse(selector, document);4console.log(text);5const { _traverse } = require('playwright/lib/client/selectorEngine');6const selector = 'input[name="txtUsername"]';7const text = await _traverse(selector, document);8console.log(text);9const { _traverse } = require('playwright/lib/client/selectorEngine');10const selector = 'input[name="txtUsername"]';11const text = await _traverse(selector, document);12console.log(text);13const { _traverse } = require('playwright/lib/client/selectorEngine');14const selector = 'input[name="txtUsername"]';15const text = await _traverse(selector, document);16console.log(text);17const { _traverse } = require('playwright/lib/client/selectorEngine');18const selector = 'input[name="txtUsername"]';19const text = await _traverse(selector, document);20console.log(text);
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!