Last active
October 30, 2018 17:03
Revisions
-
Emmanuel Garcia revised this gist
Oct 30, 2018 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,9 @@ function parse(pattern, query) { const queryWords = query.split(' '); const sol = {}; if (!match(patternWords, 0, queryWords, 0, sol)) { return null; } return sol; } -
Emmanuel Garcia revised this gist
Oct 30, 2018 . 1 changed file with 8 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,8 +16,13 @@ function isParameterName(word) { return word.substr(1); } const NUMBER_TEST = /^[0-9]+$/; function isValidValue(paramName, value) { if (paramName == 'Number') { return NUMBER_TEST.test(value); } return value != ''; } function match(patternWords, i, queryWords, j, sol) { @@ -33,7 +38,7 @@ function match(patternWords, i, queryWords, j, sol) { const currParamValue = sol[paramName] || ''; const newValue = currParamValue + queryWords[j]; if (isValidValue(paramName, newValue)) { sol[paramName] = newValue; if ( match(patternWords, i, queryWords, j + 1, sol) || -
Emmanuel Garcia created this gist
Oct 30, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ function parse(pattern, query) { const patternWords = pattern.split(' '); const queryWords = query.split(' '); const sol = {}; match(patternWords, 0, queryWords, 0, sol); return sol; } function isParameterPattern(word) { return Boolean(word.charAt(0) == '$'); } function isParameterName(word) { return word.substr(1); } function isValidValue(value) { return value.trim() != ''; } function match(patternWords, i, queryWords, j, sol) { if (i >= patternWords.length && j >= queryWords.length) { return true; } if (i >= patternWords.length || j >= queryWords.length) { return false; } if (isParameterPattern(patternWords[i])) { const paramName = isParameterName(patternWords[i]); const currParamValue = sol[paramName] || ''; const newValue = currParamValue + queryWords[j]; if (isValidValue(newValue)) { sol[paramName] = newValue; if ( match(patternWords, i, queryWords, j + 1, sol) || match(patternWords, i + 1, queryWords, j + 1, sol) ) { return true; } sol[paramName] = currParamValue; } else { sol[paramName] = newValue; if (match(patternWords, i, queryWords, j + 1, sol)) { return true; } sol[paramName] = currParamValue; } } else { if (patternWords[i] === queryWords[j]) { if (match(patternWords, i + 1, queryWords, j + 1, sol)) { return true; } } } return false; }