source: Dev/trunk/src/client/dojo/io-query.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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