From de013fe7bddd6a1dd9891fe458e994fa12c9cf3f Mon Sep 17 00:00:00 2001 From: Will Hankinson Date: Tue, 15 Jun 2021 12:12:32 -0400 Subject: [PATCH] added fuzzy matching added /* support on the xpath rules in case you want to extract specific values out of objects with unique keys --- ImportJSON.gs | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/ImportJSON.gs b/ImportJSON.gs index 731806e..a3f2c9e 100644 --- a/ImportJSON.gs +++ b/ImportJSON.gs @@ -471,9 +471,54 @@ function includeXPath_(query, path, options) { /** * Returns true if the rule applies to the given path. + * + * Use /* to match any string, which can be handy for extracting data + * from a dictionary of objects with unique keys. + * + * For example, if we only wanted the values: + * {"data": {"A":{"key":"value"}, "B":{"key","value"}} } + * + * We could use this rule to extract the values: + * "/data/ * /key" (minus spaces around "*") + * */ function applyXPathRule_(rule, path, options) { - return path.indexOf(rule) == 0; + + //original check + if(path.indexOf(rule) == 0) + { + return true; + } + + //if there are no fuzzy rules, we failed to match and can return + if(rule.indexOf("/*") == -1) + { + return false; + } + + var rule_pieces = rule.split("/"); + var path_pieces = path.split("/"); + + //exact length matches only -- i.e. /path/* will match path/to but not path/to/rule + if(rule_pieces.length != path_pieces.length) + { + return false; + } + + for(var i = 0; i < rule_pieces.length; i++) + { + if(rule_pieces[i] != "*") + { + if(rule_pieces[i] != path_pieces[i]) + { + //bail out at first failure + return false; + } + } + } + + //all our rule/path pieces lined up, keep us + return true; } /**