h3 / jquery-utils

Automatically exported from code.google.com/p/jquery-utils
MIT License
0 stars 0 forks source link

jpath cannot fetch root node. #57

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Make an object with a few levels deep. eg:

var o = {
  a : {
    1 : null
  },
  b : 2
};

2. run the following query

$(o).jpath('$');

What is the expected output? What do you see instead?

Base on the XPath/Jpath docs at http://goessner.net/articles/JsonPath/
I would have expected '$' to return the root object.  It actually returns false.

What version of the product are you using? On what operating system?

version" 0.1

Please provide any additional information below.

The bugfix was to patch the code to check for a string match against '$', ie:

        getObj: function(s, k) {
            if (k.indexOf(':') > -1) {
                var match = k.match(/(\w+):(\w+)(\(?.*\)?)/);
                var raw   = match[0];
                var k     = match[1];
                var expr  = match[2];
                var p     = match[3] && match[3].slice(1,-1) || false;
                if ($.jpath.isExpr(expr)) {
                    return $.jpath.expr[expr](s, k, p);
                }
            }
            else if (k == '$') {
                return s || false;
            }
            else {
                return s[k] || false;
            }
        },

Original issue reported on code.google.com by Mathew.B...@gmail.com on 6 Sep 2010 at 4:51