Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 158x 1x 1x 157x 157x 1x 1x 1x 1x 1x 30x 30x 15x 15x 15x 15x 15x 15x 30x 30x 1x 1x 1x 1x 1x 76x 76x 76x 76x 76x 76x 76x 1x 1x 1x 1x 1x 2509x 2509x 76x 2509x 2433x 2433x 2509x 2509x 1x 1x 1x 1x 1x 1x 40x 40x 18x 30x 18x 18x 40x 40x 40x 1x 1x 1x 1x 1x 106x 106x 15x 15x 15x 15x 15x 106x 91x 91x 91x 91x 91x 106x 106x 1x 1x 1x 1x 1x 1x 3235x 3235x 180x 180x 3235x 30x 3235x 338x 3025x 24x 2687x 72x 2663x 106x 2591x 2485x 2485x 3055x 3055x 1x 1x 1x 1x 1x 163x 162x 162x 158x 158x 1x 1x 1x 1x 1x 1x 1x 1x 1x 163x 163x 163x 163x 163x 163x 163x 163x 3235x 3235x 163x 163x 163x 163x 1x 1x | // const unlink = require('../handlers/basic/unlink.js'); const queryElementTransformer = require('./queryElementTransformer.js'); const operators = ['=', '<', '>', '!', '∈', '∉', '@', '?']; /** * Validate if length of result is either 1 or 3, otherwise throw error */ const validateResult = (result, query) => { if (result.length !== 3 && result.length !== 1) { throw new Error(`Invalid query ${query}`); } return result; }; /** * On opening bracket up array counter by one, on closing bracket lower array counter by one */ const handleBrackets = (char, actionVariables) => { const newActionVariables = actionVariables; if (char === '[') { newActionVariables.part += char; newActionVariables.arrayCounter += 1; } else if (char === ']') { newActionVariables.part += char; newActionVariables.arrayCounter += -1; } return newActionVariables; }; /** * Call if first operator is found, pushes current path into results */ const handleFirstNonOperator = (char, actionVariables, funcs) => { const newActionVariables = actionVariables; newActionVariables.result.push( queryElementTransformer(newActionVariables.part, funcs), ); newActionVariables.part = char; return newActionVariables; }; /** * Default handler of characters, adds to current part until operator is found */ const defaultCharHandler = (char, actionVariables, funcs) => { let newActionVariables = actionVariables; if (operators.indexOf(newActionVariables.part) > -1) { newActionVariables = handleFirstNonOperator(char, newActionVariables, funcs); } else { newActionVariables.part += char; } return newActionVariables; }; /** * Handles quotes to set isString key of actionVariables */ // eslint-disable-next-line complexity const handleQuotes = (char, actionVariables, funcs) => { let newActionVariables = actionVariables; if (!newActionVariables.isString) { newActionVariables.isString = char; } else if (newActionVariables.isString && char === newActionVariables.isString) { newActionVariables.isString = null; } newActionVariables = defaultCharHandler(char, newActionVariables, funcs); return newActionVariables; }; /** * Pushes operators into results */ const handleOperators = (char, actionVariables, funcs) => { const newActionVariables = actionVariables; if (operators.indexOf(newActionVariables.part) > -1) { newActionVariables.part += char; newActionVariables.result.push( queryElementTransformer(newActionVariables.part, funcs), ); newActionVariables.part = ''; } else { newActionVariables.result.push( queryElementTransformer(newActionVariables.part, funcs), ); newActionVariables.part = char; } return newActionVariables; }; /** * Handles each character of string and redirects to specific handlers */ // eslint-disable-next-line complexity const handleCharacters = (char, actionVariables, funcs) => { let newActionVariables = actionVariables; if (char === ' ' && !newActionVariables.arrayCounter && !newActionVariables.isString) { return newActionVariables; } if (['[', ']'].indexOf(char) > -1) { newActionVariables = handleBrackets(char, newActionVariables); } else if (newActionVariables.arrayCounter > 0) { newActionVariables.part += char; } else if (['"', "'"].indexOf(char) > -1) { newActionVariables = handleQuotes(char, newActionVariables, funcs); } else if (newActionVariables.isString) { newActionVariables.part += char; } else if (operators.indexOf(char) > -1) { newActionVariables = handleOperators(char, newActionVariables, funcs); } else { newActionVariables = defaultCharHandler(char, newActionVariables, funcs); } return newActionVariables; }; /** * Push remaining parts into results */ const addFinalResult = (actionVariables, funcs) => { if (actionVariables.part) { actionVariables.result.push(queryElementTransformer(actionVariables.part, funcs)); } return actionVariables; }; /** * Transforms string representation of query into workable array representation * @param {String} query - string representation of query to be performed * @param {Object} funcs - object with functions provided by the user that may be part of the query * @returns {Array} - array representation of query with length of 1 or 3, * second element is the operator */ const queryTransformer = (query, funcs) => { const substractedQuery = query.split(''); let actionVariables = { result: [], isString: null, part: '', arrayCounter: 0, }; substractedQuery.forEach((char) => { actionVariables = handleCharacters(char, actionVariables, funcs); }); addFinalResult(actionVariables, funcs); validateResult(actionVariables.result, query); return actionVariables.result; }; module.exports = queryTransformer; |