UNPKG

@dialpad/dialtone

Version:

Dialpad's Dialtone design system monorepo

1 lines 4.33 kB
{"version":3,"file":"node-traversal.cjs","names":[],"sources":["../../../common/test_utils/node_traversal.js"],"sourcesContent":["/**\n * Utility functions for traversing and searching nodes in editor JSON structures\n */\n\n/**\n * Traverses a node tree and calls a callback function for each node\n * @param {Object} node - The node to traverse\n * @param {Function} callback - Function to call for each node\n */\nexport function traverseNode(node, callback) {\n callback(node);\n if (node.content) {\n node.content.forEach(childNode => traverseNode(childNode, callback));\n }\n}\n\n/**\n * Traverses an array of nodes (typically the root content array)\n * @param {Array} nodes - Array of nodes to traverse\n * @param {Function} callback - Function to call for each node\n */\nexport function traverseNodes(nodes, callback) {\n if (nodes && Array.isArray(nodes)) {\n nodes.forEach(node => traverseNode(node, callback));\n }\n}\n\n/**\n * Finds a node matching a condition in the node tree\n * @param {Array} nodes - Array of nodes to search\n * @param {Function} predicate - Function that returns true when the desired node is found\n * @returns {Object|null} The found node or null\n */\nexport function findNode(nodes, predicate) {\n let foundNode = null;\n\n traverseNodes(nodes, (node) => {\n if (!foundNode && predicate(node)) {\n foundNode = node;\n }\n });\n\n return foundNode;\n}\n\n/**\n * Finds a variable node by its ID\n * @param {Array} nodes - Array of nodes to search\n * @param {string} variableId - The ID of the variable to find\n * @returns {Object|null} The variable node or null\n */\nexport function findVariable(nodes, variableId) {\n return findNode(nodes, node =>\n node.type === 'variable' && node.attrs?.id === variableId,\n );\n}\n\n/**\n * Finds all nodes matching a condition\n * @param {Array} nodes - Array of nodes to search\n * @param {Function} predicate - Function that returns true for nodes to include\n * @returns {Array} Array of matching nodes\n */\nexport function findAllNodes(nodes, predicate) {\n const foundNodes = [];\n\n traverseNodes(nodes, (node) => {\n if (predicate(node)) {\n foundNodes.push(node);\n }\n });\n\n return foundNodes;\n}\n\n/**\n * Finds all variable nodes in the node tree\n * @param {Array} nodes - Array of nodes to search\n * @returns {Array} Array of variable nodes\n */\nexport function findAllVariables(nodes) {\n return findAllNodes(nodes, node => node.type === 'variable');\n}\n\n/**\n * Counts nodes matching a condition\n * @param {Array} nodes - Array of nodes to search\n * @param {Function} predicate - Function that returns true for nodes to count\n * @returns {number} Count of matching nodes\n */\nexport function countNodes(nodes, predicate) {\n let count = 0;\n\n traverseNodes(nodes, (node) => {\n if (predicate(node)) {\n count++;\n }\n });\n\n return count;\n}\n\n/**\n * Counts variable nodes in the node tree\n * @param {Array} nodes - Array of nodes to search\n * @returns {number} Count of variable nodes\n */\nexport function countVariables(nodes) {\n return countNodes(nodes, node => node.type === 'variable');\n}\n\n/**\n * Checks if a variable with a specific ID exists\n * @param {Array} nodes - Array of nodes to search\n * @param {string} variableId - The ID of the variable to check\n * @returns {boolean} True if the variable exists\n */\nexport function variableExists(nodes, variableId) {\n return findVariable(nodes, variableId) !== null;\n}\n"],"mappings":"mEASA,SAAgB,EAAa,EAAM,EAAU,CAC3C,EAAS,EAAK,CACV,EAAK,SACP,EAAK,QAAQ,QAAQ,GAAa,EAAa,EAAW,EAAS,CAAC,CASxE,SAAgB,EAAc,EAAO,EAAU,CACzC,GAAS,MAAM,QAAQ,EAAM,EAC/B,EAAM,QAAQ,GAAQ,EAAa,EAAM,EAAS,CAAC,CAUvD,SAAgB,EAAS,EAAO,EAAW,CACzC,IAAI,EAAY,KAQhB,OANA,EAAc,EAAQ,GAAS,CACzB,CAAC,GAAa,EAAU,EAAK,GAC/B,EAAY,IAEd,CAEK,EAST,SAAgB,EAAa,EAAO,EAAY,CAC9C,OAAO,EAAS,EAAO,GACrB,EAAK,OAAS,YAAc,EAAK,OAAO,KAAO,EAChD,CASH,SAAgB,EAAa,EAAO,EAAW,CAC7C,IAAM,EAAa,EAAE,CAQrB,OANA,EAAc,EAAQ,GAAS,CACzB,EAAU,EAAK,EACjB,EAAW,KAAK,EAAK,EAEvB,CAEK,EAQT,SAAgB,EAAiB,EAAO,CACtC,OAAO,EAAa,EAAO,GAAQ,EAAK,OAAS,WAAW,CAS9D,SAAgB,EAAW,EAAO,EAAW,CAC3C,IAAI,EAAQ,EAQZ,OANA,EAAc,EAAQ,GAAS,CACzB,EAAU,EAAK,EACjB,KAEF,CAEK,EAQT,SAAgB,EAAe,EAAO,CACpC,OAAO,EAAW,EAAO,GAAQ,EAAK,OAAS,WAAW,CAS5D,SAAgB,EAAe,EAAO,EAAY,CAChD,OAAO,EAAa,EAAO,EAAW,GAAK"}