[256] | 1 | define(["./_base/lang"], function(lang){ |
---|
| 2 | // module: |
---|
| 3 | // dojo/io-query |
---|
| 4 | // summary: |
---|
| 5 | // This module defines query string processing functions. |
---|
| 6 | |
---|
| 7 | var backstop = {}; |
---|
| 8 | |
---|
| 9 | function objectToQuery(/*Object*/ map){ |
---|
| 10 | // summary: |
---|
| 11 | // takes a name/value mapping object and returns a string representing |
---|
| 12 | // a URL-encoded version of that object. |
---|
| 13 | // example: |
---|
| 14 | // this object: |
---|
| 15 | // |
---|
| 16 | // | { |
---|
| 17 | // | blah: "blah", |
---|
| 18 | // | multi: [ |
---|
| 19 | // | "thud", |
---|
| 20 | // | "thonk" |
---|
| 21 | // | ] |
---|
| 22 | // | }; |
---|
| 23 | // |
---|
| 24 | // yields the following query string: |
---|
| 25 | // |
---|
| 26 | // | "blah=blah&multi=thud&multi=thonk" |
---|
| 27 | |
---|
| 28 | // FIXME: need to implement encodeAscii!! |
---|
| 29 | var enc = encodeURIComponent, pairs = []; |
---|
| 30 | for(var name in map){ |
---|
| 31 | var value = map[name]; |
---|
| 32 | if(value != backstop[name]){ |
---|
| 33 | var assign = enc(name) + "="; |
---|
| 34 | if(lang.isArray(value)){ |
---|
| 35 | for(var i = 0, l = value.length; i < l; ++i){ |
---|
| 36 | pairs.push(assign + enc(value[i])); |
---|
| 37 | } |
---|
| 38 | }else{ |
---|
| 39 | pairs.push(assign + enc(value)); |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | return pairs.join("&"); // String |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | function queryToObject(/*String*/ str){ |
---|
| 47 | // summary: |
---|
| 48 | // Create an object representing a de-serialized query section of a |
---|
| 49 | // URL. Query keys with multiple values are returned in an array. |
---|
| 50 | // |
---|
| 51 | // example: |
---|
| 52 | // This string: |
---|
| 53 | // |
---|
| 54 | // | "foo=bar&foo=baz&thinger=%20spaces%20=blah&zonk=blarg&" |
---|
| 55 | // |
---|
| 56 | // results in this object structure: |
---|
| 57 | // |
---|
| 58 | // | { |
---|
| 59 | // | foo: [ "bar", "baz" ], |
---|
| 60 | // | thinger: " spaces =blah", |
---|
| 61 | // | zonk: "blarg" |
---|
| 62 | // | } |
---|
| 63 | // |
---|
| 64 | // Note that spaces and other urlencoded entities are correctly |
---|
| 65 | // handled. |
---|
| 66 | |
---|
| 67 | // FIXME: should we grab the URL string if we're not passed one? |
---|
| 68 | var dec = decodeURIComponent, qp = str.split("&"), ret = {}, name, val; |
---|
| 69 | for(var i = 0, l = qp.length, item; i < l; ++i){ |
---|
| 70 | item = qp[i]; |
---|
| 71 | if(item.length){ |
---|
| 72 | var s = item.indexOf("="); |
---|
| 73 | if(s < 0){ |
---|
| 74 | name = dec(item); |
---|
| 75 | val = ""; |
---|
| 76 | }else{ |
---|
| 77 | name = dec(item.slice(0, s)); |
---|
| 78 | val = dec(item.slice(s + 1)); |
---|
| 79 | } |
---|
| 80 | if(typeof ret[name] == "string"){ // inline'd type check |
---|
| 81 | ret[name] = [ret[name]]; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | if(lang.isArray(ret[name])){ |
---|
| 85 | ret[name].push(val); |
---|
| 86 | }else{ |
---|
| 87 | ret[name] = val; |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | return ret; // Object |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | return { |
---|
| 95 | objectToQuery: objectToQuery, |
---|
| 96 | queryToObject: queryToObject |
---|
| 97 | }; |
---|
| 98 | }); |
---|