-
Notifications
You must be signed in to change notification settings - Fork 7
/
jsonurl.d.ts
360 lines (351 loc) · 15.1 KB
/
jsonurl.d.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* GetMissingValue function signature
*/
type GetMissingValue = {
(key: string): any;
};
/**
* Options for the JsonURL.stringify function.
*
* @see https://github.com/jsonurl/specification#291-implied-arrays
* @see https://github.com/jsonurl/specification#292-implied-objects
* @see https://github.com/jsonurl/specification#293-x-www-form-urlencoded-arrays-and-objects
* @see https://github.com/jsonurl/specification#295-empty-objects-and-arrays
* @see https://github.com/jsonurl/specification#296-address-bar-query-string-friendly
*/
type JsonURLStringifyOptions = {
/**
* @type {boolean} [allowEmptyUnquotedKeys] Normally an empty string
* value is represented as `''` (two single quotes). If this
* is true the stringifier will omit a value rather than writing an
* empty string.
*/
allowEmptyUnquotedValues?: boolean,
/**
* @type {boolean} [allowEmptyUnquotedKeys] Normally a key whose value is
* the empty string is represented as `''` (two single quotes). If this
* is true the stringifier will omit a value rather than writing an
* empty string.
*/
allowEmptyUnquotedKeys?: boolean,
/**
* @type {boolean} [AQF] Enable the address bar query string friendly
* (AQF) syntax option, and emit a string as oulined in section
* 2.9.6 of the JSON→URL specification.
*/
AQF?: boolean,
/**
* @type {boolean} [coerceNullToEmptyString] Normally the stringifier will
* emit a `null` value as a literal `null`. If this is true `null`
* values will be coerced to the empty string.
*/
coerceNullToEmptyString?: boolean,
/**
* @type {Array<any>} [impliedArray] Emit an implied array
* as oulined in section 2.9.1 of the JSON→URL specification.
*/
impliedArray?: boolean,
/**
* @type {Object} [impliedObject] Emit an implied object
* as oulined in section 2.9.1 of the JSON→URL specification.
*/
impliedObject?: boolean,
/**
* @type {boolean} [impliedStringLiterals] If true the stringifier will
* emit JSON→URL text that assumes all literals are
* strings.
*/
impliedStringLiterals?: boolean,
/**
* @type {boolean} [noEmptyComposite] Emit JSON→URL text as
* oulined in section 2.9.4 of the JSON→URL specification.
*/
noEmptyComposite?: boolean,
/**
* @type {boolean} [wwwFormUrlEncoded] Emit x-www-form-urlencoded content
* as oulined in section 2.9.3 of the JSON→URL specification.
*/
wwwFormUrlEncoded?: boolean,
/**
* @type {boolean} [callFunctions] The stringifier will inspect each
* value to see if it's a function.
* If `callFunctions` is true then functions will be called to resolve
* the value; otherwise, they will be omitted.
*/
callFunctions?: boolean,
/**
* @deprecated
* @type {boolean} [isImplied] Emit an implied object or array. For
* consistency with parse() use `impliedArray` or `impliedObject`.
*/
isImplied?: boolean,
/**
* @type {boolean} [ignoreNullArrayMembers=false] Ignore null array members.
* This is `true` by default if impliedStringLiterals is true,
* and `false` by default otherwise.
*/
ignoreNullArrayMembers?: boolean,
/**
* @type {boolean} [ignoreNullObjectMembers=false] Ignore null object
* members. This is `true` by default if impliedStringLiterals is true,
* and `false` by default otherwise.
*/
ignoreNullObjectMembers?: boolean,
/**
* @type {boolean} [ignoreUndefinedArrayMembers] Ignore undefined array
* members. This is `true` by default if impliedStringLiterals is true,
* and `false` by default otherwise. Undefined values will be stringified
* as `null` because `undefined` is not a valid JSON value.
*/
ignoreUndefinedArrayMembers?: boolean,
/**
* @type {boolean} [ignoreUndefinedObjectMembers] Ignore undefined object
* members. This is `true` by default if impliedStringLiterals is true,
* and `false` by default otherwise. Undefined values will be stringified
* as `null` because `undefined` is not a valid JSON value.
*/
ignoreUndefinedObjectMembers?: boolean,
}
/**
* Options for the JsonURL.parse function.
*
* @see https://github.com/jsonurl/specification#291-implied-arrays
* @see https://github.com/jsonurl/specification#292-implied-objects
* @see https://github.com/jsonurl/specification#293-x-www-form-urlencoded-arrays-and-objects
* @see https://github.com/jsonurl/specification#294-implied-object-missing-values
* @see https://github.com/jsonurl/specification#295-empty-objects-and-arrays
* @see https://github.com/jsonurl/specification#296-address-bar-query-string-friendly
*/
type JsonURLParseOptions = {
/**
* @type {boolean} [allowEmptyUnquotedValues] Normally the empty string
* is represented as `''` (two single quotes). This option allows the
* parser to recognize an omitted value as the empty string. Note, in the
* case of an object value the separator is still required.
*/
allowEmptyUnquotedValues?: boolean,
/**
* @type {boolean} [allowEmptyUnquotedKeys] Normally the empty string
* is represented as `''` (two single quotes). This option allows the
* parser to recognize an omitted object key as the empty string. Note,
* the separator is still required.
*/
allowEmptyUnquotedKeys?: boolean,
/**
* @type {boolean} [AQF] Enable the address bar query string friendly
* (AQF) syntax option, and implement the grammar oulined in section
* 2.9.6 of the JSON→URL specification.
*/
AQF?: boolean,
/**
* @type {boolean} [coerceNullToEmptyString] Normally the `null` literal
* is parsed as a JavaScript `null` value, however, if this is true
* then `null` literals will be coerced to the empty string.
*/
coerceNullToEmptyString?: boolean,
/**
* @type {Array<any>} [impliedArray] An implied array.
*
* If provied, parse will implement a parser for the grammar oulined in
* section 2.9.1 of the JSON→URL specification. The given parse
* text is assumed to be an array, and the leading and trailing parens
* must not be present. The given array value will be populated and
* returned.
*/
impliedArray?: Array<any>,
/**
* @type {Object} [impliedObject] An implied object.
*
* If provided, parse will implement the grammar oulined in section 2.9.2
* of the JSON→URL specification. The given parse text is assumed
* to be an object, and the leading and trailing parens must not be
* present. The given object value will be populated and returned.
*/
impliedObject?: object,
/**
* @type {boolean} [impliedStringLiterals] Instruct the parser to assume
* that all literals are strings. In this case, the single quote character
* loses its significance and is parsed as-is.
*/
impliedStringLiterals?: boolean,
/**
* @type {boolean} [noEmptyComposite] Implement the grammar
* oulined in section 2.9.4 of the JSON→URL specification.
*
* If true, the parser will distinguish between empty array and empty
* object. Empty array is back-to-back parens, e.g. `()`. Empty object
* is two parens with a single colon inside, e.g. `(:)`. Note that this
* prevents the parser from recognizing `(:)` as an object with a single
* member whose key and value is the unquoted empty string when
* `allowEmptyUnquotedValues` and `allowEmptyUnquotedValues` are also
* both true.
*/
noEmptyComposite?: boolean,
/**
* @type {boolean} [wwwFormUrlEncoded] Implement the grammar oulined in
* section 2.9.3 of the JSON→URL specification.
*
* The given parse text is may use ampersand and equal characters as the
* value and member separator characters, respetively, at the top-level.
* This may be combined with `impliedArray` or `impliedObject`.
*/
wwwFormUrlEncoded?: boolean,
/**
* @param {*} [emptyValue] The value which represents the empty composite.
* This may be any type. If it is a function then it will be called
* until it resolves to something that is not a function. The default
* is an empty Object.
*/
emptyValue?: any,
/**
* @type {function} [getMissingValue] Implement the grammar oulined in
* section 2.9.4 of the JSON→URL specification.
*
* This function provides a missing top-level value for the given key.
*/
getMissingValue?: GetMissingValue,
/**
* @type {*} [nullValue=null] The value which represents the `null` value.
* This may be any type. If it is a function then it will be called
* until it resolves to something that is not a function. The default
* is `null`.
*/
nullValue?: any,
/**
* @type {number} [maxParseChars=65535] Maximum number of characters to
* parse. The parse() method will throw an Error if it parses more than
* this number of characters. The default is 64K.
*/
maxParseChars?: number,
/**
* @type {number} [maxParseDepth=64] Maximum parse depth.
* The parse() method will throw an Error if the depth
* of the input exceeds this value. The default is 64.
*/
maxParseDepth?: number,
/**
* @type {number} [maxParseValues=4096] Maximum number of values to parse.
* The parse() method will throw an Error if it parses more than this
* number of values. The default is 4096.
*/
maxParseValues?: number,
/**
* @type {boolean} [ignoreNullArrayMembers=false] Ignore null array
* members.
*/
ignoreNullArrayMembers?: boolean,
/**
* @type {boolean} [ignoreNullObjectMembers=false] Ignore null object
* members.
*/
ignoreNullObjectMembers?: boolean,
}
declare class JsonURL {
/**
* Construct a new JsonURL class.
*
* Each instance of this class contains a number of properties that manage
* the behavior of the parser and the values it returns; these are documented
* below. The class instance does not manage parse state -- that is local to
* the parse() function itself. As long as you don't need different
* properties (e.g. limits, null value, etc) you may re-use the same Parser
* instance, even by multiple Workers.
* @param {Object} [prop] Initialization properties.
* @deprecated this constructior will be removed and the JsonURL class
* will simply have two static functions (mirroring the interface
* of the JSON Object). These properties may be sent as options to
* parse() and stringify().
*/
constructor(prop?: {
/**
* @type {number} [maxParseDepth=64] Maximum parse depth.
* The parse() method will throw an Error if the depth
* of the input exceeds this value. The default is 64.
*/
maxParseDepth?: number;
/**
* @type {number} [maxParseValues=4096] Maximum number of values to parse.
* The parse() method will throw an Error if it parses more than this
* number of values. The default is 4K.
*/
maxParseValues?: number;
/**
* @type {number} [maxParseChars=65535] Maximum number of characters to
* parse. The parse() method will throw an Error if it parses more than
* this number of characters. The default is 64K.
*/
maxParseChars?: number;
/**
* @param {*} [emptyValue] The value which represents the empty composite.
* This may be any type. If it is a function then it will be called
* until it resolves to something that is not a function. The default
* is an empty Object.
*/
emptyValue?: any;
/**
* @type {*} [nullValue=null] The value which represents the `null` value.
* This may be any type. If it is a function then it will be called
* until it resolves to something that is not a function. The default
* is `null`.
*/
nullValue?: any;
});
/**
* Parse JSON→URL text and return a JavaScript value.
*
* The `text` parameter must be provided. The second parameter may be
* either the index into `text` where the parse should start (a number)
* or parse options. If an offset is provided then the third parameter
* may be either the index into `text` where the parse should end (a
* number) or parse options. If the an end index is provided then the
* forth parameter may be parse options.
*
* @public
* @param {string} text The text to parse.
* @param {number|JsonURLParseOptions} [startOrOpt] index into `text`
* where parse should start (a number) or parse options.
* @param {number|JsonURLParseOptions} [endOrOpt] index into `text`
* where parse should end (a number) or parse options.
* @param {JsonURLParseOptions} [options] parse options.
* @return {any} the parsed value
* @throws SyntaxError if there is a syntax error in the given text
* @throws Error if a limit given in the constructor (or its default)
* is exceeded.
*/
static parse(text: string, startOrOpt?: number | JsonURLParseOptions, endOrOpt?: number | JsonURLParseOptions, options?: JsonURLParseOptions): any;
/**
* Parse JSON→URL text and return a JavaScript value.
*
* The `text` parameter must be provided. The second parameter may be
* either the index into `text` where the parse should start (a number)
* or parse options. If an offset is provided then the third parameter
* may be either the index into `text` where the parse should end (a
* number) or parse options. If the an end index is provided then the
* forth parameter may be parse options.
*
* @param {string} text The text to parse.
* @param {number|JsonURLParseOptions} [startOrOpt] index into `text`
* where parse should start (a number) or parse options.
* @param {number|JsonURLParseOptions} [endOrOpt] index into `text`
* where parse should end (a number) or parse options.
* @param {JsonURLParseOptions} [options] parse options.
* @return {any} the parsed value
* @throws SyntaxError if there is a syntax error in the given text
* @throws Error if a limit given in the constructor (or its default)
* is exceeded.
* @deprecated this function will be removed and the JsonURL class
* will simply have two static functions (mirroring the interface
* of the JSON Object). Call `JsonURL.parse()` instead.
*/
parse(text: string, startOrOpt?: number | JsonURLParseOptions, endOrOpt?: number | JsonURLParseOptions, options?: JsonURLParseOptions): any;
/**
* A static method for coverting a JavaScript value to JSON→URL text.
* @public
* @param {*} value Any value
* @param {JsonURLStringifyOptions} [options] stringify options.
* @returns {string} JSON→URL text, or `undefined` if the given value
* is `undefined`.
*/
static stringify(value: any, options?: JsonURLStringifyOptions): string | undefined;
}
export default JsonURL;