This repository has been archived by the owner on Dec 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
QueryParams.ts
210 lines (193 loc) · 5.29 KB
/
QueryParams.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*!
* @author electricessence / https://github.com/electricessence/
* Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
*/
import * as Serialize from "../Serialization/Utility";
import {UriComponent} from "./UriComponent";
import {QueryParam} from "./QueryParam";
import {Type} from "../Types";
import {extractKeyValue} from "../KeyValueExtract";
import {forEach, isEnumerableOrArrayLike} from "../Collections/Enumeration/Enumerator";
import {IMap} from "../../IMap";
import {Primitive} from "../Primitive";
import {IStringKeyValuePair} from "../KeyValuePair";
import {IEnumerableOrArray} from "../Collections/IEnumerableOrArray";
/*
* This module is provided as a lighter weight utility for acquiring query params.
* If more detailed operations are necessary, consider importing QueryBuilder.
*/
const
EMPTY = "",
QUERY_SEPARATOR = "?",
ENTRY_SEPARATOR = "&",
KEY_VALUE_SEPARATOR = "=",
TO_URI_COMPONENT = "toUriComponent";
/**
* Returns the encoded URI string
* @param values
* @param prefixIfNotEmpty
* @returns {string}
*/
export function encode(
values:UriComponent.Map | QueryParam.EnumerableOrArray,
prefixIfNotEmpty?:boolean):string
{
if(!values) return EMPTY;
const entries:string[] = [];
if(isEnumerableOrArrayLike(values))
{
forEach(values, entry=>
extractKeyValue(entry,
(key, value)=> appendKeyValue(entries, key, value))
);
}
else
{
Object.keys(values).forEach(
key=> appendKeyValue(entries, key, values[key])
);
}
return (entries.length && prefixIfNotEmpty ? QUERY_SEPARATOR : EMPTY)
+ entries.join(ENTRY_SEPARATOR);
}
function appendKeyValueSingle(
entries:string[],
key:string,
value:UriComponent.Value):void
{
entries.push(key + KEY_VALUE_SEPARATOR + encodeValue(value));
}
// According to spec, if there is an array of values with the same key, then each value is replicated with that key.
function appendKeyValue(
entries:string[],
key:string,
value:UriComponent.Value|IEnumerableOrArray<UriComponent.Value>):void
{
if(isEnumerableOrArrayLike(value))
{
forEach(value, v=> appendKeyValueSingle(entries, key, v));
}
else
{
appendKeyValueSingle(entries, key, value)
}
}
/**
* Converts any primitive, serializable or uri-component object to an encoded string.
* @param value
* @returns {string}
*/
export function encodeValue(value:UriComponent.Value):string
{
if(isUriComponentFormattable(value))
{
const v:string = value.toUriComponent();
if(v && v.indexOf(ENTRY_SEPARATOR)!=1)
throw '.toUriComponent() did not encode the value.';
return v;
}
else
{
return encodeURIComponent(Serialize.toString(value));
}
}
/**
* A shortcut for identifying an UriComponent.Formattable object.
* @param instance
* @returns {boolean}
*/
export function isUriComponentFormattable(instance:any):instance is UriComponent.Formattable
{
return Type.hasMemberOfType<UriComponent.Formattable>(instance, TO_URI_COMPONENT, Type.FUNCTION);
}
/**
* Parses a string for valid query param entries and pipes them through a handler.
* @param query
* @param entryHandler
* @param deserialize Default is true.
* @param decodeValues Default is true.
*/
export function parse(
query:string,
entryHandler:(key:string, value:Primitive)=>void,
deserialize:boolean = true,
decodeValues:boolean = true):void
{
if(query && (query = query.replace(/^\s*\?+/, '')))
{
const entries = query.split(ENTRY_SEPARATOR);
for(let entry of entries)
{
/*
* Since it is technically possible to have multiple '=' we need to identify the first one.
* And if there is no '=' then the entry is ignored.
*/
const si = entry.indexOf(KEY_VALUE_SEPARATOR);
if(si!= -1)
{
let key = entry.substring(0, si);
let value = <any>entry.substring(si + 1);
if(decodeValues) value = decodeURIComponent(value);
if(deserialize) value = Serialize.toPrimitive(value);
entryHandler(key, value);
}
}
}
}
/**
* Parses a string for valid query params and returns a key-value map of the entries.
* @param query
* @param deserialize Default is true.
* @param decodeValues Default is true.
* @returns {IMap<Primitive>}
*/
export function parseToMap(
query:string,
deserialize:boolean = true,
decodeValues:boolean = true):IMap<Primitive|Primitive[]>
{
const result:IMap<Primitive|Primitive[]> = {};
parse(query,
(key, value)=>
{
if((key) in (result))
{
let prev:any = result[key];
if(!((prev)instanceof(Array)))
result[key] = prev = [prev];
prev.push(value);
}
else
result[key] = value;
},
deserialize,
decodeValues);
return result;
}
/**
* Parses a string for valid query params and returns a key-value pair array of the entries.
* @param query
* @param deserialize Default is true.
* @param decodeValues Default is true.
* @returns {IKeyValuePair<string, Primitive>[]}
*/
export function parseToArray(
query:string,
deserialize:boolean = true,
decodeValues:boolean = true):IStringKeyValuePair<Primitive>[]
{
const result:IStringKeyValuePair<Primitive>[] = [];
parse(query,
(key, value)=> {result.push({key: key, value: value});},
deserialize,
decodeValues
);
return result;
}
export module Separator
{
export const Query:string = QUERY_SEPARATOR;
export const Entry:string = ENTRY_SEPARATOR;
export const KeyValue:string = KEY_VALUE_SEPARATOR;
}
Object.freeze(Separator);