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 151 152 153 154 155 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 54x 1x 1x 53x 53x 1x 1x 1x 1x 1x 22x 22x 11x 11x 11x 11x 11x 11x 22x 22x 1x 1x 1x 1x 1x 32x 32x 32x 32x 32x 32x 32x 1x 1x 1x 1x 1x 876x 876x 32x 876x 844x 844x 876x 876x 1x 1x 1x 1x 1x 1x 25x 25x 12x 15x 12x 12x 25x 25x 25x 1x 1x 1x 1x 1x 50x 50x 9x 9x 9x 9x 9x 50x 41x 41x 41x 41x 41x 50x 50x 1x 1x 1x 1x 1x 1x 1283x 1283x 80x 80x 1283x 22x 1283x 231x 1181x 9x 950x 24x 941x 50x 917x 867x 867x 1203x 1203x 1x 1x 1x 1x 1x 59x 58x 58x 54x 54x 1x 1x 1x 1x 1x 1x 1x 1x 90x 31x 31x 59x 59x 59x 59x 59x 59x 59x 59x 1283x 1283x 59x 59x 59x 59x 59x 1x 1x | // const unlink = require('../handlers/basic/unlink.js'); const queryElementTransformer = require('./queryElementTransformer.js'); const transformCache = {}; 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) => { const newActionVariables = actionVariables; newActionVariables.result.push( queryElementTransformer(newActionVariables.part), ); newActionVariables.part = char; return newActionVariables; }; /** * Default handler of characters, adds to current part until operator is found */ const defaultCharHandler = (char, actionVariables) => { let newActionVariables = actionVariables; if (operators.indexOf(newActionVariables.part) > -1) { newActionVariables = handleFirstNonOperator(char, newActionVariables); } else { newActionVariables.part += char; } return newActionVariables; }; /** * Handles quotes to set isString key of actionVariables */ // eslint-disable-next-line complexity const handleQuotes = (char, actionVariables) => { let newActionVariables = actionVariables; if (!newActionVariables.isString) { newActionVariables.isString = char; } else if (newActionVariables.isString && char === newActionVariables.isString) { newActionVariables.isString = null; } newActionVariables = defaultCharHandler(char, newActionVariables); return newActionVariables; }; /** * Pushes operators into results */ const handleOperators = (char, actionVariables) => { const newActionVariables = actionVariables; if (operators.indexOf(newActionVariables.part) > -1) { newActionVariables.part += char; newActionVariables.result.push( queryElementTransformer(newActionVariables.part), ); newActionVariables.part = ''; } else { newActionVariables.result.push( queryElementTransformer(newActionVariables.part), ); newActionVariables.part = char; } return newActionVariables; }; /** * Handles each character of string and redirects to specific handlers */ // eslint-disable-next-line complexity const handleCharacters = (char, actionVariables) => { 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); } else if (newActionVariables.isString) { newActionVariables.part += char; } else if (operators.indexOf(char) > -1) { newActionVariables = handleOperators(char, newActionVariables); } else { newActionVariables = defaultCharHandler(char, newActionVariables); } return newActionVariables; }; /** * Push remaining parts into results */ const addFinalResult = (actionVariables) => { if (actionVariables.part) { actionVariables.result.push(queryElementTransformer(actionVariables.part)); } return actionVariables; }; /** * Transforms string representation of query into workable array representation * @param {String} query - string representation of query to be performed * @returns {Array} - array representation of query with length of 1 or 3, * second element is the operator */ const queryTransformer = (query) => { if (transformCache[JSON.stringify(query)]) { return transformCache[JSON.stringify(query)]; } const substractedQuery = query.split(''); let actionVariables = { result: [], isString: null, part: '', arrayCounter: 0, }; substractedQuery.forEach((char) => { actionVariables = handleCharacters(char, actionVariables); }); addFinalResult(actionVariables); validateResult(actionVariables.result, query); transformCache[JSON.stringify(query)] = actionVariables.result; return actionVariables.result; }; module.exports = queryTransformer; |