1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.parseQuery = void 0;
|
4 | const JSON5 = require("json5");
|
5 | const specialValues = {
|
6 | null: null,
|
7 | true: true,
|
8 | false: false,
|
9 | };
|
10 | function parseQuery(query) {
|
11 | if (query.substr(0, 1) !== '?') {
|
12 | throw new Error("A valid query string passed to parseQuery should begin with '?'");
|
13 | }
|
14 | query = query.substr(1);
|
15 | if (!query) {
|
16 | return {};
|
17 | }
|
18 | if (query.substr(0, 1) === '{' && query.substr(-1) === '}') {
|
19 | return JSON5.parse(query);
|
20 | }
|
21 | const queryArgs = query.split(/[,&]/g);
|
22 | const result = {};
|
23 | queryArgs.forEach((arg) => {
|
24 | const idx = arg.indexOf('=');
|
25 | if (idx >= 0) {
|
26 | let name = arg.substr(0, idx);
|
27 | let value = decodeURIComponent(arg.substr(idx + 1));
|
28 |
|
29 |
|
30 | if (specialValues.hasOwnProperty(value)) {
|
31 | value = specialValues[value];
|
32 | }
|
33 | if (name.substr(-2) === '[]') {
|
34 | name = decodeURIComponent(name.substr(0, name.length - 2));
|
35 | if (!Array.isArray(result[name])) {
|
36 | result[name] = [];
|
37 | }
|
38 | result[name].push(value);
|
39 | }
|
40 | else {
|
41 | name = decodeURIComponent(name);
|
42 | result[name] = value;
|
43 | }
|
44 | }
|
45 | else {
|
46 | if (arg.substr(0, 1) === '-') {
|
47 | result[decodeURIComponent(arg.substr(1))] = false;
|
48 | }
|
49 | else if (arg.substr(0, 1) === '+') {
|
50 | result[decodeURIComponent(arg.substr(1))] = true;
|
51 | }
|
52 | else {
|
53 | result[decodeURIComponent(arg)] = true;
|
54 | }
|
55 | }
|
56 | });
|
57 | return result;
|
58 | }
|
59 | exports.parseQuery = parseQuery;
|