Skip to content

Instantly share code, notes, and snippets.

@blasten
Last active October 30, 2018 17:03

Revisions

  1. Emmanuel Garcia revised this gist Oct 30, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion pattern_matching.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,9 @@ function parse(pattern, query) {
    const queryWords = query.split(' ');
    const sol = {};

    match(patternWords, 0, queryWords, 0, sol);
    if (!match(patternWords, 0, queryWords, 0, sol)) {
    return null;
    }

    return sol;
    }
  2. Emmanuel Garcia revised this gist Oct 30, 2018. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions pattern_matching.js
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,13 @@ function isParameterName(word) {
    return word.substr(1);
    }

    function isValidValue(value) {
    return value.trim() != '';
    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(newValue)) {
    if (isValidValue(paramName, newValue)) {
    sol[paramName] = newValue;
    if (
    match(patternWords, i, queryWords, j + 1, sol) ||
  3. Emmanuel Garcia created this gist Oct 30, 2018.
    60 changes: 60 additions & 0 deletions pattern_matching.js
    Original 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;
    }