Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

more php

  • Loading branch information...
commit 11027ef8c093c2145134116a0cd44563d1826109 1 parent bc6f365
@rodneyrehm rodneyrehm authored
Showing with 27 additions and 7 deletions.
  1. +10 −1 src/URIhooks.js
  2. +17 −6 test/test_hooks.js
View
11 src/URIhooks.js
@@ -119,10 +119,19 @@ var modifiers = {
var a = [];
keys.sort();
for (i=0, length = keys.length; i < length; i++) {
+ // arrays have consecutive indexes,
+ // if the input is not numbered consequtively, it's an object!
+ if (i && keys[i] -1 != keys[i - 1]) {
+ console.log(keys, data);
+ a = undefined;
+ break;
+ }
a.push(data[keys[i]]);
}
- return a;
+ if (a) {
+ return a;
+ }
}
View
23 test/test_hooks.js
@@ -23,31 +23,42 @@ test("URI.hooks.php", function() {
var d, r, s;
URI.queryHook = URI.hooks.php;
-
+
+ // correctly numbered array
d = encodeBrackets("foo=bar&list[0]=one&list[1]=two");
r = {foo: "bar", list: ['one', 'two']};
s = URI.parseQuery(d);
- equal(JSON.stringify(s), JSON.stringify(r), "parsing query with array");
+ deepEqual(s, r, "parsing query with array");
equal(URI.buildQuery(s), d, "reverse - parsing query with array");
+ // implicitly numbered array
d = encodeBrackets("foo=bar&list[]=one&list[]=two");
s = URI.parseQuery(d);
- equal(JSON.stringify(s), JSON.stringify(r), "parsing query with auto-index array");
+ deepEqual(s, r, "parsing query with auto-index array");
d = encodeBrackets("foo=bar&list[0]=one&list[1]=two");
- equal(URI.buildQuery(s), d, "reverse - parsing query with auto-index array");
+ deepEqual(URI.buildQuery(s), d, "reverse - parsing query with auto-index array");
+ // array and object
d = encodeBrackets("list[0]=one&list[1]=two&foo[bar]=one&foo[baz]=two");
r = {list: ['one', 'two'], foo: {bar: "one", baz: 'two'}};
s = URI.parseQuery(d);
- equal(JSON.stringify(s), JSON.stringify(r), "building query with map");
+ deepEqual(s, r, "building query with map");
equal(URI.buildQuery(s), d, "reverse - parsing query with map");
+ // nested array and object
d = encodeBrackets("list[0]=one&list[1][a]=two&list[1][b]=three&foo[bar]=one&foo[baz][0]=aa&foo[baz][1]=bb");
r = {list: ['one', {a: 'two', b: 'three'}], foo: {bar: "one", baz: ['aa', 'bb']}};
s = URI.parseQuery(d);
- equal(JSON.stringify(s), JSON.stringify(r), "building query with nesting");
+ deepEqual(s, r, "building query with nesting");
equal(URI.buildQuery(s), d, "reverse - building query with nesting");
+ // incorrectly numbered array -> object
+ d = encodeBrackets("foo=bar&list[0]=one&list[5]=two");
+ r = {foo: "bar", list: {"0": "one", "5": "two"}};
+ s = URI.parseQuery(d);
+ deepEqual(s, r, "parsing query with non-consequtively indexed array");
+ equal(decodeBrackets(URI.buildQuery(s)), decodeBrackets(d), "reverse - parsing query with non-consequtively indexed array");
+
URI.queryHook = null;
});
Please sign in to comment.
Something went wrong with that request. Please try again.